Changeset 14064 in josm
- Timestamp:
- 2018-07-28T23:18:31+02:00 (6 years ago)
- Location:
- trunk/test/unit/org/openstreetmap/josm/gui/mappaint/mapcss
- Files:
-
- 5 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/unit/org/openstreetmap/josm/gui/mappaint/mapcss/ChildOrParentSelectorTest.java
r14051 r14064 1 1 // License: GPL. For details, see LICENSE file. 2 package org.openstreetmap.josm.gui.mappaint.mapcss 2 package org.openstreetmap.josm.gui.mappaint.mapcss; 3 3 4 import java.util.logging.Logger 4 import static org.junit.Assert.assertEquals; 5 import static org.junit.Assert.assertFalse; 6 import static org.junit.Assert.assertTrue; 5 7 6 import org.junit.* 7 import org.openstreetmap.josm.JOSMFixture 8 import org.openstreetmap.josm.data.coor.LatLon 9 import org.openstreetmap.josm.data.osm.DataSet 10 import org.openstreetmap.josm.data.osm.Node 11 import org.openstreetmap.josm.data.osm.OsmPrimitiveType 12 import org.openstreetmap.josm.data.osm.Relation 13 import org.openstreetmap.josm.data.osm.RelationMember 14 import org.openstreetmap.josm.data.osm.Way 15 import org.openstreetmap.josm.gui.mappaint.Environment 16 import org.openstreetmap.josm.gui.mappaint.MultiCascade 17 import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.ChildOrParentSelector 18 import org.openstreetmap.josm.io.OsmReader 8 import java.io.FileInputStream; 9 import java.util.Arrays; 19 10 20 class ChildOrParentSelectorTest { 21 static private Logger logger = Logger.getLogger(ChildOrParentSelectorTest.class.getName()); 11 import org.junit.Before; 12 import org.junit.Ignore; 13 import org.junit.Rule; 14 import org.junit.Test; 15 import org.openstreetmap.josm.data.coor.LatLon; 16 import org.openstreetmap.josm.data.osm.DataSet; 17 import org.openstreetmap.josm.data.osm.Node; 18 import org.openstreetmap.josm.data.osm.OsmPrimitiveType; 19 import org.openstreetmap.josm.data.osm.Relation; 20 import org.openstreetmap.josm.data.osm.RelationMember; 21 import org.openstreetmap.josm.data.osm.Way; 22 import org.openstreetmap.josm.gui.mappaint.Environment; 23 import org.openstreetmap.josm.gui.mappaint.MultiCascade; 24 import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.ChildOrParentSelector; 25 import org.openstreetmap.josm.io.OsmReader; 26 import org.openstreetmap.josm.testutils.JOSMTestRules; 22 27 23 def DataSet ds;28 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; 24 29 25 @BeforeClass 26 public static void createJOSMFixture(){ 27 JOSMFixture.createUnitTestFixture().init() 30 /** 31 * Unit tests of {@link ChildOrParentSelector}. 32 */ 33 public class ChildOrParentSelectorTest { 34 35 private DataSet ds; 36 37 /** 38 * Setup rule 39 */ 40 @Rule 41 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") 42 public JOSMTestRules test = new JOSMTestRules().projection(); 43 44 /** 45 * Setup test 46 */ 47 @Before 48 public void setUp() { 49 ds = new DataSet(); 28 50 } 29 51 30 @Before 31 public void setUp() { 32 ds = new DataSet() 52 Relation relation(int id) { 53 Relation r = new Relation(id, 1); 54 ds.addPrimitive(r); 55 return r; 33 56 } 34 57 35 def relation(id) { 36 def r = new Relation(id,1) 37 ds.addPrimitive(r) 38 return r 58 Node node(int id) { 59 Node n = new Node(id, 1); 60 n.setCoor(LatLon.ZERO); 61 ds.addPrimitive(n); 62 return n; 39 63 } 40 64 41 def node(id) { 42 def n = new Node(id,1) 43 n.setCoor(LatLon.ZERO) 44 ds.addPrimitive(n) 45 return n 65 Way way(int id) { 66 Way w = new Way(id, 1); 67 ds.addPrimitive(w); 68 return w; 46 69 } 47 70 48 def way(id){ 49 def w = new Way(id,1) 50 ds.addPrimitive(w) 51 return w 52 } 53 54 def ChildOrParentSelector parse(css){ 55 MapCSSStyleSource source = new MapCSSStyleSource(css) 56 source.loadStyleSource() 57 assert source.rules.size() == 1 58 return source.rules[0].selector 71 ChildOrParentSelector parse(String css) { 72 MapCSSStyleSource source = new MapCSSStyleSource(css); 73 source.loadStyleSource(); 74 assertEquals(1, source.rules.size()); 75 return (ChildOrParentSelector) source.rules.get(0).selector; 59 76 } 60 77 … … 62 79 @Ignore 63 80 public void matches_1() { 64 def css = """ 65 relation >[role="my_role"] node {} 66 """ 67 ChildOrParentSelector selector = parse(css) 81 String css = "relation >[role=\"my_role\"] node {}"; 82 ChildOrParentSelector selector = parse(css); 68 83 69 Relation r = relation(1) 70 Node n = node(1) 71 r.addMember(new RelationMember("my_role", n)) 72 Environment e = new Environment().withChild(n) 84 Relation r = relation(1); 85 Node n = node(1); 86 r.addMember(new RelationMember("my_role", n)); 87 Environment e = new Environment().withChild(n); 73 88 74 assert selector.matches(e)89 assertTrue(selector.matches(e)); 75 90 } 76 91 … … 78 93 @Ignore 79 94 public void matches_2() { 80 def css = """ 81 relation >["my_role"] node {} 82 """ 83 ChildOrParentSelector selector = parse(css) 95 String css = "relation >[\"my_role\"] node {}"; 96 ChildOrParentSelector selector = parse(css); 84 97 85 Relation r = relation(1) 86 Node n = node(1) 87 r.addMember(new RelationMember("my_role", n)) 88 Environment e = new Environment().withChild(n) 98 Relation r = relation(1); 99 Node n = node(1); 100 r.addMember(new RelationMember("my_role", n)); 101 Environment e = new Environment().withChild(n); 89 102 90 assert selector.matches(e)103 assertTrue(selector.matches(e)); 91 104 } 92 105 … … 94 107 @Ignore 95 108 public void matches_3() { 96 def css = """ 97 relation >[!"my_role"] node {} 98 """ 99 ChildOrParentSelector selector = parse(css) 109 String css = "relation >[!\"my_role\"] node {}"; 110 ChildOrParentSelector selector = parse(css); 100 111 101 Relation r = relation(1) 102 Node n = node(1) 103 r.addMember(new RelationMember("my_role", n)) 104 Environment e = new Environment().withChild(n) 112 Relation r = relation(1); 113 Node n = node(1); 114 r.addMember(new RelationMember("my_role", n)); 115 Environment e = new Environment().withChild(n); 105 116 106 assert !selector.matches(e)117 assertFalse(selector.matches(e)); 107 118 } 108 119 … … 110 121 @Ignore 111 122 public void matches_4() { 112 def css = """ 113 way < relation {} 114 """ 115 ChildOrParentSelector selector = parse(css) 116 assert selector.type == Selector.ChildOrParentSelectorType.PARENT 123 String css = "way < relation {}"; 124 ChildOrParentSelector selector = parse(css); 125 assertEquals(Selector.ChildOrParentSelectorType.PARENT, selector.type); 117 126 118 127 } 128 119 129 @Test 120 130 public void matches_5() { 121 def css = """ 122 way <[role != "my_role"] relation {text: index();} 123 """ 124 ChildOrParentSelector selector = parse(css) 125 assert selector.type == Selector.ChildOrParentSelectorType.PARENT 131 String css = "way <[role != \"my_role\"] relation {text: index();}"; 132 ChildOrParentSelector selector = parse(css); 133 assertEquals(Selector.ChildOrParentSelectorType.PARENT, selector.type); 126 134 127 Relation r = relation(1) 128 Way w1 = way(1) 129 w1.setNodes( [node(11), node(12)])135 Relation r = relation(1); 136 Way w1 = way(1); 137 w1.setNodes(Arrays.asList(node(11), node(12))); 130 138 131 Way w2 = way(2) 132 w2.setNodes( [node(21), node(22)])139 Way w2 = way(2); 140 w2.setNodes(Arrays.asList(node(21), node(22))); 133 141 134 Way w3 = way(3) 135 w3.setNodes( [node(31), node(32)])142 Way w3 = way(3); 143 w3.setNodes(Arrays.asList(node(31), node(32))); 136 144 137 r.addMember(new RelationMember("my_role", w1)) 138 r.addMember(new RelationMember("my_role", w2)) 139 r.addMember(new RelationMember("another role", w3)) 140 r.addMember(new RelationMember("yet another role", w3)) 145 r.addMember(new RelationMember("my_role", w1)); 146 r.addMember(new RelationMember("my_role", w2)); 147 r.addMember(new RelationMember("another role", w3)); 148 r.addMember(new RelationMember("yet another role", w3)); 141 149 142 Environment e = new Environment(r, new MultiCascade(), Environment.DEFAULT_LAYER, null) 143 assert selector.matches(e)150 Environment e = new Environment(r, new MultiCascade(), Environment.DEFAULT_LAYER, null); 151 assertTrue(selector.matches(e)); 144 152 145 MapCSSStyleSource source = new MapCSSStyleSource(css) 146 source.loadStyleSource() 147 source.rules [0].declaration.execute(e)148 assert Float.valueOf(3f).equals(e.getCascade(Environment.DEFAULT_LAYER).get("text", null, Float.class))153 MapCSSStyleSource source = new MapCSSStyleSource(css); 154 source.loadStyleSource(); 155 source.rules.get(0).declaration.execute(e); 156 assertEquals(Float.valueOf(3f), e.getCascade(Environment.DEFAULT_LAYER).get("text", null, Float.class)); 149 157 } 150 158 151 159 @Test 152 160 public void matches_6() { 153 def css = """ 154 relation >[role != "my_role"] way {} 155 """ 156 ChildOrParentSelector selector = parse(css) 161 String css = "relation >[role != \"my_role\"] way {}"; 162 ChildOrParentSelector selector = parse(css); 157 163 158 Relation r = relation(1) 159 Way w1 = way(1) 160 w1.setNodes( [node(11), node(12)])164 Relation r = relation(1); 165 Way w1 = way(1); 166 w1.setNodes(Arrays.asList(node(11), node(12))); 161 167 162 Way w2 = way(2) 163 w2.setNodes( [node(21), node(22)])168 Way w2 = way(2); 169 w2.setNodes(Arrays.asList(node(21), node(22))); 164 170 165 Way w3 = way(3) 166 w3.setNodes( [node(31), node(32)])171 Way w3 = way(3); 172 w3.setNodes(Arrays.asList(node(31), node(32))); 167 173 168 r.addMember(new RelationMember("my_role", w1)) 169 r.addMember(new RelationMember("my_role", w2)) 170 r.addMember(new RelationMember("another role", w3)) 174 r.addMember(new RelationMember("my_role", w1)); 175 r.addMember(new RelationMember("my_role", w2)); 176 r.addMember(new RelationMember("another role", w3)); 171 177 172 Environment e = new Environment(w1) 173 assert !selector.matches(e)178 Environment e = new Environment(w1); 179 assertFalse(selector.matches(e)); 174 180 175 e = new Environment(w2) 176 assert !selector.matches(e)181 e = new Environment(w2); 182 assertFalse(selector.matches(e)); 177 183 178 e = new Environment(w3) 179 assert selector.matches(e)184 e = new Environment(w3); 185 assertTrue(selector.matches(e)); 180 186 } 181 187 182 188 @Test 183 189 public void testContains() throws Exception { 184 d ef ds = OsmReader.parseDataSet(new FileInputStream("data_nodist/amenity-in-amenity.osm"), null)185 def css = parse("node[tag(\"amenity\") = parent_tag(\"amenity\")] ∈ *[amenity] {}")186 assert css.matches(new Environment(ds.getPrimitiveById(123, OsmPrimitiveType.WAY)))187 assert css.matches(new Environment(ds.getPrimitiveById(123, OsmPrimitiveType.RELATION)))190 ds = OsmReader.parseDataSet(new FileInputStream("data_nodist/amenity-in-amenity.osm"), null); 191 ChildOrParentSelector css = parse("node[tag(\"amenity\") = parent_tag(\"amenity\")] ∈ *[amenity] {}"); 192 assertTrue(css.matches(new Environment(ds.getPrimitiveById(123, OsmPrimitiveType.WAY)))); 193 assertTrue(css.matches(new Environment(ds.getPrimitiveById(123, OsmPrimitiveType.RELATION)))); 188 194 } 189 195 } -
trunk/test/unit/org/openstreetmap/josm/gui/mappaint/mapcss/KeyConditionTest.java
r14051 r14064 2 2 package org.openstreetmap.josm.gui.mappaint.mapcss; 3 3 4 import static groovy.test.GroovyAssert.shouldFail 5 import static org.junit.Assert.* 4 import static org.junit.Assert.assertFalse; 5 import static org.junit.Assert.assertTrue; 6 import static org.junit.Assert.fail; 6 7 7 import org.junit.* 8 import org.openstreetmap.josm.JOSMFixture 9 import org.openstreetmap.josm.data.coor.LatLon 10 import org.openstreetmap.josm.data.osm.DataSet 11 import org.openstreetmap.josm.data.osm.Node 12 import org.openstreetmap.josm.data.osm.Relation 13 import org.openstreetmap.josm.data.osm.RelationMember 14 import org.openstreetmap.josm.gui.mappaint.Environment 15 import org.openstreetmap.josm.gui.mappaint.mapcss.Condition.Context 16 import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.KeyMatchType 8 import org.junit.Before; 9 import org.junit.Rule; 10 import org.junit.Test; 11 import org.openstreetmap.josm.data.coor.LatLon; 12 import org.openstreetmap.josm.data.osm.DataSet; 13 import org.openstreetmap.josm.data.osm.Node; 14 import org.openstreetmap.josm.data.osm.Relation; 15 import org.openstreetmap.josm.data.osm.RelationMember; 16 import org.openstreetmap.josm.gui.mappaint.Environment; 17 import org.openstreetmap.josm.gui.mappaint.mapcss.Condition.Context; 18 import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.KeyCondition; 19 import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.KeyMatchType; 20 import org.openstreetmap.josm.testutils.JOSMTestRules; 21 import org.openstreetmap.josm.tools.Logging; 17 22 18 class KeyConditionTest { 23 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; 19 24 20 def DataSet ds; 25 /** 26 * Unit tests of {@link KeyCondition}. 27 */ 28 public class KeyConditionTest { 21 29 22 @BeforeClass 23 public static void createJOSMFixture(){ 24 JOSMFixture.createUnitTestFixture().init() 30 private DataSet ds; 31 32 /** 33 * Setup rule 34 */ 35 @Rule 36 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") 37 public JOSMTestRules test = new JOSMTestRules().projection(); 38 39 /** 40 * Setup test 41 */ 42 @Before 43 public void setUp() { 44 ds = new DataSet(); 25 45 } 26 46 27 @Before 28 public void setUp() { 29 ds = new DataSet() 47 Relation relation(int id) { 48 Relation r = new Relation(id, 1); 49 ds.addPrimitive(r); 50 return r; 30 51 } 31 52 32 def relation(id) { 33 def r = new Relation(id,1) 34 ds.addPrimitive(r) 35 return r 53 Node node(int id) { 54 Node n = new Node(id, 1); 55 n.setCoor(LatLon.ZERO); 56 ds.addPrimitive(n); 57 return n; 36 58 } 37 59 38 def node(id) { 39 def n = new Node(id,1) 40 n.setCoor(LatLon.ZERO) 41 ds.addPrimitive(n) 42 return n 60 private static void shouldFail(Runnable r) { 61 try { 62 r.run(); 63 fail("should throw exception"); 64 } catch (MapCSSException e) { 65 Logging.trace(e); 66 } 43 67 } 44 68 69 /** 70 * Test {@link ConditionFactory#createKeyCondition}. 71 */ 45 72 @Test 46 73 public void create() { 47 74 48 75 // ["a label"] 49 Condition c = ConditionFactory.createKeyCondition("a key", false, KeyMatchType.FALSE, Context.PRIMITIVE)76 ConditionFactory.createKeyCondition("a key", false, KeyMatchType.FALSE, Context.PRIMITIVE); 50 77 // ["a label"?] 51 c = ConditionFactory.createKeyCondition("a key", false, KeyMatchType.TRUE, Context.PRIMITIVE)78 ConditionFactory.createKeyCondition("a key", false, KeyMatchType.TRUE, Context.PRIMITIVE); 52 79 // [!"a label"] 53 c = ConditionFactory.createKeyCondition("a key", true, KeyMatchType.FALSE, Context.PRIMITIVE)80 ConditionFactory.createKeyCondition("a key", true, KeyMatchType.FALSE, Context.PRIMITIVE); 54 81 // [!"a label"?] 55 c = ConditionFactory.createKeyCondition("a key", true, KeyMatchType.TRUE, Context.PRIMITIVE)82 ConditionFactory.createKeyCondition("a key", true, KeyMatchType.TRUE, Context.PRIMITIVE); 56 83 57 84 // ["a label"] 58 c = ConditionFactory.createKeyCondition("a key", false, null, Context.LINK)85 ConditionFactory.createKeyCondition("a key", false, null, Context.LINK); 59 86 // [!"a label"] 60 c = ConditionFactory.createKeyCondition("a key", true, null, Context.LINK)87 ConditionFactory.createKeyCondition("a key", true, null, Context.LINK); 61 88 62 shouldFail(MapCSSException) {63 // ["a label"?]64 c =ConditionFactory.createKeyCondition("a key", false, KeyMatchType.TRUE, Context.LINK)65 }89 // ["a label"?] 90 shouldFail(() -> 91 ConditionFactory.createKeyCondition("a key", false, KeyMatchType.TRUE, Context.LINK) 92 ); 66 93 67 shouldFail(MapCSSException) {68 // [!"a label"?]69 c =ConditionFactory.createKeyCondition("a key", true, KeyMatchType.TRUE, Context.LINK)70 }94 // [!"a label"?] 95 shouldFail(() -> 96 ConditionFactory.createKeyCondition("a key", true, KeyMatchType.TRUE, Context.LINK) 97 ); 71 98 } 72 99 73 100 @Test 74 101 public void applies_1() { 75 Relation r = relation(1) 76 Node n = node(1) 77 r.addMember(new RelationMember("my_role", n)) 102 Relation r = relation(1); 103 Node n = node(1); 104 r.addMember(new RelationMember("my_role", n)); 78 105 79 Environment e = new Environment(n).withParent(r).withIndex(0, r. membersCount).withLinkContext()106 Environment e = new Environment(n).withParent(r).withIndex(0, r.getMembersCount()).withLinkContext(); 80 107 81 Condition cond = ConditionFactory.createKeyCondition("my_role", false, null, Context.LINK) 82 assert cond.applies(e)108 Condition cond = ConditionFactory.createKeyCondition("my_role", false, null, Context.LINK); 109 assertTrue(cond.applies(e)); 83 110 84 cond = ConditionFactory.createKeyCondition("my_role", true, null, Context.LINK) 85 assert !cond.applies(e)111 cond = ConditionFactory.createKeyCondition("my_role", true, null, Context.LINK); 112 assertFalse(cond.applies(e)); 86 113 } 87 114 88 115 @Test 89 116 public void applies_2() { 90 Relation r = relation(1) 91 Node n = node(1) 92 r.addMember(new RelationMember("my_role", n)) 117 Relation r = relation(1); 118 Node n = node(1); 119 r.addMember(new RelationMember("my_role", n)); 93 120 94 Environment e = new Environment(n).withParent(r).withIndex(0, r. membersCount).withLinkContext()121 Environment e = new Environment(n).withParent(r).withIndex(0, r.getMembersCount()).withLinkContext(); 95 122 96 Condition cond = ConditionFactory.createKeyCondition("another_role", false, null, Context.LINK) 97 assert !cond.applies(e)123 Condition cond = ConditionFactory.createKeyCondition("another_role", false, null, Context.LINK); 124 assertFalse(cond.applies(e)); 98 125 99 cond = ConditionFactory.createKeyCondition("another_role", true, null, Context.LINK) 100 assert cond.applies(e)126 cond = ConditionFactory.createKeyCondition("another_role", true, null, Context.LINK); 127 assertTrue(cond.applies(e)); 101 128 } 102 129 } -
trunk/test/unit/org/openstreetmap/josm/gui/mappaint/mapcss/KeyValueConditionTest.java
r14051 r14064 2 2 package org.openstreetmap.josm.gui.mappaint.mapcss; 3 3 4 import static groovy.test.GroovyAssert.shouldFail 4 import static org.junit.Assert.assertFalse; 5 import static org.junit.Assert.assertTrue; 6 import static org.junit.Assert.fail; 5 7 6 import org.junit.* 7 import org.openstreetmap.josm.JOSMFixture 8 import org.openstreetmap.josm.data.coor.LatLon 9 import org.openstreetmap.josm.data.osm.DataSet 10 import org.openstreetmap.josm.data.osm.Node 11 import org.openstreetmap.josm.data.osm.OsmUtils 12 import org.openstreetmap.josm.data.osm.Relation 13 import org.openstreetmap.josm.data.osm.RelationMember 14 import org.openstreetmap.josm.gui.mappaint.Environment 15 import org.openstreetmap.josm.gui.mappaint.mapcss.Condition.Context 16 import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.Op 17 import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.MapCSSParser 8 import java.io.StringReader; 18 9 19 class KeyValueConditionTest { 10 import org.junit.Before; 11 import org.junit.Rule; 12 import org.junit.Test; 13 import org.openstreetmap.josm.data.coor.LatLon; 14 import org.openstreetmap.josm.data.osm.DataSet; 15 import org.openstreetmap.josm.data.osm.Node; 16 import org.openstreetmap.josm.data.osm.OsmUtils; 17 import org.openstreetmap.josm.data.osm.Relation; 18 import org.openstreetmap.josm.data.osm.RelationMember; 19 import org.openstreetmap.josm.gui.mappaint.Environment; 20 import org.openstreetmap.josm.gui.mappaint.mapcss.Condition.Context; 21 import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.KeyValueCondition; 22 import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.Op; 23 import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.MapCSSParser; 24 import org.openstreetmap.josm.testutils.JOSMTestRules; 25 import org.openstreetmap.josm.tools.Logging; 20 26 21 def DataSet ds;27 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; 22 28 23 @BeforeClass 24 public static void createJOSMFixture(){ 25 JOSMFixture.createUnitTestFixture().init() 29 /** 30 * Unit tests of {@link KeyValueCondition}. 31 */ 32 public class KeyValueConditionTest { 33 34 private DataSet ds; 35 36 /** 37 * Setup rule 38 */ 39 @Rule 40 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") 41 public JOSMTestRules test = new JOSMTestRules().projection(); 42 43 /** 44 * Setup test 45 */ 46 @Before 47 public void setUp() { 48 ds = new DataSet(); 26 49 } 27 50 28 @Before 29 public void setUp() { 30 ds = new DataSet() 51 Relation relation(int id) { 52 Relation r = new Relation(id, 1); 53 ds.addPrimitive(r); 54 return r; 31 55 } 32 56 33 def relation(id) { 34 def r = new Relation(id,1) 35 ds.addPrimitive(r) 36 return r 57 Node node(int id) { 58 Node n = new Node(id, 1); 59 n.setCoor(LatLon.ZERO); 60 ds.addPrimitive(n); 61 return n; 37 62 } 38 63 39 def node(id) { 40 def n = new Node(id,1) 41 n.setCoor(LatLon.ZERO) 42 ds.addPrimitive(n) 43 return n 64 private static void shouldFail(Runnable r) { 65 try { 66 r.run(); 67 fail("should throw exception"); 68 } catch (MapCSSException e) { 69 Logging.trace(e); 70 } 44 71 } 45 72 46 73 @Test 47 74 public void create() { 48 Condition c = ConditionFactory.createKeyValueCondition("a key", "a value", Op.EQ, Context.PRIMITIVE, false)75 ConditionFactory.createKeyValueCondition("a key", "a value", Op.EQ, Context.PRIMITIVE, false); 49 76 50 c = ConditionFactory.createKeyValueCondition("role", "a role", Op.EQ, Context.LINK, false)51 c = ConditionFactory.createKeyValueCondition("RoLe", "a role", Op.EQ, Context.LINK, false)77 ConditionFactory.createKeyValueCondition("role", "a role", Op.EQ, Context.LINK, false); 78 ConditionFactory.createKeyValueCondition("RoLe", "a role", Op.EQ, Context.LINK, false); 52 79 53 shouldFail( MapCSSException) {54 c =ConditionFactory.createKeyValueCondition("an arbitry tag", "a role", Op.EQ, Context.LINK, false)55 }80 shouldFail(() -> 81 ConditionFactory.createKeyValueCondition("an arbitry tag", "a role", Op.EQ, Context.LINK, false) 82 ); 56 83 } 57 84 58 85 @Test 59 86 public void applies_1() { 60 Relation r = relation(1) 61 Node n = node(1) 62 r.addMember(new RelationMember("my_role", n)) 87 Relation r = relation(1); 88 Node n = node(1); 89 r.addMember(new RelationMember("my_role", n)); 63 90 64 Environment e = new Environment(n).withParent(r).withLinkContext().withIndex(0, r. membersCount)91 Environment e = new Environment(n).withParent(r).withLinkContext().withIndex(0, r.getMembersCount()); 65 92 66 Condition cond = new ConditionFactory.RoleCondition("my_role", Op.EQ) 67 assert cond.applies(e)93 Condition cond = new ConditionFactory.RoleCondition("my_role", Op.EQ); 94 assertTrue(cond.applies(e)); 68 95 69 cond = new ConditionFactory.RoleCondition("another_role", Op.EQ) 70 assert !cond.applies(e)96 cond = new ConditionFactory.RoleCondition("another_role", Op.EQ); 97 assertFalse(cond.applies(e)); 71 98 } 72 99 73 100 @Test 74 101 public void applies_2() { 75 Relation r = relation(1) 76 Node n = node(1) 77 r.addMember(new RelationMember("my_role", n)) 102 Relation r = relation(1); 103 Node n = node(1); 104 r.addMember(new RelationMember("my_role", n)); 78 105 79 Environment e = new Environment(n).withParent(r).withIndex(0, r. membersCount).withLinkContext()106 Environment e = new Environment(n).withParent(r).withIndex(0, r.getMembersCount()).withLinkContext(); 80 107 81 Condition cond = ConditionFactory.createKeyValueCondition("role", "my_role", Op.NEQ, Context.LINK, false) 82 assert !cond.applies(e)108 Condition cond = ConditionFactory.createKeyValueCondition("role", "my_role", Op.NEQ, Context.LINK, false); 109 assertFalse(cond.applies(e)); 83 110 84 cond = ConditionFactory.createKeyValueCondition("role", "another_role", Op.NEQ, Context.LINK, false) 85 assert cond.applies(e)111 cond = ConditionFactory.createKeyValueCondition("role", "another_role", Op.NEQ, Context.LINK, false); 112 assertTrue(cond.applies(e)); 86 113 } 87 114 88 115 @Test 89 116 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")))117 Selector selPos = new MapCSSParser(new StringReader("*[/^source/ =~ /.*,.*/]")).selector(); 118 Selector selNeg = new MapCSSParser(new StringReader("*[/^source/ !~ /.*,.*/]")).selector(); 119 assertFalse(selPos.matches(new Environment(OsmUtils.createPrimitive("way foo=bar")))); 120 assertTrue(selPos.matches(new Environment(OsmUtils.createPrimitive("way source=1,2")))); 121 assertTrue(selPos.matches(new Environment(OsmUtils.createPrimitive("way source_foo_bar=1,2")))); 122 assertFalse(selPos.matches(new Environment(OsmUtils.createPrimitive("way source=1")))); 123 assertFalse(selPos.matches(new Environment(OsmUtils.createPrimitive("way source=1")))); 124 assertFalse(selNeg.matches(new Environment(OsmUtils.createPrimitive("way source=1,2")))); 125 assertFalse(selNeg.matches(new Environment(OsmUtils.createPrimitive("way foo=bar source=1,2")))); 126 assertTrue(selNeg.matches(new Environment(OsmUtils.createPrimitive("way foo=bar source=baz")))); 127 assertTrue(selNeg.matches(new Environment(OsmUtils.createPrimitive("way foo=bar src=1,2")))); 101 128 } 102 129 … … 104 131 public void testValueFive() throws Exception { 105 132 // 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")))133 Selector sel = new MapCSSParser(new StringReader("*[width=5]")).selector(); 134 assertTrue(sel.matches(new Environment(OsmUtils.createPrimitive("way highway=track width=5")))); 135 assertFalse(sel.matches(new Environment(OsmUtils.createPrimitive("way highway=track width=2")))); 109 136 } 110 137 … … 112 139 public void testValueZero() throws Exception { 113 140 // 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")))141 Selector sel = new MapCSSParser(new StringReader("*[frequency=0]")).selector(); 142 assertTrue(sel.matches(new Environment(OsmUtils.createPrimitive("way railway=rail frequency=0")))); 143 assertFalse(sel.matches(new Environment(OsmUtils.createPrimitive("way railway=rail frequency=50")))); 117 144 } 118 145 } -
trunk/test/unit/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSParserTest.java
r14051 r14064 1 package org.openstreetmap.josm.gui.mappaint.mapcss 2 3 import java.awt.Color 4 5 import org.junit.Before 6 import org.junit.Test 7 import org.openstreetmap.josm.JOSMFixture 8 import org.openstreetmap.josm.data.coor.LatLon 9 import org.openstreetmap.josm.data.osm.DataSet 10 import org.openstreetmap.josm.data.osm.Node 11 import org.openstreetmap.josm.data.osm.OsmUtils 12 import org.openstreetmap.josm.data.osm.Way 13 import org.openstreetmap.josm.gui.mappaint.Environment 14 import org.openstreetmap.josm.gui.mappaint.MultiCascade 15 import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.ClassCondition 16 import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.KeyCondition 17 import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.KeyMatchType 18 import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.KeyValueCondition 19 import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.Op 20 import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.PseudoClassCondition 21 import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.SimpleKeyValueCondition 22 import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.MapCSSParser 23 import org.openstreetmap.josm.tools.ColorHelper 24 25 class MapCSSParserTest { 1 // License: GPL. For details, see LICENSE file. 2 package org.openstreetmap.josm.gui.mappaint.mapcss; 3 4 import static org.junit.Assert.assertEquals; 5 import static org.junit.Assert.assertFalse; 6 import static org.junit.Assert.assertNull; 7 import static org.junit.Assert.assertTrue; 8 9 import java.awt.Color; 10 import java.io.StringReader; 11 import java.net.URL; 12 import java.util.List; 13 14 import org.junit.Rule; 15 import org.junit.Test; 16 import org.openstreetmap.josm.data.coor.LatLon; 17 import org.openstreetmap.josm.data.osm.DataSet; 18 import org.openstreetmap.josm.data.osm.Node; 19 import org.openstreetmap.josm.data.osm.OsmUtils; 20 import org.openstreetmap.josm.data.osm.Way; 21 import org.openstreetmap.josm.gui.mappaint.Environment; 22 import org.openstreetmap.josm.gui.mappaint.MultiCascade; 23 import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.ClassCondition; 24 import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.KeyCondition; 25 import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.KeyMatchType; 26 import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.KeyValueCondition; 27 import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.Op; 28 import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.PseudoClassCondition; 29 import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.SimpleKeyValueCondition; 30 import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.ChildOrParentSelector; 31 import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.MapCSSParser; 32 import org.openstreetmap.josm.testutils.JOSMTestRules; 33 import org.openstreetmap.josm.tools.ColorHelper; 34 35 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; 36 37 /** 38 * Unit tests of {@link MapCSSParser}. 39 */ 40 public class MapCSSParserTest { 26 41 27 42 protected static Environment getEnvironment(String key, String value) { 28 return new Environment(OsmUtils.createPrimitive("way " + key + "=" + value)) 43 return new Environment(OsmUtils.createPrimitive("way " + key + "=" + value)); 29 44 } 30 45 … … 33 48 } 34 49 35 @Before 36 public void setUp() throws Exception { 37 JOSMFixture.createUnitTestFixture().init(); 38 } 50 /** 51 * Setup rule 52 */ 53 @Rule 54 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") 55 public JOSMTestRules test = new JOSMTestRules().projection(); 39 56 40 57 @Test 41 58 public void testKothicStylesheets() throws Exception { 42 new MapCSSParser(new URL("https://raw.githubusercontent.com/kothic/kothic/master/src/styles/default.mapcss").openStream(), "UTF-8") 43 new MapCSSParser(new URL("https://raw.githubusercontent.com/kothic/kothic/master/src/styles/mapink.mapcss").openStream(), "UTF-8") 44 } 45 46 @Test 47 public void testDeclarations() {48 getParser("{ opacity: 0.5; color: rgb(1.0, 0.0, 0.0); }").declaration() 49 getParser("{ set tag=value; }").declaration() //set a tag50 getParser("{ set tag; }").declaration() // set a tag to 'yes'51 getParser("{ opacity: eval(\"tag('population')/100000\"); }").declaration() 52 getParser("{ set width_in_metres=eval(\"tag('lanes')*3\"); }").declaration() 59 new MapCSSParser(new URL("https://raw.githubusercontent.com/kothic/kothic/master/src/styles/default.mapcss").openStream(), "UTF-8"); 60 new MapCSSParser(new URL("https://raw.githubusercontent.com/kothic/kothic/master/src/styles/mapink.mapcss").openStream(), "UTF-8"); 61 } 62 63 @Test 64 public void testDeclarations() throws Exception { 65 getParser("{ opacity: 0.5; color: rgb(1.0, 0.0, 0.0); }").declaration(); 66 getParser("{ set tag=value; }").declaration(); //set a tag 67 getParser("{ set tag; }").declaration(); // set a tag to 'yes' 68 getParser("{ opacity: eval(\"tag('population')/100000\"); }").declaration(); 69 getParser("{ set width_in_metres=eval(\"tag('lanes')*3\"); }").declaration(); 53 70 } 54 71 55 72 @Test 56 73 public void testClassCondition() throws Exception { 57 def conditions = ((Selector.GeneralSelector) getParser("way[name=X].highway:closed").selector()).conds58 assert conditions.get(0) instanceof SimpleKeyValueCondition59 assert conditions.get(0).applies(getEnvironment("name", "X"))60 assert conditions.get(1) instanceof ClassCondition61 assert conditions.get(2) instanceof PseudoClassCondition62 assert !conditions.get(2).applies(getEnvironment("name", "X"))74 List<Condition> conditions = ((Selector.GeneralSelector) getParser("way[name=X].highway:closed").selector()).conds; 75 assertTrue(conditions.get(0) instanceof SimpleKeyValueCondition); 76 assertTrue(conditions.get(0).applies(getEnvironment("name", "X"))); 77 assertTrue(conditions.get(1) instanceof ClassCondition); 78 assertTrue(conditions.get(2) instanceof PseudoClassCondition); 79 assertFalse(conditions.get(2).applies(getEnvironment("name", "X"))); 63 80 } 64 81 65 82 @Test 66 83 public void testPseudoClassCondition() throws Exception { 67 def c1 = ((Selector.GeneralSelector) getParser("way!:area-style").selector()).conds.get(0)68 def c2 = ((Selector.GeneralSelector) getParser("way!:areaStyle").selector()).conds.get(0)69 def c3 = ((Selector.GeneralSelector) getParser("way!:area_style").selector()).conds.get(0)70 assert c1.toString() == "!:areaStyle"71 assert c2.toString() == "!:areaStyle"72 assert c3.toString() == "!:areaStyle"84 Condition c1 = ((Selector.GeneralSelector) getParser("way!:area-style").selector()).conds.get(0); 85 Condition c2 = ((Selector.GeneralSelector) getParser("way!:areaStyle").selector()).conds.get(0); 86 Condition c3 = ((Selector.GeneralSelector) getParser("way!:area_style").selector()).conds.get(0); 87 assertEquals("!:areaStyle", c1.toString()); 88 assertEquals("!:areaStyle", c2.toString()); 89 assertEquals("!:areaStyle", c3.toString()); 73 90 } 74 91 75 92 @Test 76 93 public void testClassMatching() throws Exception { 77 def css = new MapCSSStyleSource("" +94 MapCSSStyleSource css = new MapCSSStyleSource( 78 95 "way[highway=footway] { set .path; color: #FF6644; width: 2; }\n" + 79 96 "way[highway=path] { set path; color: brown; width: 2; }\n" + … … 81 98 "way.path { text:auto; text-color: green; text-position: line; text-offset: 5; }\n" + 82 99 "way!.path { color: orange; }\n" 83 ) 84 css.loadStyleSource() 85 assert css.getErrors().isEmpty()86 def mc1 = new MultiCascade()100 ); 101 css.loadStyleSource(); 102 assertTrue(css.getErrors().isEmpty()); 103 MultiCascade mc1 = new MultiCascade(); 87 104 css.apply(mc1, OsmUtils.createPrimitive("way highway=path"), 1, false); 88 assert "green".equals(mc1.getCascade("default").get("text-color", null, String.class))89 assert "brown".equals(mc1.getCascade("default").get("color", null, String.class))90 def mc2 = new MultiCascade()105 assertEquals("green", mc1.getCascade("default").get("text-color", null, String.class)); 106 assertEquals("brown", mc1.getCascade("default").get("color", null, String.class)); 107 MultiCascade mc2 = new MultiCascade(); 91 108 css.apply(mc2, OsmUtils.createPrimitive("way highway=residential"), 1, false); 92 assert "orange".equals(mc2.getCascade("default").get("color", null, String.class))93 assert mc2.getCascade("default").get("text-color", null, String.class) == null94 def mc3 = new MultiCascade()109 assertEquals("orange", mc2.getCascade("default").get("color", null, String.class)); 110 assertNull(mc2.getCascade("default").get("text-color", null, String.class)); 111 MultiCascade mc3 = new MultiCascade(); 95 112 css.apply(mc3, OsmUtils.createPrimitive("way highway=footway"), 1, false); 96 assert ColorHelper.html2color("#FF6644").equals(mc3.getCascade("default").get("color", null, Color.class))113 assertEquals(ColorHelper.html2color("#FF6644"), mc3.getCascade("default").get("color", null, Color.class)); 97 114 } 98 115 99 116 @Test 100 117 public void testEqualCondition() throws Exception { 101 def condition = (SimpleKeyValueCondition) getParser("[surface=paved]").condition(Condition.Context.PRIMITIVE)102 assert condition instanceof SimpleKeyValueCondition103 assert "surface".equals(condition.k)104 assert "paved".equals(condition.v)105 assert condition.applies(getEnvironment("surface", "paved"))106 assert !condition.applies(getEnvironment("surface", "unpaved"))118 Condition condition = getParser("[surface=paved]").condition(Condition.Context.PRIMITIVE); 119 assertTrue(condition instanceof SimpleKeyValueCondition); 120 assertEquals("surface", ((SimpleKeyValueCondition) condition).k); 121 assertEquals("paved", ((SimpleKeyValueCondition) condition).v); 122 assertTrue(condition.applies(getEnvironment("surface", "paved"))); 123 assertFalse(condition.applies(getEnvironment("surface", "unpaved"))); 107 124 } 108 125 109 126 @Test 110 127 public void testNotEqualCondition() throws Exception { 111 def condition = (KeyValueCondition) getParser("[surface!=paved]").condition(Condition.Context.PRIMITIVE)112 assert Op.NEQ.equals(condition.op)113 assert !condition.applies(getEnvironment("surface", "paved"))114 assert condition.applies(getEnvironment("surface", "unpaved"))128 KeyValueCondition condition = (KeyValueCondition) getParser("[surface!=paved]").condition(Condition.Context.PRIMITIVE); 129 assertEquals(Op.NEQ, condition.op); 130 assertFalse(condition.applies(getEnvironment("surface", "paved"))); 131 assertTrue(condition.applies(getEnvironment("surface", "unpaved"))); 115 132 } 116 133 117 134 @Test 118 135 public void testRegexCondition() throws Exception { 119 def condition = (KeyValueCondition) getParser("[surface=~/paved|unpaved/]").condition(Condition.Context.PRIMITIVE)120 assert Op.REGEX.equals(condition.op)121 assert condition.applies(getEnvironment("surface", "unpaved"))122 assert !condition.applies(getEnvironment("surface", "grass"))136 KeyValueCondition condition = (KeyValueCondition) getParser("[surface=~/paved|unpaved/]").condition(Condition.Context.PRIMITIVE); 137 assertEquals(Op.REGEX, condition.op); 138 assertTrue(condition.applies(getEnvironment("surface", "unpaved"))); 139 assertFalse(condition.applies(getEnvironment("surface", "grass"))); 123 140 } 124 141 125 142 @Test 126 143 public void testRegexConditionParenthesis() throws Exception { 127 def condition = (KeyValueCondition) getParser("[name =~ /^\\(foo\\)/]").condition(Condition.Context.PRIMITIVE)128 assert condition.applies(getEnvironment("name", "(foo)"))129 assert !condition.applies(getEnvironment("name", "foo"))130 assert !condition.applies(getEnvironment("name", "((foo))"))144 KeyValueCondition condition = (KeyValueCondition) getParser("[name =~ /^\\(foo\\)/]").condition(Condition.Context.PRIMITIVE); 145 assertTrue(condition.applies(getEnvironment("name", "(foo)"))); 146 assertFalse(condition.applies(getEnvironment("name", "foo"))); 147 assertFalse(condition.applies(getEnvironment("name", "((foo))"))); 131 148 } 132 149 133 150 @Test 134 151 public void testNegatedRegexCondition() throws Exception { 135 def condition = (KeyValueCondition) getParser("[surface!~/paved|unpaved/]").condition(Condition.Context.PRIMITIVE)136 assert Op.NREGEX.equals(condition.op)137 assert !condition.applies(getEnvironment("surface", "unpaved"))138 assert condition.applies(getEnvironment("surface", "grass"))152 KeyValueCondition condition = (KeyValueCondition) getParser("[surface!~/paved|unpaved/]").condition(Condition.Context.PRIMITIVE); 153 assertEquals(Op.NREGEX, condition.op); 154 assertFalse(condition.applies(getEnvironment("surface", "unpaved"))); 155 assertTrue(condition.applies(getEnvironment("surface", "grass"))); 139 156 } 140 157 141 158 @Test 142 159 public void testBeginsEndsWithCondition() throws Exception { 143 def condition = (KeyValueCondition) getParser('[foo ^= bar]').condition(Condition.Context.PRIMITIVE)144 assert Op.BEGINS_WITH.equals(condition.op)145 assert condition.applies(getEnvironment("foo", "bar123"))146 assert !condition.applies(getEnvironment("foo", "123bar"))147 assert !condition.applies(getEnvironment("foo", "123bar123"))148 condition = (KeyValueCondition) getParser( '[foo $= bar]').condition(Condition.Context.PRIMITIVE)149 assert Op.ENDS_WITH.equals(condition.op)150 assert !condition.applies(getEnvironment("foo", "bar123"))151 assert condition.applies(getEnvironment("foo", "123bar"))152 assert !condition.applies(getEnvironment("foo", "123bar123"))160 KeyValueCondition condition = (KeyValueCondition) getParser("[foo ^= bar]").condition(Condition.Context.PRIMITIVE); 161 assertEquals(Op.BEGINS_WITH, condition.op); 162 assertTrue(condition.applies(getEnvironment("foo", "bar123"))); 163 assertFalse(condition.applies(getEnvironment("foo", "123bar"))); 164 assertFalse(condition.applies(getEnvironment("foo", "123bar123"))); 165 condition = (KeyValueCondition) getParser("[foo $= bar]").condition(Condition.Context.PRIMITIVE); 166 assertEquals(Op.ENDS_WITH, condition.op); 167 assertFalse(condition.applies(getEnvironment("foo", "bar123"))); 168 assertTrue(condition.applies(getEnvironment("foo", "123bar"))); 169 assertFalse(condition.applies(getEnvironment("foo", "123bar123"))); 153 170 } 154 171 155 172 @Test 156 173 public void testOneOfCondition() throws Exception { 157 def condition = getParser('[vending~=stamps]').condition(Condition.Context.PRIMITIVE)158 assert condition.applies(getEnvironment("vending", "stamps"))159 assert condition.applies(getEnvironment("vending", "bar;stamps;foo"))160 assert !condition.applies(getEnvironment("vending", "every;thing;else"))161 assert !condition.applies(getEnvironment("vending", "or_nothing"))174 Condition condition = getParser("[vending~=stamps]").condition(Condition.Context.PRIMITIVE); 175 assertTrue(condition.applies(getEnvironment("vending", "stamps"))); 176 assertTrue(condition.applies(getEnvironment("vending", "bar;stamps;foo"))); 177 assertFalse(condition.applies(getEnvironment("vending", "every;thing;else"))); 178 assertFalse(condition.applies(getEnvironment("vending", "or_nothing"))); 162 179 } 163 180 164 181 @Test 165 182 public void testStandardKeyCondition() throws Exception { 166 def c1 = (KeyCondition) getParser("[ highway ]").condition(Condition.Context.PRIMITIVE)167 assert KeyMatchType.EQ.equals(c1.matchType)168 assert c1.applies(getEnvironment("highway", "unclassified"))169 assert !c1.applies(getEnvironment("railway", "rail"))170 def c2 = (KeyCondition) getParser("[\"/slash/\"]").condition(Condition.Context.PRIMITIVE)171 assert KeyMatchType.EQ.equals(c2.matchType)172 assert c2.applies(getEnvironment("/slash/", "yes"))173 assert !c2.applies(getEnvironment("\"slash\"", "no"))183 KeyCondition c1 = (KeyCondition) getParser("[ highway ]").condition(Condition.Context.PRIMITIVE); 184 assertEquals(KeyMatchType.EQ, c1.matchType); 185 assertTrue(c1.applies(getEnvironment("highway", "unclassified"))); 186 assertFalse(c1.applies(getEnvironment("railway", "rail"))); 187 KeyCondition c2 = (KeyCondition) getParser("[\"/slash/\"]").condition(Condition.Context.PRIMITIVE); 188 assertEquals(KeyMatchType.EQ, c2.matchType); 189 assertTrue(c2.applies(getEnvironment("/slash/", "yes"))); 190 assertFalse(c2.applies(getEnvironment("\"slash\"", "no"))); 174 191 } 175 192 176 193 @Test 177 194 public void testYesNoKeyCondition() throws Exception { 178 def c1 = (KeyCondition) getParser("[oneway?]").condition(Condition.Context.PRIMITIVE)179 def c2 = (KeyCondition) getParser("[oneway?!]").condition(Condition.Context.PRIMITIVE)180 def c3 = (KeyCondition) getParser("[!oneway?]").condition(Condition.Context.PRIMITIVE)181 def c4 = (KeyCondition) getParser("[!oneway?!]").condition(Condition.Context.PRIMITIVE)182 def yes = getEnvironment("oneway", "yes")183 def no = getEnvironment("oneway", "no")184 def none = getEnvironment("no-oneway", "foo")185 assert c1.applies(yes)186 assert !c1.applies(no)187 assert !c1.applies(none)188 assert !c2.applies(yes)189 assert c2.applies(no)190 assert !c2.applies(none)191 assert !c3.applies(yes)192 assert c3.applies(no)193 assert c3.applies(none)194 assert c4.applies(yes)195 assert !c4.applies(no)196 assert c4.applies(none)195 KeyCondition c1 = (KeyCondition) getParser("[oneway?]").condition(Condition.Context.PRIMITIVE); 196 KeyCondition c2 = (KeyCondition) getParser("[oneway?!]").condition(Condition.Context.PRIMITIVE); 197 KeyCondition c3 = (KeyCondition) getParser("[!oneway?]").condition(Condition.Context.PRIMITIVE); 198 KeyCondition c4 = (KeyCondition) getParser("[!oneway?!]").condition(Condition.Context.PRIMITIVE); 199 Environment yes = getEnvironment("oneway", "yes"); 200 Environment no = getEnvironment("oneway", "no"); 201 Environment none = getEnvironment("no-oneway", "foo"); 202 assertTrue(c1.applies(yes)); 203 assertFalse(c1.applies(no)); 204 assertFalse(c1.applies(none)); 205 assertFalse(c2.applies(yes)); 206 assertTrue(c2.applies(no)); 207 assertFalse(c2.applies(none)); 208 assertFalse(c3.applies(yes)); 209 assertTrue(c3.applies(no)); 210 assertTrue(c3.applies(none)); 211 assertTrue(c4.applies(yes)); 212 assertFalse(c4.applies(no)); 213 assertTrue(c4.applies(none)); 197 214 } 198 215 199 216 @Test 200 217 public void testRegexKeyCondition() throws Exception { 201 def c1 = (KeyCondition) getParser("[/.*:(backward|forward)\$/]").condition(Condition.Context.PRIMITIVE)202 assert KeyMatchType.REGEX.equals(c1.matchType)203 assert !c1.applies(getEnvironment("lanes", "3"))204 assert c1.applies(getEnvironment("lanes:forward", "3"))205 assert c1.applies(getEnvironment("lanes:backward", "3"))206 assert !c1.applies(getEnvironment("lanes:foobar", "3"))218 KeyCondition c1 = (KeyCondition) getParser("[/.*:(backward|forward)$/]").condition(Condition.Context.PRIMITIVE); 219 assertEquals(KeyMatchType.REGEX, c1.matchType); 220 assertFalse(c1.applies(getEnvironment("lanes", "3"))); 221 assertTrue(c1.applies(getEnvironment("lanes:forward", "3"))); 222 assertTrue(c1.applies(getEnvironment("lanes:backward", "3"))); 223 assertFalse(c1.applies(getEnvironment("lanes:foobar", "3"))); 207 224 } 208 225 209 226 @Test 210 227 public void testNRegexKeyConditionSelector() throws Exception { 211 def s1 = getParser("*[sport][tourism != hotel]").selector()212 assert s1.matches(new Environment(OsmUtils.createPrimitive("node sport=foobar")))213 assert !s1.matches(new Environment(OsmUtils.createPrimitive("node sport=foobar tourism=hotel")))214 def s2 = getParser("*[sport][tourism != hotel][leisure !~ /^(sports_centre|stadium|)\$/]").selector()215 assert s2.matches(new Environment(OsmUtils.createPrimitive("node sport=foobar")))216 assert !s2.matches(new Environment(OsmUtils.createPrimitive("node sport=foobar tourism=hotel")))217 assert !s2.matches(new Environment(OsmUtils.createPrimitive("node sport=foobar leisure=stadium")))228 Selector s1 = getParser("*[sport][tourism != hotel]").selector(); 229 assertTrue(s1.matches(new Environment(OsmUtils.createPrimitive("node sport=foobar")))); 230 assertFalse(s1.matches(new Environment(OsmUtils.createPrimitive("node sport=foobar tourism=hotel")))); 231 Selector s2 = getParser("*[sport][tourism != hotel][leisure !~ /^(sports_centre|stadium|)$/]").selector(); 232 assertTrue(s2.matches(new Environment(OsmUtils.createPrimitive("node sport=foobar")))); 233 assertFalse(s2.matches(new Environment(OsmUtils.createPrimitive("node sport=foobar tourism=hotel")))); 234 assertFalse(s2.matches(new Environment(OsmUtils.createPrimitive("node sport=foobar leisure=stadium")))); 218 235 } 219 236 220 237 @Test 221 238 public void testKeyKeyCondition() throws Exception { 222 def c1 = (KeyValueCondition) getParser("[foo = *bar]").condition(Condition.Context.PRIMITIVE)223 def w1 = new Way()224 w1.put("foo", "123") 225 w1.put("bar", "456") 226 assert !c1.applies(new Environment(w1))227 w1.put("bar", "123") 228 assert c1.applies(new Environment(w1))229 def c2 = (KeyValueCondition) getParser("[foo =~ */bar/]").condition(Condition.Context.PRIMITIVE)230 def w2 = new Way(w1)231 w2.put("bar", "[0-9]{3}") 232 assert c2.applies(new Environment(w2))233 w2.put("bar", "[0-9]") 234 assert c2.applies(new Environment(w2))235 w2.put("bar", "^[0-9] \$")236 assert !c2.applies(new Environment(w2))239 KeyValueCondition c1 = (KeyValueCondition) getParser("[foo = *bar]").condition(Condition.Context.PRIMITIVE); 240 Way w1 = new Way(); 241 w1.put("foo", "123"); 242 w1.put("bar", "456"); 243 assertFalse(c1.applies(new Environment(w1))); 244 w1.put("bar", "123"); 245 assertTrue(c1.applies(new Environment(w1))); 246 KeyValueCondition c2 = (KeyValueCondition) getParser("[foo =~ */bar/]").condition(Condition.Context.PRIMITIVE); 247 Way w2 = new Way(w1); 248 w2.put("bar", "[0-9]{3}"); 249 assertTrue(c2.applies(new Environment(w2))); 250 w2.put("bar", "[0-9]"); 251 assertTrue(c2.applies(new Environment(w2))); 252 w2.put("bar", "^[0-9]$"); 253 assertFalse(c2.applies(new Environment(w2))); 237 254 } 238 255 239 256 @Test 240 257 public void testParentTag() throws Exception { 241 def c1 = getParser("way[foo] > node[tag(\"foo\")=parent_tag(\"foo\")] {}").child_selector()242 def ds = new DataSet()243 def w1 = new Way()244 def w2 = new Way()245 def n1 = new Node(new LatLon(1, 1))246 def n2 = new Node(new LatLon(2, 2))247 w1.put("foo", "123") 248 w2.put("foo", "123") 249 n1.put("foo", "123") 250 n2.put("foo", "0") 251 ds.addPrimitive(w1) 252 ds.addPrimitive(n1) 253 ds.addPrimitive(n2) 254 w1.addNode(n1) 255 w2.addNode(n2) 256 assert c1.matches(new Environment(n1))257 assert !c1.matches(new Environment(n2))258 assert !c1.matches(new Environment(w1))259 assert !c1.matches(new Environment(w2))260 n1.put("foo", "0") 261 assert !c1.matches(new Environment(n1))262 n1.put("foo", "123") 263 assert c1.matches(new Environment(n1))258 Selector c1 = getParser("way[foo] > node[tag(\"foo\")=parent_tag(\"foo\")] {}").child_selector(); 259 DataSet ds = new DataSet(); 260 Way w1 = new Way(); 261 Way w2 = new Way(); 262 Node n1 = new Node(new LatLon(1, 1)); 263 Node n2 = new Node(new LatLon(2, 2)); 264 w1.put("foo", "123"); 265 w2.put("foo", "123"); 266 n1.put("foo", "123"); 267 n2.put("foo", "0"); 268 ds.addPrimitive(w1); 269 ds.addPrimitive(n1); 270 ds.addPrimitive(n2); 271 w1.addNode(n1); 272 w2.addNode(n2); 273 assertTrue(c1.matches(new Environment(n1))); 274 assertFalse(c1.matches(new Environment(n2))); 275 assertFalse(c1.matches(new Environment(w1))); 276 assertFalse(c1.matches(new Environment(w2))); 277 n1.put("foo", "0"); 278 assertFalse(c1.matches(new Environment(n1))); 279 n1.put("foo", "123"); 280 assertTrue(c1.matches(new Environment(n1))); 264 281 } 265 282 266 283 @Test 267 284 public void testTicket8568() throws Exception { 268 def sheet = new MapCSSStyleSource("" +285 MapCSSStyleSource sheet = new MapCSSStyleSource( 269 286 "way { width: 5; }\n" + 270 "way[keyA], way[keyB] { width: eval(prop(width)+10); }") 271 sheet.loadStyleSource() 272 def mc = new MultiCascade()273 sheet.apply(mc, OsmUtils.createPrimitive("way foo=bar"), 20, false) 274 assert mc.getCascade(Environment.DEFAULT_LAYER).get("width") == 5275 sheet.apply(mc, OsmUtils.createPrimitive("way keyA=true"), 20, false) 276 assert mc.getCascade(Environment.DEFAULT_LAYER).get("width") == 15277 sheet.apply(mc, OsmUtils.createPrimitive("way keyB=true"), 20, false) 278 assert mc.getCascade(Environment.DEFAULT_LAYER).get("width") == 15279 sheet.apply(mc, OsmUtils.createPrimitive("way keyA=true keyB=true"), 20, false) 280 assert mc.getCascade(Environment.DEFAULT_LAYER).get("width") == 15287 "way[keyA], way[keyB] { width: eval(prop(width)+10); }"); 288 sheet.loadStyleSource(); 289 MultiCascade mc = new MultiCascade(); 290 sheet.apply(mc, OsmUtils.createPrimitive("way foo=bar"), 20, false); 291 assertEquals(Float.valueOf(5f), mc.getCascade(Environment.DEFAULT_LAYER).get("width")); 292 sheet.apply(mc, OsmUtils.createPrimitive("way keyA=true"), 20, false); 293 assertEquals(Float.valueOf(15f), mc.getCascade(Environment.DEFAULT_LAYER).get("width")); 294 sheet.apply(mc, OsmUtils.createPrimitive("way keyB=true"), 20, false); 295 assertEquals(Float.valueOf(15f), mc.getCascade(Environment.DEFAULT_LAYER).get("width")); 296 sheet.apply(mc, OsmUtils.createPrimitive("way keyA=true keyB=true"), 20, false); 297 assertEquals(Float.valueOf(15f), mc.getCascade(Environment.DEFAULT_LAYER).get("width")); 281 298 } 282 299 283 300 @Test 284 301 public void testTicket8071() throws Exception { 285 def sheet = new MapCSSStyleSource("" +286 "*[rcn_ref], *[name] {text: concat(tag(rcn_ref), \" \", tag(name)); }") 287 sheet.loadStyleSource() 288 def mc = new MultiCascade()289 sheet.apply(mc, OsmUtils.createPrimitive("way name=Foo"), 20, false) 290 assert mc.getCascade(Environment.DEFAULT_LAYER).get("text") == " Foo"291 sheet.apply(mc, OsmUtils.createPrimitive("way rcn_ref=15"), 20, false) 292 assert mc.getCascade(Environment.DEFAULT_LAYER).get("text") == "15 "293 sheet.apply(mc, OsmUtils.createPrimitive("way rcn_ref=15 name=Foo"), 20, false) 294 assert mc.getCascade(Environment.DEFAULT_LAYER).get("text") == "15 Foo"302 MapCSSStyleSource sheet = new MapCSSStyleSource( 303 "*[rcn_ref], *[name] {text: concat(tag(rcn_ref), \" \", tag(name)); }"); 304 sheet.loadStyleSource(); 305 MultiCascade mc = new MultiCascade(); 306 sheet.apply(mc, OsmUtils.createPrimitive("way name=Foo"), 20, false); 307 assertEquals(" Foo", mc.getCascade(Environment.DEFAULT_LAYER).get("text")); 308 sheet.apply(mc, OsmUtils.createPrimitive("way rcn_ref=15"), 20, false); 309 assertEquals("15 ", mc.getCascade(Environment.DEFAULT_LAYER).get("text")); 310 sheet.apply(mc, OsmUtils.createPrimitive("way rcn_ref=15 name=Foo"), 20, false); 311 assertEquals("15 Foo", mc.getCascade(Environment.DEFAULT_LAYER).get("text")); 295 312 296 313 sheet = new MapCSSStyleSource("" + 297 "*[rcn_ref], *[name] {text: join(\" - \", tag(rcn_ref), tag(ref), tag(name)); }") 298 sheet.loadStyleSource() 299 sheet.apply(mc, OsmUtils.createPrimitive("way rcn_ref=15 ref=1.5 name=Foo"), 20, false) 300 assert mc.getCascade(Environment.DEFAULT_LAYER).get("text") == "15 - 1.5 - Foo"314 "*[rcn_ref], *[name] {text: join(\" - \", tag(rcn_ref), tag(ref), tag(name)); }"); 315 sheet.loadStyleSource(); 316 sheet.apply(mc, OsmUtils.createPrimitive("way rcn_ref=15 ref=1.5 name=Foo"), 20, false); 317 assertEquals("15 - 1.5 - Foo", mc.getCascade(Environment.DEFAULT_LAYER).get("text")); 301 318 } 302 319 303 320 @Test 304 321 public void testColorNameTicket9191() throws Exception { 305 def e = new Environment(null, new MultiCascade(), Environment.DEFAULT_LAYER, null)306 getParser("{color: testcolour1#88DD22}").declaration().instructions.get(0).execute(e) 307 def expected = new Color(0x88DD22)308 assert e.getCascade(Environment.DEFAULT_LAYER).get("color") == expected322 Environment e = new Environment(null, new MultiCascade(), Environment.DEFAULT_LAYER, null); 323 getParser("{color: testcolour1#88DD22}").declaration().instructions.get(0).execute(e); 324 Color expected = new Color(0x88DD22); 325 assertEquals(expected, e.getCascade(Environment.DEFAULT_LAYER).get("color")); 309 326 } 310 327 311 328 @Test 312 329 public void testColorNameTicket9191Alpha() throws Exception { 313 def e = new Environment(null, new MultiCascade(), Environment.DEFAULT_LAYER, null)314 getParser("{color: testcolour2#12345678}").declaration().instructions.get(0).execute(e) 315 def expected = new Color(0x12, 0x34, 0x56, 0x78)316 assert e.getCascade(Environment.DEFAULT_LAYER).get("color") == expected330 Environment e = new Environment(null, new MultiCascade(), Environment.DEFAULT_LAYER, null); 331 getParser("{color: testcolour2#12345678}").declaration().instructions.get(0).execute(e); 332 Color expected = new Color(0x12, 0x34, 0x56, 0x78); 333 assertEquals(expected, e.getCascade(Environment.DEFAULT_LAYER).get("color")); 317 334 } 318 335 319 336 @Test 320 337 public void testColorParsing() throws Exception { 321 assert ColorHelper.html2color("#12345678") == new Color(0x12, 0x34, 0x56, 0x78)338 assertEquals(new Color(0x12, 0x34, 0x56, 0x78), ColorHelper.html2color("#12345678")); 322 339 } 323 340 324 341 @Test 325 342 public void testChildSelectorGreaterThanSignIsOptional() throws Exception { 326 assert getParser("relation[type=route] way[highway]").child_selector().toString() == 327 getParser("relation[type=route] > way[highway]").child_selector().toString() 343 assertEquals( 344 getParser("relation[type=route] way[highway]").child_selector().toString(), 345 getParser("relation[type=route] > way[highway]").child_selector().toString()); 328 346 } 329 347 330 348 @Test 331 349 public void testSiblingSelector() throws Exception { 332 def s1 = (Selector.ChildOrParentSelector) getParser("*[a?][parent_tag(\"highway\")=\"unclassified\"] + *[b?]").child_selector() 333 def ds = new DataSet() 334 def n1 = new org.openstreetmap.josm.data.osm.Node(new LatLon(1, 2)) 335 n1.put("a", "true") 336 def n2 = new org.openstreetmap.josm.data.osm.Node(new LatLon(1.1, 2.2)) 337 n2.put("b", "true") 338 def w = new Way() 339 w.put("highway", "unclassified") 340 ds.addPrimitive(n1) 341 ds.addPrimitive(n2) 342 ds.addPrimitive(w) 343 w.addNode(n1) 344 w.addNode(n2) 345 346 def e = new Environment(n2) 347 assert s1.matches(e) 348 assert e.osm == n2 349 assert e.child == n1 350 assert e.parent == w 351 assert !s1.matches(new Environment(n1)) 352 assert !s1.matches(new Environment(w)) 350 ChildOrParentSelector s1 = (Selector.ChildOrParentSelector) getParser( 351 "*[a?][parent_tag(\"highway\")=\"unclassified\"] + *[b?]").child_selector(); 352 DataSet ds = new DataSet(); 353 Node n1 = new Node(new LatLon(1, 2)); 354 n1.put("a", "true"); 355 Node n2 = new Node(new LatLon(1.1, 2.2)); 356 n2.put("b", "true"); 357 Way w = new Way(); 358 w.put("highway", "unclassified"); 359 ds.addPrimitive(n1); 360 ds.addPrimitive(n2); 361 ds.addPrimitive(w); 362 w.addNode(n1); 363 w.addNode(n2); 364 365 Environment e = new Environment(n2); 366 assertTrue(s1.matches(e)); 367 assertEquals(n2, e.osm); 368 assertEquals(n1, e.child); 369 assertEquals(w, e.parent); 370 assertFalse(s1.matches(new Environment(n1))); 371 assertFalse(s1.matches(new Environment(w))); 353 372 } 354 373 355 374 @Test 356 375 public void testParentTags() throws Exception { 357 def ds = new DataSet()358 def n = new org.openstreetmap.josm.data.osm.Node(new LatLon(1, 2))359 n.put("foo", "bar") 360 def w1 = new Way()361 w1.put("ref", "x10") 362 def w2 = new Way()363 w2.put("ref", "x2") 364 def w3 = new Way()365 ds.addPrimitive(n) 366 ds.addPrimitive(w1) 367 ds.addPrimitive(w2) 368 ds.addPrimitive(w3) 369 w1.addNode(n) 370 w2.addNode(n) 371 w3.addNode(n) 372 373 MapCSSStyleSource source = new MapCSSStyleSource("node[foo=bar] {refs: join_list(\";\", parent_tags(\"ref\"));}") 374 source.loadStyleSource() 375 assert source.rules.size() == 1376 def e = new Environment(n, new MultiCascade(), Environment.DEFAULT_LAYER, null)377 assert source.rules.get(0).selector.matches(e)378 source.rules.get(0).declaration.execute(e) 379 assert e.getCascade(Environment.DEFAULT_LAYER).get("refs", null, String.class) == "x2;x10"376 DataSet ds = new DataSet(); 377 Node n = new Node(new LatLon(1, 2)); 378 n.put("foo", "bar"); 379 Way w1 = new Way(); 380 w1.put("ref", "x10"); 381 Way w2 = new Way(); 382 w2.put("ref", "x2"); 383 Way w3 = new Way(); 384 ds.addPrimitive(n); 385 ds.addPrimitive(w1); 386 ds.addPrimitive(w2); 387 ds.addPrimitive(w3); 388 w1.addNode(n); 389 w2.addNode(n); 390 w3.addNode(n); 391 392 MapCSSStyleSource source = new MapCSSStyleSource("node[foo=bar] {refs: join_list(\";\", parent_tags(\"ref\"));}"); 393 source.loadStyleSource(); 394 assertEquals(1, source.rules.size()); 395 Environment e = new Environment(n, new MultiCascade(), Environment.DEFAULT_LAYER, null); 396 assertTrue(source.rules.get(0).selector.matches(e)); 397 source.rules.get(0).declaration.execute(e); 398 assertEquals("x2;x10", e.getCascade(Environment.DEFAULT_LAYER).get("refs", null, String.class)); 380 399 } 381 400 382 401 @Test 383 402 public void testSiblingSelectorInterpolation() throws Exception { 384 defs1 = (Selector.ChildOrParentSelector) getParser(403 ChildOrParentSelector s1 = (Selector.ChildOrParentSelector) getParser( 385 404 "*[tag(\"addr:housenumber\") > child_tag(\"addr:housenumber\")][regexp_test(\"even|odd\", parent_tag(\"addr:interpolation\"))]" + 386 " + *[addr:housenumber]").child_selector() 387 def ds = new DataSet()388 def n1 = new org.openstreetmap.josm.data.osm.Node(new LatLon(1, 2))389 n1.put("addr:housenumber", "10") 390 def n2 = new org.openstreetmap.josm.data.osm.Node(new LatLon(1.1, 2.2))391 n2.put("addr:housenumber", "100") 392 def n3 = new org.openstreetmap.josm.data.osm.Node(new LatLon(1.2, 2.3))393 n3.put("addr:housenumber", "20") 394 def w = new Way()395 w.put("addr:interpolation", "even") 396 ds.addPrimitive(n1) 397 ds.addPrimitive(n2) 398 ds.addPrimitive(n3) 399 ds.addPrimitive(w) 400 w.addNode(n1) 401 w.addNode(n2) 402 w.addNode(n3) 403 404 assert s1.right.matches(new Environment(n3))405 assert s1.left.matches(new Environment(n2).withChild(n3).withParent(w))406 assert s1.matches(new Environment(n3))407 assert !s1.matches(new Environment(n1))408 assert !s1.matches(new Environment(n2))409 assert !s1.matches(new Environment(w))405 " + *[addr:housenumber]").child_selector(); 406 DataSet ds = new DataSet(); 407 Node n1 = new Node(new LatLon(1, 2)); 408 n1.put("addr:housenumber", "10"); 409 Node n2 = new Node(new LatLon(1.1, 2.2)); 410 n2.put("addr:housenumber", "100"); 411 Node n3 = new Node(new LatLon(1.2, 2.3)); 412 n3.put("addr:housenumber", "20"); 413 Way w = new Way(); 414 w.put("addr:interpolation", "even"); 415 ds.addPrimitive(n1); 416 ds.addPrimitive(n2); 417 ds.addPrimitive(n3); 418 ds.addPrimitive(w); 419 w.addNode(n1); 420 w.addNode(n2); 421 w.addNode(n3); 422 423 assertTrue(s1.right.matches(new Environment(n3))); 424 assertTrue(s1.left.matches(new Environment(n2).withChild(n3).withParent(w))); 425 assertTrue(s1.matches(new Environment(n3))); 426 assertFalse(s1.matches(new Environment(n1))); 427 assertFalse(s1.matches(new Environment(n2))); 428 assertFalse(s1.matches(new Environment(w))); 410 429 } 411 430 412 431 @Test 413 432 public void testInvalidBaseSelector() throws Exception { 414 def css = new MapCSSStyleSource("invalid_base[key=value] {}")415 css.loadStyleSource() 416 assert !css.getErrors().isEmpty()417 assert css.getErrors().iterator().next().toString().contains("Unknown MapCSS base selector invalid_base")433 MapCSSStyleSource css = new MapCSSStyleSource("invalid_base[key=value] {}"); 434 css.loadStyleSource(); 435 assertFalse(css.getErrors().isEmpty()); 436 assertTrue(css.getErrors().iterator().next().toString().contains("Unknown MapCSS base selector invalid_base")); 418 437 } 419 438 420 439 @Test 421 440 public void testMinMaxFunctions() throws Exception { 422 defsheet = new MapCSSStyleSource("* {" +441 MapCSSStyleSource sheet = new MapCSSStyleSource("* {" + 423 442 "min_value: min(tag(x), tag(y), tag(z)); " + 424 443 "max_value: max(tag(x), tag(y), tag(z)); " + 425 444 "max_split: max(split(\";\", tag(widths))); " + 426 "}") 427 sheet.loadStyleSource() 428 def mc = new MultiCascade()429 430 sheet.apply(mc, OsmUtils.createPrimitive("way x=4 y=6 z=8 u=100"), 20, false) 431 assert mc.getCascade(Environment.DEFAULT_LAYER).get("min_value", Float.NaN, Float.class) == 4.0f432 assert mc.getCascade(Environment.DEFAULT_LAYER).get("max_value", Float.NaN, Float.class) == 8.0f433 434 sheet.apply(mc, OsmUtils.createPrimitive("way x=4 y=6 widths=1;2;8;56;3;a"), 20, false) 435 assert mc.getCascade(Environment.DEFAULT_LAYER).get("min_value", -777f, Float.class) == 4436 assert mc.getCascade(Environment.DEFAULT_LAYER).get("max_value", -777f, Float.class) == 6437 assert mc.getCascade(Environment.DEFAULT_LAYER).get("max_split", -777f, Float.class) == 56445 "}"); 446 sheet.loadStyleSource(); 447 MultiCascade mc = new MultiCascade(); 448 449 sheet.apply(mc, OsmUtils.createPrimitive("way x=4 y=6 z=8 u=100"), 20, false); 450 assertEquals(Float.valueOf(4.0f), mc.getCascade(Environment.DEFAULT_LAYER).get("min_value", Float.NaN, Float.class)); 451 assertEquals(Float.valueOf(8.0f), mc.getCascade(Environment.DEFAULT_LAYER).get("max_value", Float.NaN, Float.class)); 452 453 sheet.apply(mc, OsmUtils.createPrimitive("way x=4 y=6 widths=1;2;8;56;3;a"), 20, false); 454 assertEquals(Float.valueOf(4f), mc.getCascade(Environment.DEFAULT_LAYER).get("min_value", -777f, Float.class)); 455 assertEquals(Float.valueOf(6f), mc.getCascade(Environment.DEFAULT_LAYER).get("max_value", -777f, Float.class)); 456 assertEquals(Float.valueOf(56f), mc.getCascade(Environment.DEFAULT_LAYER).get("max_split", -777f, Float.class)); 438 457 } 439 458 440 459 @Test 441 460 public void testTicket12549() throws Exception { 442 def condition = getParser("[name =~ /^(?i)(?u)fóo\$/]").condition(Condition.Context.PRIMITIVE)443 assert condition.applies(new Environment(OsmUtils.createPrimitive("way name=fóo")))444 assert condition.applies(new Environment(OsmUtils.createPrimitive("way name=fÓo")))445 condition = getParser("[name =~ /^(\\p{Lower})+ \$/]").condition(Condition.Context.PRIMITIVE)446 assert !condition.applies(new Environment(OsmUtils.createPrimitive("way name=fóo")))447 condition = getParser("[name =~ /^(?U)(\\p{Lower})+ \$/]").condition(Condition.Context.PRIMITIVE)448 assert condition.applies(new Environment(OsmUtils.createPrimitive("way name=fóo")))449 assert !condition.applies(new Environment(OsmUtils.createPrimitive("way name=fÓo")))461 Condition condition = getParser("[name =~ /^(?i)(?u)fóo$/]").condition(Condition.Context.PRIMITIVE); 462 assertTrue(condition.applies(new Environment(OsmUtils.createPrimitive("way name=fóo")))); 463 assertTrue(condition.applies(new Environment(OsmUtils.createPrimitive("way name=fÓo")))); 464 condition = getParser("[name =~ /^(\\p{Lower})+$/]").condition(Condition.Context.PRIMITIVE); 465 assertFalse(condition.applies(new Environment(OsmUtils.createPrimitive("way name=fóo")))); 466 condition = getParser("[name =~ /^(?U)(\\p{Lower})+$/]").condition(Condition.Context.PRIMITIVE); 467 assertTrue(condition.applies(new Environment(OsmUtils.createPrimitive("way name=fóo")))); 468 assertFalse(condition.applies(new Environment(OsmUtils.createPrimitive("way name=fÓo")))); 450 469 } 451 470 } -
trunk/test/unit/org/openstreetmap/josm/gui/mappaint/mapcss/ParsingLinkSelectorTest.java
r14051 r14064 2 2 package org.openstreetmap.josm.gui.mappaint.mapcss; 3 3 4 import static org.junit.Assert. *4 import static org.junit.Assert.assertEquals; 5 5 6 import org.junit.* 7 import org.openstreetmap.josm.JOSMFixture; 6 import org.junit.Rule; 7 import org.junit.Test; 8 import org.openstreetmap.josm.testutils.JOSMTestRules; 8 9 10 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; 9 11 10 class ParsingLinkSelectorTest { 12 /** 13 * Unit tests of {@code ParsingLinkSelector}. 14 */ 15 public class ParsingLinkSelectorTest { 11 16 12 @BeforeClass 13 public static void createJOSMFixture(){ 14 JOSMFixture.createUnitTestFixture().init() 15 } 17 /** 18 * Setup rule 19 */ 20 @Rule 21 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") 22 public JOSMTestRules test = new JOSMTestRules().projection(); 16 23 17 24 @Test 18 25 public void parseEmptyChildSelector() { 19 def css = """ 20 relation > way {} 21 """ 22 MapCSSStyleSource source = new MapCSSStyleSource(css) 23 source.loadStyleSource() 24 assert source.rules.size() == 1 26 String css = "relation > way {}"; 27 MapCSSStyleSource source = new MapCSSStyleSource(css); 28 source.loadStyleSource(); 29 assertEquals(1, source.rules.size()); 25 30 } 26 31 27 32 @Test 28 33 public void parseEmptyParentSelector() { 29 def css = """ 30 way < relation {} 31 """ 32 MapCSSStyleSource source = new MapCSSStyleSource(css) 33 source.loadStyleSource() 34 assert source.rules.size() == 1 34 String css = "way < relation {}"; 35 MapCSSStyleSource source = new MapCSSStyleSource(css); 36 source.loadStyleSource(); 37 assertEquals(1, source.rules.size()); 35 38 } 36 37 39 38 40 @Test 39 41 public void parseChildSelectorWithKeyValueCondition() { 40 def css = """ 41 relation >[role="my_role"] way {} 42 """ 43 MapCSSStyleSource source = new MapCSSStyleSource(css) 44 source.loadStyleSource() 45 assert source.rules.size() == 1 42 String css = "relation >[role=\"my_role\"] way {}"; 43 MapCSSStyleSource source = new MapCSSStyleSource(css); 44 source.loadStyleSource(); 45 assertEquals(1, source.rules.size()); 46 46 } 47 47 48 48 @Test 49 49 public void parseChildSelectorWithKeyCondition() { 50 def css = """ 51 relation >["my_role"] way{} 52 """ 53 MapCSSStyleSource source = new MapCSSStyleSource(css) 54 source.loadStyleSource() 55 assert source.rules.size() == 1 50 String css = "relation >[\"my_role\"] way{}"; 51 MapCSSStyleSource source = new MapCSSStyleSource(css); 52 source.loadStyleSource(); 53 assertEquals(1, source.rules.size()); 56 54 } 57 55 } 58
Note:
See TracChangeset
for help on using the changeset viewer.