From 316cb66c74b7c3cd41e12bd46e94dfd8421c7f90 Mon Sep 17 00:00:00 2001 From: Alexandre Agostinho Date: Thu, 27 Apr 2023 14:53:41 +0200 Subject: [PATCH] add unit test for recipe + ToString() method for all --- MCTG/ConsoleApp/Program.cs | 15 ++++-- MCTG/Model/PreparationStep.cs | 16 ++++-- MCTG/Model/Recipe.cs | 53 ++++++++++++++++++-- MCTG/Model/RecipeCollection.cs | 66 +++++++++++++++---------- MCTG/Tests/Model_UnitTests/Recipe_UT.cs | 37 +++++++------- 5 files changed, 131 insertions(+), 56 deletions(-) diff --git a/MCTG/ConsoleApp/Program.cs b/MCTG/ConsoleApp/Program.cs index c677863..ac6bfb0 100644 --- a/MCTG/ConsoleApp/Program.cs +++ b/MCTG/ConsoleApp/Program.cs @@ -3,10 +3,19 @@ using Model; -Console.WriteLine("Hello, World!\n"); +Console.WriteLine("Hello, World!\n\n"); // TESTS: -RecipeCollection rc = new RecipeCollection(); -Console.WriteLine($"rc.Description: {rc.Description}"); \ No newline at end of file +RecipeCollection rc = new RecipeCollection("Desserts", + new Recipe("Cookies", "Cookies au chocolats", null, + new PreparationStep(1, "Faire cuire le truc."), + new PreparationStep(2, "Elaboré un autre truc"), + new PreparationStep(3, null)), + new Recipe(null, null, 99, + new PreparationStep(1, null), + new PreparationStep(1, null)), + new Recipe()); + +Console.WriteLine(rc.ToString()); diff --git a/MCTG/Model/PreparationStep.cs b/MCTG/Model/PreparationStep.cs index 8fe4071..6057273 100644 --- a/MCTG/Model/PreparationStep.cs +++ b/MCTG/Model/PreparationStep.cs @@ -19,11 +19,10 @@ namespace Model get => _description; set { - if (string.IsNullOrEmpty(value)) - { - _description = "No data."; return; - } - _description = value; + if (string.IsNullOrWhiteSpace(value)) + _description = "No description."; + else + _description = value; } } #endregion @@ -35,5 +34,12 @@ namespace Model Description = description; } #endregion + + #region Methods + public override string ToString() + { + return $"{Order}- {Description}"; + } + #endregion } } diff --git a/MCTG/Model/Recipe.cs b/MCTG/Model/Recipe.cs index c253ef4..e493212 100644 --- a/MCTG/Model/Recipe.cs +++ b/MCTG/Model/Recipe.cs @@ -1,25 +1,59 @@ using System; using System.Collections.Generic; using System.Runtime.CompilerServices; +using System.Text; namespace Model { public class Recipe : IEquatable { + #region Attributes private static int idCreator = 0; + private string? _title; + private string? _description; + #endregion + #region Properties public int Id { get; init; } - public string Description { get; init; } - public string Title { get; set; } + public string? Title + { + get => _title; + set + { + if (string.IsNullOrWhiteSpace(value)) + _title = "No title."; + else + _title = value; + } + } + public string? Description + { + get => _description; + set + { + if (string.IsNullOrWhiteSpace(value)) + _description = "No description."; + else + _description = value; + } + } + public List? PreparationSteps { get; private set; } + #endregion - public Recipe(string title, string description = "No Description.") + #region Constructors + public Recipe(string? title = null, string? description = null, int? id = null, + params PreparationStep[] preparationSteps) { - Id = idCreator++; + if (id == null) Id = idCreator++; + else Id = (int)id; Title = title; Description = description; + PreparationSteps = new List(preparationSteps); } + #endregion + #region Methods public bool Equals(Recipe? other) { if (other == null) return false; @@ -31,5 +65,16 @@ namespace Model { return Id.GetHashCode(); } + + public override string ToString() + { + StringBuilder sb = new StringBuilder($"[Recipe] - {Title}: {Description}\n"); + foreach (PreparationStep ps in PreparationSteps) + { + sb.AppendFormat("\t* {0}\n", ps.ToString()); + } + return sb.ToString(); + } + #endregion } } diff --git a/MCTG/Model/RecipeCollection.cs b/MCTG/Model/RecipeCollection.cs index bde57ba..283af8e 100644 --- a/MCTG/Model/RecipeCollection.cs +++ b/MCTG/Model/RecipeCollection.cs @@ -2,6 +2,7 @@ using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; +using System.Text; namespace Model { @@ -9,98 +10,111 @@ namespace Model public class RecipeCollection : IList, IEquatable { #region Attributes - private List _recipes; + private List recipes; + private string? _description; #endregion #region Properties - public string Description { get; set; } + public string? Description + { + get => _description; + set + { + if (string.IsNullOrWhiteSpace(value)) + _description = "No description."; + else + _description = value; + } + } #region IList Prperties - public int Count => _recipes.Count; + public int Count => recipes.Count; public bool IsReadOnly => false; - public Recipe this[int index] { get => _recipes[index]; set => _recipes[index] = value; } + public Recipe this[int index] { get => recipes[index]; set => recipes[index] = value; } #endregion #endregion #region Constructors - public RecipeCollection(string description = "No Description.") + public RecipeCollection(string? description = null, params Recipe[] recipes) { - _recipes = new List(); + this.recipes = new List(recipes); Description = description; } - - public RecipeCollection(params Recipe[] recipes) - { - _recipes = new List(recipes); - Description = "No Description."; - } #endregion #region Methods #region IList Methods public int IndexOf(Recipe item) { - return _recipes.IndexOf(item); + return recipes.IndexOf(item); } public void Insert(int index, Recipe item) { - _recipes.Insert(index, item); + recipes.Insert(index, item); } public void RemoveAt(int index) { - _recipes.RemoveAt(index); + recipes.RemoveAt(index); } public void Add(Recipe item) { - _recipes.Add(item); + recipes.Add(item); } public void Clear() { - _recipes.Clear(); + recipes.Clear(); } public bool Contains(Recipe item) { - return _recipes.Contains(item); + return recipes.Contains(item); } public void CopyTo(Recipe[] array, int arrayIndex) { - _recipes.CopyTo(array, arrayIndex); + recipes.CopyTo(array, arrayIndex); } public bool Remove(Recipe item) { - return _recipes.Remove(item); + return recipes.Remove(item); } public IEnumerator GetEnumerator() { - return _recipes.GetEnumerator(); + return recipes.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { - return _recipes.GetEnumerator(); + return recipes.GetEnumerator(); } #endregion - #region IEquatable Mathods public bool Equals(RecipeCollection? other) { if (other == null) return false; if (other == this) return true; - return Description.Equals(other.Description) && _recipes.Equals(other._recipes); + return Description.Equals(other.Description) && recipes.Equals(other.recipes); } - #endregion public override int GetHashCode() { - return Description.GetHashCode() + _recipes.GetHashCode(); + return Description.GetHashCode() + recipes.GetHashCode(); + } + + public override string ToString() + { + StringBuilder sb = new StringBuilder($"[RecipeCollection] - {Description}:\n"); + foreach (Recipe r in recipes) + { + sb.AppendFormat("\t - {0}\n", r.ToString()); + } + return sb.ToString(); } #endregion } diff --git a/MCTG/Tests/Model_UnitTests/Recipe_UT.cs b/MCTG/Tests/Model_UnitTests/Recipe_UT.cs index 40d8504..e1d6720 100644 --- a/MCTG/Tests/Model_UnitTests/Recipe_UT.cs +++ b/MCTG/Tests/Model_UnitTests/Recipe_UT.cs @@ -5,28 +5,29 @@ namespace Model_UnitTests public class Recipe_UT { [Fact] - public void TestConstructorNotNull() + public void TestVoidConstructor() { - Recipe r1 = new Recipe("R1"); - Assert.NotNull(r1); + Recipe r = new Recipe(id: 999); // id is given to avoid tests errors with the static atrribute 'idCreator'. + Assert.NotNull(r.Title); + Assert.NotNull(r.Description); } - [Fact] - public void TestConstructorWithValidDefaults() - { - Recipe r2 = new Recipe("Recipe n2"); - Assert.Equal("Recipe n2", r2.Title); - Assert.Equal("No Description.", r2.Description); - } - - [Fact] - public void TestRecipeId() + [Theory] + [InlineData("Cookies", "Choco Cookies", 23, "Cookies", "Choco Cookies", 23)] + [InlineData("Cookies", "No description.", 1, "Cookies", "", 1)] + [InlineData("No title.", "Choco Cookies", 1, "", "Choco Cookies", 1)] + [InlineData("Cookies", "Choco Cookies", 0, "Cookies", "Choco Cookies", null)] + [InlineData("Cookies", "No description.", 1, "Cookies", null, 1)] + [InlineData("No title.", "Choco Cookies", 1, null, "Choco Cookies", 1)] + public void TestConstructor(string expectedTitle, string expectedDescription, int expectedId, + string title, string description, int? id) { - Recipe r3 = new Recipe("R3"); - Recipe r4 = new Recipe("R4"); - - Assert.Equal(2, r3.Id); - Assert.Equal(3, r4.Id); + Recipe r = new Recipe(title, description, id); + Assert.NotNull(r.Title); + Assert.NotNull(r.Description); + Assert.Equal(expectedId, r.Id); + Assert.Equal(expectedTitle, r.Title); + Assert.Equal(expectedDescription, r.Description); } } }