summaryrefslogtreecommitdiff
path: root/src/test/java/sevenUnitsGUI
diff options
context:
space:
mode:
authorAdrien Hopkins <ahopk127@my.yorku.ca>2022-07-17 16:26:49 -0500
committerAdrien Hopkins <ahopk127@my.yorku.ca>2022-07-17 16:26:49 -0500
commita5f088ae43c285bc3708303bdcc99bd8936477d2 (patch)
tree8d3ac45478468fe772618aa6d44c4879152738b5 /src/test/java/sevenUnitsGUI
parentcc79db65bc347c50267d0a719278ef1d90cf6b1a (diff)
parentb76c06eb393c7c6d9a3ece66efec1fd20311b7e8 (diff)
Merge branch 'release-0.4.0' into stable
Diffstat (limited to 'src/test/java/sevenUnitsGUI')
-rw-r--r--src/test/java/sevenUnitsGUI/PrefixRepetitionTest.java123
-rw-r--r--src/test/java/sevenUnitsGUI/PrefixSearchTest.java158
-rw-r--r--src/test/java/sevenUnitsGUI/PresenterTest.java423
-rw-r--r--src/test/java/sevenUnitsGUI/RoundingTest.java287
-rw-r--r--src/test/java/sevenUnitsGUI/TabbedViewTest.java95
5 files changed, 1086 insertions, 0 deletions
diff --git a/src/test/java/sevenUnitsGUI/PrefixRepetitionTest.java b/src/test/java/sevenUnitsGUI/PrefixRepetitionTest.java
new file mode 100644
index 0000000..8ea3fd0
--- /dev/null
+++ b/src/test/java/sevenUnitsGUI/PrefixRepetitionTest.java
@@ -0,0 +1,123 @@
+/**
+ * Copyright (C) 2022 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
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+package sevenUnitsGUI;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static sevenUnitsGUI.DefaultPrefixRepetitionRule.COMPLEX_REPETITION;
+import static sevenUnitsGUI.DefaultPrefixRepetitionRule.NO_REPETITION;
+import static sevenUnitsGUI.DefaultPrefixRepetitionRule.NO_RESTRICTION;
+
+import java.util.List;
+
+import org.junit.jupiter.api.Test;
+
+import sevenUnits.unit.Metric;
+
+/**
+ * Tests for the default prefix repetition rules.
+ *
+ * @since v0.4.0
+ * @since 2022-07-17
+ */
+class PrefixRepetitionTest {
+ /**
+ * Ensures that the complex repetition rule disallows invalid prefix lists.
+ *
+ * @since v0.4.0
+ * @since 2022-07-17
+ */
+ @Test
+ void testInvalidComplexRepetition() {
+ assertFalse(COMPLEX_REPETITION.test(List.of(Metric.KILO, Metric.YOTTA)),
+ "Complex repetition does not factor order of prefixes");
+ assertFalse(COMPLEX_REPETITION.test(List.of(Metric.MEGA, Metric.KILO)),
+ "\"kilomega\" allowed (should use \"giga\")");
+ assertFalse(
+ COMPLEX_REPETITION
+ .test(List.of(Metric.YOTTA, Metric.MEGA, Metric.KILO)),
+ "\"kilomega\" allowed after yotta (should use \"giga\")");
+ assertFalse(COMPLEX_REPETITION.test(List.of(Metric.YOTTA, Metric.MILLI)),
+ "Complex repetition does not factor direction of prefixes");
+ }
+
+ /**
+ * Tests the {@code NO_REPETITION} rule.
+ *
+ * @since v0.4.0
+ * @since 2022-07-17
+ */
+ @Test
+ void testNoRepetition() {
+ assertTrue(NO_REPETITION.test(List.of(Metric.KILO)));
+ assertFalse(NO_REPETITION.test(List.of(Metric.KILO, Metric.YOTTA)));
+ assertTrue(NO_REPETITION.test(List.of(Metric.MILLI)));
+ assertTrue(NO_REPETITION.test(List.of()), "Empty list yields false");
+ }
+
+ /**
+ * Tests the {@code NO_RESTRICTION} rule.
+ *
+ * @since v0.4.0
+ * @since 2022-07-17
+ */
+ @Test
+ void testNoRestriction() {
+ assertTrue(NO_RESTRICTION.test(List.of(Metric.KILO)));
+ assertTrue(NO_RESTRICTION.test(List.of(Metric.KILO, Metric.YOTTA)));
+ assertTrue(NO_RESTRICTION.test(List.of(Metric.MILLI)));
+ assertTrue(NO_RESTRICTION.test(List.of()));
+ }
+
+ /**
+ * Ensures that the complex repetition rule allows valid prefix lists.
+ *
+ * @since v0.4.0
+ * @since 2022-07-17
+ */
+ @Test
+ void testValidComplexRepetition() {
+ // simple situations
+ assertTrue(COMPLEX_REPETITION.test(List.of(Metric.KILO)),
+ "Single prefix not allowed");
+ assertTrue(COMPLEX_REPETITION.test(List.of()), "No prefixes not allowed");
+
+ // valid complex repetition
+ assertTrue(COMPLEX_REPETITION.test(List.of(Metric.YOTTA, Metric.KILO)),
+ "Valid complex repetition (kiloyotta) not allowed");
+ assertTrue(COMPLEX_REPETITION.test(List.of(Metric.KILO, Metric.DEKA)),
+ "Valid complex repetition (dekakilo) not allowed");
+ assertTrue(
+ COMPLEX_REPETITION
+ .test(List.of(Metric.YOTTA, Metric.KILO, Metric.DEKA)),
+ "Valid complex repetition (dekakiloyotta) not allowed");
+ assertTrue(
+ COMPLEX_REPETITION.test(List.of(Metric.YOTTA, Metric.YOTTA,
+ Metric.KILO, Metric.DEKA)),
+ "Valid complex repetition (dekakiloyottayotta) not allowed");
+
+ // valid with negative prefixes
+ assertTrue(COMPLEX_REPETITION.test(List.of(Metric.YOCTO, Metric.MILLI)),
+ "Valid complex repetition (milliyocto) not allowed");
+ assertTrue(COMPLEX_REPETITION.test(List.of(Metric.MILLI, Metric.CENTI)),
+ "Valid complex repetition (centimilli) not allowed");
+ assertTrue(
+ COMPLEX_REPETITION
+ .test(List.of(Metric.YOCTO, Metric.MILLI, Metric.CENTI)),
+ "Valid complex repetition (centimilliyocto) not allowed");
+ }
+}
diff --git a/src/test/java/sevenUnitsGUI/PrefixSearchTest.java b/src/test/java/sevenUnitsGUI/PrefixSearchTest.java
new file mode 100644
index 0000000..ca238fe
--- /dev/null
+++ b/src/test/java/sevenUnitsGUI/PrefixSearchTest.java
@@ -0,0 +1,158 @@
+/**
+ * Copyright (C) 2022 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
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+package sevenUnitsGUI;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
+import static sevenUnitsGUI.PrefixSearchRule.COMMON_PREFIXES;
+import static sevenUnitsGUI.PrefixSearchRule.NO_PREFIXES;
+import static sevenUnitsGUI.PrefixSearchRule.getCoherentOnlyRule;
+import static sevenUnitsGUI.PrefixSearchRule.getUniversalRule;
+
+import java.util.Map;
+import java.util.Set;
+
+import org.junit.jupiter.api.Test;
+
+import sevenUnits.unit.BritishImperial;
+import sevenUnits.unit.LinearUnit;
+import sevenUnits.unit.Metric;
+
+/**
+ * Tests for {@link PrefixSearchRule}
+ *
+ * @since v0.4.0
+ * @since 2022-07-17
+ */
+class PrefixSearchTest {
+ /**
+ * A method that creates duplicate copies of the common prefix rule.
+ */
+ private static final PrefixSearchRule getCommonRuleCopy() {
+ return getCoherentOnlyRule(Set.of(Metric.KILO, Metric.MILLI));
+ }
+
+ /**
+ * Test method for
+ * {@link sevenUnitsGUI.PrefixSearchRule#apply(java.util.Map.Entry)}, for a
+ * coherent unit and {@link PrefixSearchRule#COMMON_PREFIXES}.
+ *
+ * @since v0.4.0
+ * @since 2022-07-17
+ */
+ @Test
+ final void testCoherentPrefixSearch() {
+ final var expected = Map.of("metre", Metric.METRE, "kilometre",
+ Metric.KILOMETRE, "millimetre", Metric.MILLIMETRE);
+ final var actual = COMMON_PREFIXES
+ .apply(Map.entry("metre", Metric.METRE));
+
+ assertEquals(expected, actual,
+ "Prefixes not correctly applied to coherent unit.");
+ }
+
+ /**
+ * Test method for
+ * {@link sevenUnitsGUI.PrefixSearchRule#equals(java.lang.Object)}.
+ *
+ * @since v0.4.0
+ * @since 2022-07-17
+ */
+ @Test
+ final void testEquals() {
+ assertEquals(getCommonRuleCopy(), getCommonRuleCopy(),
+ "equals considers something other than prefixes/rule");
+
+ assertNotEquals(getCoherentOnlyRule(Set.of()), getUniversalRule(Set.of()),
+ "equals ignores rule");
+ }
+
+ /**
+ * Test method for {@link sevenUnitsGUI.PrefixSearchRule#getPrefixes()}.
+ *
+ * @since v0.4.0
+ * @since 2022-07-17
+ */
+ @Test
+ final void testGetPrefixes() {
+ assertEquals(Set.of(), NO_PREFIXES.getPrefixes());
+ assertEquals(Metric.ALL_PREFIXES,
+ PrefixSearchRule.ALL_METRIC_PREFIXES.getPrefixes());
+ }
+
+ /**
+ * Test method for {@link sevenUnitsGUI.PrefixSearchRule#hashCode()}.
+ *
+ * @since v0.4.0
+ * @since 2022-07-17
+ */
+ @Test
+ final void testHashCode() {
+ assertEquals(getCommonRuleCopy().hashCode(),
+ getCommonRuleCopy().hashCode());
+ }
+
+ /**
+ * Tests prefix searching for a non-coherent unit and
+ * {@link PrefixSearchRule#COMMON_PREFIXES}.
+ *
+ * @since v0.4.0
+ * @since 2022-07-17
+ */
+ @Test
+ final void testNonCoherentPrefixSearch() {
+ final var input = Map.entry("inch", BritishImperial.Length.INCH);
+ final var expected = Map.ofEntries(input);
+ final var actual = COMMON_PREFIXES.apply(input);
+
+ assertEquals(expected, actual, "Prefixes applied to non-coherent unit.");
+ }
+
+ /**
+ * Tests that {@link PrefixSearchRule#NO_PREFIXES} returns the original unit.
+ *
+ * @since v0.4.0
+ * @since 2022-07-17
+ */
+ @Test
+ void testNoPrefixes() {
+ for (final String name : Set.of("name1", "hello", "there")) {
+ for (final LinearUnit unit : Set.of(Metric.METRE, Metric.KILOMETRE,
+ BritishImperial.Length.INCH)) {
+ final var testEntry = Map.entry(name, unit);
+ final var expected = Map.ofEntries(testEntry);
+ final var actual = NO_PREFIXES.apply(testEntry);
+ assertEquals(expected, actual, () -> String
+ .format("NO_PREFIXES.apply(%s) != %s", testEntry, actual));
+ }
+ }
+ }
+
+ /**
+ * Test method for {@link sevenUnitsGUI.PrefixSearchRule#toString()}.
+ *
+ * @since v0.4.0
+ * @since 2022-07-17
+ */
+ @Test
+ final void testToString() {
+ assertEquals(
+ "Apply the following prefixes: [kilo (\u00D7 1000.0), milli (\u00D7 0.001)]",
+ COMMON_PREFIXES.toString());
+ }
+
+}
diff --git a/src/test/java/sevenUnitsGUI/PresenterTest.java b/src/test/java/sevenUnitsGUI/PresenterTest.java
new file mode 100644
index 0000000..13d7986
--- /dev/null
+++ b/src/test/java/sevenUnitsGUI/PresenterTest.java
@@ -0,0 +1,423 @@
+/**
+ * Copyright (C) 2022 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
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+package sevenUnitsGUI;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+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;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+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;
+import sevenUnits.unit.UnitType;
+import sevenUnits.utils.NameSymbol;
+import sevenUnits.utils.Nameable;
+import sevenUnits.utils.ObjectProduct;
+import sevenUnits.utils.UncertainDouble;
+
+/**
+ * Various tests for the {@link Presenter}.
+ * <p>
+ * <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
+ */
+public final class PresenterTest {
+ private static final Path TEST_SETTINGS = Path.of("src", "test", "resources",
+ "test-settings.txt");
+ static final Set<Unit> testUnits = Set.of(Metric.METRE, Metric.KILOMETRE,
+ Metric.METRE_PER_SECOND, Metric.KILOMETRE_PER_HOUR);
+
+ static final Set<ObjectProduct<BaseDimension>> testDimensions = Set
+ .of(Metric.Dimensions.LENGTH, Metric.Dimensions.VELOCITY);
+
+ private static final Stream<Function<Map.Entry<String, LinearUnit>, Map<String, LinearUnit>>> SEARCH_RULES = Stream
+ .of(PrefixSearchRule.NO_PREFIXES, PrefixSearchRule.COMMON_PREFIXES,
+ PrefixSearchRule.ALL_METRIC_PREFIXES);
+
+ /**
+ * @return rounding rules used by {@link #testRoundingRules}
+ * @since v0.4.0
+ * @since 2022-04-16
+ */
+ private static final 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);
+
+ return Stream.of(SCIENTIFIC_ROUNDING, INTEGER_ROUNDING, SIG_FIG_ROUNDING);
+ }
+
+ private static final Stream<Function<Map.Entry<String, LinearUnit>, Map<String, LinearUnit>>> getSearchRules() {
+ return SEARCH_RULES;
+ }
+
+ private static final Set<String> names(Set<? extends Nameable> units) {
+ return units.stream().map(Nameable::getName).collect(Collectors.toSet());
+ }
+
+ /**
+ * Test method for {@link Presenter#convertExpressions}
+ *
+ * @since v0.4.0
+ * @since 2022-02-12
+ */
+ @Test
+ void testConvertExpressions() {
+ // setup
+ final ViewBot viewBot = new ViewBot();
+ final Presenter presenter = new Presenter(viewBot);
+
+ viewBot.setFromExpression("10000.0 m");
+ viewBot.setToExpression("km");
+
+ // 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());
+ }
+
+ /**
+ * Tests that unit-conversion Views can correctly convert units
+ *
+ * @since v0.4.0
+ * @since 2022-02-12
+ */
+ @Test
+ void testConvertUnits() {
+ // setup
+ final ViewBot viewBot = new ViewBot();
+ final Presenter presenter = new Presenter(viewBot);
+
+ viewBot.setFromUnitNames(names(testUnits));
+ viewBot.setToUnitNames(names(testUnits));
+ viewBot.setFromSelection("metre");
+ viewBot.setToSelection("kilometre");
+ viewBot.setInputValue("10000.0");
+
+ // convert units
+ 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());
+ }
+
+ /**
+ * Tests that duplicate units are successfully removed, if that is asked for
+ *
+ * @since v0.4.0
+ * @since 2022-04-16
+ */
+ @Test
+ void testDuplicateUnits() {
+ final var metre = Metric.METRE;
+ final var meter = Metric.METRE.withName(NameSymbol.of("meter", "m"));
+
+ // load 2 duplicate units
+ final var viewBot = new ViewBot();
+ final var presenter = new Presenter(viewBot);
+ presenter.database.clear();
+ presenter.database.addUnit("metre", metre);
+ presenter.database.addUnit("meter", meter);
+ presenter.setOneWayConversionEnabled(false);
+ presenter.setSearchRule(PrefixSearchRule.NO_PREFIXES);
+
+ // test that only one of them is included if duplicate units disabled
+ presenter.setShowDuplicates(false);
+ presenter.updateView();
+ assertEquals(1, viewBot.getFromUnitNames().size());
+ assertEquals(1, viewBot.getToUnitNames().size());
+
+ // test that both of them is included if duplicate units enabled
+ presenter.setShowDuplicates(true);
+ presenter.updateView();
+ assertEquals(2, viewBot.getFromUnitNames().size());
+ assertEquals(2, viewBot.getToUnitNames().size());
+ }
+
+ /**
+ * Tests that one-way conversion correctly filters From and To units
+ *
+ * @since v0.4.0
+ * @since 2022-04-16
+ */
+ @Test
+ void testOneWayConversion() {
+ // metre is metric, inch is non-metric, tempC is semi-metric
+ final var allNames = Set.of("metre", "inch", "tempC");
+ final var metricNames = Set.of("metre", "tempC");
+ final var nonMetricNames = Set.of("inch", "tempC");
+
+ // load view with one metric and one non-metric unit
+ final var viewBot = new ViewBot();
+ final var presenter = new Presenter(viewBot);
+ presenter.database.clear();
+ presenter.database.addUnit("metre", Metric.METRE);
+ presenter.database.addUnit("inch", BritishImperial.Length.INCH);
+ presenter.database.addUnit("tempC", Metric.CELSIUS);
+ presenter.setSearchRule(PrefixSearchRule.NO_PREFIXES);
+
+ // test that units are removed from each side when one-way conversion is
+ // enabled
+ presenter.setOneWayConversionEnabled(true);
+ assertEquals(nonMetricNames, viewBot.getFromUnitNames());
+ assertEquals(metricNames, viewBot.getToUnitNames());
+
+ // test that units are kept when one-way conversion is disabled
+ presenter.setOneWayConversionEnabled(false);
+ assertEquals(allNames, viewBot.getFromUnitNames());
+ assertEquals(allNames, viewBot.getToUnitNames());
+ }
+
+ /**
+ * Tests the prefix-viewing functionality.
+ *
+ * @since v0.4.0
+ * @since 2022-04-16
+ */
+ @Test
+ void testPrefixViewing() {
+ // setup
+ final var viewBot = new ViewBot();
+ final var presenter = new Presenter(viewBot);
+ viewBot.setViewablePrefixNames(Set.of("kilo", "milli"));
+ presenter.setNumberDisplayRule(UncertainDouble::toString);
+
+ // view prefix
+ viewBot.setViewedPrefixName("kilo");
+ presenter.prefixSelected(); // just in case
+
+ // get correct values
+ final var expectedNameSymbol = presenter.database.getPrefix("kilo")
+ .getNameSymbol();
+ final var expectedMultiplierString = String
+ .valueOf(Metric.KILO.getMultiplier());
+
+ // test that presenter's values are correct
+ final var prefixRecord = viewBot.prefixViewList().get(0);
+ assertEquals(expectedNameSymbol, prefixRecord.getNameSymbol());
+ assertEquals(expectedMultiplierString, prefixRecord.multiplierString());
+ }
+
+ /**
+ * Tests that rounding rules are used correctly.
+ *
+ * @since v0.4.0
+ * @since 2022-04-16
+ */
+ @ParameterizedTest
+ @MethodSource("getRoundingRules")
+ void testRoundingRules(Function<UncertainDouble, String> roundingRule) {
+ // setup
+ final var viewBot = new ViewBot();
+ final var presenter = new Presenter(viewBot);
+ presenter.setNumberDisplayRule(roundingRule);
+
+ // convert and round
+ viewBot.setInputValue("12345.6789");
+ viewBot.setFromSelection("metre");
+ viewBot.setToSelection("kilometre");
+ presenter.convertUnits();
+
+ // test the result of the rounding
+ final String expectedOutputString = roundingRule
+ .apply(UncertainDouble.fromRoundedString("12.3456789"));
+ final String actualOutputString = viewBot.unitConversionList().get(0)
+ .outputValueString();
+ assertEquals(expectedOutputString, actualOutputString);
+ }
+
+ /**
+ * Tests that the Presenter correctly applies search rules.
+ *
+ * @param searchRule search rule to test
+ * @since v0.4.0
+ * @since 2022-07-08
+ */
+ @ParameterizedTest
+ @MethodSource("getSearchRules")
+ void testSearchRules(
+ Function<Map.Entry<String, LinearUnit>, Map<String, LinearUnit>> searchRule) {
+ // setup
+ final var viewBot = new ViewBot();
+ final var presenter = new Presenter(viewBot);
+
+ presenter.setSearchRule(searchRule);
+ presenter.setOneWayConversionEnabled(false);
+
+ presenter.database.clear();
+ presenter.database.addUnit("metre", Metric.METRE);
+ presenter.database.addUnit("inch", BritishImperial.Length.INCH);
+ presenter.updateView();
+
+ // create expected output based on rule
+ final Set<String> expectedOutput = new HashSet<>();
+ expectedOutput.addAll(searchRule
+ .apply(Map.entry("inch", BritishImperial.Length.INCH)).keySet());
+ expectedOutput.addAll(
+ searchRule.apply(Map.entry("metre", Metric.METRE)).keySet());
+ final Set<String> actualOutput = viewBot.getFromUnitNames();
+
+ // test output
+ assertEquals(expectedOutput, actualOutput);
+ }
+
+ /**
+ * Tests that settings can be saved to and loaded from a file.
+ *
+ * @since v0.4.0
+ * @since 2022-04-16
+ */
+ @Test
+ void testSettingsSaving() {
+ // setup
+ final var viewBot = new ViewBot();
+ final var presenter = new Presenter(viewBot);
+
+ // set and save custom settings
+ presenter.setOneWayConversionEnabled(true);
+ presenter.setShowDuplicates(true);
+ presenter.setNumberDisplayRule(StandardDisplayRules.fixedPrecision(11));
+ presenter.setPrefixRepetitionRule(
+ DefaultPrefixRepetitionRule.COMPLEX_REPETITION);
+ presenter.saveSettings(TEST_SETTINGS);
+
+ // overwrite custom settings
+ presenter.setOneWayConversionEnabled(false);
+ presenter.setShowDuplicates(false);
+ presenter.setNumberDisplayRule(StandardDisplayRules.uncertaintyBased());
+
+ // load settings & test that they're the same
+ presenter.loadSettings(TEST_SETTINGS);
+ assertTrue(presenter.oneWayConversionEnabled());
+ assertTrue(presenter.duplicatesShown());
+ assertEquals(StandardDisplayRules.fixedPrecision(11),
+ presenter.getNumberDisplayRule());
+ }
+
+ /**
+ * Ensures the Presenter generates the correct data upon a unit-viewing.
+ *
+ * @since v0.4.0
+ * @since 2022-04-16
+ */
+ @Test
+ void testUnitViewing() {
+ // setup
+ final var viewBot = new ViewBot();
+ final var presenter = new Presenter(viewBot);
+ viewBot.setViewableUnitNames(names(testUnits));
+
+ // view unit
+ viewBot.setViewedUnitName("metre");
+ presenter.unitNameSelected(); // just in case this isn't triggered
+ // automatically
+
+ // get correct values
+ final var expectedNameSymbol = presenter.database.getUnit("metre")
+ .getNameSymbol();
+ final var expectedDefinition = "(Base unit)";
+ final var expectedDimensionName = presenter
+ .getDimensionName(Metric.METRE.getDimension());
+ final var expectedUnitType = UnitType.METRIC;
+
+ // test for correctness
+ final var viewRecord = viewBot.unitViewList().get(0);
+ assertEquals(expectedNameSymbol, viewRecord.getNameSymbol());
+ assertEquals(expectedDefinition, viewRecord.definition());
+ assertEquals(expectedDimensionName, viewRecord.dimensionName());
+ assertEquals(expectedUnitType, viewRecord.unitType());
+ }
+
+ /**
+ * 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);
+ presenter.setOneWayConversionEnabled(false);
+ presenter.setSearchRule(PrefixSearchRule.NO_PREFIXES);
+
+ // override default database units
+ presenter.database.clear();
+ for (final Unit unit : testUnits) {
+ presenter.database.addUnit(unit.getPrimaryName().orElseThrow(), unit);
+ }
+ for (final var dimension : testDimensions) {
+ presenter.database.addDimension(
+ dimension.getPrimaryName().orElseThrow(), dimension);
+ }
+
+ // set from and to units
+ viewBot.setFromUnitNames(names(testUnits));
+ viewBot.setToUnitNames(names(testUnits));
+ viewBot.setDimensionNames(names(testDimensions));
+ viewBot.setSelectedDimensionName(Metric.Dimensions.LENGTH.getName());
+
+ // 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();
+
+ // test that fromUnits/toUnits is [METRE, KILOMETRE]
+ assertEquals(Set.of("metre", "kilometre"), fromUnits);
+ assertEquals(Set.of("metre", "kilometre"), toUnits);
+ }
+}
diff --git a/src/test/java/sevenUnitsGUI/RoundingTest.java b/src/test/java/sevenUnitsGUI/RoundingTest.java
new file mode 100644
index 0000000..ca1a272
--- /dev/null
+++ b/src/test/java/sevenUnitsGUI/RoundingTest.java
@@ -0,0 +1,287 @@
+/**
+ * Copyright (C) 2022 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
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+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.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static sevenUnitsGUI.StandardDisplayRules.fixedDecimals;
+import static sevenUnitsGUI.StandardDisplayRules.fixedPrecision;
+import static sevenUnitsGUI.StandardDisplayRules.getStandardRule;
+import static sevenUnitsGUI.StandardDisplayRules.uncertaintyBased;
+
+import java.util.Objects;
+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.utils.UncertainDouble;
+import sevenUnitsGUI.StandardDisplayRules.FixedDecimals;
+import sevenUnitsGUI.StandardDisplayRules.FixedPrecision;
+import sevenUnitsGUI.StandardDisplayRules.UncertaintyBased;
+
+/**
+ * Tests that ensure the rounding rules work as intended.
+ *
+ * @since v0.4.0
+ * @since 2022-07-17
+ */
+class RoundingTest {
+ // rounding rules to test
+ private static final FixedDecimals ZERO_DECIMALS = fixedDecimals(0);
+ private static final FixedDecimals TWO_DECIMALS = fixedDecimals(2);
+ private static final FixedDecimals SIX_DECIMALS = fixedDecimals(6);
+
+ private static final FixedPrecision ONE_SIG_FIG = fixedPrecision(1);
+ private static final FixedPrecision THREE_SIG_FIGS = fixedPrecision(3);
+ private static final FixedPrecision TWELVE_SIG_FIGS = fixedPrecision(12);
+
+ private static final UncertaintyBased UNCERTAINTY_BASED = uncertaintyBased();
+
+ // numbers to test rounding with
+ private static final UncertainDouble INPUT1 = UncertainDouble.of(12.3456789,
+ 0.0);
+ private static final UncertainDouble INPUT2 = UncertainDouble.of(300.9,
+ 0.005);
+ private static final UncertainDouble INPUT3 = UncertainDouble.of(12345432.1,
+ 0.0);
+ private static final UncertainDouble INPUT4 = UncertainDouble.of(0.00001234,
+ 0.000001);
+
+ /**
+ * @return arguments for
+ * {@link #testFixedDecimalRounding(UncertainDouble, String, String, String)}
+ * @since v0.4.0
+ * @since 2022-07-17
+ */
+ private static final Stream<Arguments> fixedDecimalRoundingExamples() {
+ // input, zero decimal string, two decimal string, six decimal string
+ return Stream.of(Arguments.of(INPUT1, "12", "12.35", "12.345679"),
+ Arguments.of(INPUT2, "301", "300.90", "300.900000"),
+ Arguments.of(INPUT3, "12345432", "12345432.10", "12345432.100000"),
+ Arguments.of(INPUT4, "0", "0.00", "0.000012"));
+ }
+
+ /**
+ * @return arguments for
+ * {@link #testFixedPrecisionRounding(UncertainDouble, String, String, String)}
+ * @since v0.4.0
+ * @since 2022-07-17
+ */
+ private static final Stream<Arguments> fixedPrecisionRoundingExamples() {
+ // input, one sig fig string, three s.f. string, six s.f. string
+ return Stream.of(Arguments.of(INPUT1, "1E+1", "12.3", "12.3456789000"),
+ Arguments.of(INPUT2, "3E+2", "301", "300.900000000"),
+ Arguments.of(INPUT3, "1E+7", "1.23E+7", "12345432.1000"),
+ Arguments.of(INPUT4, "0.00001", "0.0000123", "0.0000123400000000"));
+ }
+
+ /**
+ * @return arguments for
+ * {@link #testUncertaintyRounding(UncertainDouble, String)}
+ * @since v0.4.0
+ * @since 2022-07-17
+ */
+ private static final Stream<Arguments> uncertaintyRoundingExamples() {
+ // input, uncertainty rounding string
+ return Stream.of(Arguments.of(INPUT1, "12.3456789"),
+ Arguments.of(INPUT2, "300.900"),
+ Arguments.of(INPUT3, "1.23454321E7"),
+ Arguments.of(INPUT4, "0.0000123"));
+ }
+
+ /**
+ * Test for {@link FixedDecimals#decimalPlaces()} and
+ * {@link FixedPrecision#significantFigures()}.
+ *
+ * @since v0.4.0
+ * @since 2022-07-17
+ */
+ @Test
+ void testDataMethods() {
+ // ensure # of decimal places can be accessed
+ assertEquals(0, ZERO_DECIMALS.decimalPlaces(),
+ "ZERO_DECIMALS has " + ZERO_DECIMALS.decimalPlaces() + " decimals");
+ assertEquals(2, TWO_DECIMALS.decimalPlaces(),
+ "TWO_DECIMALS has " + TWO_DECIMALS.decimalPlaces() + " decimals");
+ assertEquals(6, SIX_DECIMALS.decimalPlaces(),
+ "SIX_DECIMALS has " + SIX_DECIMALS.decimalPlaces() + " decimals");
+
+ // ensure # of sig figs can be accessed
+ assertEquals(1, ONE_SIG_FIG.significantFigures(), "ONE_SIG_FIG has "
+ + ONE_SIG_FIG.significantFigures() + " significant figures");
+ assertEquals(3, THREE_SIG_FIGS.significantFigures(), "THREE_SIG_FIGS has "
+ + THREE_SIG_FIGS.significantFigures() + " significant figures");
+ assertEquals(12, TWELVE_SIG_FIGS.significantFigures(),
+ "TWELVE_SIG_FIGS has " + TWELVE_SIG_FIGS.significantFigures()
+ + " significant figures");
+ }
+
+ /**
+ * Tests that the rounding methods' equals() methods work.
+ *
+ * @since v0.4.0
+ * @since 2022-07-17
+ */
+ @Test
+ void testEquals() {
+ // basic equals tests
+ assertTrue(ZERO_DECIMALS.equals(ZERO_DECIMALS),
+ "ZERO_DECIMALS does not equal itself");
+ assertFalse(TWO_DECIMALS.equals(SIX_DECIMALS),
+ "TWO_DECIMALS == SIX_DECIMALS");
+ assertTrue(Objects.equals(fixedDecimals(0), fixedDecimals(0)),
+ "FixedDecimals.equals() depends on something other than decimal places.");
+
+ assertTrue(ONE_SIG_FIG.equals(ONE_SIG_FIG),
+ "ONE_SIG_FIG does not equal itself");
+ assertFalse(THREE_SIG_FIGS.equals(TWELVE_SIG_FIGS),
+ "THREE_SIG_FIGS == TWELVE_SIG_FIGS");
+ assertTrue(Objects.equals(fixedPrecision(1), fixedPrecision(1)),
+ "FixedPrecision.equals() depends on something other than significant figures.");
+
+ // test that FixedDecimals is never equal to FixedPrecision
+ // this unlikely argument is the test - the equals should return false!
+ @SuppressWarnings("unlikely-arg-type")
+ final boolean differentRulesEqual = Objects.equals(fixedDecimals(4),
+ fixedPrecision(4));
+ assertFalse(differentRulesEqual, "fixedDecimals(4) == fixedPrecision(4)");
+ }
+
+ /**
+ * Ensures that fixed decimal rounding works as expected
+ *
+ * @param input number to test
+ * @param zeroDecimalString expected string for zero decimal places
+ * @param twoDecimalString expected string for two decimal places
+ * @param sixDecimalString expected string for six decimal places
+ * @since 2022-07-17
+ */
+ @ParameterizedTest
+ @MethodSource("fixedDecimalRoundingExamples")
+ void testFixedDecimalRounding(UncertainDouble input,
+ String zeroDecimalString, String twoDecimalString,
+ String sixDecimalString) {
+ // test the three rounding rules against the provided strings
+ assertEquals(zeroDecimalString, ZERO_DECIMALS.apply(input),
+ "ZERO_DECIMALS rounded " + input + " as "
+ + ZERO_DECIMALS.apply(input) + " (should be "
+ + zeroDecimalString + ")");
+ assertEquals(twoDecimalString, TWO_DECIMALS.apply(input),
+ "TWO_DECIMALS rounded " + input + " as " + TWO_DECIMALS.apply(input)
+ + " (should be " + twoDecimalString + ")");
+ assertEquals(sixDecimalString, SIX_DECIMALS.apply(input),
+ "TWO_DECIMALS rounded " + input + " as " + SIX_DECIMALS.apply(input)
+ + " (should be " + sixDecimalString + ")");
+ }
+
+ /**
+ * Ensures that fixed precision rounding works as expected
+ *
+ * @param input number to test
+ * @param oneSigFigString expected string for one significant figure
+ * @param threeSigFigString expected string for three significant figures
+ * @param twelveSigFigString expected string for twelve significant figures
+ * @since v0.4.0
+ * @since 2022-07-17
+ */
+ @ParameterizedTest
+ @MethodSource("fixedPrecisionRoundingExamples")
+ void testFixedPrecisionRounding(UncertainDouble input,
+ String oneSigFigString, String threeSigFigString,
+ String twelveSigFigString) {
+ // test the three rounding rules against the provided strings
+ assertEquals(oneSigFigString, ONE_SIG_FIG.apply(input),
+ "ONE_SIG_FIG rounded " + input + " as " + ONE_SIG_FIG.apply(input)
+ + " (should be " + oneSigFigString + ")");
+ assertEquals(threeSigFigString, THREE_SIG_FIGS.apply(input),
+ "THREE_SIG_FIGS rounded " + input + " as "
+ + THREE_SIG_FIGS.apply(input) + " (should be "
+ + threeSigFigString + ")");
+ assertEquals(twelveSigFigString, TWELVE_SIG_FIGS.apply(input),
+ "TWELVE_SIG_FIGS rounded " + input + " as "
+ + TWELVE_SIG_FIGS.apply(input) + " (should be "
+ + twelveSigFigString + ")");
+ }
+
+ /**
+ * Tests that {@link StandardDisplayRules#getStandardRule} gets rounding
+ * rules as intended.
+ *
+ * @since v0.4.0
+ * @since 2022-07-17
+ */
+ @Test
+ void testGetStandardRule() {
+ assertEquals(ZERO_DECIMALS, getStandardRule("Round to 0 decimal places"));
+ assertEquals(THREE_SIG_FIGS,
+ getStandardRule("Round to 3 significant figures"));
+ assertEquals(UNCERTAINTY_BASED,
+ getStandardRule("Uncertainty-Based Rounding"));
+
+ assertThrows(IllegalArgumentException.class,
+ () -> getStandardRule("Not a rounding rule"));
+ }
+
+ /**
+ * Tests that the rounding methods' equals() methods work.
+ *
+ * @since v0.4.0
+ * @since 2022-07-17
+ */
+ @Test
+ void testHashCode() {
+ assertEquals(ZERO_DECIMALS.hashCode(), ZERO_DECIMALS.hashCode());
+ assertEquals(ONE_SIG_FIG.hashCode(), ONE_SIG_FIG.hashCode());
+ assertEquals(UNCERTAINTY_BASED.hashCode(), UNCERTAINTY_BASED.hashCode());
+ }
+
+ /**
+ * Tests that the {@code toString()} methods of the three rounding rule
+ * classes work correctly.
+ *
+ * @since v0.4.0
+ * @since 2022-07-17
+ */
+ @Test
+ void testToString() {
+ assertEquals("Round to 0 decimal places", ZERO_DECIMALS.toString());
+ assertEquals("Round to 3 significant figures", THREE_SIG_FIGS.toString());
+ assertEquals("Uncertainty-Based Rounding", UNCERTAINTY_BASED.toString());
+ }
+
+ /**
+ * Tests that Uncertainty Rounding works as expected
+ *
+ * @param input input number
+ * @param output expected output string
+ * @since v0.4.0
+ * @since 2022-07-17
+ */
+ @ParameterizedTest
+ @MethodSource("uncertaintyRoundingExamples")
+ void testUncertaintyRounding(UncertainDouble input, String output) {
+ assertEquals(output, UNCERTAINTY_BASED.apply(input),
+ () -> String.format(
+ "Uncertainty Rounding rounded %s as %s (should be %s)", input,
+ UNCERTAINTY_BASED.apply(input), output));
+ }
+}
diff --git a/src/test/java/sevenUnitsGUI/TabbedViewTest.java b/src/test/java/sevenUnitsGUI/TabbedViewTest.java
new file mode 100644
index 0000000..00092a4
--- /dev/null
+++ b/src/test/java/sevenUnitsGUI/TabbedViewTest.java
@@ -0,0 +1,95 @@
+/**
+ * Copyright (C) 2022 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
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+package sevenUnitsGUI;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.junit.jupiter.api.Test;
+
+/**
+ * Test for the TabbedView
+ *
+ * @since v0.4.0
+ * @since 2022-07-17
+ */
+class TabbedViewTest {
+ /**
+ * @return a view with all settings set to standard values
+ *
+ * @since v0.4.0
+ * @since 2022-07-17
+ */
+ private static final TabbedView setupView() {
+ final var view = new TabbedView();
+ final var presenter = view.getPresenter();
+
+ presenter.setNumberDisplayRule(StandardDisplayRules.uncertaintyBased());
+ presenter.setPrefixRepetitionRule(
+ DefaultPrefixRepetitionRule.NO_RESTRICTION);
+ presenter.setSearchRule(PrefixSearchRule.COMMON_PREFIXES);
+ presenter.setOneWayConversionEnabled(false);
+ presenter.setShowDuplicates(true);
+
+ return view;
+ }
+
+ /**
+ * Simulates an expression conversion operation, and ensures it works
+ * properly.
+ *
+ * @since v0.4.0
+ * @since 2022-07-17
+ */
+ @Test
+ void testExpressionConversion() {
+ final var view = setupView();
+
+ // prepare for unit conversion
+ view.masterPane.setSelectedIndex(1);
+ view.fromEntry.setText("250.0 inch");
+ view.toEntry.setText("metre");
+
+ view.convertExpressionButton.doClick();
+
+ // check result of conversion
+ assertEquals("250.0 inch = 6.350 metre", view.expressionOutput.getText());
+ }
+
+ /**
+ * Simulates a unit conversion operation, and ensures it works properly.
+ *
+ * @since v0.4.0
+ * @since 2022-07-17
+ */
+ @Test
+ void testUnitConversion() {
+ final var view = setupView();
+
+ // prepare for unit conversion
+ view.masterPane.setSelectedIndex(0);
+ view.dimensionSelector.setSelectedItem("Length");
+ view.fromSearch.getSearchList().setSelectedValue("inch", true);
+ view.toSearch.getSearchList().setSelectedValue("metre", true);
+ view.valueInput.setText("250.0");
+
+ view.convertUnitButton.doClick();
+
+ // check result of conversion
+ assertEquals("250.0 inch = 6.350 metre", view.unitOutput.getText());
+ }
+
+}