1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.mappaint.mapcss;
|
---|
3 |
|
---|
4 | import static org.junit.jupiter.api.Assertions.assertFalse;
|
---|
5 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
---|
6 | import static org.junit.jupiter.api.Assertions.fail;
|
---|
7 |
|
---|
8 | import java.io.StringReader;
|
---|
9 |
|
---|
10 | import org.junit.jupiter.api.BeforeEach;
|
---|
11 | import org.junit.jupiter.api.Test;
|
---|
12 | import org.junit.jupiter.api.extension.RegisterExtension;
|
---|
13 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
14 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
15 | import org.openstreetmap.josm.data.osm.Node;
|
---|
16 | import org.openstreetmap.josm.data.osm.OsmUtils;
|
---|
17 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
18 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
19 | import org.openstreetmap.josm.gui.mappaint.Environment;
|
---|
20 | import org.openstreetmap.josm.gui.mappaint.mapcss.Condition.Context;
|
---|
21 | import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.KeyValueCondition;
|
---|
22 | import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.Op;
|
---|
23 | import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.MapCSSParser;
|
---|
24 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
25 | import org.openstreetmap.josm.tools.Logging;
|
---|
26 |
|
---|
27 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Unit tests of {@link KeyValueCondition}.
|
---|
31 | */
|
---|
32 | class KeyValueConditionTest {
|
---|
33 |
|
---|
34 | private DataSet ds;
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Setup rule
|
---|
38 | */
|
---|
39 | @RegisterExtension
|
---|
40 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
41 | public JOSMTestRules test = new JOSMTestRules().projection();
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Setup test
|
---|
45 | */
|
---|
46 | @BeforeEach
|
---|
47 | public void setUp() {
|
---|
48 | ds = new DataSet();
|
---|
49 | }
|
---|
50 |
|
---|
51 | Relation relation(int id) {
|
---|
52 | Relation r = new Relation(id, 1);
|
---|
53 | ds.addPrimitive(r);
|
---|
54 | return r;
|
---|
55 | }
|
---|
56 |
|
---|
57 | Node node(int id) {
|
---|
58 | Node n = new Node(id, 1);
|
---|
59 | n.setCoor(LatLon.ZERO);
|
---|
60 | ds.addPrimitive(n);
|
---|
61 | return n;
|
---|
62 | }
|
---|
63 |
|
---|
64 | private static void shouldFail(Runnable r) {
|
---|
65 | try {
|
---|
66 | r.run();
|
---|
67 | fail("should throw exception");
|
---|
68 | } catch (MapCSSException e) {
|
---|
69 | Logging.trace(e);
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | @Test
|
---|
74 | public void create() {
|
---|
75 | ConditionFactory.createKeyValueCondition("a key", "a value", Op.EQ, Context.PRIMITIVE, false);
|
---|
76 |
|
---|
77 | ConditionFactory.createKeyValueCondition("role", "a role", Op.EQ, Context.LINK, false);
|
---|
78 | ConditionFactory.createKeyValueCondition("RoLe", "a role", Op.EQ, Context.LINK, false);
|
---|
79 |
|
---|
80 | shouldFail(() ->
|
---|
81 | ConditionFactory.createKeyValueCondition("an arbitry tag", "a role", Op.EQ, Context.LINK, false)
|
---|
82 | );
|
---|
83 | }
|
---|
84 |
|
---|
85 | @Test
|
---|
86 | public void applies_1() {
|
---|
87 | Relation r = relation(1);
|
---|
88 | Node n = node(1);
|
---|
89 | r.addMember(new RelationMember("my_role", n));
|
---|
90 |
|
---|
91 | Environment e = new Environment(n).withParent(r).withLinkContext().withIndex(0, r.getMembersCount());
|
---|
92 |
|
---|
93 | Condition cond = new ConditionFactory.RoleCondition("my_role", Op.EQ);
|
---|
94 | assertTrue(cond.applies(e));
|
---|
95 |
|
---|
96 | cond = new ConditionFactory.RoleCondition("another_role", Op.EQ);
|
---|
97 | assertFalse(cond.applies(e));
|
---|
98 | }
|
---|
99 |
|
---|
100 | @Test
|
---|
101 | public void applies_2() {
|
---|
102 | Relation r = relation(1);
|
---|
103 | Node n = node(1);
|
---|
104 | r.addMember(new RelationMember("my_role", n));
|
---|
105 |
|
---|
106 | Environment e = new Environment(n).withParent(r).withIndex(0, r.getMembersCount()).withLinkContext();
|
---|
107 |
|
---|
108 | Condition cond = ConditionFactory.createKeyValueCondition("role", "my_role", Op.NEQ, Context.LINK, false);
|
---|
109 | assertFalse(cond.applies(e));
|
---|
110 |
|
---|
111 | cond = ConditionFactory.createKeyValueCondition("role", "another_role", Op.NEQ, Context.LINK, false);
|
---|
112 | assertTrue(cond.applies(e));
|
---|
113 | }
|
---|
114 |
|
---|
115 | @Test
|
---|
116 | void testKeyRegexValueRegex() throws Exception {
|
---|
117 | Selector selPos = new MapCSSParser(new StringReader("*[/^source/ =~ /.*,.*/]")).selector();
|
---|
118 | Selector selNeg = new MapCSSParser(new StringReader("*[/^source/ !~ /.*,.*/]")).selector();
|
---|
119 | assertFalse(selPos.matches(new Environment(OsmUtils.createPrimitive("way foo=bar"))));
|
---|
120 | assertTrue(selPos.matches(new Environment(OsmUtils.createPrimitive("way source=1,2"))));
|
---|
121 | assertTrue(selPos.matches(new Environment(OsmUtils.createPrimitive("way source_foo_bar=1,2"))));
|
---|
122 | assertFalse(selPos.matches(new Environment(OsmUtils.createPrimitive("way source=1"))));
|
---|
123 | assertFalse(selPos.matches(new Environment(OsmUtils.createPrimitive("way source=1"))));
|
---|
124 | assertFalse(selNeg.matches(new Environment(OsmUtils.createPrimitive("way source=1,2"))));
|
---|
125 | assertFalse(selNeg.matches(new Environment(OsmUtils.createPrimitive("way foo=bar source=1,2"))));
|
---|
126 | assertTrue(selNeg.matches(new Environment(OsmUtils.createPrimitive("way foo=bar source=baz"))));
|
---|
127 | assertTrue(selNeg.matches(new Environment(OsmUtils.createPrimitive("way foo=bar src=1,2"))));
|
---|
128 | }
|
---|
129 |
|
---|
130 | @Test
|
---|
131 | void testValueFive() throws Exception {
|
---|
132 | // ticket #5985
|
---|
133 | Selector sel = new MapCSSParser(new StringReader("*[width=5]")).selector();
|
---|
134 | assertTrue(sel.matches(new Environment(OsmUtils.createPrimitive("way highway=track width=5"))));
|
---|
135 | assertFalse(sel.matches(new Environment(OsmUtils.createPrimitive("way highway=track width=2"))));
|
---|
136 | }
|
---|
137 |
|
---|
138 | @Test
|
---|
139 | void testValueZero() throws Exception {
|
---|
140 | // ticket #12267
|
---|
141 | Selector sel = new MapCSSParser(new StringReader("*[frequency=0]")).selector();
|
---|
142 | assertTrue(sel.matches(new Environment(OsmUtils.createPrimitive("way railway=rail frequency=0"))));
|
---|
143 | assertFalse(sel.matches(new Environment(OsmUtils.createPrimitive("way railway=rail frequency=50"))));
|
---|
144 | }
|
---|
145 | }
|
---|