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

Last change on this file since 15315 was 15315, checked in by Don-vip, 5 years ago

fix #18057 - add tag_regex mapcss method to get keys by regex for comparison functions (patch by taylor.smock)

  • Property svn:eol-style set to native
File size: 29.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.mappaint.mapcss;
3
4import static org.junit.Assert.assertEquals;
5import static org.junit.Assert.assertFalse;
6import static org.junit.Assert.assertNull;
7import static org.junit.Assert.assertTrue;
8import static org.openstreetmap.josm.gui.mappaint.mapcss.Condition.Context.PRIMITIVE;
9
10import java.awt.Color;
11import java.io.StringReader;
12import java.util.Arrays;
13import java.util.List;
14
15import org.junit.Assert;
16import org.junit.Rule;
17import org.junit.Test;
18import org.openstreetmap.josm.TestUtils;
19import org.openstreetmap.josm.data.coor.LatLon;
20import org.openstreetmap.josm.data.osm.DataSet;
21import org.openstreetmap.josm.data.osm.Node;
22import org.openstreetmap.josm.data.osm.OsmUtils;
23import org.openstreetmap.josm.data.osm.Relation;
24import org.openstreetmap.josm.data.osm.RelationMember;
25import org.openstreetmap.josm.data.osm.Way;
26import org.openstreetmap.josm.gui.mappaint.Environment;
27import org.openstreetmap.josm.gui.mappaint.MultiCascade;
28import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.ClassCondition;
29import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.KeyCondition;
30import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.KeyMatchType;
31import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.KeyValueCondition;
32import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.Op;
33import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.PseudoClassCondition;
34import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.RegexpKeyValueRegexpCondition;
35import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.SimpleKeyValueCondition;
36import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.ChildOrParentSelector;
37import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.MapCSSParser;
38import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.ParseException;
39import org.openstreetmap.josm.testutils.JOSMTestRules;
40import org.openstreetmap.josm.tools.ColorHelper;
41
42import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
43
44/**
45 * Unit tests of {@link MapCSSParser}.
46 */
47public 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 testCountRoles() throws Exception {
469 DataSet ds = new DataSet();
470 Way way1 = TestUtils.newWay("highway=residential name=1",
471 new Node(new LatLon(0, 0)), new Node((new LatLon(0.001, 0.001))));
472 for (Node node : way1.getNodes()) {
473 ds.addPrimitive(node);
474 }
475 ds.addPrimitive(way1);
476
477 Relation rel1 = TestUtils.newRelation("type=destination_sign", new RelationMember("", way1));
478 ds.addPrimitive(rel1);
479
480 /* Check with empty role and one object */
481 Environment e = new Environment(rel1, new MultiCascade(), Environment.DEFAULT_LAYER, null);
482 assertEquals(1, Functions.count_roles(e, ""));
483
484 /* Check with non-empty role and one object */
485 e = new Environment(rel1, new MultiCascade(), Environment.DEFAULT_LAYER, null);
486 assertEquals(0, Functions.count_roles(e, "from"));
487
488 /* Check with empty role and two objects */
489 Way way2 = TestUtils.newWay("highway=residential name=2", way1.firstNode(), way1.lastNode());
490 ds.addPrimitive(way2);
491 rel1.addMember(new RelationMember("", way2));
492 e = new Environment(rel1, new MultiCascade(), Environment.DEFAULT_LAYER, null);
493 assertEquals(2, Functions.count_roles(e, ""));
494
495 /* Check with non-empty role and two objects */
496 rel1.setMember(0, new RelationMember("from", way1));
497 e = new Environment(rel1, new MultiCascade(), Environment.DEFAULT_LAYER, null);
498 assertEquals(1, Functions.count_roles(e, "from"));
499
500 /* Check with multiple roles */
501 assertEquals(1, Functions.count_roles(e, "from", "to"));
502
503 /* Check with non-relation */
504 e = new Environment(way1, new MultiCascade(), Environment.DEFAULT_LAYER, null);
505 assertEquals(0, Functions.count_roles(e, "from", "to"));
506
507 /* Check with actual call to mapcss functions */
508 MapCSSStyleSource source = new MapCSSStyleSource("relation[type=destination_sign] {roles: count_roles(\"from\");}");
509 source.loadStyleSource();
510 assertEquals(1, source.rules.size());
511 e = new Environment(rel1, new MultiCascade(), Environment.DEFAULT_LAYER, null);
512 assertTrue(source.rules.get(0).selector.matches(e));
513 source.rules.get(0).declaration.execute(e);
514 assertEquals((Integer) 1, e.getCascade(Environment.DEFAULT_LAYER).get("roles", null, Integer.class));
515 }
516
517 @Test
518 public void testSiblingSelectorInterpolation() throws Exception {
519 ChildOrParentSelector s1 = (Selector.ChildOrParentSelector) getParser(
520 "*[tag(\"addr:housenumber\") > child_tag(\"addr:housenumber\")][regexp_test(\"even|odd\", parent_tag(\"addr:interpolation\"))]" +
521 " + *[addr:housenumber]").child_selector();
522 DataSet ds = new DataSet();
523 Node n1 = new Node(new LatLon(1, 2));
524 n1.put("addr:housenumber", "10");
525 Node n2 = new Node(new LatLon(1.1, 2.2));
526 n2.put("addr:housenumber", "100");
527 Node n3 = new Node(new LatLon(1.2, 2.3));
528 n3.put("addr:housenumber", "20");
529 Way w = new Way();
530 w.put("addr:interpolation", "even");
531 ds.addPrimitive(n1);
532 ds.addPrimitive(n2);
533 ds.addPrimitive(n3);
534 ds.addPrimitive(w);
535 w.addNode(n1);
536 w.addNode(n2);
537 w.addNode(n3);
538
539 assertTrue(s1.right.matches(new Environment(n3)));
540 assertTrue(s1.left.matches(new Environment(n2).withChild(n3).withParent(w)));
541 assertTrue(s1.matches(new Environment(n3)));
542 assertFalse(s1.matches(new Environment(n1)));
543 assertFalse(s1.matches(new Environment(n2)));
544 assertFalse(s1.matches(new Environment(w)));
545 }
546
547 @Test
548 public void testInvalidBaseSelector() throws Exception {
549 MapCSSStyleSource css = new MapCSSStyleSource("invalid_base[key=value] {}");
550 css.loadStyleSource();
551 assertFalse(css.getErrors().isEmpty());
552 assertTrue(css.getErrors().iterator().next().toString().contains("Unknown MapCSS base selector invalid_base"));
553 }
554
555 @Test
556 public void testMinMaxFunctions() throws Exception {
557 MapCSSStyleSource sheet = new MapCSSStyleSource("* {" +
558 "min_value: min(tag(x), tag(y), tag(z)); " +
559 "max_value: max(tag(x), tag(y), tag(z)); " +
560 "max_split: max(split(\";\", tag(widths))); " +
561 "}");
562 sheet.loadStyleSource();
563 MultiCascade mc = new MultiCascade();
564
565 sheet.apply(mc, OsmUtils.createPrimitive("way x=4 y=6 z=8 u=100"), 20, false);
566 assertEquals(Float.valueOf(4.0f), mc.getCascade(Environment.DEFAULT_LAYER).get("min_value", Float.NaN, Float.class));
567 assertEquals(Float.valueOf(8.0f), mc.getCascade(Environment.DEFAULT_LAYER).get("max_value", Float.NaN, Float.class));
568
569 sheet.apply(mc, OsmUtils.createPrimitive("way x=4 y=6 widths=1;2;8;56;3;a"), 20, false);
570 assertEquals(Float.valueOf(4f), mc.getCascade(Environment.DEFAULT_LAYER).get("min_value", -777f, Float.class));
571 assertEquals(Float.valueOf(6f), mc.getCascade(Environment.DEFAULT_LAYER).get("max_value", -777f, Float.class));
572 assertEquals(Float.valueOf(56f), mc.getCascade(Environment.DEFAULT_LAYER).get("max_split", -777f, Float.class));
573 }
574
575 /**
576 * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/12549">Bug #12549</a>.
577 * @throws ParseException if a parsing error occurs
578 */
579 @Test
580 public void testTicket12549() throws ParseException {
581 Condition condition = getParser("[name =~ /^(?i)(?u)fóo$/]").condition(PRIMITIVE);
582 assertTrue(condition.applies(getEnvironment("name", "fóo")));
583 assertTrue(condition.applies(getEnvironment("name", "fÓo")));
584 condition = getParser("[name =~ /^(\\p{Lower})+$/]").condition(PRIMITIVE);
585 assertFalse(condition.applies(getEnvironment("name", "fóo")));
586 condition = getParser("[name =~ /^(?U)(\\p{Lower})+$/]").condition(PRIMITIVE);
587 assertTrue(condition.applies(getEnvironment("name", "fóo")));
588 assertFalse(condition.applies(getEnvironment("name", "fÓo")));
589 }
590
591 /**
592 * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/17053">Bug #17053</a>.
593 */
594 @Test
595 public void testTicket17053() {
596 MapCSSStyleSource sheet = new MapCSSStyleSource(
597 "way {\n" +
598 " placement_offset: eval(\n" +
599 " cond(prop(\"placement_value\")=\"right_of:1\",eval((prop(lane_width_forward_1)/2)-prop(lane_offset_forward_1)),\n" +
600 " 0\n" +
601 " )\n" +
602 " );\n" +
603 "}");
604 sheet.loadStyleSource();
605 assertTrue(sheet.getErrors().toString(), sheet.getErrors().isEmpty());
606 }
607}
Note: See TracBrowser for help on using the repository browser.