summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main/java/sevenUnitsGUI/PrefixSearchRule.java22
-rw-r--r--src/test/java/sevenUnitsGUI/PrefixSearchTest.java158
2 files changed, 162 insertions, 18 deletions
diff --git a/src/main/java/sevenUnitsGUI/PrefixSearchRule.java b/src/main/java/sevenUnitsGUI/PrefixSearchRule.java
index a62ccda..a5034c9 100644
--- a/src/main/java/sevenUnitsGUI/PrefixSearchRule.java
+++ b/src/main/java/sevenUnitsGUI/PrefixSearchRule.java
@@ -21,6 +21,7 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
+import java.util.Objects;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Predicate;
@@ -136,17 +137,8 @@ public final class PrefixSearchRule implements
if (!(obj instanceof PrefixSearchRule))
return false;
final PrefixSearchRule other = (PrefixSearchRule) obj;
- if (this.prefixableUnitRule == null) {
- if (other.prefixableUnitRule != null)
- return false;
- } else if (!this.prefixableUnitRule.equals(other.prefixableUnitRule))
- return false;
- if (this.prefixes == null) {
- if (other.prefixes != null)
- return false;
- } else if (!this.prefixes.equals(other.prefixes))
- return false;
- return true;
+ return Objects.equals(this.prefixableUnitRule, other.prefixableUnitRule)
+ && Objects.equals(this.prefixes, other.prefixes);
}
/**
@@ -169,13 +161,7 @@ public final class PrefixSearchRule implements
@Override
public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + (this.prefixableUnitRule == null ? 0
- : this.prefixableUnitRule.hashCode());
- result = prime * result
- + (this.prefixes == null ? 0 : this.prefixes.hashCode());
- return result;
+ return Objects.hash(this.prefixableUnitRule, this.prefixes);
}
@Override
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());
+ }
+
+}