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