source: josm/trunk/test/unit/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSParserTest.groovy@ 7081

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

fixes for unit tests

File size: 14.6 KB
Line 
1package org.openstreetmap.josm.gui.mappaint.mapcss
2
3import java.awt.Color
4
5import org.junit.Before
6import org.junit.Test
7import org.openstreetmap.josm.JOSMFixture
8import org.openstreetmap.josm.Main
9import org.openstreetmap.josm.TestUtils
10import org.openstreetmap.josm.data.coor.LatLon
11import org.openstreetmap.josm.data.osm.DataSet
12import org.openstreetmap.josm.data.osm.OsmPrimitive
13import org.openstreetmap.josm.data.osm.Way
14import org.openstreetmap.josm.gui.mappaint.Environment
15import org.openstreetmap.josm.gui.mappaint.MultiCascade
16import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.MapCSSParser
17import org.openstreetmap.josm.tools.ColorHelper
18
19class MapCSSParserTest {
20
21 protected static OsmPrimitive getPrimitive(String key, String value) {
22 def w = new Way()
23 w.put(key, value)
24 return w
25 }
26
27 protected static Environment getEnvironment(String key, String value) {
28 return new Environment().withPrimitive(getPrimitive(key, value))
29 }
30
31 protected static MapCSSParser getParser(String stringToParse) {
32 return new MapCSSParser(new StringReader(stringToParse));
33 }
34
35 @Before
36 public void setUp() throws Exception {
37 JOSMFixture.createUnitTestFixture().init();
38 }
39
40 @Test
41 public void testKothicStylesheets() throws Exception {
42 new MapCSSParser(new URL("http://kothic.googlecode.com/hg/src/styles/default.mapcss").openStream(), "UTF-8")
43 new MapCSSParser(new URL("http://kothic.googlecode.com/hg/src/styles/mapink.mapcss").openStream(), "UTF-8")
44 }
45
46 @Test
47 public void testDeclarations() {
48 getParser("{ opacity: 0.5; color: rgb(1.0, 0.0, 0.0); }").declaration()
49 getParser("{ set tag=value; }").declaration() //set a tag
50 getParser("{ set tag; }").declaration() // set a tag to 'yes'
51 getParser("{ opacity: eval(\"tag('population')/100000\"); }").declaration()
52 getParser("{ set width_in_metres=eval(\"tag('lanes')*3\"); }").declaration()
53 }
54
55 @Test
56 public void testClassCondition() throws Exception {
57 def conditions = ((Selector.GeneralSelector) getParser("way[name=X].highway:closed").selector()).conds
58 assert conditions.get(0) instanceof Condition.SimpleKeyValueCondition
59 assert conditions.get(0).applies(getEnvironment("name", "X"))
60 assert conditions.get(1) instanceof Condition.ClassCondition
61 assert conditions.get(2) instanceof Condition.PseudoClassCondition
62 }
63
64 @Test
65 public void testClassMatching() throws Exception {
66 def css = new MapCSSStyleSource("")
67 getParser("" +
68 "way[highway=footway] { set .path; color: #FF6644; width: 2; }\n" +
69 "way[highway=path] { set path; color: brown; width: 2; }\n" +
70 "way[\"set\"=escape] { }\n" +
71 "way.path { text:auto; text-color: green; text-position: line; text-offset: 5; }\n" +
72 "way!.path { color: orange; }\n"
73 ).sheet(css)
74 assert css.getErrors().isEmpty()
75 def mc1 = new MultiCascade()
76 css.apply(mc1, getPrimitive("highway", "path"), 1, null, false);
77 assert "green".equals(mc1.getCascade("default").get("text-color", null, String.class))
78 assert "brown".equals(mc1.getCascade("default").get("color", null, String.class))
79 def mc2 = new MultiCascade()
80 css.apply(mc2, getPrimitive("highway", "residential"), 1, null, false);
81 assert "orange".equals(mc2.getCascade("default").get("color", null, String.class))
82 assert mc2.getCascade("default").get("text-color", null, String.class) == null
83 def mc3 = new MultiCascade()
84 css.apply(mc3, getPrimitive("highway", "footway"), 1, null, false);
85 assert ColorHelper.html2color("#FF6644").equals(mc3.getCascade("default").get("color", null, Color.class))
86 }
87
88 @Test
89 public void testEqualCondition() throws Exception {
90 def condition = (Condition.SimpleKeyValueCondition) getParser("[surface=paved]").condition(Condition.Context.PRIMITIVE)
91 assert condition instanceof Condition.SimpleKeyValueCondition
92 assert "surface".equals(condition.k)
93 assert "paved".equals(condition.v)
94 assert condition.applies(getEnvironment("surface", "paved"))
95 assert !condition.applies(getEnvironment("surface", "unpaved"))
96 }
97
98 @Test
99 public void testNotEqualCondition() throws Exception {
100 def condition = (Condition.KeyValueCondition) getParser("[surface!=paved]").condition(Condition.Context.PRIMITIVE)
101 assert Condition.Op.NEQ.equals(condition.op)
102 assert !condition.applies(getEnvironment("surface", "paved"))
103 assert condition.applies(getEnvironment("surface", "unpaved"))
104 }
105
106 @Test
107 public void testRegexCondition() throws Exception {
108 def condition = (Condition.KeyValueCondition) getParser("[surface=~/paved|unpaved/]").condition(Condition.Context.PRIMITIVE)
109 assert Condition.Op.REGEX.equals(condition.op)
110 assert condition.applies(getEnvironment("surface", "unpaved"))
111 assert !condition.applies(getEnvironment("surface", "grass"))
112 }
113
114 @Test
115 public void testNegatedRegexCondition() throws Exception {
116 def condition = (Condition.KeyValueCondition) getParser("[surface!~/paved|unpaved/]").condition(Condition.Context.PRIMITIVE)
117 assert Condition.Op.NREGEX.equals(condition.op)
118 assert !condition.applies(getEnvironment("surface", "unpaved"))
119 assert condition.applies(getEnvironment("surface", "grass"))
120 }
121
122 @Test
123 public void testStandardKeyCondition() throws Exception {
124 def c1 = (Condition.KeyCondition) getParser("[ highway ]").condition(Condition.Context.PRIMITIVE)
125 assert c1.matchType == null
126 assert c1.applies(getEnvironment("highway", "unclassified"))
127 assert !c1.applies(getEnvironment("railway", "rail"))
128 def c2 = (Condition.KeyCondition) getParser("[\"/slash/\"]").condition(Condition.Context.PRIMITIVE)
129 assert c2.matchType == null
130 assert c2.applies(getEnvironment("/slash/", "yes"))
131 assert !c2.applies(getEnvironment("\"slash\"", "no"))
132 }
133
134 @Test
135 public void testYesNoKeyCondition() throws Exception {
136 def c1 = (Condition.KeyCondition) getParser("[oneway?]").condition(Condition.Context.PRIMITIVE)
137 def c2 = (Condition.KeyCondition) getParser("[oneway?!]").condition(Condition.Context.PRIMITIVE)
138 def c3 = (Condition.KeyCondition) getParser("[!oneway?]").condition(Condition.Context.PRIMITIVE)
139 def c4 = (Condition.KeyCondition) getParser("[!oneway?!]").condition(Condition.Context.PRIMITIVE)
140 def yes = getEnvironment("oneway", "yes")
141 def no = getEnvironment("oneway", "no")
142 def none = getEnvironment("no-oneway", "foo")
143 assert c1.applies(yes)
144 assert !c1.applies(no)
145 assert !c1.applies(none)
146 assert !c2.applies(yes)
147 assert c2.applies(no)
148 assert !c2.applies(none)
149 assert !c3.applies(yes)
150 assert c3.applies(no)
151 assert c3.applies(none)
152 assert c4.applies(yes)
153 assert !c4.applies(no)
154 assert c4.applies(none)
155 }
156
157 @Test
158 public void testRegexKeyCondition() throws Exception {
159 def c1 = (Condition.KeyCondition) getParser("[/.*:(backward|forward)\$/]").condition(Condition.Context.PRIMITIVE)
160 assert Condition.KeyMatchType.REGEX.equals(c1.matchType)
161 assert !c1.applies(getEnvironment("lanes", "3"))
162 assert c1.applies(getEnvironment("lanes:forward", "3"))
163 assert c1.applies(getEnvironment("lanes:backward", "3"))
164 assert !c1.applies(getEnvironment("lanes:foobar", "3"))
165 }
166
167 @Test
168 public void testNRegexKeyConditionSelector() throws Exception {
169 def s1 = getParser("*[sport][tourism != hotel]").selector()
170 assert s1.matches(new Environment().withPrimitive(TestUtils.createPrimitive("node sport=foobar")))
171 assert !s1.matches(new Environment().withPrimitive(TestUtils.createPrimitive("node sport=foobar tourism=hotel")))
172 def s2 = getParser("*[sport][tourism != hotel][leisure !~ /^(sports_centre|stadium|)\$/]").selector()
173 assert s2.matches(new Environment().withPrimitive(TestUtils.createPrimitive("node sport=foobar")))
174 assert !s2.matches(new Environment().withPrimitive(TestUtils.createPrimitive("node sport=foobar tourism=hotel")))
175 assert !s2.matches(new Environment().withPrimitive(TestUtils.createPrimitive("node sport=foobar leisure=stadium")))
176 }
177
178 @Test
179 public void testKeyKeyCondition() throws Exception {
180 def c1 = (Condition.KeyValueCondition) getParser("[foo = *bar]").condition(Condition.Context.PRIMITIVE)
181 def w1 = new Way()
182 w1.put("foo", "123")
183 w1.put("bar", "456")
184 assert !c1.applies(new Environment().withPrimitive(w1))
185 w1.put("bar", "123")
186 assert c1.applies(new Environment().withPrimitive(w1))
187 def c2 = (Condition.KeyValueCondition) getParser("[foo =~ */bar/]").condition(Condition.Context.PRIMITIVE)
188 def w2 = new Way(w1)
189 w2.put("bar", "[0-9]{3}")
190 assert c2.applies(new Environment().withPrimitive(w2))
191 w2.put("bar", "[0-9]")
192 assert c2.applies(new Environment().withPrimitive(w2))
193 w2.put("bar", "^[0-9]\$")
194 assert !c2.applies(new Environment().withPrimitive(w2))
195 }
196
197 @Test
198 public void testTicket8568() throws Exception {
199 def sheet = new MapCSSStyleSource("")
200 getParser("" +
201 "way { width: 5; }\n" +
202 "way[keyA], way[keyB] { width: eval(prop(width)+10); }").sheet(sheet)
203 def mc = new MultiCascade()
204 sheet.apply(mc, TestUtils.createPrimitive("way foo=bar"), 20, null, false)
205 assert mc.getCascade(Environment.DEFAULT_LAYER).get("width") == 5
206 sheet.apply(mc, TestUtils.createPrimitive("way keyA=true"), 20, null, false)
207 assert mc.getCascade(Environment.DEFAULT_LAYER).get("width") == 15
208 sheet.apply(mc, TestUtils.createPrimitive("way keyB=true"), 20, null, false)
209 assert mc.getCascade(Environment.DEFAULT_LAYER).get("width") == 15
210 sheet.apply(mc, TestUtils.createPrimitive("way keyA=true keyB=true"), 20, null, false)
211 assert mc.getCascade(Environment.DEFAULT_LAYER).get("width") == 15
212 }
213
214 @Test
215 public void testTicket8071() throws Exception {
216 def sheet = new MapCSSStyleSource("")
217 getParser("*[rcn_ref], *[name] {text: concat(tag(rcn_ref), \" \", tag(name)); }").sheet(sheet)
218 def mc = new MultiCascade()
219 sheet.apply(mc, TestUtils.createPrimitive("way name=Foo"), 20, null, false)
220 assert mc.getCascade(Environment.DEFAULT_LAYER).get("text") == " Foo"
221 sheet.apply(mc, TestUtils.createPrimitive("way rcn_ref=15"), 20, null, false)
222 assert mc.getCascade(Environment.DEFAULT_LAYER).get("text") == "15 "
223 sheet.apply(mc, TestUtils.createPrimitive("way rcn_ref=15 name=Foo"), 20, null, false)
224 assert mc.getCascade(Environment.DEFAULT_LAYER).get("text") == "15 Foo"
225
226 sheet = new MapCSSStyleSource("")
227 getParser("*[rcn_ref], *[name] {text: join(\" - \", tag(rcn_ref), tag(ref), tag(name)); }").sheet(sheet)
228 sheet.apply(mc, TestUtils.createPrimitive("way rcn_ref=15 ref=1.5 name=Foo"), 20, null, false)
229 assert mc.getCascade(Environment.DEFAULT_LAYER).get("text") == "15 - 1.5 - Foo"
230 }
231
232 @Test
233 public void testColorNameTicket9191() throws Exception {
234 def e = new Environment(null, new MultiCascade(), Environment.DEFAULT_LAYER, null)
235 getParser("{color: testcolour1#88DD22}").declaration().instructions.get(0).execute(e)
236 def expected = new Color(0x88DD22)
237 assert e.getCascade(Environment.DEFAULT_LAYER).get("color") == expected
238 assert Main.pref.getDefaultColor("mappaint.mapcss.testcolour1") == expected
239 }
240
241 @Test
242 public void testColorNameTicket9191Alpha() throws Exception {
243 def e = new Environment(null, new MultiCascade(), Environment.DEFAULT_LAYER, null)
244 getParser("{color: testcolour2#12345678}").declaration().instructions.get(0).execute(e)
245 def expected = new Color(0x12, 0x34, 0x56, 0x78)
246 assert e.getCascade(Environment.DEFAULT_LAYER).get("color") == expected
247 assert Main.pref.getDefaultColor("mappaint.mapcss.testcolour2") == expected
248 }
249
250 @Test
251 public void testColorParsing() throws Exception {
252 assert ColorHelper.html2color("#12345678") == new Color(0x12, 0x34, 0x56, 0x78)
253 }
254
255 @Test
256 public void testSiblingSelector() throws Exception {
257 def s1 = (Selector.ChildOrParentSelector) getParser("*[a?][parent_tag(\"highway\")=\"unclassified\"] + *[b?]").child_selector()
258 def ds = new DataSet()
259 def n1 = new org.openstreetmap.josm.data.osm.Node(new LatLon(1, 2))
260 n1.put("a", "true")
261 def n2 = new org.openstreetmap.josm.data.osm.Node(new LatLon(1.1, 2.2))
262 n2.put("b", "true")
263 def w = new Way()
264 w.put("highway", "unclassified")
265 ds.addPrimitive(n1)
266 ds.addPrimitive(n2)
267 ds.addPrimitive(w)
268 w.addNode(n1)
269 w.addNode(n2)
270
271 def e = new Environment().withPrimitive(n2)
272 assert s1.matches(e)
273 assert e.osm == n2
274 assert e.child == n1
275 assert e.parent == w
276 assert !s1.matches(new Environment().withPrimitive(n1))
277 assert !s1.matches(new Environment().withPrimitive(w))
278 }
279
280 @Test
281 public void testSiblingSelectorInterpolation() throws Exception {
282 def s1 = (Selector.ChildOrParentSelector) getParser(
283 "*[tag(\"addr:housenumber\") > child_tag(\"addr:housenumber\")][regexp_test(\"even|odd\", parent_tag(\"addr:interpolation\"))]" +
284 " + *[addr:housenumber]").child_selector()
285 def ds = new DataSet()
286 def n1 = new org.openstreetmap.josm.data.osm.Node(new LatLon(1, 2))
287 n1.put("addr:housenumber", "10")
288 def n2 = new org.openstreetmap.josm.data.osm.Node(new LatLon(1.1, 2.2))
289 n2.put("addr:housenumber", "100")
290 def n3 = new org.openstreetmap.josm.data.osm.Node(new LatLon(1.2, 2.3))
291 n3.put("addr:housenumber", "20")
292 def w = new Way()
293 w.put("addr:interpolation", "even")
294 ds.addPrimitive(n1)
295 ds.addPrimitive(n2)
296 ds.addPrimitive(n3)
297 ds.addPrimitive(w)
298 w.addNode(n1)
299 w.addNode(n2)
300 w.addNode(n3)
301
302 assert s1.right.matches(new Environment().withPrimitive(n3))
303 assert s1.left.matches(new Environment().withPrimitive(n2).withChild(n3).withParent(w))
304 assert s1.matches(new Environment().withPrimitive(n3))
305 assert !s1.matches(new Environment().withPrimitive(n1))
306 assert !s1.matches(new Environment().withPrimitive(n2))
307 assert !s1.matches(new Environment().withPrimitive(w))
308 }
309}
Note: See TracBrowser for help on using the repository browser.