source: josm/trunk/test/unit/org/openstreetmap/josm/gui/mappaint/mapcss/KeyValueConditionTest.groovy @ 14019

Last change on this file since 14019 was 14019, checked in by Don-vip, 12 months ago

update to Groovy 2.5.0 and Eclipse Photon (4.8.0)

  • Property svn:eol-style set to native
File size: 4.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.mappaint.mapcss;
3
4import static groovy.test.GroovyAssert.shouldFail
5
6import org.junit.*
7import org.openstreetmap.josm.JOSMFixture
8import org.openstreetmap.josm.data.coor.LatLon
9import org.openstreetmap.josm.data.osm.DataSet
10import org.openstreetmap.josm.data.osm.Node
11import org.openstreetmap.josm.data.osm.OsmUtils
12import org.openstreetmap.josm.data.osm.Relation
13import org.openstreetmap.josm.data.osm.RelationMember
14import org.openstreetmap.josm.gui.mappaint.Environment
15import org.openstreetmap.josm.gui.mappaint.mapcss.Condition.Context
16import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.Op
17import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.MapCSSParser
18
19class KeyValueConditionTest {
20
21    def DataSet ds;
22
23    @BeforeClass
24    public static void createJOSMFixture(){
25        JOSMFixture.createUnitTestFixture().init()
26    }
27
28    @Before
29    public void setUp() {
30        ds = new DataSet()
31    }
32
33    def relation(id) {
34        def r = new Relation(id,1)
35        ds.addPrimitive(r)
36        return r
37    }
38
39    def node(id) {
40        def n = new Node(id,1)
41        n.setCoor(LatLon.ZERO)
42        ds.addPrimitive(n)
43        return n
44    }
45
46    @Test
47    public void create() {
48        Condition c = ConditionFactory.createKeyValueCondition("a key", "a value", Op.EQ, Context.PRIMITIVE, false)
49
50        c = ConditionFactory.createKeyValueCondition("role", "a role", Op.EQ, Context.LINK, false)
51        c = ConditionFactory.createKeyValueCondition("RoLe", "a role", Op.EQ, Context.LINK, false)
52
53        shouldFail(MapCSSException) {
54            c = ConditionFactory.createKeyValueCondition("an arbitry tag", "a role", Op.EQ, Context.LINK, false)
55        }
56    }
57
58    @Test
59    public void applies_1() {
60        Relation r = relation(1)
61        Node n = node(1)
62        r.addMember(new RelationMember("my_role", n))
63
64        Environment e = new Environment(n).withParent(r).withLinkContext().withIndex(0, r.membersCount)
65
66        Condition cond = new ConditionFactory.RoleCondition("my_role", Op.EQ)
67        assert cond.applies(e)
68
69        cond = new ConditionFactory.RoleCondition("another_role", Op.EQ)
70        assert !cond.applies(e)
71    }
72
73    @Test
74    public void applies_2() {
75        Relation r = relation(1)
76        Node n = node(1)
77        r.addMember(new RelationMember("my_role", n))
78
79        Environment e = new Environment(n).withParent(r).withIndex(0, r.membersCount).withLinkContext()
80
81        Condition cond = ConditionFactory.createKeyValueCondition("role", "my_role", Op.NEQ, Context.LINK, false)
82        assert !cond.applies(e)
83
84        cond = ConditionFactory.createKeyValueCondition("role", "another_role", Op.NEQ, Context.LINK, false)
85        assert cond.applies(e)
86    }
87
88    @Test
89    public void testKeyRegexValueRegex() throws Exception {
90        def selPos = new MapCSSParser(new StringReader("*[/^source/ =~ /.*,.*/]")).selector()
91        def selNeg = new MapCSSParser(new StringReader("*[/^source/ !~ /.*,.*/]")).selector()
92        assert !selPos.matches(new Environment(OsmUtils.createPrimitive("way foo=bar")))
93        assert selPos.matches(new Environment(OsmUtils.createPrimitive("way source=1,2")))
94        assert selPos.matches(new Environment(OsmUtils.createPrimitive("way source_foo_bar=1,2")))
95        assert !selPos.matches(new Environment(OsmUtils.createPrimitive("way source=1")))
96        assert !selPos.matches(new Environment(OsmUtils.createPrimitive("way source=1")))
97        assert !selNeg.matches(new Environment(OsmUtils.createPrimitive("way source=1,2")))
98        assert !selNeg.matches(new Environment(OsmUtils.createPrimitive("way foo=bar source=1,2")))
99        assert selNeg.matches(new Environment(OsmUtils.createPrimitive("way foo=bar source=baz")))
100        assert selNeg.matches(new Environment(OsmUtils.createPrimitive("way foo=bar src=1,2")))
101    }
102
103    @Test
104    public void testValueFive() throws Exception {
105        // ticket #5985
106        def sel = new MapCSSParser(new StringReader("*[width=5]")).selector()
107        assert sel.matches(new Environment(OsmUtils.createPrimitive("way highway=track width=5")))
108        assert !sel.matches(new Environment(OsmUtils.createPrimitive("way highway=track width=2")))
109    }
110
111    @Test
112    public void testValueZero() throws Exception {
113        // ticket #12267
114        def sel = new MapCSSParser(new StringReader("*[frequency=0]")).selector()
115        assert sel.matches(new Environment(OsmUtils.createPrimitive("way railway=rail frequency=0")))
116        assert !sel.matches(new Environment(OsmUtils.createPrimitive("way railway=rail frequency=50")))
117    }
118}
Note: See TracBrowser for help on using the repository browser.