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