source: josm/trunk/test/unit/org/openstreetmap/josm/gui/mappaint/mapcss/ChildOrParentSelectorTest.groovy@ 8833

Last change on this file since 8833 was 8833, checked in by simon04, 8 years ago

fix #11939 - MapCSS: fix parent_tag() in ∈ and ⧉ tests

  • Property svn:eol-style set to native
File size: 5.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.mappaint.mapcss
3
4import org.openstreetmap.josm.data.osm.OsmPrimitiveType
5import org.openstreetmap.josm.gui.mappaint.MultiCascade
6import org.openstreetmap.josm.io.OsmReader
7
8import java.util.logging.Logger
9
10import org.junit.*
11import org.openstreetmap.josm.JOSMFixture
12import org.openstreetmap.josm.data.coor.LatLon
13import org.openstreetmap.josm.data.osm.DataSet
14import org.openstreetmap.josm.data.osm.Node
15import org.openstreetmap.josm.data.osm.Relation
16import org.openstreetmap.josm.data.osm.RelationMember
17import org.openstreetmap.josm.data.osm.Way
18import org.openstreetmap.josm.gui.mappaint.Environment
19import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.ChildOrParentSelector
20
21class ChildOrParentSelectorTest {
22 static private Logger logger = Logger.getLogger(ChildOrParentSelectorTest.class.getName());
23
24 def shouldFail = new GroovyTestCase().&shouldFail
25
26 def DataSet ds;
27
28 @BeforeClass
29 public static void createJOSMFixture(){
30 JOSMFixture.createUnitTestFixture().init()
31 }
32
33 @Before
34 public void setUp() {
35 ds = new DataSet()
36 }
37
38 def relation(id) {
39 def r = new Relation(id,1)
40 ds.addPrimitive(r)
41 return r
42 }
43
44 def node(id) {
45 def n = new Node(id,1)
46 n.setCoor(new LatLon(0,0))
47 ds.addPrimitive(n)
48 return n
49 }
50
51 def way(id){
52 def w = new Way(id,1)
53 ds.addPrimitive(w)
54 return w
55 }
56
57 def ChildOrParentSelector parse(css){
58 MapCSSStyleSource source = new MapCSSStyleSource(css)
59 source.loadStyleSource()
60 assert source.rules.size() == 1
61 return source.rules[0].selector
62 }
63
64 @Test
65 @Ignore
66 public void matches_1() {
67 def css = """
68 relation >[role="my_role"] node {}
69 """
70 ChildOrParentSelector selector = parse(css)
71
72 Relation r = relation(1)
73 Node n = node(1)
74 r.addMember(new RelationMember("my_role", n))
75 Environment e = new Environment().withChild(n)
76
77 assert selector.matches(e)
78 }
79
80 @Test
81 @Ignore
82 public void matches_2() {
83 def css = """
84 relation >["my_role"] node {}
85 """
86 ChildOrParentSelector selector = parse(css)
87
88 Relation r = relation(1)
89 Node n = node(1)
90 r.addMember(new RelationMember("my_role", n))
91 Environment e = new Environment().withChild(n)
92
93 assert selector.matches(e)
94 }
95
96 @Test
97 @Ignore
98 public void matches_3() {
99 def css = """
100 relation >[!"my_role"] node {}
101 """
102 ChildOrParentSelector selector = parse(css)
103
104 Relation r = relation(1)
105 Node n = node(1)
106 r.addMember(new RelationMember("my_role", n))
107 Environment e = new Environment().withChild(n)
108
109 assert !selector.matches(e)
110 }
111
112 @Test
113 @Ignore
114 public void matches_4() {
115 def css = """
116 way < relation {}
117 """
118 ChildOrParentSelector selector = parse(css)
119 assert selector.type == Selector.ChildOrParentSelectorType.PARENT
120
121 }
122 @Test
123 public void matches_5() {
124 def css = """
125 way <[role != "my_role"] relation {text: index();}
126 """
127 ChildOrParentSelector selector = parse(css)
128 assert selector.type == Selector.ChildOrParentSelectorType.PARENT
129
130 Relation r = relation(1)
131 Way w1 = way(1)
132 w1.setNodes([node(11), node(12)])
133
134 Way w2 = way(2)
135 w2.setNodes([node(21), node(22)])
136
137 Way w3 = way(3)
138 w3.setNodes([node(31), node(32)])
139
140 r.addMember(new RelationMember("my_role", w1))
141 r.addMember(new RelationMember("my_role", w2))
142 r.addMember(new RelationMember("another role", w3))
143 r.addMember(new RelationMember("yet another role", w3))
144
145 Environment e = new Environment(r, new MultiCascade(), Environment.DEFAULT_LAYER, null)
146 assert selector.matches(e)
147
148 MapCSSStyleSource source = new MapCSSStyleSource(css)
149 source.loadStyleSource()
150 source.rules[0].declaration.execute(e)
151 assert Float.valueOf(3f).equals(e.getCascade(Environment.DEFAULT_LAYER).get("text", null, Float.class))
152 }
153
154 @Test
155 public void matches_6() {
156 def css = """
157 relation >[role != "my_role"] way {}
158 """
159 ChildOrParentSelector selector = parse(css)
160
161 Relation r = relation(1)
162 Way w1 = way(1)
163 w1.setNodes([node(11), node(12)])
164
165 Way w2 = way(2)
166 w2.setNodes([node(21), node(22)])
167
168 Way w3 = way(3)
169 w3.setNodes([node(31), node(32)])
170
171 r.addMember(new RelationMember("my_role", w1))
172 r.addMember(new RelationMember("my_role", w2))
173 r.addMember(new RelationMember("another role", w3))
174
175 Environment e = new Environment(w1)
176 assert !selector.matches(e)
177
178 e = new Environment(w2)
179 assert !selector.matches(e)
180
181 e = new Environment(w3)
182 assert selector.matches(e)
183 }
184
185 @Test
186 public void testContains() throws Exception {
187 def ds = OsmReader.parseDataSet(new FileInputStream("data_nodist/amenity-in-amenity.osm"), null)
188 def css = parse("node[tag(\"amenity\") = parent_tag(\"amenity\")] ∈ *[amenity] {}")
189 assert css.matches(new Environment(ds.getPrimitiveById(123, OsmPrimitiveType.WAY)))
190 assert css.matches(new Environment(ds.getPrimitiveById(123, OsmPrimitiveType.RELATION)))
191 }
192}
Note: See TracBrowser for help on using the repository browser.