1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.autofilter;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertArrayEquals;
|
---|
5 | import static org.junit.Assert.assertEquals;
|
---|
6 |
|
---|
7 | import java.util.NoSuchElementException;
|
---|
8 |
|
---|
9 | import org.junit.Rule;
|
---|
10 | import org.junit.Test;
|
---|
11 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
12 | import org.openstreetmap.josm.data.osm.OsmUtils;
|
---|
13 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
14 |
|
---|
15 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * Unit tests of {@link AutoFilterRule} class.
|
---|
19 | */
|
---|
20 | public class AutoFilterRuleTest {
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Setup tests
|
---|
24 | */
|
---|
25 | @Rule
|
---|
26 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
27 | public JOSMTestRules test = new JOSMTestRules().i18n();
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Unit test of {@link AutoFilterRule#getTagValuesForPrimitive}.
|
---|
31 | */
|
---|
32 | @Test
|
---|
33 | public void testTagValuesForPrimitive() {
|
---|
34 | // #17109, support values like 0.5 or 1.5 - level values are multiplied by 2 when parsing, values are divided by 2 for formatting
|
---|
35 | final AutoFilterRule level = AutoFilterRule.getDefaultRule("level").orElseThrow(NoSuchElementException::new);
|
---|
36 | assertTagValuesForPrimitive(level, "way level=-4--5", -10, -9, -8);
|
---|
37 | assertTagValuesForPrimitive(level, "way level=-2", -4);
|
---|
38 | assertTagValuesForPrimitive(level, "node level=0", 0);
|
---|
39 | assertTagValuesForPrimitive(level, "way level=1", 2);
|
---|
40 | assertTagValuesForPrimitive(level, "way level=2;3", 4, 6);
|
---|
41 | assertTagValuesForPrimitive(level, "way level=6-9", 12, 13, 14, 15, 16, 17, 18);
|
---|
42 | assertTagValuesForPrimitive(level, "way level=10;12-13", 20, 24, 25, 26);
|
---|
43 | assertTagValuesForPrimitive(level, "way level=0;0.5;1;1.5;2;2.5;3", 0, 1, 2, 3, 4, 5, 6);
|
---|
44 | }
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Unit test of {@link AutoFilterRule#getTagValuesForPrimitive} to deal with {@code %} of key {@code incline}.
|
---|
48 | */
|
---|
49 | @Test
|
---|
50 | public void testTagValuesForPrimitiveInclineUnit() {
|
---|
51 | final AutoFilterRule incline = AutoFilterRule.getDefaultRule("incline").orElseThrow(NoSuchElementException::new);
|
---|
52 | assertTagValuesForPrimitive(incline, "way incline=up");
|
---|
53 | assertTagValuesForPrimitive(incline, "way incline=20", 20);
|
---|
54 | assertTagValuesForPrimitive(incline, "way incline=20%", 20);
|
---|
55 | }
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Unit test of {@link AutoFilterRule#getTagValuesForPrimitive} provides sensible defaults, see #17496.
|
---|
59 | */
|
---|
60 | @Test
|
---|
61 | public void testTagValuesForPrimitivesDefaults() {
|
---|
62 | final AutoFilterRule layer = AutoFilterRule.getDefaultRule("layer").orElseThrow(NoSuchElementException::new);
|
---|
63 | assertTagValuesForPrimitive(layer, "way foo=bar");
|
---|
64 | assertTagValuesForPrimitive(layer, "way bridge=yes", 1);
|
---|
65 | assertTagValuesForPrimitive(layer, "way power=line", 1);
|
---|
66 | assertTagValuesForPrimitive(layer, "way tunnel=yes", -1);
|
---|
67 | assertTagValuesForPrimitive(layer, "way tunnel=building_passage", 0);
|
---|
68 | assertTagValuesForPrimitive(layer, "way highway=residential", 0);
|
---|
69 | assertTagValuesForPrimitive(layer, "way railway=rail", 0);
|
---|
70 | assertTagValuesForPrimitive(layer, "way waterway=canal", 0);
|
---|
71 | }
|
---|
72 |
|
---|
73 | private void assertTagValuesForPrimitive(AutoFilterRule rule, String assertion, int... expected) {
|
---|
74 | final OsmPrimitive primitive = OsmUtils.createPrimitive(assertion);
|
---|
75 | final int[] actual = rule.getTagValuesForPrimitive(primitive).toArray();
|
---|
76 | assertArrayEquals(expected, actual);
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Unit test of {@link AutoFilterRule#formatValue}
|
---|
81 | */
|
---|
82 | @Test
|
---|
83 | public void testValueFormatter() {
|
---|
84 | final AutoFilterRule voltage = AutoFilterRule.getDefaultRule("voltage").orElseThrow(NoSuchElementException::new);
|
---|
85 | assertEquals("230V", voltage.formatValue(230));
|
---|
86 | assertEquals("1kV", voltage.formatValue(1000));
|
---|
87 | assertEquals("15kV", voltage.formatValue(15000));
|
---|
88 | assertEquals("380kV", voltage.formatValue(380000));
|
---|
89 | }
|
---|
90 | }
|
---|