summaryrefslogtreecommitdiff
path: root/src/test/java/sevenUnitsGUI/PresenterTest.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/java/sevenUnitsGUI/PresenterTest.java')
-rw-r--r--src/test/java/sevenUnitsGUI/PresenterTest.java159
1 files changed, 88 insertions, 71 deletions
diff --git a/src/test/java/sevenUnitsGUI/PresenterTest.java b/src/test/java/sevenUnitsGUI/PresenterTest.java
index 9e25a08..9ac5b84 100644
--- a/src/test/java/sevenUnitsGUI/PresenterTest.java
+++ b/src/test/java/sevenUnitsGUI/PresenterTest.java
@@ -1,5 +1,5 @@
/**
- * Copyright (C) 2022 Adrien Hopkins
+ * Copyright (C) 2022-2025 Adrien Hopkins
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
@@ -17,13 +17,12 @@
package sevenUnitsGUI;
import static org.junit.jupiter.api.Assertions.assertEquals;
+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;
@@ -32,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;
@@ -53,11 +52,11 @@ 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
* @since 2022-02-10
+ * @since v0.4.0
*/
public final class PresenterTest {
private static final Path TEST_SETTINGS = Path.of("src", "test", "resources",
@@ -74,10 +73,10 @@ public final class PresenterTest {
/**
* @return rounding rules used by {@link #testRoundingRules}
- * @since v0.4.0
* @since 2022-04-16
+ * @since v0.4.0
*/
- 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);
@@ -85,81 +84,99 @@ 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
+ * @since v0.4.0
*/
- @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
- *
- * @since v0.4.0
+ * Test method for {@link Presenter#convertUnits}
+ *
* @since 2022-02-12
+ * @since v0.4.0
*/
- @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 2025-02-23
+ * @since v1.0.0
+ */
+ @Test
+ void testDisableDefault() {
+ final var viewBot = new ViewBot();
+ final var presenter = new Presenter(viewBot);
+ assumeTrue(presenter.database.containsUnitName("joule"),
+ "Attempted to test disabling default on unit not in default file.");
+ presenter.setUseDefaultDatafiles(false);
+ assertFalse(presenter.database.containsUnitName("joule"),
+ "Presenter disabled default datafiles, but still contains the joule.");
}
/**
* Tests that duplicate units are successfully removed, if that is asked for
- *
- * @since v0.4.0
+ *
* @since 2022-04-16
+ * @since v0.4.0
*/
@Test
void testDuplicateUnits() {
@@ -190,9 +207,9 @@ public final class PresenterTest {
/**
* Tests that one-way conversion correctly filters From and To units
- *
- * @since v0.4.0
+ *
* @since 2022-04-16
+ * @since v0.4.0
*/
@Test
void testOneWayConversion() {
@@ -224,9 +241,9 @@ public final class PresenterTest {
/**
* Tests the prefix-viewing functionality.
- *
- * @since v0.4.0
+ *
* @since 2022-04-16
+ * @since v0.4.0
*/
@Test
void testPrefixViewing() {
@@ -254,9 +271,9 @@ public final class PresenterTest {
/**
* Tests that rounding rules are used correctly.
- *
- * @since v0.4.0
+ *
* @since 2022-04-16
+ * @since v0.4.0
*/
@ParameterizedTest
@MethodSource("getRoundingRules")
@@ -273,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);
}
@@ -284,8 +301,8 @@ public final class PresenterTest {
* Tests that the Presenter correctly applies search rules.
*
* @param searchRule search rule to test
- * @since v0.4.0
* @since 2022-07-08
+ * @since v0.4.0
*/
@ParameterizedTest
@MethodSource("getSearchRules")
@@ -309,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);
@@ -317,9 +334,9 @@ public final class PresenterTest {
/**
* Tests that settings can be saved to and loaded from a file.
- *
- * @since v0.4.0
+ *
* @since 2022-04-16
+ * @since v0.4.0
*/
@Test
void testSettingsSaving() {
@@ -351,9 +368,9 @@ public final class PresenterTest {
/**
* Ensures the Presenter generates the correct data upon a unit-viewing.
- *
- * @since v0.4.0
+ *
* @since 2022-04-16
+ * @since v0.4.0
*/
@Test
void testUnitViewing() {
@@ -385,15 +402,15 @@ public final class PresenterTest {
/**
* Test for {@link Presenter#updateView()}
- *
- * @since v0.4.0
+ *
* @since 2022-02-12
+ * @since v0.4.0
*/
@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);
@@ -415,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);