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

Last change on this file since 18690 was 18690, checked in by taylor.smock, 13 months ago

See #16567: Convert all assertion calls to JUnit 5 (patch by gaben, modified)

The modifications are as follows:

  • Merge DomainValidatorTest.testIDN and DomainValidatorTest.testIDNJava6OrLater
  • Update some tests to use @ParameterizedTest (DomainValidatorTest)
  • Replace various exception blocks with assertThrows. These typically looked like
        try {
            // Something that should throw an exception here
            fail("An exception should have been thrown");
        } catch (Exception e) {
            // Verify the exception matches expectations here
        }
    
  • Replace assertTrue(val instanceof Clazz) with assertInstanceOf
  • Replace JUnit 4 @Suite with JUnit 5 @Suite

Both the original patch and the modified patch fix various lint issues.

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