/** * 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 . */ package sevenUnitsGUI; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assumptions.assumeTrue; import java.util.List; import java.util.Set; import java.util.stream.Collectors; import org.junit.jupiter.api.Test; import sevenUnits.unit.BaseDimension; import sevenUnits.unit.Metric; import sevenUnits.unit.Unit; import sevenUnits.unit.UnitValue; import sevenUnits.utils.Nameable; import sevenUnits.utils.NamedObjectProduct; /** * @author Adrien Hopkins * * @since 2022-02-10 */ public final class PresenterTest { static final Set testUnits = Set.of(Metric.METRE, Metric.KILOMETRE, Metric.METRE_PER_SECOND, Metric.KILOMETRE_PER_HOUR); static final Set> testDimensions = Set .of(Metric.Dimensions.LENGTH, Metric.Dimensions.VELOCITY); private static final Set names(Set units) { return units.stream().map(Nameable::getName).collect(Collectors.toSet()); } /** * 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 outputs = viewBot .expressionConversionList(); assertEquals("10000.0 m = 10.0 km", outputs.get(outputs.size() - 1).toString()); } /** * 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.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 UnitValue expectedInput = UnitValue.of(Metric.METRE, 10000.0); final UnitValue expectedOutput = expectedInput .convertTo(Metric.KILOMETRE); final UnitConversionRecord expectedUC = UnitConversionRecord .fromValues(expectedInput, expectedOutput); assertEquals(List.of(expectedUC), viewBot.unitConversionList()); } @Test void testDuplicateUnits() { assumeTrue(false, "Not yet implemented"); /* * enable and disable duplicate units and check for those in From and To, * include duplicate units in the input set */ } @Test void testOneWayConversion() { assumeTrue(false, "Not yet implemented"); /* * enable and disable one-way conversion, testing the units in From and To * on each setting to ensure they match the rule. Include at least one * metric exception. */ } /** * Test for {@link Presenter#updateView()} * * @since 2022-02-12 */ @Test void testUpdateView() { // setup final ViewBot viewBot = new ViewBot(); final Presenter presenter = new Presenter(viewBot); // 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 fromUnits = viewBot.getFromUnitNames(); final Set toUnits = viewBot.getToUnitNames(); // test that fromUnits/toUnits is [METRE, KILOMETRE] assertEquals(Set.of("metre", "kilometre"), fromUnits); assertEquals(Set.of("metre", "kilometre"), toUnits); } }