diff options
author | Adrien Hopkins <adrien.p.hopkins@gmail.com> | 2025-05-09 21:20:00 -0500 |
---|---|---|
committer | Adrien Hopkins <adrien.p.hopkins@gmail.com> | 2025-05-09 21:20:00 -0500 |
commit | 06bd0e63f1b7bc83009ec3f4d7b048dea015529f (patch) | |
tree | cd7d703e6d6c329f5affb5d84131f14c25e5bd32 | |
parent | 4910b914392753986526bc28102ddef42e275e6c (diff) |
Add tests for Presenter
-rw-r--r-- | src/main/resources/unitsfile.txt | 4 | ||||
-rw-r--r-- | src/test/java/sevenUnitsGUI/PresenterTest.java | 119 |
2 files changed, 61 insertions, 62 deletions
diff --git a/src/main/resources/unitsfile.txt b/src/main/resources/unitsfile.txt index dc33abd..17fe98a 100644 --- a/src/main/resources/unitsfile.txt +++ b/src/main/resources/unitsfile.txt @@ -194,8 +194,8 @@ ch chain furlong 10 chain mile 1760 yard mi mile -ftin foot; inch -ydftin yard; foot; inch +ftin ft; in +ydftin yd; ft; in # Imperial area units acre chain * furlong diff --git a/src/test/java/sevenUnitsGUI/PresenterTest.java b/src/test/java/sevenUnitsGUI/PresenterTest.java index 8b16365..1d9b45b 100644 --- a/src/test/java/sevenUnitsGUI/PresenterTest.java +++ b/src/test/java/sevenUnitsGUI/PresenterTest.java @@ -21,10 +21,8 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assumptions.assumeTrue; -import java.math.RoundingMode; import java.nio.file.Path; import java.util.HashSet; -import java.util.List; import java.util.Map; import java.util.Set; import java.util.function.Function; @@ -33,12 +31,12 @@ 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; import sevenUnits.unit.BaseDimension; import sevenUnits.unit.BritishImperial; import sevenUnits.unit.LinearUnit; -import sevenUnits.unit.LinearUnitValue; import sevenUnits.unit.Metric; import sevenUnits.unit.Unit; import sevenUnits.unit.UnitDatabase; @@ -54,7 +52,7 @@ import sevenUnits.utils.UncertainDouble; * <em>Note: this test outputs a lot to the standard output, because creating a * {@link UnitDatabase} and converting with a {@link ViewBot} both trigger * println statements.</em> - * + * * @author Adrien Hopkins * * @since v0.4.0 @@ -78,7 +76,7 @@ public final class PresenterTest { * @since v0.4.0 * @since 2022-04-16 */ - private static final Stream<Function<UncertainDouble, String>> getRoundingRules() { + private static Stream<Function<UncertainDouble, String>> getRoundingRules() { final var SCIENTIFIC_ROUNDING = StandardDisplayRules.uncertaintyBased(); final var INTEGER_ROUNDING = StandardDisplayRules.fixedDecimals(0); final var SIG_FIG_ROUNDING = StandardDisplayRules.fixedPrecision(4); @@ -86,79 +84,80 @@ public final class PresenterTest { return Stream.of(SCIENTIFIC_ROUNDING, INTEGER_ROUNDING, SIG_FIG_ROUNDING); } - private static final Stream<Function<Map.Entry<String, LinearUnit>, Map<String, LinearUnit>>> getSearchRules() { + private static Stream<Function<Map.Entry<String, LinearUnit>, Map<String, LinearUnit>>> getSearchRules() { return SEARCH_RULES; } - private static final Set<String> names(Set<? extends Nameable> units) { + private static Set<String> names(Set<? extends Nameable> units) { return units.stream().map(Nameable::getName).collect(Collectors.toSet()); } + private static Stream<Arguments> testConvertExpressions() { + return Stream.of( + Arguments.of("10000.0 m", "km", "10000.0 m = 10.00000 km"), + Arguments.of("1.0 m", "ft; in", "1.0 m = 3 ft + 3 in"), + Arguments.of("1.0 m", "ftin", "1.0 m = 3 ft + 3 in")); + } + + private static Stream<Arguments> testConvertUnits() { + return Stream.of( + Arguments.of("m", "km", 10000.0, "10000.0 m = 10.00000 km"), + Arguments.of("m", "ftin", 1.0, "1.0 m = 3 ft + 3 in")); + } + /** * Test method for {@link Presenter#convertExpressions} - * + * * @since v0.4.0 * @since 2022-02-12 */ - @Test - void testConvertExpressions() { + @ParameterizedTest + @MethodSource + void testConvertExpressions(String from, String to, String expectedOutput) { // setup - final ViewBot viewBot = new ViewBot(); - final Presenter presenter = new Presenter(viewBot); + final var viewBot = new ViewBot(); + final var presenter = new Presenter(viewBot); - viewBot.setFromExpression("10000.0 m"); - viewBot.setToExpression("km"); + viewBot.setFromExpression(from); + viewBot.setToExpression(to); // convert expression presenter.convertExpressions(); // test result - final List<UnitConversionRecord> outputs = viewBot - .expressionConversionList(); - assertEquals("10000.0 m = 10.00000 km", - outputs.get(outputs.size() - 1).toString()); + final var outputs = viewBot.expressionConversionList(); + assertEquals(expectedOutput, outputs.get(outputs.size() - 1).toString()); } /** - * Tests that unit-conversion Views can correctly convert units - * + * Test method for {@link Presenter#convertUnits} + * * @since v0.4.0 * @since 2022-02-12 */ - @Test - void testConvertUnits() { + @ParameterizedTest + @MethodSource + void testConvertUnits(String from, String to, double value, + String expectedOutput) { // setup - final ViewBot viewBot = new ViewBot(); - final Presenter presenter = new Presenter(viewBot); + final var viewBot = new ViewBot(); + final var presenter = new Presenter(viewBot); - viewBot.setFromUnitNames(names(testUnits)); - viewBot.setToUnitNames(names(testUnits)); - viewBot.setFromSelection("metre"); - viewBot.setToSelection("kilometre"); - viewBot.setInputValue("10000.0"); + viewBot.setFromSelection(from); + viewBot.setToSelection(to); + viewBot.setInputValue(Double.toString(value)); - // convert units + // convert expression presenter.convertUnits(); - /* - * use result from system as expected - I'm not testing unit conversion - * here (that's for the backend tests), I'm just testing that it correctly - * calls the unit conversion system - */ - final LinearUnitValue expectedInput = LinearUnitValue.of(Metric.METRE, - UncertainDouble.fromRoundedString("10000.0")); - final LinearUnitValue expectedOutput = expectedInput - .convertTo(Metric.KILOMETRE); - final UnitConversionRecord expectedUC = UnitConversionRecord.valueOf( - expectedInput.getUnit().getName(), - expectedOutput.getUnit().getName(), "10000.0", - expectedOutput.getValue().toString(false, RoundingMode.HALF_EVEN)); - assertEquals(List.of(expectedUC), viewBot.unitConversionList()); + // test result + final var outputs = viewBot.unitConversionList(); + assertEquals(expectedOutput, outputs.get(outputs.size() - 1).toString()); } - + /** * Ensures that the default unitfile can be disabled. - * + * * @since v1.0.0 * @since 2025-02-23 */ @@ -175,7 +174,7 @@ public final class PresenterTest { /** * Tests that duplicate units are successfully removed, if that is asked for - * + * * @since v0.4.0 * @since 2022-04-16 */ @@ -208,7 +207,7 @@ public final class PresenterTest { /** * Tests that one-way conversion correctly filters From and To units - * + * * @since v0.4.0 * @since 2022-04-16 */ @@ -242,7 +241,7 @@ public final class PresenterTest { /** * Tests the prefix-viewing functionality. - * + * * @since v0.4.0 * @since 2022-04-16 */ @@ -272,7 +271,7 @@ public final class PresenterTest { /** * Tests that rounding rules are used correctly. - * + * * @since v0.4.0 * @since 2022-04-16 */ @@ -291,9 +290,9 @@ public final class PresenterTest { presenter.convertUnits(); // test the result of the rounding - final String expectedOutputString = roundingRule + final var expectedOutputString = roundingRule .apply(UncertainDouble.fromRoundedString("12.3456789")); - final String actualOutputString = viewBot.unitConversionList().get(0) + final var actualOutputString = viewBot.unitConversionList().get(0) .outputValueString(); assertEquals(expectedOutputString, actualOutputString); } @@ -327,7 +326,7 @@ public final class PresenterTest { .apply(Map.entry("inch", BritishImperial.Length.INCH)).keySet()); expectedOutput.addAll( searchRule.apply(Map.entry("metre", Metric.METRE)).keySet()); - final Set<String> actualOutput = viewBot.getFromUnitNames(); + final var actualOutput = viewBot.getFromUnitNames(); // test output assertEquals(expectedOutput, actualOutput); @@ -335,7 +334,7 @@ public final class PresenterTest { /** * Tests that settings can be saved to and loaded from a file. - * + * * @since v0.4.0 * @since 2022-04-16 */ @@ -369,7 +368,7 @@ public final class PresenterTest { /** * Ensures the Presenter generates the correct data upon a unit-viewing. - * + * * @since v0.4.0 * @since 2022-04-16 */ @@ -403,15 +402,15 @@ public final class PresenterTest { /** * Test for {@link Presenter#updateView()} - * + * * @since v0.4.0 * @since 2022-02-12 */ @Test void testUpdateView() { // setup - final ViewBot viewBot = new ViewBot(); - final Presenter presenter = new Presenter(viewBot); + final var viewBot = new ViewBot(); + final var presenter = new Presenter(viewBot); presenter.setOneWayConversionEnabled(false); presenter.setSearchRule(PrefixSearchRule.NO_PREFIXES); @@ -433,8 +432,8 @@ public final class PresenterTest { // filter to length units only, then get the filtered sets of units presenter.updateView(); - final Set<String> fromUnits = viewBot.getFromUnitNames(); - final Set<String> toUnits = viewBot.getToUnitNames(); + final var fromUnits = viewBot.getFromUnitNames(); + final var toUnits = viewBot.getToUnitNames(); // test that fromUnits/toUnits is [METRE, KILOMETRE] assertEquals(Set.of("metre", "kilometre"), fromUnits); |