🚨 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"),
new Recipe("Cookies", 23), new Recipe("Cookies", 23),
new Recipe("Cookies", null), new Recipe("Cookies", null),
new Recipe(null, null), new Recipe("", null),
new Recipe(null, 24), new Recipe("", 24),
new Recipe("Cookies", 24, new Recipe("Cookies", 24,
new PreparationStep(1, null)), new PreparationStep(1)),
new Recipe("Cookies", 26, new Recipe("Cookies", 26,
new PreparationStep(1, null), new PreparationStep(1),
new PreparationStep(2, "Faire cuire.")) new PreparationStep(2, "Faire cuire."))
}); });
return stub; return stub;

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

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

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

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

Loading…
Cancel
Save