source: josm/trunk/test/unit/org/openstreetmap/josm/gui/mappaint/mapcss/KeyConditionTest.groovy @ 10837

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

see #11924 - extract MapCSS conditions to new class ConditionFactory (on the same model than ExpressionFactory) - should workaround Groovy bug with Java 9 (https://issues.apache.org/jira/browse/GROOVY-7879 ?)

  • Property svn:eol-style set to native
File size: 3.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.mappaint.mapcss;
3
4import static org.junit.Assert.*
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.Relation
12import org.openstreetmap.josm.data.osm.RelationMember
13import org.openstreetmap.josm.gui.mappaint.Environment
14import org.openstreetmap.josm.gui.mappaint.mapcss.Condition.Context
15import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.KeyMatchType
16
17class KeyConditionTest {
18
19    def shouldFail = new GroovyTestCase().&shouldFail
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
49        // ["a label"]
50        Condition c = ConditionFactory.createKeyCondition("a key", false, KeyMatchType.FALSE, Context.PRIMITIVE)
51        // ["a label"?]
52        c = ConditionFactory.createKeyCondition("a key", false, KeyMatchType.TRUE, Context.PRIMITIVE)
53        // [!"a label"]
54        c = ConditionFactory.createKeyCondition("a key", true, KeyMatchType.FALSE, Context.PRIMITIVE)
55        // [!"a label"?]
56        c = ConditionFactory.createKeyCondition("a key", true, KeyMatchType.TRUE, Context.PRIMITIVE)
57
58        // ["a label"]
59        c = ConditionFactory.createKeyCondition("a key", false, null, Context.LINK)
60        // [!"a label"]
61        c = ConditionFactory.createKeyCondition("a key", true, null, Context.LINK)
62
63        shouldFail(MapCSSException) {
64            // ["a label"?]
65           c = ConditionFactory.createKeyCondition("a key", false, KeyMatchType.TRUE, Context.LINK)
66        }
67
68        shouldFail(MapCSSException) {
69            // [!"a label"?]
70            c = ConditionFactory.createKeyCondition("a key", true, KeyMatchType.TRUE, Context.LINK)
71        }
72    }
73
74    @Test
75    public void applies_1() {
76        Relation r = relation(1)
77        Node n = node(1)
78        r.addMember(new RelationMember("my_role", n))
79
80        Environment e = new Environment(n).withParent(r).withIndex(0, r.membersCount).withLinkContext()
81
82        Condition cond = ConditionFactory.createKeyCondition("my_role", false, null, Context.LINK)
83        assert cond.applies(e)
84
85        cond = ConditionFactory.createKeyCondition("my_role", true, null, Context.LINK)
86        assert !cond.applies(e)
87    }
88
89    @Test
90    public void applies_2() {
91        Relation r = relation(1)
92        Node n = node(1)
93        r.addMember(new RelationMember("my_role", n))
94
95        Environment e = new Environment(n).withParent(r).withIndex(0, r.membersCount).withLinkContext()
96
97        Condition cond = ConditionFactory.createKeyCondition("another_role", false, null, Context.LINK)
98        assert !cond.applies(e)
99
100        cond = ConditionFactory.createKeyCondition("another_role", true, null, Context.LINK)
101        assert cond.applies(e)
102    }
103}
Note: See TracBrowser for help on using the repository browser.