Skip to content
Snippets Groups Projects
Commit 1432ee61 authored by HSoreide's avatar HSoreide
Browse files

Tidy up unit tests of detailed and simplified IngredientsAtHand classes

parent 9b5cefb4
No related branches found
No related tags found
3 merge requests!42Hs frontend recipes,!41Hs frontend recipes,!36Hs rewrite file handling for recipes and ingredients
Pipeline #215608 passed
......@@ -29,45 +29,6 @@ public class IngredientsAtHand {
return ingredientsAtHand.stream().anyMatch( (in) -> in.equals(foodItem));
}
/* *//**
* Returns null if no ingredient of the requested type is found in the collection.
* @param ingredientType What type of food the ingredient is.
* @return The ingredient of the specified type found among the ingredients at hand, null otherwise.
*//*
public Ingredient getIngredient(FoodItem ingredientType) {
if(ingredientType == null) return null;
return this.getIngredientsAtHand().stream()
.filter((ingredient) -> ingredient.getFoodType() == ingredientType)
.findFirst().orElse(null);
}*/
/**
* The method takes in three parameters. The method first checks if the Ingredient is at hand in the first place.
* If it is, the old amount and unit of this ingredient are replaced by the provided amount and unit if they
* differ. If not, the ingredient is left as is. If the ingredient is not in the collection,
* @param ingredientType What type of food the ingredient is.
* @param amount The amount of the ingredient.
* @return True if Ingredient is successfully altered or added, false if not.
*/
/* public boolean alterIngredient(FoodItem ingredientType, double amount, MeasuringUnit unit) {
//TODO: Consider handling exceptions differently.
if(ingredientsAtHand.stream().anyMatch((ingredient) -> ingredient.getFoodType() == ingredientType)) {
try {
getIngredient(ingredientType).setAmount(amount);
getIngredient(ingredientType).setUnit(unit);
} catch (IllegalArgumentException e) {
return false;
}
} else {
try {
addIngredient(new Ingredient(ingredientType, amount, unit));
} catch (IllegalArgumentException e) {
return false;
}
}
return true;
}*/
/**
* The method takes in a value of the FoodItem enum as a parameter and removes it from the collection of
......
package no.ntnu.idatt1002.demo.data.recipes;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class IngredientsAtHandDetailedTest {
IngredientsAtHandDetailed ingredientsAtHand = new IngredientsAtHandDetailed();
@BeforeEach
void beforeEach() {
ingredientsAtHand.addIngredient(new Ingredient(FoodItem.LEMON, 3, MeasuringUnit.PC));
ingredientsAtHand.addIngredient(new Ingredient(FoodItem.MILK, 1.5f, MeasuringUnit.L));
ingredientsAtHand.addIngredient(new Ingredient(FoodItem.MINCED_MEAT, 400, MeasuringUnit.GR));
}
@Test
@DisplayName("The getIngredient method returns the correct ingredient.")
void getIngredientReturnsSuccessfully() {
assertEquals(new Ingredient(FoodItem.LEMON, 3, MeasuringUnit.PC),
ingredientsAtHand.getIngredient(FoodItem.LEMON));
}
@Test
@DisplayName("The getIngredient method returns null if the ingredient is not in the collection.")
void getIngredientReturnsNullWhenNotFoundOrNull(){
assertNull(ingredientsAtHand.getIngredient(FoodItem.ONION));
assertNull(ingredientsAtHand.getIngredient(null));
}
@Test
@DisplayName("Altering ingredient successfully and return true.")
void alterIngredientSuccessfully() {
assertNotEquals(new Ingredient(FoodItem.ONION, 500, MeasuringUnit.GR),
ingredientsAtHand.getIngredient(FoodItem.ONION));
assertTrue(ingredientsAtHand.alterIngredient(FoodItem.ONION, 500, MeasuringUnit.GR));
assertEquals(new Ingredient(FoodItem.ONION, 500, MeasuringUnit.GR),
ingredientsAtHand.getIngredient(FoodItem.ONION));
}
@Test
@DisplayName("Altering ingredient that does not yet exist in collection adds it and returns true.")
void alterNewIngredientAddsIt() {
int ingredientsAtStart = ingredientsAtHand.getIngredientsAtHand().size();
assertTrue(ingredientsAtHand.alterIngredient(FoodItem.ORANGE, 8, MeasuringUnit.PC));
assertEquals(ingredientsAtStart + 1, ingredientsAtHand.getIngredientsAtHand().size());
}
@Test
@DisplayName("Attempting to alter ingredient in illegal way does not alter the collection and returns false.")
void alterIngredientUnchangedForInvalidChange() {
assertFalse(ingredientsAtHand.alterIngredient(null, 350, MeasuringUnit.GR));
assertEquals(new Ingredient(FoodItem.LEMON, 3, MeasuringUnit.PC),
ingredientsAtHand.getIngredient(FoodItem.LEMON));
}
@Test
@DisplayName("Altering an ingredients without changing values, leaves no change on the collection and returns true.")
void alterNothingLeavesCollectionIntact() {
int ingredientsAtStart = ingredientsAtHand.getIngredientsAtHand().size();
assertTrue(ingredientsAtHand.alterIngredient(FoodItem.LEMON, 3, MeasuringUnit.PC));
assertEquals(ingredientsAtStart, ingredientsAtHand.getIngredientsAtHand().size());
}
@Test
@DisplayName("Ingredient is removed successfully and true is returned.")
void removeIngredientSuccessfully() {
int ingredientsAtStart = ingredientsAtHand.getIngredientsAtHand().size();
assertTrue(ingredientsAtHand.removeIngredient(FoodItem.LEMON));
assertEquals(ingredientsAtStart-1, ingredientsAtHand.getIngredientsAtHand().size());
}
@Test
@DisplayName("Removing ingredient that is not in the collection, leaves it unchanged and returns false.")
void removeIngredientNotInCollection() {
assertFalse(ingredientsAtHand.removeIngredient(FoodItem.SALSA_SAUCE));
}
}
\ No newline at end of file
......@@ -17,12 +17,6 @@ class IngredientsAtHandTest {
ingredientsAtHand.addIngredient(FoodItem.MINCED_MEAT);
}
/* @Test
@DisplayName("The getIngredient method returns the correct ingredient.")
void getIngredientReturnsSuccessfully() {
assertEquals(FoodItem.LEMON),
ingredientsAtHand.getIngredient(FoodItem.LEMON));
}*/
@Test
@DisplayName("The atHand method returns true/false correctly.")
......@@ -30,56 +24,6 @@ class IngredientsAtHandTest {
assertTrue(ingredientsAtHand.atHand(FoodItem.LEMON));
assertFalse(ingredientsAtHand.atHand(FoodItem.YELLOW_CHEESE));
}
/*
@Test
@DisplayName("The getIngredient method returns null if the ingredient is not in the collection.")
void getIngredientReturnsNullWhenNotFoundOrNull(){
assertNull(ingredientsAtHand.getIngredient(FoodItem.ONION));
assertNull(ingredientsAtHand.getIngredient(null));
}
@Test
@DisplayName("Altering ingredient successfully and return true.")
void alterIngredientSuccessfully() {
assertNotEquals(new Ingredient(FoodItem.ONION, 500, MeasuringUnit.GR),
ingredientsAtHand.getIngredient(FoodItem.ONION));
assertTrue(ingredientsAtHand.alterIngredient(FoodItem.ONION, 500, MeasuringUnit.GR));
assertEquals(new Ingredient(FoodItem.ONION, 500, MeasuringUnit.GR),
ingredientsAtHand.getIngredient(FoodItem.ONION));
}
@Test
@DisplayName("Altering ingredient that does not yet exist in collection adds it and returns true.")
void alterNewIngredientAddsIt() {
int ingredientsAtStart = ingredientsAtHand.getIngredientsAtHand().size();
assertTrue(ingredientsAtHand.alterIngredient(FoodItem.ORANGE, 8, MeasuringUnit.PC));
assertEquals(ingredientsAtStart + 1, ingredientsAtHand.getIngredientsAtHand().size());
}
@Test
@DisplayName("Attempting to alter ingredient in illegal way does not alter the collection and returns false.")
void alterIngredientUnchangedForInvalidChange() {
assertFalse(ingredientsAtHand.alterIngredient(null, 350, MeasuringUnit.GR));
assertEquals(new Ingredient(FoodItem.LEMON, 3, MeasuringUnit.PC),
ingredientsAtHand.getIngredient(FoodItem.LEMON));
}
@Test
@DisplayName("Altering an ingredients without changing values, leaves no change on the collection and returns true.")
void alterNothingLeavesCollectionIntact() {
int ingredientsAtStart = ingredientsAtHand.getIngredientsAtHand().size();
assertTrue(ingredientsAtHand.alterIngredient(FoodItem.LEMON, 3, MeasuringUnit.PC));
assertEquals(ingredientsAtStart, ingredientsAtHand.getIngredientsAtHand().size());
}
*/
@Test
@DisplayName("Ingredient is removed successfully and true is returned.")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment