summaryrefslogtreecommitdiff
path: root/src/test/java/sevenUnitsGUI/PresenterTest.java
blob: 82842d80145e3b96d247070e0ca2ade953507109 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/**
 * 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.Collection;
import java.util.List;
import java.util.Optional;
import java.util.OptionalDouble;
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.NamedObjectProduct;

/**
 * @author Adrien Hopkins
 *
 * @since 2022-02-10
 */
public final class PresenterTest {
	static final Set<Unit> testUnits = Set.of(Metric.METRE, Metric.KILOMETRE,
			Metric.METRE_PER_SECOND, Metric.KILOMETRE_PER_HOUR);
	
	static final Set<NamedObjectProduct<BaseDimension>> testDimensions = Set
			.of(Metric.Dimensions.LENGTH, Metric.Dimensions.VELOCITY);
	
	private static final List<String> unitNames(
			Collection<? extends Unit> units) {
		return units.stream().map(Unit::getShortName)
				.collect(Collectors.toList());
	}
	
	/**
	 * 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(testUnits);
		viewBot.setToUnits(testUnits);
		viewBot.setFromSelection(Optional.of(Metric.METRE));
		viewBot.setToSelection(Optional.of(Metric.KILOMETRE));
		viewBot.setInputValue(OptionalDouble.of(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 List<UnitValue> inputs = viewBot.getUnitConversionInputValues();
		final List<UnitValue> outputs = viewBot.getUnitConversionOutputValues();
		assertEquals(expectedInput, inputs.get(inputs.size() - 1));
		assertEquals(expectedOutput, outputs.get(outputs.size() - 1));
	}
	
	/**
	 * 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);
		}
		
		// set from and to units
		viewBot.setFromUnits(testUnits);
		viewBot.setToUnits(testUnits);
		viewBot.setDimensions(testDimensions);
		viewBot.setSelectedDimension(Optional.of(Metric.Dimensions.LENGTH));
		
		// filter to length units only, then get the filtered sets of units
		presenter.updateView();
		final Set<Unit> fromUnits = viewBot.getFromUnits();
		final Set<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): " + unitNames(fromUnits));
		assertEquals(2, toUnits.size(),
				"Invalid toUnits (length != 2): " + unitNames(toUnits));
		assertTrue(fromUnits.contains(Metric.METRE),
				"Invaild fromUnits (METRE missing): " + unitNames(fromUnits));
		assertTrue(toUnits.contains(Metric.METRE),
				"Invaild toUnits (METRE missing): " + unitNames(toUnits));
		assertTrue(fromUnits.contains(Metric.KILOMETRE),
				"Invaild fromUnits (KILOMETRE missing): " + unitNames(fromUnits));
		assertTrue(toUnits.contains(Metric.KILOMETRE),
				"Invaild toUnits (KILOMETRE missing): " + unitNames(toUnits));
	}
}