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

Last change on this file since 6455 was 6455, checked in by simon04, 6 years ago

fix #7939 - MapCSS: add support for negated regular expressions

File size: 2.9 KB
Line 
1package org.openstreetmap.josm.gui.mappaint.mapcss
2
3import org.junit.Test
4import org.openstreetmap.josm.Main
5import org.openstreetmap.josm.data.Preferences
6import org.openstreetmap.josm.data.osm.OsmPrimitive
7import org.openstreetmap.josm.data.osm.Way
8import org.openstreetmap.josm.gui.mappaint.Environment
9import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.MapCSSParser
10
11class MapCSSParserTest {
12
13    protected static OsmPrimitive getPrimitive(String key, String value) {
14        def w = new Way()
15        w.put(key, value)
16        return w
17    }
18
19    @Test
20    public void testEqualCondition() throws Exception {
21        def condition = (Condition.KeyValueCondition) new MapCSSParser(new StringReader("[surface=paved]")).condition(Condition.Context.PRIMITIVE)
22        assert condition instanceof Condition.KeyValueCondition
23        assert Condition.Op.EQ.equals(condition.op)
24        assert "surface".equals(condition.k)
25        assert "paved".equals(condition.v)
26        Main.pref = new Preferences()
27        assert condition.applies(new Environment().withPrimitive(getPrimitive("surface", "paved")))
28        assert !condition.applies(new Environment().withPrimitive(getPrimitive("surface", "unpaved")))
29    }
30
31    @Test
32    public void testNotEqualCondition() throws Exception {
33        def condition = (Condition.KeyValueCondition) new MapCSSParser(new StringReader("[surface!=paved]")).condition(Condition.Context.PRIMITIVE)
34        assert condition instanceof Condition.KeyValueCondition
35        assert Condition.Op.NEQ.equals(condition.op)
36        Main.pref = new Preferences()
37        assert !condition.applies(new Environment().withPrimitive(getPrimitive("surface", "paved")))
38        assert condition.applies(new Environment().withPrimitive(getPrimitive("surface", "unpaved")))
39    }
40
41    @Test
42    public void testRegexCondition() throws Exception {
43        def condition = (Condition.KeyValueCondition) new MapCSSParser(new StringReader("[surface=~/paved|unpaved/]")).condition(Condition.Context.PRIMITIVE)
44        assert condition instanceof Condition.KeyValueCondition
45        assert Condition.Op.REGEX.equals(condition.op)
46        Main.pref = new Preferences()
47        assert condition.applies(new Environment().withPrimitive(getPrimitive("surface", "unpaved")))
48        assert !condition.applies(new Environment().withPrimitive(getPrimitive("surface", "grass")))
49    }
50
51    @Test
52    public void testNegatedRegexCondition() throws Exception {
53        def condition = (Condition.KeyValueCondition) new MapCSSParser(new StringReader("[surface!~/paved|unpaved/]")).condition(Condition.Context.PRIMITIVE)
54        assert condition instanceof Condition.KeyValueCondition
55        assert Condition.Op.NREGEX.equals(condition.op)
56        Main.pref = new Preferences()
57        assert !condition.applies(new Environment().withPrimitive(getPrimitive("surface", "unpaved")))
58        assert condition.applies(new Environment().withPrimitive(getPrimitive("surface", "grass")))
59    }
60}
Note: See TracBrowser for help on using the repository browser.