diff options
-rw-r--r-- | src/main/java/sevenUnitsGUI/ViewBot.java | 44 | ||||
-rw-r--r-- | src/test/java/sevenUnitsGUI/PresenterTest.java | 135 | ||||
-rw-r--r-- | src/test/java/sevenUnitsGUI/package-info.java | 23 |
3 files changed, 196 insertions, 6 deletions
diff --git a/src/main/java/sevenUnitsGUI/ViewBot.java b/src/main/java/sevenUnitsGUI/ViewBot.java index bc4103c..cc070e2 100644 --- a/src/main/java/sevenUnitsGUI/ViewBot.java +++ b/src/main/java/sevenUnitsGUI/ViewBot.java @@ -16,6 +16,7 @@ */ package sevenUnitsGUI; +import java.util.Collections; import java.util.List; import java.util.Objects; import java.util.Optional; @@ -56,6 +57,8 @@ final class ViewBot implements UnitConversionView, ExpressionConversionView { private List<? extends Unit> fromUnits; /** The units available in the To selection */ private List<? extends Unit> toUnits; + /** Saved output values of all unit conversions */ + private List<String> unitConversionOutputValues; /** * Creates a new {@code ViewBot} with a new presenter. @@ -75,6 +78,10 @@ final class ViewBot implements UnitConversionView, ExpressionConversionView { return this.dimensions; } + public List<String> getExpressionConversionOutputs() { + throw new UnsupportedOperationException("Not implemented yet"); + } + @Override public String getFromExpression() { return this.fromExpression; @@ -89,8 +96,8 @@ final class ViewBot implements UnitConversionView, ExpressionConversionView { * @return the units available for selection in From * @since 2022-01-29 */ - public List<? extends Unit> getFromUnits() { - return this.fromUnits; + public List<Unit> getFromUnits() { + return Collections.unmodifiableList(this.fromUnits); } @Override @@ -107,8 +114,8 @@ final class ViewBot implements UnitConversionView, ExpressionConversionView { } @Override - public Optional<? extends ObjectProduct<BaseDimension>> getSelectedDimension() { - return this.selectedDimension; + public Optional<ObjectProduct<BaseDimension>> getSelectedDimension() { + return this.selectedDimension.map(x -> x); } @Override @@ -125,8 +132,16 @@ final class ViewBot implements UnitConversionView, ExpressionConversionView { * @return the units available for selection in To * @since 2022-01-29 */ - public List<? extends Unit> getToUnits() { - return this.toUnits; + public List<Unit> getToUnits() { + return Collections.unmodifiableList(this.toUnits); + } + + /** + * @return the unitConversionOutputValues + * @since 2022-02-10 + */ + public List<String> getUnitConversionOutputValues() { + return this.unitConversionOutputValues; } @Override @@ -157,6 +172,14 @@ final class ViewBot implements UnitConversionView, ExpressionConversionView { "fromSelection cannot be null"); } + /** + * @param fromSelection the fromSelection to set + * @since 2022-02-10 + */ + public void setFromSelection(Unit fromSelection) { + this.setFromSelection(Optional.of(fromSelection)); + } + @Override public void setFromUnits(List<? extends Unit> units) { this.fromUnits = Objects.requireNonNull(units, "units may not be null"); @@ -170,6 +193,11 @@ final class ViewBot implements UnitConversionView, ExpressionConversionView { this.inputValue = inputValue; } + public void setSelectedDimension( + ObjectProduct<BaseDimension> selectedDimension) { + this.setSelectedDimension(Optional.of(selectedDimension)); + } + /** * @param selectedDimension the selectedDimension to set * @since 2022-01-29 @@ -200,6 +228,10 @@ final class ViewBot implements UnitConversionView, ExpressionConversionView { "toSelection cannot be null."); } + public void setToSelection(Unit toSelection) { + this.setToSelection(Optional.of(toSelection)); + } + @Override public void setToUnits(List<? extends Unit> units) { this.toUnits = Objects.requireNonNull(units, "units may not be null"); diff --git a/src/test/java/sevenUnitsGUI/PresenterTest.java b/src/test/java/sevenUnitsGUI/PresenterTest.java new file mode 100644 index 0000000..675e3ab --- /dev/null +++ b/src/test/java/sevenUnitsGUI/PresenterTest.java @@ -0,0 +1,135 @@ +/** + * 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.util.List; +import java.util.Optional; + +import org.junit.jupiter.api.Test; + +import sevenUnits.unit.BaseDimension; +import sevenUnits.unit.Metric; +import sevenUnits.unit.NameSymbol; +import sevenUnits.unit.Unit; +import sevenUnits.utils.NamedObjectProduct; + +/** + * @author Adrien Hopkins + * + * @since 2022-02-10 + */ +public final class PresenterTest { + List<Unit> testUnits = List.of(Metric.METRE, Metric.KILOMETRE, + Metric.METRE_PER_SECOND, Metric.KILOMETRE_PER_HOUR); + List<NamedObjectProduct<BaseDimension>> testDimensions = List.of( + Metric.Dimensions.LENGTH.withName(NameSymbol.ofName("Length")), + Metric.Dimensions.VELOCITY.withName(NameSymbol.ofName("Velocity"))); + + /** + * Test for {@link Presenter#applyDimensionFilter()} + * + * @since 2022-02-12 + */ + @Test + void testApplyDimensionFilter() { + // setup + final ViewBot viewBot = new ViewBot(); + final Presenter presenter = new Presenter(viewBot); + + viewBot.setFromUnits(this.testUnits); + viewBot.setToUnits(this.testUnits); + viewBot.setDimensions(this.testDimensions); + viewBot.setSelectedDimension(Optional.of(this.testDimensions.get(0))); + + // filter to length units only, then get the filtered sets of units + presenter.applyDimensionFilter(); + final List<Unit> fromUnits = viewBot.getFromUnits(); + final List<Unit> toUnits = viewBot.getToUnits(); + + // test that fromUnits/toUnits is [METRE, KILOMETRE] + // HOWEVER I don't care about the order so I'm testing it this way + assertEquals(2, fromUnits.size(), + "Invalid fromUnits (length != 2): " + fromUnits); + assertEquals(2, toUnits.size(), + "Invalid toUnits (length != 2): " + toUnits); + assertTrue(fromUnits.contains(Metric.METRE), + "Invaild fromUnits (METRE missing): " + fromUnits); + assertTrue(toUnits.contains(Metric.METRE), + "Invaild toUnits (METRE missing): " + toUnits); + assertTrue(fromUnits.contains(Metric.KILOMETRE), + "Invaild fromUnits (KILOMETRE missing): " + fromUnits); + assertTrue(toUnits.contains(Metric.KILOMETRE), + "Invaild toUnits (KILOMETRE missing): " + toUnits); + } + + /** + * Test method for {@link Presenter#convertExpressions} + * + * @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<String> outputs = viewBot.getExpressionConversionOutputs(); + assertEquals("10000.0 m = 10.0 km", outputs.get(outputs.size() - 1)); + } + + /** + * Tests that unit-conversion Views can correctly convert units + * + * @since 2022-02-12 + */ + @Test + void testConvertUnits() { + // setup + final ViewBot viewBot = new ViewBot(); + final Presenter presenter = new Presenter(viewBot); + + viewBot.setFromUnits(this.testUnits); + viewBot.setToUnits(this.testUnits); + viewBot.setFromSelection(Optional.of(Metric.METRE)); + viewBot.setToSelection(Optional.of(Metric.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 String expected = String + .valueOf(Metric.METRE.convertTo(Metric.KILOMETRE, 10000.0)); + + final List<String> outputs = viewBot.getUnitConversionOutputValues(); + assertEquals(expected, outputs.get(outputs.size() - 1)); + } +} diff --git a/src/test/java/sevenUnitsGUI/package-info.java b/src/test/java/sevenUnitsGUI/package-info.java new file mode 100644 index 0000000..96bdbd9 --- /dev/null +++ b/src/test/java/sevenUnitsGUI/package-info.java @@ -0,0 +1,23 @@ +/** + * 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/>. + */ +/** + * Tests for the new 7Units GUI + * + * @author Adrien Hopkins + * @since 2022-01-29 + */ +package sevenUnitsGUI;
\ No newline at end of file |