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

Add and restructure tests for Ingredients and add missing JavaDoc

parent 02920fc6
No related branches found
No related tags found
2 merge requests!42Hs frontend recipes,!41Hs frontend recipes
......@@ -66,10 +66,10 @@ public class AllRecipesController implements Initializable {
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
......@@ -94,7 +94,8 @@ public class AllRecipesController implements Initializable {
.getSelectedItem().split("-|#")[1].strip();
try {
showRecipe(selectedRecipeName);
showRecipe(selectedRecipeName);
} catch (IOException e) {
throw new RuntimeException(e);
}
......
......@@ -41,4 +41,19 @@ public class RecipeIngredient extends Ingredient{
this.atHand = atHand;
}
/**
* The method returns a String representation of an Recipe Ingredient object, listing its type, amount, unit
* and whether it is at hand or not.
* @return A String representation of the recipe ingredient object.
*/
@Override
public String toString() {
return "Ingredient{" +
"foodType=" + this.getFoodType().label +
", amount=" + this.getAmount() +
", unit=" + this.getUnit().label +
", at hand=" + this.isAtHand() +
'}';
}
}
......@@ -16,14 +16,14 @@
<top>
<HBox prefHeight="125.0" prefWidth="1130.0" BorderPane.alignment="CENTER">
<children>
<Pane prefHeight="200.0" prefWidth="200.0">
<Pane prefHeight="125.0" prefWidth="284.0">
<children>
<Button layoutX="60.0" layoutY="38.0" mnemonicParsing="false" onAction="#goBack" styleClass="button-style" stylesheets="@../style.css" text="GoBack" />
<Button fx:id="goBackBtn" layoutX="60.0" layoutY="38.0" mnemonicParsing="false" onAction="#goBack" styleClass="button-style" stylesheets="@../style.css" text="Back to Suggestions" />
</children>
</Pane>
<Pane prefHeight="100.0" prefWidth="623.0">
<children>
<Label layoutX="195.0" layoutY="21.0" text="All recipes">
<Label layoutX="115.0" layoutY="24.0" text="All recipes">
<font>
<Font size="48.0" />
</font>
......
......@@ -2,6 +2,7 @@ package no.ntnu.idatt1002.demo.data.recipes;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
......@@ -15,62 +16,86 @@ class IngredientTest {
testIngredient = new Ingredient(FoodItem.ONION, 0.5f, MeasuringUnit.KG);
}
@Test
@DisplayName("The constructor creates an ingredient object successfully")
void constructValidIngredient() {
Ingredient validIngredient = new Ingredient(FoodItem.ONION, 1, MeasuringUnit.KG);
assertEquals(validIngredient, new Ingredient(FoodItem.ONION, 1, MeasuringUnit.KG));
}
@Nested
@DisplayName("Test creation of an Ingredient")
class testCreation{
@Test
@DisplayName("The constructor throws exceptions for illegal input.")
void constructorThrowsExceptionsWhenItShould() {
assertThrows(IllegalArgumentException.class, () -> new Ingredient(null, 2, MeasuringUnit.DL));
assertThrows(IllegalArgumentException.class, () -> new Ingredient(FoodItem.ONION, -5, MeasuringUnit.DL));
assertThrows(IllegalArgumentException.class, () -> new Ingredient(FoodItem.ONION, 0, MeasuringUnit.DL));
assertThrows(IllegalArgumentException.class, () -> new Ingredient(FoodItem.ONION, 2, null));
}
// Indirectly tests equals method.
@Test
@DisplayName("The constructor creates an ingredient object successfully")
void constructValidIngredient() {
Ingredient validIngredient = new Ingredient(FoodItem.ONION, 1, MeasuringUnit.KG);
assertEquals(validIngredient, new Ingredient(FoodItem.ONION, 1, MeasuringUnit.KG));
}
@Test
@DisplayName("Change of food type works for valid food type.")
void setFoodTypeWorksForValidType() {
testIngredient.setFoodType(FoodItem.LEMON);
assertEquals(new Ingredient(FoodItem.LEMON, 0.5f, MeasuringUnit.KG), testIngredient);
@Test
@DisplayName("The constructor throws exceptions for illegal input.")
void constructorThrowsExceptionsWhenItShould() {
assertThrows(IllegalArgumentException.class, () -> new Ingredient(null, 2, MeasuringUnit.DL));
assertThrows(IllegalArgumentException.class, () -> new Ingredient(FoodItem.ONION, -5, MeasuringUnit.DL));
assertThrows(IllegalArgumentException.class, () -> new Ingredient(FoodItem.ONION, 0, MeasuringUnit.DL));
assertThrows(IllegalArgumentException.class, () -> new Ingredient(FoodItem.ONION, 2, null));
}
}
@Test
@DisplayName("Change of food type to invalid type throws exception.")
void setFoodTypeThrowsException() {
assertThrows(IllegalArgumentException.class, () -> testIngredient.setFoodType(null));
}
@Nested
@DisplayName("Test alternation of Ingredient object")
class testAlternations{
@Test
@DisplayName("Change of food amount works for valid amount.")
void setAmountWorksForValidAmount() {
testIngredient.setAmount(2.5);
assertEquals(new Ingredient(FoodItem.ONION, 2.5f, MeasuringUnit.KG), testIngredient);
}
@Nested
@DisplayName("Valid alternations")
class testValidAlternations {
@Test
@DisplayName("Change of food type works for valid food type.")
void setFoodTypeWorksForValidType() {
testIngredient.setFoodType(FoodItem.LEMON);
assertEquals(new Ingredient(FoodItem.LEMON, 0.5f, MeasuringUnit.KG), testIngredient);
}
@Test
@DisplayName("Change of food amount to invalid amount throws exception.")
void setAmountThrowsException() {
assertThrows(IllegalArgumentException.class, () -> testIngredient.setAmount(0));
assertThrows(IllegalArgumentException.class, () -> testIngredient.setAmount(-1));
}
@Test
@DisplayName("Change of food amount works for valid amount.")
void setAmountWorksForValidAmount() {
testIngredient.setAmount(2.5);
assertEquals(new Ingredient(FoodItem.ONION, 2.5f, MeasuringUnit.KG), testIngredient);
}
@Test
@DisplayName("Change of measuring unit works for valid unit.")
void setUnitWorksForValidUnit() {
testIngredient.setUnit(MeasuringUnit.TBS);
assertEquals(new Ingredient(FoodItem.ONION, 0.5f, MeasuringUnit.TBS), testIngredient);
}
@Test
@DisplayName("Change of measuring unit works for valid unit.")
void setUnitWorksForValidUnit() {
testIngredient.setUnit(MeasuringUnit.TBS);
assertEquals(new Ingredient(FoodItem.ONION, 0.5f, MeasuringUnit.TBS), testIngredient);
}
@Test
@DisplayName("Change of measuring to invalid unit throws exception.")
void setUnitThrowsException() {
assertThrows(IllegalArgumentException.class, () -> testIngredient.setUnit(null));
}
@Nested
@DisplayName("Invalid alternations")
class testInvalidAlternations {
@Test
@DisplayName("Change of food type to invalid type throws exception.")
void setFoodTypeThrowsException() {
assertThrows(IllegalArgumentException.class, () -> testIngredient.setFoodType(null));
}
@Test
@DisplayName("Change of food amount to invalid amount throws exception.")
void setAmountThrowsException() {
assertThrows(IllegalArgumentException.class, () -> testIngredient.setAmount(0));
assertThrows(IllegalArgumentException.class, () -> testIngredient.setAmount(-1));
}
@Test
@DisplayName("Change of measuring to invalid unit throws exception.")
void setUnitThrowsException() {
assertThrows(IllegalArgumentException.class, () -> testIngredient.setUnit(null));
}
}
}
//TODO: Test for equals method?
}
\ No newline at end of file
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 RecipeIngredientTest {
RecipeIngredient testIngredient;
@BeforeEach
void beforeEach() {
testIngredient = new RecipeIngredient(FoodItem.ONION, 0.5f, MeasuringUnit.KG);
}
@Test
@DisplayName("Check that recipe ingredient is initiated not at hand")
void initiateAsNotAtHand() {
assertFalse(testIngredient.isAtHand());
}
@Test
@DisplayName("AtHand correctly set")
void setAtHandCorrectly() {
assertFalse(testIngredient.isAtHand());
testIngredient.setAtHand(false);
assertFalse(testIngredient.isAtHand());
}
@Test
@DisplayName("Validate that two ingredients are equal despite at hand status.")
void equalIngredientsDespiteStatus() {
RecipeIngredient secondTestIngredient = new RecipeIngredient(FoodItem.ONION, 0.5f, MeasuringUnit.KG);
testIngredient.setAtHand(true);
assertEquals(testIngredient, secondTestIngredient);
}
}
\ No newline at end of file
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