|
|
@@ -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));
|
|
|
+ }
|
|
|
+}
|