🚨 null ref fix
continuous-integration/drone/push Build is passing Details

pull/24/head
Alexandre AGOSTINHO 2 years ago
parent 2e4c8fa29f
commit 7d6cf0bb56

@ -18,12 +18,12 @@ namespace ConsoleApp
new Recipe("Cookies"),
new Recipe("Cookies", 23),
new Recipe("Cookies", null),
new Recipe(null, null),
new Recipe(null, 24),
new Recipe("", null),
new Recipe("", 24),
new Recipe("Cookies", 24,
new PreparationStep(1, null)),
new PreparationStep(1)),
new Recipe("Cookies", 26,
new PreparationStep(1, null),
new PreparationStep(1),
new PreparationStep(2, "Faire cuire."))
});
return stub;

@ -12,7 +12,7 @@ namespace Model
public class PreparationStep : IEquatable<PreparationStep>
{
#region Attributes
private string? _description;
private string _description = "";
#endregion
#region Properties
@ -25,7 +25,7 @@ namespace Model
/// The description of the task the user need to do for this step of the preparation. <br/>
/// Set to "No description." when the value passed is null, empty or contain white spaces.
/// </summary>
public string? Description
public string Description
{
get => _description;
private set
@ -44,7 +44,7 @@ namespace Model
/// </summary>
/// <param name="order">The number of the order in preparation</param>
/// <param name="description">The description of the task</param>
public PreparationStep(int order, string? description)
public PreparationStep(int order, string description = "")
{
Order = order;
Description = description;
@ -52,8 +52,10 @@ namespace Model
#endregion
#region Methods
public bool Equals(PreparationStep? other)
public virtual bool Equals(PreparationStep? other)
{
if (other == null) return false;
if (other == this) return true;
return Order.Equals(other.Order) && Description.Equals(other.Description);
}

@ -11,7 +11,7 @@ namespace Model
public class Recipe : IEquatable<Recipe>
{
#region Attributes
private string? _title;
private string _title = "";
#endregion
#region Properties
@ -24,7 +24,7 @@ namespace Model
/// The Title of the recipe. <br/>
/// Set to "No title." when the value passed is null, empty or contain white spaces.
/// </summary>
public string? Title
public string Title
{
get => _title;
set
@ -39,7 +39,7 @@ namespace Model
/// <summary>
/// The steps of the preparation. See: <see cref="PreparationStep"/>.
/// </summary>
public List<PreparationStep>? PreparationSteps { get; private set; }
public List<PreparationStep> PreparationSteps { get; set; }
#endregion
#region Constructors
@ -49,7 +49,7 @@ namespace Model
/// <param name="title">The title of the recipe</param>
/// <param name="id">The id of the recipe. If not given, get a new id.</param>
/// <param name="preparationSteps">The steps of the preparation of the meal</param>
public Recipe(string? title = null, int? id = null,
public Recipe(string title = "", int? id = null,
params PreparationStep[] preparationSteps)
{
Title = title;

@ -13,8 +13,8 @@ namespace Model
public class RecipeCollection : IList<Recipe>, IEquatable<RecipeCollection>
{
#region Attributes
private readonly List<Recipe> recipes;
private string? _description;
private readonly List<Recipe> _recipes;
private string _description = "";
#endregion
#region Properties
@ -22,7 +22,7 @@ namespace Model
/// A short description of what this collection contain. <br/>
/// Set to "No description." when the value passed is null, empty or contain white spaces.
/// </summary>
public string? Description
public string Description
{
get => _description;
set
@ -35,21 +35,21 @@ namespace Model
}
#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
/// <summary>
/// Construct a new collection of recipes.
/// Construct a new collection of _recipes.
/// </summary>
/// <param name="description">A short description of what this list will contain</param>
/// <param name="recipes">Recipes to add in this new collection</param>
public RecipeCollection(string? description = null, params Recipe[] recipes)
public RecipeCollection(string description, params Recipe[] recipes)
{
this.recipes = new List<Recipe>(recipes);
_recipes = new List<Recipe>(recipes);
Description = description;
}
#endregion
@ -63,79 +63,80 @@ namespace Model
/// <exception cref="ArgumentException"/>
public Recipe? GetRecipeById(int id)
{
Recipe? recipe = recipes.Find(r => r.Id == id);
if (recipe == null) throw new ArgumentException("No recipes match the given id.");
Recipe? recipe = _recipes.Find(r => r.Id == id);
if (recipe == null) throw new ArgumentException("No _recipes match the given id.");
return recipe;
}
#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
public virtual 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);
}
public override int GetHashCode()
{
return recipes.GetHashCode();
return _recipes.GetHashCode();
}
public override string ToString()
{
StringBuilder sb = new StringBuilder($"[RecipeCollection] - {Description}:\n");
foreach (Recipe r in recipes)
foreach (Recipe r in _recipes)
{
sb.AppendFormat("\t - {0}\n", r.ToString());
}

@ -15,7 +15,6 @@ namespace Model_UnitTests
[InlineData("Cookies", 23, "Cookies", 23)]
[InlineData("Cookies", 1, "Cookies", 1)]
[InlineData("No title.", 1, "", 1)]
[InlineData("No title.", 1, null, 1)]
public void TestConstructor(string expectedTitle, int expectedId, string title, int? id)
{
Recipe r = new Recipe(title, id);

Loading…
Cancel
Save