1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.mappaint.mapcss;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertEquals;
|
---|
5 | import static org.junit.Assert.assertFalse;
|
---|
6 | import static org.junit.Assert.assertNull;
|
---|
7 | import static org.junit.Assert.assertTrue;
|
---|
8 | import static org.openstreetmap.josm.gui.mappaint.mapcss.Condition.Context.PRIMITIVE;
|
---|
9 |
|
---|
10 | import java.awt.Color;
|
---|
11 | import java.io.StringReader;
|
---|
12 | import java.util.Arrays;
|
---|
13 | import java.util.List;
|
---|
14 |
|
---|
15 | import org.junit.Assert;
|
---|
16 | import org.junit.Rule;
|
---|
17 | import org.junit.Test;
|
---|
18 | import org.openstreetmap.josm.TestUtils;
|
---|
19 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
20 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
21 | import org.openstreetmap.josm.data.osm.Node;
|
---|
22 | import org.openstreetmap.josm.data.osm.OsmUtils;
|
---|
23 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
24 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
25 | import org.openstreetmap.josm.data.osm.Way;
|
---|
26 | import org.openstreetmap.josm.gui.mappaint.Environment;
|
---|
27 | import org.openstreetmap.josm.gui.mappaint.MultiCascade;
|
---|
28 | import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.ClassCondition;
|
---|
29 | import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.KeyCondition;
|
---|
30 | import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.KeyMatchType;
|
---|
31 | import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.KeyValueCondition;
|
---|
32 | import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.Op;
|
---|
33 | import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.PseudoClassCondition;
|
---|
34 | import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.RegexpKeyValueRegexpCondition;
|
---|
35 | import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.SimpleKeyValueCondition;
|
---|
36 | import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.ChildOrParentSelector;
|
---|
37 | import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.MapCSSParser;
|
---|
38 | import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.ParseException;
|
---|
39 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
40 | import org.openstreetmap.josm.tools.ColorHelper;
|
---|
41 |
|
---|
42 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Unit tests of {@link MapCSSParser}.
|
---|
46 | */
|
---|
47 | public class MapCSSParserTest {
|
---|
48 |
|
---|
49 | protected static Environment getEnvironment(String key, String value) {
|
---|
50 | return new Environment(OsmUtils.createPrimitive("way " + key + "=" + value));
|
---|
51 | }
|
---|
52 |
|
---|
53 | protected static MapCSSParser getParser(String stringToParse) {
|
---|
54 | return new MapCSSParser(new StringReader(stringToParse));
|
---|
55 | }
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Setup rule
|
---|
59 | */
|
---|
60 | @Rule
|
---|
61 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
62 | public JOSMTestRules test = new JOSMTestRules().projection();
|
---|
63 |
|
---|
64 | @Test
|
---|
65 | public void testDeclarations() throws Exception {
|
---|
66 | getParser("{ opacity: 0.5; color: rgb(1.0, 0.0, 0.0); }").declaration();
|
---|
67 | getParser("{ set tag=value; }").declaration(); //set a tag
|
---|
68 | getParser("{ set tag; }").declaration(); // set a tag to 'yes'
|
---|
69 | getParser("{ opacity: eval(\"tag('population')/100000\"); }").declaration();
|
---|
70 | getParser("{ set width_in_metres=eval(\"tag('lanes')*3\"); }").declaration();
|
---|
71 | }
|
---|
72 |
|
---|
73 | @Test
|
---|
74 | public void testClassCondition() throws Exception {
|
---|
75 | List<Condition> conditions = ((Selector.GeneralSelector) getParser("way[name=X].highway:closed").selector()).conds;
|
---|
76 | assertTrue(conditions.get(0) instanceof SimpleKeyValueCondition);
|
---|
77 | assertTrue(conditions.get(0).applies(getEnvironment("name", "X")));
|
---|
78 | assertTrue(conditions.get(1) instanceof ClassCondition);
|
---|
79 | assertTrue(conditions.get(2) instanceof PseudoClassCondition);
|
---|
80 | assertFalse(conditions.get(2).applies(getEnvironment("name", "X")));
|
---|
81 | }
|
---|
82 |
|
---|
83 | @Test
|
---|
84 | public void testPseudoClassCondition() throws Exception {
|
---|
85 | Condition c1 = ((Selector.GeneralSelector) getParser("way!:area-style").selector()).conds.get(0);
|
---|
86 | Condition c2 = ((Selector.GeneralSelector) getParser("way!:areaStyle").selector()).conds.get(0);
|
---|
87 | Condition c3 = ((Selector.GeneralSelector) getParser("way!:area_style").selector()).conds.get(0);
|
---|
88 | assertEquals("!:areaStyle", c1.toString());
|
---|
89 | assertEquals("!:areaStyle", c2.toString());
|
---|
90 | assertEquals("!:areaStyle", c3.toString());
|
---|
91 | }
|
---|
92 |
|
---|
93 | @Test
|
---|
94 | public void testClassMatching() throws Exception {
|
---|
95 | MapCSSStyleSource css = new MapCSSStyleSource(
|
---|
96 | "way[highway=footway] { set .path; color: #FF6644; width: 2; }\n" +
|
---|
97 | "way[highway=path] { set path; color: brown; width: 2; }\n" +
|
---|
98 | "way[\"set\"=escape] { }\n" +
|
---|
99 | "way.path { text:auto; text-color: green; text-position: line; text-offset: 5; }\n" +
|
---|
100 | "way!.path { color: orange; }\n"
|
---|
101 | );
|
---|
102 | css.loadStyleSource();
|
---|
103 | assertTrue(css.getErrors().isEmpty());
|
---|
104 | MultiCascade mc1 = new MultiCascade();
|
---|
105 | css.apply(mc1, OsmUtils.createPrimitive("way highway=path"), 1, false);
|
---|
106 | assertEquals("green", mc1.getCascade("default").get("text-color", null, String.class));
|
---|
107 | assertEquals("brown", mc1.getCascade("default").get("color", null, String.class));
|
---|
108 | MultiCascade mc2 = new MultiCascade();
|
---|
109 | css.apply(mc2, OsmUtils.createPrimitive("way highway=residential"), 1, false);
|
---|
110 | assertEquals("orange", mc2.getCascade("default").get("color", null, String.class));
|
---|
111 | assertNull(mc2.getCascade("default").get("text-color", null, String.class));
|
---|
112 | MultiCascade mc3 = new MultiCascade();
|
---|
113 | css.apply(mc3, OsmUtils.createPrimitive("way highway=footway"), 1, false);
|
---|
114 | assertEquals(ColorHelper.html2color("#FF6644"), mc3.getCascade("default").get("color", null, Color.class));
|
---|
115 | }
|
---|
116 |
|
---|
117 | @Test
|
---|
118 | public void testEqualCondition() throws Exception {
|
---|
119 | Condition condition = getParser("[surface=paved]").condition(PRIMITIVE);
|
---|
120 | assertTrue(condition instanceof SimpleKeyValueCondition);
|
---|
121 | assertEquals("surface", ((SimpleKeyValueCondition) condition).k);
|
---|
122 | assertEquals("paved", ((SimpleKeyValueCondition) condition).v);
|
---|
123 | assertTrue(condition.applies(getEnvironment("surface", "paved")));
|
---|
124 | assertFalse(condition.applies(getEnvironment("surface", "unpaved")));
|
---|
125 | }
|
---|
126 |
|
---|
127 | @Test
|
---|
128 | public void testNotEqualCondition() throws Exception {
|
---|
129 | KeyValueCondition condition = (KeyValueCondition) getParser("[surface!=paved]").condition(PRIMITIVE);
|
---|
130 | assertEquals(Op.NEQ, condition.op);
|
---|
131 | assertFalse(condition.applies(getEnvironment("surface", "paved")));
|
---|
132 | assertTrue(condition.applies(getEnvironment("surface", "unpaved")));
|
---|
133 | }
|
---|
134 |
|
---|
135 | @Test
|
---|
136 | public void testRegexCondition() throws Exception {
|
---|
137 | KeyValueCondition condition = (KeyValueCondition) getParser("[surface=~/paved|unpaved/]").condition(PRIMITIVE);
|
---|
138 | assertEquals(Op.REGEX, condition.op);
|
---|
139 | assertTrue(condition.applies(getEnvironment("surface", "unpaved")));
|
---|
140 | assertFalse(condition.applies(getEnvironment("surface", "grass")));
|
---|
141 | }
|
---|
142 |
|
---|
143 | @Test
|
---|
144 | public void testRegexConditionParenthesis() throws Exception {
|
---|
145 | KeyValueCondition condition = (KeyValueCondition) getParser("[name =~ /^\\(foo\\)/]").condition(PRIMITIVE);
|
---|
146 | assertTrue(condition.applies(getEnvironment("name", "(foo)")));
|
---|
147 | assertFalse(condition.applies(getEnvironment("name", "foo")));
|
---|
148 | assertFalse(condition.applies(getEnvironment("name", "((foo))")));
|
---|
149 | }
|
---|
150 |
|
---|
151 | @Test
|
---|
152 | public void testNegatedRegexCondition() throws Exception {
|
---|
153 | KeyValueCondition condition = (KeyValueCondition) getParser("[surface!~/paved|unpaved/]").condition(PRIMITIVE);
|
---|
154 | assertEquals(Op.NREGEX, condition.op);
|
---|
155 | assertFalse(condition.applies(getEnvironment("surface", "unpaved")));
|
---|
156 | assertTrue(condition.applies(getEnvironment("surface", "grass")));
|
---|
157 | }
|
---|
158 |
|
---|
159 | @Test
|
---|
160 | public void testBeginsEndsWithCondition() throws Exception {
|
---|
161 | KeyValueCondition condition = (KeyValueCondition) getParser("[foo ^= bar]").condition(PRIMITIVE);
|
---|
162 | assertEquals(Op.BEGINS_WITH, condition.op);
|
---|
163 | assertTrue(condition.applies(getEnvironment("foo", "bar123")));
|
---|
164 | assertFalse(condition.applies(getEnvironment("foo", "123bar")));
|
---|
165 | assertFalse(condition.applies(getEnvironment("foo", "123bar123")));
|
---|
166 | condition = (KeyValueCondition) getParser("[foo $= bar]").condition(PRIMITIVE);
|
---|
167 | assertEquals(Op.ENDS_WITH, condition.op);
|
---|
168 | assertFalse(condition.applies(getEnvironment("foo", "bar123")));
|
---|
169 | assertTrue(condition.applies(getEnvironment("foo", "123bar")));
|
---|
170 | assertFalse(condition.applies(getEnvironment("foo", "123bar123")));
|
---|
171 | }
|
---|
172 |
|
---|
173 | @Test
|
---|
174 | public void testOneOfCondition() throws Exception {
|
---|
175 | Condition condition = getParser("[vending~=stamps]").condition(PRIMITIVE);
|
---|
176 | assertTrue(condition.applies(getEnvironment("vending", "stamps")));
|
---|
177 | assertTrue(condition.applies(getEnvironment("vending", "bar;stamps;foo")));
|
---|
178 | assertFalse(condition.applies(getEnvironment("vending", "every;thing;else")));
|
---|
179 | assertFalse(condition.applies(getEnvironment("vending", "or_nothing")));
|
---|
180 | }
|
---|
181 |
|
---|
182 | @Test
|
---|
183 | public void testStandardKeyCondition() throws Exception {
|
---|
184 | KeyCondition c1 = (KeyCondition) getParser("[ highway ]").condition(PRIMITIVE);
|
---|
185 | assertEquals(KeyMatchType.EQ, c1.matchType);
|
---|
186 | assertTrue(c1.applies(getEnvironment("highway", "unclassified")));
|
---|
187 | assertFalse(c1.applies(getEnvironment("railway", "rail")));
|
---|
188 | KeyCondition c2 = (KeyCondition) getParser("[\"/slash/\"]").condition(PRIMITIVE);
|
---|
189 | assertEquals(KeyMatchType.EQ, c2.matchType);
|
---|
190 | assertTrue(c2.applies(getEnvironment("/slash/", "yes")));
|
---|
191 | assertFalse(c2.applies(getEnvironment("\"slash\"", "no")));
|
---|
192 | }
|
---|
193 |
|
---|
194 | @Test
|
---|
195 | public void testYesNoKeyCondition() throws Exception {
|
---|
196 | KeyCondition c1 = (KeyCondition) getParser("[oneway?]").condition(PRIMITIVE);
|
---|
197 | KeyCondition c2 = (KeyCondition) getParser("[oneway?!]").condition(PRIMITIVE);
|
---|
198 | KeyCondition c3 = (KeyCondition) getParser("[!oneway?]").condition(PRIMITIVE);
|
---|
199 | KeyCondition c4 = (KeyCondition) getParser("[!oneway?!]").condition(PRIMITIVE);
|
---|
200 | Environment yes = getEnvironment("oneway", "yes");
|
---|
201 | Environment no = getEnvironment("oneway", "no");
|
---|
202 | Environment none = getEnvironment("no-oneway", "foo");
|
---|
203 | assertTrue(c1.applies(yes));
|
---|
204 | assertFalse(c1.applies(no));
|
---|
205 | assertFalse(c1.applies(none));
|
---|
206 | assertFalse(c2.applies(yes));
|
---|
207 | assertTrue(c2.applies(no));
|
---|
208 | assertFalse(c2.applies(none));
|
---|
209 | assertFalse(c3.applies(yes));
|
---|
210 | assertTrue(c3.applies(no));
|
---|
211 | assertTrue(c3.applies(none));
|
---|
212 | assertTrue(c4.applies(yes));
|
---|
213 | assertFalse(c4.applies(no));
|
---|
214 | assertTrue(c4.applies(none));
|
---|
215 | }
|
---|
216 |
|
---|
217 | @Test
|
---|
218 | public void testRegexKeyCondition() throws Exception {
|
---|
219 | KeyCondition c1 = (KeyCondition) getParser("[/.*:(backward|forward)$/]").condition(PRIMITIVE);
|
---|
220 | assertEquals(KeyMatchType.REGEX, c1.matchType);
|
---|
221 | assertFalse(c1.applies(getEnvironment("lanes", "3")));
|
---|
222 | assertTrue(c1.applies(getEnvironment("lanes:forward", "3")));
|
---|
223 | assertTrue(c1.applies(getEnvironment("lanes:backward", "3")));
|
---|
224 | assertFalse(c1.applies(getEnvironment("lanes:foobar", "3")));
|
---|
225 | }
|
---|
226 |
|
---|
227 | @Test
|
---|
228 | public void testRegexKeyValueRegexpCondition() throws Exception {
|
---|
229 | RegexpKeyValueRegexpCondition c1 = (RegexpKeyValueRegexpCondition) getParser("[/^name/=~/Test/]").condition(PRIMITIVE);
|
---|
230 | assertEquals("^name", c1.keyPattern.pattern());
|
---|
231 | assertEquals("Test", c1.pattern.pattern());
|
---|
232 | assertTrue(c1.applies(getEnvironment("name", "Test St")));
|
---|
233 | assertFalse(c1.applies(getEnvironment("alt_name", "Test St")));
|
---|
234 | }
|
---|
235 |
|
---|
236 | @Test
|
---|
237 | public void testNRegexKeyConditionSelector() throws Exception {
|
---|
238 | Selector s1 = getParser("*[sport][tourism != hotel]").selector();
|
---|
239 | assertTrue(s1.matches(new Environment(OsmUtils.createPrimitive("node sport=foobar"))));
|
---|
240 | assertFalse(s1.matches(new Environment(OsmUtils.createPrimitive("node sport=foobar tourism=hotel"))));
|
---|
241 | Selector s2 = getParser("*[sport][tourism != hotel][leisure !~ /^(sports_centre|stadium|)$/]").selector();
|
---|
242 | assertTrue(s2.matches(new Environment(OsmUtils.createPrimitive("node sport=foobar"))));
|
---|
243 | assertFalse(s2.matches(new Environment(OsmUtils.createPrimitive("node sport=foobar tourism=hotel"))));
|
---|
244 | assertFalse(s2.matches(new Environment(OsmUtils.createPrimitive("node sport=foobar leisure=stadium"))));
|
---|
245 | }
|
---|
246 |
|
---|
247 | @Test
|
---|
248 | public void testKeyKeyCondition() throws Exception {
|
---|
249 | KeyValueCondition c1 = (KeyValueCondition) getParser("[foo = *bar]").condition(PRIMITIVE);
|
---|
250 | Way w1 = new Way();
|
---|
251 | w1.put("foo", "123");
|
---|
252 | w1.put("bar", "456");
|
---|
253 | assertFalse(c1.applies(new Environment(w1)));
|
---|
254 | w1.put("bar", "123");
|
---|
255 | assertTrue(c1.applies(new Environment(w1)));
|
---|
256 | KeyValueCondition c2 = (KeyValueCondition) getParser("[foo =~ */bar/]").condition(PRIMITIVE);
|
---|
257 | Way w2 = new Way(w1);
|
---|
258 | w2.put("bar", "[0-9]{3}");
|
---|
259 | assertTrue(c2.applies(new Environment(w2)));
|
---|
260 | w2.put("bar", "[0-9]");
|
---|
261 | assertTrue(c2.applies(new Environment(w2)));
|
---|
262 | w2.put("bar", "^[0-9]$");
|
---|
263 | assertFalse(c2.applies(new Environment(w2)));
|
---|
264 | }
|
---|
265 |
|
---|
266 | /**
|
---|
267 | * Make certain that getting tags by regex works
|
---|
268 | * @throws Exception if there is an assert error (or another error)
|
---|
269 | */
|
---|
270 | @Test
|
---|
271 | public void testTagRegex() throws Exception {
|
---|
272 | DataSet ds = new DataSet();
|
---|
273 | Way way1 = TestUtils.newWay("old_ref=A1 ref=A2", new Node(new LatLon(1, 1)), new Node(new LatLon(2, 2)));
|
---|
274 | for (Node node : way1.getNodes()) {
|
---|
275 | ds.addPrimitive(node);
|
---|
276 | }
|
---|
277 | ds.addPrimitive(way1);
|
---|
278 |
|
---|
279 | tagRegex(way1, "way[ref][count(tag_regex(\"ref\")) > 1] {}", new Boolean[] {true, false, false, true, false});
|
---|
280 | way1.keySet().stream().forEach(key -> way1.put(key, null));
|
---|
281 | way1.put("old_ref", "A1");
|
---|
282 | way1.put("ref", "A2");
|
---|
283 | tagRegex(way1, "way[ref][count(tag_regex(\"ref\", \"i\")) > 1] {}", new Boolean[] {true, false, false, true, true});
|
---|
284 | }
|
---|
285 |
|
---|
286 | private void tagRegex(Way way, String parserString, Boolean[] expected) throws Exception {
|
---|
287 | Selector selector = getParser(parserString).selector();
|
---|
288 | Assert.assertEquals(expected[0], selector.matches(new Environment(way)));
|
---|
289 | way.put("old_ref", null);
|
---|
290 | Assert.assertEquals(expected[1], selector.matches(new Environment(way)));
|
---|
291 | way.put("no_match_tag", "false");
|
---|
292 | Assert.assertEquals(expected[2], selector.matches(new Environment(way)));
|
---|
293 | way.put("old_ref", "A22");
|
---|
294 | Assert.assertEquals(expected[3], selector.matches(new Environment(way)));
|
---|
295 | way.put("old_ref", null);
|
---|
296 | way.put("OLD_REF", "A23");
|
---|
297 | Assert.assertEquals(expected[4], selector.matches(new Environment(way)));
|
---|
298 | }
|
---|
299 |
|
---|
300 | @Test
|
---|
301 | public void testParentTag() throws Exception {
|
---|
302 | Selector c1 = getParser("way[foo] > node[tag(\"foo\")=parent_tag(\"foo\")] {}").child_selector();
|
---|
303 | DataSet ds = new DataSet();
|
---|
304 | Way w1 = new Way();
|
---|
305 | Way w2 = new Way();
|
---|
306 | Node n1 = new Node(new LatLon(1, 1));
|
---|
307 | Node n2 = new Node(new LatLon(2, 2));
|
---|
308 | w1.put("foo", "123");
|
---|
309 | w2.put("foo", "123");
|
---|
310 | n1.put("foo", "123");
|
---|
311 | n2.put("foo", "0");
|
---|
312 | ds.addPrimitive(w1);
|
---|
313 | ds.addPrimitive(n1);
|
---|
314 | ds.addPrimitive(n2);
|
---|
315 | w1.addNode(n1);
|
---|
316 | w2.addNode(n2);
|
---|
317 | assertTrue(c1.matches(new Environment(n1)));
|
---|
318 | assertFalse(c1.matches(new Environment(n2)));
|
---|
319 | assertFalse(c1.matches(new Environment(w1)));
|
---|
320 | assertFalse(c1.matches(new Environment(w2)));
|
---|
321 | n1.put("foo", "0");
|
---|
322 | assertFalse(c1.matches(new Environment(n1)));
|
---|
323 | n1.put("foo", "123");
|
---|
324 | assertTrue(c1.matches(new Environment(n1)));
|
---|
325 | }
|
---|
326 |
|
---|
327 | @Test
|
---|
328 | public void testTicket8568() throws Exception {
|
---|
329 | MapCSSStyleSource sheet = new MapCSSStyleSource(
|
---|
330 | "way { width: 5; }\n" +
|
---|
331 | "way[keyA], way[keyB] { width: eval(prop(width)+10); }");
|
---|
332 | sheet.loadStyleSource();
|
---|
333 | MultiCascade mc = new MultiCascade();
|
---|
334 | sheet.apply(mc, OsmUtils.createPrimitive("way foo=bar"), 20, false);
|
---|
335 | assertEquals(Float.valueOf(5f), mc.getCascade(Environment.DEFAULT_LAYER).get("width"));
|
---|
336 | sheet.apply(mc, OsmUtils.createPrimitive("way keyA=true"), 20, false);
|
---|
337 | assertEquals(Float.valueOf(15f), mc.getCascade(Environment.DEFAULT_LAYER).get("width"));
|
---|
338 | sheet.apply(mc, OsmUtils.createPrimitive("way keyB=true"), 20, false);
|
---|
339 | assertEquals(Float.valueOf(15f), mc.getCascade(Environment.DEFAULT_LAYER).get("width"));
|
---|
340 | sheet.apply(mc, OsmUtils.createPrimitive("way keyA=true keyB=true"), 20, false);
|
---|
341 | assertEquals(Float.valueOf(15f), mc.getCascade(Environment.DEFAULT_LAYER).get("width"));
|
---|
342 | }
|
---|
343 |
|
---|
344 | @Test
|
---|
345 | public void testTicket8071() throws Exception {
|
---|
346 | MapCSSStyleSource sheet = new MapCSSStyleSource(
|
---|
347 | "*[rcn_ref], *[name] {text: concat(tag(rcn_ref), \" \", tag(name)); }");
|
---|
348 | sheet.loadStyleSource();
|
---|
349 | MultiCascade mc = new MultiCascade();
|
---|
350 | sheet.apply(mc, OsmUtils.createPrimitive("way name=Foo"), 20, false);
|
---|
351 | assertEquals(" Foo", mc.getCascade(Environment.DEFAULT_LAYER).get("text"));
|
---|
352 | sheet.apply(mc, OsmUtils.createPrimitive("way rcn_ref=15"), 20, false);
|
---|
353 | assertEquals("15 ", mc.getCascade(Environment.DEFAULT_LAYER).get("text"));
|
---|
354 | sheet.apply(mc, OsmUtils.createPrimitive("way rcn_ref=15 name=Foo"), 20, false);
|
---|
355 | assertEquals("15 Foo", mc.getCascade(Environment.DEFAULT_LAYER).get("text"));
|
---|
356 |
|
---|
357 | sheet = new MapCSSStyleSource("" +
|
---|
358 | "*[rcn_ref], *[name] {text: join(\" - \", tag(rcn_ref), tag(ref), tag(name)); }");
|
---|
359 | sheet.loadStyleSource();
|
---|
360 | sheet.apply(mc, OsmUtils.createPrimitive("way rcn_ref=15 ref=1.5 name=Foo"), 20, false);
|
---|
361 | assertEquals("15 - 1.5 - Foo", mc.getCascade(Environment.DEFAULT_LAYER).get("text"));
|
---|
362 | }
|
---|
363 |
|
---|
364 | @Test
|
---|
365 | public void testColorNameTicket9191() throws Exception {
|
---|
366 | Environment e = new Environment(null, new MultiCascade(), Environment.DEFAULT_LAYER, null);
|
---|
367 | getParser("{color: testcolour1#88DD22}").declaration().instructions.get(0).execute(e);
|
---|
368 | Color expected = new Color(0x88DD22);
|
---|
369 | assertEquals(expected, e.getCascade(Environment.DEFAULT_LAYER).get("color"));
|
---|
370 | }
|
---|
371 |
|
---|
372 | @Test
|
---|
373 | public void testColorNameTicket9191Alpha() throws Exception {
|
---|
374 | Environment e = new Environment(null, new MultiCascade(), Environment.DEFAULT_LAYER, null);
|
---|
375 | getParser("{color: testcolour2#12345678}").declaration().instructions.get(0).execute(e);
|
---|
376 | Color expected = new Color(0x12, 0x34, 0x56, 0x78);
|
---|
377 | assertEquals(expected, e.getCascade(Environment.DEFAULT_LAYER).get("color"));
|
---|
378 | }
|
---|
379 |
|
---|
380 | @Test
|
---|
381 | public void testColorParsing() throws Exception {
|
---|
382 | assertEquals(new Color(0x12, 0x34, 0x56, 0x78), ColorHelper.html2color("#12345678"));
|
---|
383 | }
|
---|
384 |
|
---|
385 | @Test
|
---|
386 | public void testChildSelectorGreaterThanSignIsOptional() throws Exception {
|
---|
387 | assertEquals(
|
---|
388 | getParser("relation[type=route] way[highway]").child_selector().toString(),
|
---|
389 | getParser("relation[type=route] > way[highway]").child_selector().toString());
|
---|
390 | }
|
---|
391 |
|
---|
392 | @Test
|
---|
393 | public void testSiblingSelector() throws Exception {
|
---|
394 | ChildOrParentSelector s1 = (Selector.ChildOrParentSelector) getParser(
|
---|
395 | "*[a?][parent_tag(\"highway\")=\"unclassified\"] + *[b?]").child_selector();
|
---|
396 | DataSet ds = new DataSet();
|
---|
397 | Node n1 = new Node(new LatLon(1, 2));
|
---|
398 | n1.put("a", "true");
|
---|
399 | Node n2 = new Node(new LatLon(1.1, 2.2));
|
---|
400 | n2.put("b", "true");
|
---|
401 | Way w = new Way();
|
---|
402 | w.put("highway", "unclassified");
|
---|
403 | ds.addPrimitive(n1);
|
---|
404 | ds.addPrimitive(n2);
|
---|
405 | ds.addPrimitive(w);
|
---|
406 | w.addNode(n1);
|
---|
407 | w.addNode(n2);
|
---|
408 |
|
---|
409 | Environment e = new Environment(n2);
|
---|
410 | assertTrue(s1.matches(e));
|
---|
411 | assertEquals(n2, e.osm);
|
---|
412 | assertEquals(n1, e.child);
|
---|
413 | assertEquals(w, e.parent);
|
---|
414 | assertFalse(s1.matches(new Environment(n1)));
|
---|
415 | assertFalse(s1.matches(new Environment(w)));
|
---|
416 | }
|
---|
417 |
|
---|
418 | @Test
|
---|
419 | public void testParentTags() throws Exception {
|
---|
420 | DataSet ds = new DataSet();
|
---|
421 | Node n = new Node(new LatLon(1, 2));
|
---|
422 | n.put("foo", "bar");
|
---|
423 | Way w1 = new Way();
|
---|
424 | w1.put("ref", "x10");
|
---|
425 | Way w2 = new Way();
|
---|
426 | w2.put("ref", "x2");
|
---|
427 | Way w3 = new Way();
|
---|
428 | ds.addPrimitive(n);
|
---|
429 | ds.addPrimitive(w1);
|
---|
430 | ds.addPrimitive(w2);
|
---|
431 | ds.addPrimitive(w3);
|
---|
432 | w1.addNode(n);
|
---|
433 | w2.addNode(n);
|
---|
434 | w3.addNode(n);
|
---|
435 |
|
---|
436 | MapCSSStyleSource source = new MapCSSStyleSource("node[foo=bar] {refs: join_list(\";\", parent_tags(\"ref\"));}");
|
---|
437 | source.loadStyleSource();
|
---|
438 | assertEquals(1, source.rules.size());
|
---|
439 | Environment e = new Environment(n, new MultiCascade(), Environment.DEFAULT_LAYER, null);
|
---|
440 | assertTrue(source.rules.get(0).selector.matches(e));
|
---|
441 | source.rules.get(0).declaration.execute(e);
|
---|
442 | assertEquals("x2;x10", e.getCascade(Environment.DEFAULT_LAYER).get("refs", null, String.class));
|
---|
443 | }
|
---|
444 |
|
---|
445 | @Test
|
---|
446 | public void testSort() throws Exception {
|
---|
447 | assertEquals(Arrays.asList(new String[] {"alpha", "beta"}), Functions.sort("beta", "alpha"));
|
---|
448 | Way way1 = TestUtils.newWay("highway=residential name=Alpha alt_name=Beta ref=\"A9;A8\"", new Node(new LatLon(0.001, 0.001)),
|
---|
449 | new Node(new LatLon(0.002, 0.002)));
|
---|
450 |
|
---|
451 | MapCSSStyleSource source = new MapCSSStyleSource("way[highway] {sorted: join_list(\",\", sort(tag(\"alt_name\"), tag(\"name\")));}");
|
---|
452 | source.loadStyleSource();
|
---|
453 | assertEquals(1, source.rules.size());
|
---|
454 | Environment e = new Environment(way1, new MultiCascade(), Environment.DEFAULT_LAYER, null);
|
---|
455 | assertTrue(source.rules.get(0).selector.matches(e));
|
---|
456 | source.rules.get(0).declaration.execute(e);
|
---|
457 | assertEquals(Functions.join(",", "Alpha", "Beta"), e.getCascade(Environment.DEFAULT_LAYER).get("sorted", null, String.class));
|
---|
458 |
|
---|
459 | source = new MapCSSStyleSource("way[ref] {sorted: join_list(\",\", sort_list(split(\";\", tag(\"ref\"))));}");
|
---|
460 | source.loadStyleSource();
|
---|
461 | e = new Environment(way1, new MultiCascade(), Environment.DEFAULT_LAYER, null);
|
---|
462 | assertTrue(source.rules.get(0).selector.matches(e));
|
---|
463 | source.rules.get(0).declaration.execute(e);
|
---|
464 | assertEquals(Functions.join(",", "A8", "A9"), e.getCascade(Environment.DEFAULT_LAYER).get("sorted", null, String.class));
|
---|
465 | }
|
---|
466 |
|
---|
467 | @Test
|
---|
468 | public void testUniqueValues() throws Exception {
|
---|
469 | assertEquals(Arrays.asList(new String[] {"alpha", "beta"}),
|
---|
470 | Functions.uniq("alpha", "alpha", "alpha", "beta"));
|
---|
471 | assertEquals(Arrays.asList(new String[] {"one", "two", "three"}),
|
---|
472 | Functions.uniq_list(Arrays.asList(new String[] {"one", "one", "two", "two", "two", "three"})));
|
---|
473 | }
|
---|
474 |
|
---|
475 | @Test
|
---|
476 | public void testCountRoles() throws Exception {
|
---|
477 | DataSet ds = new DataSet();
|
---|
478 | Way way1 = TestUtils.newWay("highway=residential name=1",
|
---|
479 | new Node(new LatLon(0, 0)), new Node((new LatLon(0.001, 0.001))));
|
---|
480 | for (Node node : way1.getNodes()) {
|
---|
481 | ds.addPrimitive(node);
|
---|
482 | }
|
---|
483 | ds.addPrimitive(way1);
|
---|
484 |
|
---|
485 | Relation rel1 = TestUtils.newRelation("type=destination_sign", new RelationMember("", way1));
|
---|
486 | ds.addPrimitive(rel1);
|
---|
487 |
|
---|
488 | /* Check with empty role and one object */
|
---|
489 | Environment e = new Environment(rel1, new MultiCascade(), Environment.DEFAULT_LAYER, null);
|
---|
490 | assertEquals(1, Functions.count_roles(e, ""));
|
---|
491 |
|
---|
492 | /* Check with non-empty role and one object */
|
---|
493 | e = new Environment(rel1, new MultiCascade(), Environment.DEFAULT_LAYER, null);
|
---|
494 | assertEquals(0, Functions.count_roles(e, "from"));
|
---|
495 |
|
---|
496 | /* Check with empty role and two objects */
|
---|
497 | Way way2 = TestUtils.newWay("highway=residential name=2", way1.firstNode(), way1.lastNode());
|
---|
498 | ds.addPrimitive(way2);
|
---|
499 | rel1.addMember(new RelationMember("", way2));
|
---|
500 | e = new Environment(rel1, new MultiCascade(), Environment.DEFAULT_LAYER, null);
|
---|
501 | assertEquals(2, Functions.count_roles(e, ""));
|
---|
502 |
|
---|
503 | /* Check with non-empty role and two objects */
|
---|
504 | rel1.setMember(0, new RelationMember("from", way1));
|
---|
505 | e = new Environment(rel1, new MultiCascade(), Environment.DEFAULT_LAYER, null);
|
---|
506 | assertEquals(1, Functions.count_roles(e, "from"));
|
---|
507 |
|
---|
508 | /* Check with multiple roles */
|
---|
509 | assertEquals(1, Functions.count_roles(e, "from", "to"));
|
---|
510 |
|
---|
511 | /* Check with non-relation */
|
---|
512 | e = new Environment(way1, new MultiCascade(), Environment.DEFAULT_LAYER, null);
|
---|
513 | assertEquals(0, Functions.count_roles(e, "from", "to"));
|
---|
514 |
|
---|
515 | /* Check with actual call to mapcss functions */
|
---|
516 | MapCSSStyleSource source = new MapCSSStyleSource("relation[type=destination_sign] {roles: count_roles(\"from\");}");
|
---|
517 | source.loadStyleSource();
|
---|
518 | assertEquals(1, source.rules.size());
|
---|
519 | e = new Environment(rel1, new MultiCascade(), Environment.DEFAULT_LAYER, null);
|
---|
520 | assertTrue(source.rules.get(0).selector.matches(e));
|
---|
521 | source.rules.get(0).declaration.execute(e);
|
---|
522 | assertEquals((Integer) 1, e.getCascade(Environment.DEFAULT_LAYER).get("roles", null, Integer.class));
|
---|
523 | }
|
---|
524 |
|
---|
525 | @Test
|
---|
526 | public void testSiblingSelectorInterpolation() throws Exception {
|
---|
527 | ChildOrParentSelector s1 = (Selector.ChildOrParentSelector) getParser(
|
---|
528 | "*[tag(\"addr:housenumber\") > child_tag(\"addr:housenumber\")][regexp_test(\"even|odd\", parent_tag(\"addr:interpolation\"))]" +
|
---|
529 | " + *[addr:housenumber]").child_selector();
|
---|
530 | DataSet ds = new DataSet();
|
---|
531 | Node n1 = new Node(new LatLon(1, 2));
|
---|
532 | n1.put("addr:housenumber", "10");
|
---|
533 | Node n2 = new Node(new LatLon(1.1, 2.2));
|
---|
534 | n2.put("addr:housenumber", "100");
|
---|
535 | Node n3 = new Node(new LatLon(1.2, 2.3));
|
---|
536 | n3.put("addr:housenumber", "20");
|
---|
537 | Way w = new Way();
|
---|
538 | w.put("addr:interpolation", "even");
|
---|
539 | ds.addPrimitive(n1);
|
---|
540 | ds.addPrimitive(n2);
|
---|
541 | ds.addPrimitive(n3);
|
---|
542 | ds.addPrimitive(w);
|
---|
543 | w.addNode(n1);
|
---|
544 | w.addNode(n2);
|
---|
545 | w.addNode(n3);
|
---|
546 |
|
---|
547 | assertTrue(s1.right.matches(new Environment(n3)));
|
---|
548 | assertTrue(s1.left.matches(new Environment(n2).withChild(n3).withParent(w)));
|
---|
549 | assertTrue(s1.matches(new Environment(n3)));
|
---|
550 | assertFalse(s1.matches(new Environment(n1)));
|
---|
551 | assertFalse(s1.matches(new Environment(n2)));
|
---|
552 | assertFalse(s1.matches(new Environment(w)));
|
---|
553 | }
|
---|
554 |
|
---|
555 | @Test
|
---|
556 | public void testInvalidBaseSelector() throws Exception {
|
---|
557 | MapCSSStyleSource css = new MapCSSStyleSource("invalid_base[key=value] {}");
|
---|
558 | css.loadStyleSource();
|
---|
559 | assertFalse(css.getErrors().isEmpty());
|
---|
560 | assertTrue(css.getErrors().iterator().next().toString().contains("Unknown MapCSS base selector invalid_base"));
|
---|
561 | }
|
---|
562 |
|
---|
563 | @Test
|
---|
564 | public void testMinMaxFunctions() throws Exception {
|
---|
565 | MapCSSStyleSource sheet = new MapCSSStyleSource("* {" +
|
---|
566 | "min_value: min(tag(x), tag(y), tag(z)); " +
|
---|
567 | "max_value: max(tag(x), tag(y), tag(z)); " +
|
---|
568 | "max_split: max(split(\";\", tag(widths))); " +
|
---|
569 | "}");
|
---|
570 | sheet.loadStyleSource();
|
---|
571 | MultiCascade mc = new MultiCascade();
|
---|
572 |
|
---|
573 | sheet.apply(mc, OsmUtils.createPrimitive("way x=4 y=6 z=8 u=100"), 20, false);
|
---|
574 | assertEquals(Float.valueOf(4.0f), mc.getCascade(Environment.DEFAULT_LAYER).get("min_value", Float.NaN, Float.class));
|
---|
575 | assertEquals(Float.valueOf(8.0f), mc.getCascade(Environment.DEFAULT_LAYER).get("max_value", Float.NaN, Float.class));
|
---|
576 |
|
---|
577 | sheet.apply(mc, OsmUtils.createPrimitive("way x=4 y=6 widths=1;2;8;56;3;a"), 20, false);
|
---|
578 | assertEquals(Float.valueOf(4f), mc.getCascade(Environment.DEFAULT_LAYER).get("min_value", -777f, Float.class));
|
---|
579 | assertEquals(Float.valueOf(6f), mc.getCascade(Environment.DEFAULT_LAYER).get("max_value", -777f, Float.class));
|
---|
580 | assertEquals(Float.valueOf(56f), mc.getCascade(Environment.DEFAULT_LAYER).get("max_split", -777f, Float.class));
|
---|
581 | }
|
---|
582 |
|
---|
583 | /**
|
---|
584 | * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/12549">Bug #12549</a>.
|
---|
585 | * @throws ParseException if a parsing error occurs
|
---|
586 | */
|
---|
587 | @Test
|
---|
588 | public void testTicket12549() throws ParseException {
|
---|
589 | Condition condition = getParser("[name =~ /^(?i)(?u)fóo$/]").condition(PRIMITIVE);
|
---|
590 | assertTrue(condition.applies(getEnvironment("name", "fóo")));
|
---|
591 | assertTrue(condition.applies(getEnvironment("name", "fÓo")));
|
---|
592 | condition = getParser("[name =~ /^(\\p{Lower})+$/]").condition(PRIMITIVE);
|
---|
593 | assertFalse(condition.applies(getEnvironment("name", "fóo")));
|
---|
594 | condition = getParser("[name =~ /^(?U)(\\p{Lower})+$/]").condition(PRIMITIVE);
|
---|
595 | assertTrue(condition.applies(getEnvironment("name", "fóo")));
|
---|
596 | assertFalse(condition.applies(getEnvironment("name", "fÓo")));
|
---|
597 | }
|
---|
598 |
|
---|
599 | /**
|
---|
600 | * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/17053">Bug #17053</a>.
|
---|
601 | */
|
---|
602 | @Test
|
---|
603 | public void testTicket17053() {
|
---|
604 | MapCSSStyleSource sheet = new MapCSSStyleSource(
|
---|
605 | "way {\n" +
|
---|
606 | " placement_offset: eval(\n" +
|
---|
607 | " cond(prop(\"placement_value\")=\"right_of:1\",eval((prop(lane_width_forward_1)/2)-prop(lane_offset_forward_1)),\n" +
|
---|
608 | " 0\n" +
|
---|
609 | " )\n" +
|
---|
610 | " );\n" +
|
---|
611 | "}");
|
---|
612 | sheet.loadStyleSource();
|
---|
613 | assertTrue(sheet.getErrors().toString(), sheet.getErrors().isEmpty());
|
---|
614 | }
|
---|
615 | }
|
---|