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
156
157
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());
}
}
|