source: josm/trunk/test/unit/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSParserTest.java@ 17745

Last change on this file since 17745 was 17745, checked in by simon04, 3 years ago

fix #20739 - MapCSS: evaluate class on current and on default layer

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