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

Last change on this file since 8874 was 8874, checked in by simon04, 9 years ago

fix #10467 - MapCSS: allow comparisons in regexp key conditions

  • Property svn:eol-style set to native
File size: 3.8 KB
RevLine 
[4069]1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.mappaint.mapcss;
3
4import org.junit.*
[7081]5import org.openstreetmap.josm.JOSMFixture
[4069]6import org.openstreetmap.josm.data.coor.LatLon
7import org.openstreetmap.josm.data.osm.DataSet
8import org.openstreetmap.josm.data.osm.Node
[8874]9import org.openstreetmap.josm.data.osm.OsmUtils
[4069]10import org.openstreetmap.josm.data.osm.Relation
11import org.openstreetmap.josm.data.osm.RelationMember
12import org.openstreetmap.josm.gui.mappaint.Environment
13import org.openstreetmap.josm.gui.mappaint.mapcss.Condition.Context
14import org.openstreetmap.josm.gui.mappaint.mapcss.Condition.Op
[8874]15import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.MapCSSParser
[4069]16
17
18class KeyValueConditionTest {
19
20 def shouldFail = new GroovyTestCase().&shouldFail
[7081]21
[4069]22 def DataSet ds;
[7081]23
[4069]24 @BeforeClass
25 public static void createJOSMFixture(){
26 JOSMFixture.createUnitTestFixture().init()
27 }
[7081]28
[4069]29 @Before
30 public void setUp() {
31 ds = new DataSet()
32 }
[7081]33
[4069]34 def relation(id) {
35 def r = new Relation(id,1)
36 ds.addPrimitive(r)
37 return r
38 }
[7081]39
[4069]40 def node(id) {
41 def n = new Node(id,1)
42 n.setCoor(new LatLon(0,0))
43 ds.addPrimitive(n)
44 return n
45 }
[7081]46
[4069]47 @Test
48 public void create() {
[7081]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
[4069]54 shouldFail(MapCSSException) {
[7081]55 c = Condition.createKeyValueCondition("an arbitry tag", "a role", Op.EQ, Context.LINK, false)
[4069]56 }
57 }
[7081]58
[4069]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))
[7081]64
[8415]65 Environment e = new Environment(n).withParent(r).withLinkContext().withIndex(0, r.membersCount)
[7081]66
[4069]67 Condition cond = new Condition.RoleCondition("my_role", Op.EQ)
[7081]68 assert cond.applies(e)
69
[4069]70 cond = new Condition.RoleCondition("another_role", Op.EQ)
71 assert !cond.applies(e)
72 }
[7081]73
[4069]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))
[7081]79
[8415]80 Environment e = new Environment(n).withParent(r).withIndex(0, r.membersCount).withLinkContext()
[7081]81
82 Condition cond = Condition.createKeyValueCondition("role", "my_role", Op.NEQ, Context.LINK, false)
[4069]83 assert !cond.applies(e)
[7081]84
85 cond = Condition.createKeyValueCondition("role", "another_role", Op.NEQ, Context.LINK, false)
[4069]86 assert cond.applies(e)
87 }
[8874]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 }
[4069]103}
Note: See TracBrowser for help on using the repository browser.