|
|
@@ -6,36 +6,49 @@ import static org.hamcrest.MatcherAssert.*;
|
|
|
import static org.hamcrest.core.IsNot.*;
|
|
|
import static org.hamcrest.core.StringContains.*;
|
|
|
|
|
|
+import static org.leumasjaffe.recipe.model.Duration.Display.*;
|
|
|
+
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
import org.junit.jupiter.params.ParameterizedTest;
|
|
|
import org.junit.jupiter.params.provider.EnumSource;
|
|
|
+import org.junit.jupiter.params.provider.ValueSource;
|
|
|
|
|
|
class DurationTest {
|
|
|
|
|
|
@Test
|
|
|
void testPlusConvertsToLowestUnit() {
|
|
|
- final Duration inSec = new Duration(Duration.Display.SECONDS, 10, 20);
|
|
|
- final Duration inMin = new Duration(Duration.Display.MINUTES, 60, 120);
|
|
|
+ final Duration inSec = new Duration(SECONDS, 10, 20);
|
|
|
+ final Duration inMin = new Duration(MINUTES, 60, 120);
|
|
|
|
|
|
- assertEquals(Duration.Display.SECONDS, inSec.plus(inMin).getDisplayAs());
|
|
|
- assertEquals(Duration.Display.SECONDS, inMin.plus(inSec).getDisplayAs());
|
|
|
+ assertEquals(SECONDS, inSec.plus(inMin).getDisplayAs());
|
|
|
+ assertEquals(SECONDS, inMin.plus(inSec).getDisplayAs());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void testPlusCanUseHigherUnitOnLargeRanges() {
|
|
|
+ assertEquals(SECONDS, new Duration(SECONDS, 0, 120).plus(Duration.ZERO).getDisplayAs());
|
|
|
+ assertEquals(MINUTES, new Duration(SECONDS, 0, 121).plus(Duration.ZERO).getDisplayAs());
|
|
|
+ assertEquals(MINUTES, new Duration(SECONDS, 0, 3600).plus(Duration.ZERO).getDisplayAs());
|
|
|
+ assertEquals(HALF_HOURS, new Duration(SECONDS, 0, 3601).plus(Duration.ZERO).getDisplayAs());
|
|
|
+ assertEquals(HALF_HOURS, new Duration(SECONDS, 0, 14400).plus(Duration.ZERO).getDisplayAs());
|
|
|
+ assertEquals(HOURS, new Duration(SECONDS, 0, 14401).plus(Duration.ZERO).getDisplayAs());
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
void testToStringNonApproxDoesNotHaveTilde() {
|
|
|
- final Duration inSec = new Duration(Duration.Display.SECONDS, 0, 0);
|
|
|
+ final Duration inSec = new Duration(SECONDS, 0, 0);
|
|
|
assertThat(inSec.toString(), not(containsString("~")));
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
void testNonZeroMinProducesRange() {
|
|
|
- final Duration inSec = new Duration(Duration.Display.SECONDS, 10, 0);
|
|
|
+ final Duration inSec = new Duration(SECONDS, 10, 0);
|
|
|
assertThat(inSec.toString(), containsString("-"));
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
void testZeroMinProducesSingleNumber() {
|
|
|
- final Duration inSec = new Duration(Duration.Display.SECONDS, 0, 0);
|
|
|
+ final Duration inSec = new Duration(SECONDS, 0, 0);
|
|
|
assertThat(inSec.toString(), not(containsString("-")));
|
|
|
}
|
|
|
|
|
|
@@ -48,9 +61,39 @@ class DurationTest {
|
|
|
|
|
|
@Test
|
|
|
void testUnitControlsOutputScale() {
|
|
|
- final Duration inSec = new Duration(Duration.Display.SECONDS, 10, 20);
|
|
|
- final Duration inMin = new Duration(Duration.Display.MINUTES, 10, 20);
|
|
|
+ final Duration inSec = new Duration(SECONDS, 10, 20);
|
|
|
+ final Duration inMin = new Duration(MINUTES, 10, 20);
|
|
|
assertEquals("10 - 20 s", inSec.toString());
|
|
|
assertEquals("0 - 0 min", inMin.toString());
|
|
|
}
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void testPerformsRoundingOnHigherDisplay() {
|
|
|
+ assertEquals("0 min", new Duration(MINUTES, 0, 29).toString());
|
|
|
+ assertEquals("0 hr", new Duration(HOURS, 0, 1799).toString());
|
|
|
+ assertEquals("1 min", new Duration(MINUTES, 0, 30).toString());
|
|
|
+ assertEquals("1 hr", new Duration(HOURS, 0, 1800).toString());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void testCanDisplayHalfHours() {
|
|
|
+ assertEquals("1 hr", new Duration(HALF_HOURS, 0, 3600).toString());
|
|
|
+ assertEquals("1 hr", new Duration(HOURS, 0, 3600).toString());
|
|
|
+
|
|
|
+ assertEquals("1.5 hr", new Duration(HALF_HOURS, 0, 5400).toString());
|
|
|
+ assertEquals("2 hr", new Duration(HOURS, 0, 5400).toString());
|
|
|
+ }
|
|
|
+
|
|
|
+ @ParameterizedTest
|
|
|
+ @ValueSource(ints= {900, 2699})
|
|
|
+ void testHalfHourDisplayIsUsedForRoundNear(int value) {
|
|
|
+ assertEquals("0.5 hr", new Duration(HALF_HOURS, 0, value).toString());
|
|
|
+ }
|
|
|
+
|
|
|
+ @ParameterizedTest
|
|
|
+ @ValueSource(ints= {899, 2700})
|
|
|
+ void testHalfHourDisplayIsNotUsedForCloserToWhole(int value) {
|
|
|
+ assertNotEquals("0.5 hr", new Duration(HALF_HOURS, 0, value).toString());
|
|
|
+ }
|
|
|
+
|
|
|
}
|