add unit test for recipe + ToString() method for all
continuous-integration/drone/push Build is passing Details

pull/24/head
Alexandre Agostinho 2 years ago
parent ffae251bda
commit 316cb66c74

@ -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}");
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());

@ -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
}
}

@ -1,25 +1,59 @@
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;
namespace Model
{
public class Recipe : IEquatable<Recipe>
{
#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<PreparationStep>? 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<PreparationStep>(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
}
}

@ -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<Recipe>, IEquatable<RecipeCollection>
{
#region Attributes
private List<Recipe> _recipes;
private List<Recipe> 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<Recipe>();
this.recipes = new List<Recipe>(recipes);
Description = description;
}
public RecipeCollection(params Recipe[] recipes)
{
_recipes = new List<Recipe>(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<Recipe> 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
}

@ -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);
}
}
}

Loading…
Cancel
Save