Browse Source

Add tests for Amount and Ingredient, as well as dependencies for using JUnit5.

Sam Jaffe 5 năm trước cách đây
mục cha
commit
ac2f298eb4

+ 24 - 0
pom.xml

@@ -120,5 +120,29 @@
       <artifactId>container</artifactId>
       <version>0.3.0</version>
     </dependency>
+    <dependency>
+      <groupId>org.junit.platform</groupId>
+      <artifactId>junit-platform-runner</artifactId>
+      <version>1.7.0</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.junit.jupiter</groupId>
+      <artifactId>junit-jupiter-engine</artifactId>
+      <version>5.7.0</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.junit.jupiter</groupId>
+      <artifactId>junit-jupiter-api</artifactId>
+      <version>5.7.0</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.junit.jupiter</groupId>
+      <artifactId>junit-jupiter-params</artifactId>
+      <version>5.7.0</version>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 </project>

+ 11 - 0
src/test/java/org/leumasjaffe/recipe/TestSuite.java

@@ -0,0 +1,11 @@
+package org.leumasjaffe.recipe;
+
+import org.junit.platform.runner.JUnitPlatform;
+import org.junit.platform.suite.api.SelectPackages;
+import org.junit.runner.RunWith;
+
+@RunWith(JUnitPlatform.class)
+@SelectPackages("org.leumasjaffe.recipe")
+public class TestSuite {
+
+}

+ 96 - 0
src/test/java/org/leumasjaffe/recipe/model/AmountTest.java

@@ -0,0 +1,96 @@
+package org.leumasjaffe.recipe.model;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+import java.util.stream.Stream;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+
+class AmountTest {
+	
+	private static Stream<Arguments> unitOfCount() {
+		return Stream.of(Arguments.of(new Amount(Amount.Unit.COUNT, 1, null, null)));
+	}
+	
+	private static Stream<Arguments> unitOfVolume() {
+		return Stream.of(Amount.Volume.values())
+				.map(vol -> new Amount(Amount.Unit.VOLUME, 1, vol, null))
+				.map(Arguments::of);
+	}
+	
+	private static Stream<Arguments> unitOfWeight() {
+		return Stream.of(Amount.Weight.values())
+				.map(wgt -> new Amount(Amount.Unit.WEIGHT, 1, null, wgt))
+				.map(Arguments::of);
+	}
+	
+	private static Stream<Arguments> crossVolume() {
+		return unitOfVolume().flatMap(arg0 -> unitOfVolume()
+				.map(arg1 -> Arguments.of(arg0.get()[0], arg1.get()[0])));
+	}
+	
+	private static Stream<Arguments> crossWeight() {
+		return unitOfWeight().flatMap(arg0 -> unitOfWeight()
+				.map(arg1 -> Arguments.of(arg0.get()[0], arg1.get()[0])));
+	}
+	
+	private static Stream<Arguments> unitStrings() {
+		return Stream.of("1.0 g", "1.0 oz", "1.0 lb", "1.0 kg", 
+			"1.0 ml", "1.0 tsp", "1.0 Tbsp", "1.0 cup", 
+			"1.0 pinch", "1.0 dash", "1.0").map(Arguments::of);
+	}
+	
+	@ParameterizedTest
+	@MethodSource("unitStrings")
+	void testCanConstructFromString(final String text) {
+		assertDoesNotThrow(() -> new Amount(text));
+	}
+	
+	@ParameterizedTest
+	@MethodSource("unitStrings")
+	void testToStringMatchesFromString(final String text) {
+		assertEquals(text, new Amount(text).toString());
+	}
+	
+	@Test
+	void testConstructFromCtLosesOnToString() {
+		assertEquals("1.0", new Amount("1 ct").toString());
+	}
+	
+	@Test
+	void testCountUnitNameReturnCt() {
+		assertEquals("ct", new Amount("1 ct").unitName());
+	}
+
+	@ParameterizedTest
+	@MethodSource({"unitOfCount", "unitOfVolume", "unitOfWeight"})
+	void testCanAddTogetherAmounts(final Amount amt) {
+		assertDoesNotThrow(() -> amt.plus(amt));
+	}
+
+	@ParameterizedTest
+	@MethodSource({"unitOfCount", "unitOfVolume", "unitOfWeight"})
+	void testAddingEqualUnitsIsStraightAddition(final Amount amt) {
+		assertEquals(amt.plus(amt).getValue(), 2 * amt.getValue());
+	}
+
+	@ParameterizedTest
+	@MethodSource({"crossWeight", "crossVolume"})
+	void testAddingDifferentScalesWillMatchLeftScale(final Amount lhs, final Amount rhs) {
+		assertEquals(lhs.plus(rhs).getWgt(), lhs.getWgt());
+		assertEquals(lhs.plus(rhs).getVol(), lhs.getVol());
+	}
+	
+	@Test
+	void testThrowsWhenTryingToCrossUnits() {
+		final Amount ct = new Amount(Amount.Unit.COUNT, 1, null, null);
+		final Amount vol = new Amount(Amount.Unit.VOLUME, 1, Amount.Volume.ml, null);
+		final Amount wgt = new Amount(Amount.Unit.WEIGHT, 1, null, Amount.Weight.g);
+		assertThrows(IllegalArgumentException.class, () -> ct.plus(vol));
+		assertThrows(IllegalArgumentException.class, () -> ct.plus(wgt));
+		assertThrows(IllegalArgumentException.class, () -> vol.plus(wgt));
+	}
+}

+ 36 - 0
src/test/java/org/leumasjaffe/recipe/model/IngredientTest.java

@@ -0,0 +1,36 @@
+package org.leumasjaffe.recipe.model;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+import org.junit.jupiter.api.Test;
+
+class IngredientTest {
+	private final Amount _1g = new Amount("1 g");
+
+	@Test
+	void testCanAddTogetherIngredients() {
+		final Ingredient in = new Ingredient("TEST", "", _1g);
+		assertDoesNotThrow(() -> in.plus(in));
+	}
+
+	@Test
+	void testThrowsIfAddingDiffIngredients() {
+		final Ingredient lhs = new Ingredient("A", "", _1g);
+		final Ingredient rhs = new Ingredient("B", "", _1g);
+		assertThrows(IllegalArgumentException.class, () -> lhs.plus(rhs));
+	}
+
+	@Test
+	void testThrowsIfAddingDiffPreparations() {
+		final Ingredient lhs = new Ingredient("TEST", "A", _1g);
+		final Ingredient rhs = new Ingredient("TEST", "B", _1g);
+		assertThrows(IllegalArgumentException.class, () -> lhs.plus(rhs));
+	}
+
+	@Test
+	void testHasPreparationIfNotNullOrEmptyString() {
+		assertFalse(new Ingredient("TEST", null, _1g).hasPreparation());
+		assertFalse(new Ingredient("TEST", "", _1g).hasPreparation());
+		assertTrue(new Ingredient("TEST", "A", _1g).hasPreparation());
+	}
+}