| 1 | package org.openstreetmap.josm.gui.mappaint.mapcss
|
|---|
| 2 |
|
|---|
| 3 | import org.junit.Before
|
|---|
| 4 | import org.junit.Test
|
|---|
| 5 | import org.openstreetmap.josm.Main
|
|---|
| 6 | import org.openstreetmap.josm.data.Preferences
|
|---|
| 7 | import org.openstreetmap.josm.data.osm.OsmPrimitive
|
|---|
| 8 | import org.openstreetmap.josm.data.osm.Way
|
|---|
| 9 | import org.openstreetmap.josm.gui.mappaint.Environment
|
|---|
| 10 | import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.MapCSSParser
|
|---|
| 11 |
|
|---|
| 12 | class MapCSSParserTest {
|
|---|
| 13 |
|
|---|
| 14 | protected static OsmPrimitive getPrimitive(String key, String value) {
|
|---|
| 15 | def w = new Way()
|
|---|
| 16 | w.put(key, value)
|
|---|
| 17 | return w
|
|---|
| 18 | }
|
|---|
| 19 |
|
|---|
| 20 | protected static Environment getEnvironment(String key, String value) {
|
|---|
| 21 | return new Environment().withPrimitive(getPrimitive(key, value))
|
|---|
| 22 | }
|
|---|
| 23 |
|
|---|
| 24 | @Before
|
|---|
| 25 | public void setUp() throws Exception {
|
|---|
| 26 | Main.pref = new Preferences()
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | @Test
|
|---|
| 30 | public void testEqualCondition() throws Exception {
|
|---|
| 31 | def condition = (Condition.KeyValueCondition) new MapCSSParser(new StringReader("[surface=paved]")).condition(Condition.Context.PRIMITIVE)
|
|---|
| 32 | assert condition instanceof Condition.KeyValueCondition
|
|---|
| 33 | assert Condition.Op.EQ.equals(condition.op)
|
|---|
| 34 | assert "surface".equals(condition.k)
|
|---|
| 35 | assert "paved".equals(condition.v)
|
|---|
| 36 | assert condition.applies(getEnvironment("surface", "paved"))
|
|---|
| 37 | assert !condition.applies(getEnvironment("surface", "unpaved"))
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | @Test
|
|---|
| 41 | public void testNotEqualCondition() throws Exception {
|
|---|
| 42 | def condition = (Condition.KeyValueCondition) new MapCSSParser(new StringReader("[surface!=paved]")).condition(Condition.Context.PRIMITIVE)
|
|---|
| 43 | assert Condition.Op.NEQ.equals(condition.op)
|
|---|
| 44 | assert !condition.applies(getEnvironment("surface", "paved"))
|
|---|
| 45 | assert condition.applies(getEnvironment("surface", "unpaved"))
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | @Test
|
|---|
| 49 | public void testRegexCondition() throws Exception {
|
|---|
| 50 | def condition = (Condition.KeyValueCondition) new MapCSSParser(new StringReader("[surface=~/paved|unpaved/]")).condition(Condition.Context.PRIMITIVE)
|
|---|
| 51 | assert Condition.Op.REGEX.equals(condition.op)
|
|---|
| 52 | assert condition.applies(getEnvironment("surface", "unpaved"))
|
|---|
| 53 | assert !condition.applies(getEnvironment("surface", "grass"))
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | @Test
|
|---|
| 57 | public void testNegatedRegexCondition() throws Exception {
|
|---|
| 58 | def condition = (Condition.KeyValueCondition) new MapCSSParser(new StringReader("[surface!~/paved|unpaved/]")).condition(Condition.Context.PRIMITIVE)
|
|---|
| 59 | assert Condition.Op.NREGEX.equals(condition.op)
|
|---|
| 60 | assert !condition.applies(getEnvironment("surface", "unpaved"))
|
|---|
| 61 | assert condition.applies(getEnvironment("surface", "grass"))
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | @Test
|
|---|
| 65 | public void testStandardKeyCondition() throws Exception {
|
|---|
| 66 | def c1 = (Condition.KeyCondition) new MapCSSParser(new StringReader("[ highway ]")).condition(Condition.Context.PRIMITIVE)
|
|---|
| 67 | assert c1.matchType == null
|
|---|
| 68 | assert c1.applies(getEnvironment("highway", "unclassified"))
|
|---|
| 69 | assert !c1.applies(getEnvironment("railway", "rail"))
|
|---|
| 70 | def c2 = (Condition.KeyCondition) new MapCSSParser(new StringReader("[\"/slash/\"]")).condition(Condition.Context.PRIMITIVE)
|
|---|
| 71 | assert c2.matchType == null
|
|---|
| 72 | assert c2.applies(getEnvironment("/slash/", "yes"))
|
|---|
| 73 | assert !c2.applies(getEnvironment("\"slash\"", "no"))
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | @Test
|
|---|
| 77 | public void testYesNoKeyCondition() throws Exception {
|
|---|
| 78 | def c1 = (Condition.KeyCondition) new MapCSSParser(new StringReader("[oneway?]")).condition(Condition.Context.PRIMITIVE)
|
|---|
| 79 | def c2 = (Condition.KeyCondition) new MapCSSParser(new StringReader("[oneway?!]")).condition(Condition.Context.PRIMITIVE)
|
|---|
| 80 | def c3 = (Condition.KeyCondition) new MapCSSParser(new StringReader("[!oneway?]")).condition(Condition.Context.PRIMITIVE)
|
|---|
| 81 | def c4 = (Condition.KeyCondition) new MapCSSParser(new StringReader("[!oneway?!]")).condition(Condition.Context.PRIMITIVE)
|
|---|
| 82 | def yes = getEnvironment("oneway", "yes")
|
|---|
| 83 | def no = getEnvironment("oneway", "no")
|
|---|
| 84 | def none = getEnvironment("no-oneway", "foo")
|
|---|
| 85 | assert c1.applies(yes)
|
|---|
| 86 | assert !c1.applies(no)
|
|---|
| 87 | assert !c1.applies(none)
|
|---|
| 88 | assert !c2.applies(yes)
|
|---|
| 89 | assert c2.applies(no)
|
|---|
| 90 | assert !c2.applies(none)
|
|---|
| 91 | assert !c3.applies(yes)
|
|---|
| 92 | assert c3.applies(no)
|
|---|
| 93 | assert c3.applies(none)
|
|---|
| 94 | assert c4.applies(yes)
|
|---|
| 95 | assert !c4.applies(no)
|
|---|
| 96 | assert c4.applies(none)
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | @Test
|
|---|
| 100 | public void testRegexKeyCondition() throws Exception {
|
|---|
| 101 | def c1 = (Condition.KeyCondition) new MapCSSParser(new StringReader("[/.*:(backward|forward)\$/]")).condition(Condition.Context.PRIMITIVE)
|
|---|
| 102 | assert Condition.KeyMatchType.REGEX.equals(c1.matchType)
|
|---|
| 103 | assert !c1.applies(getEnvironment("lanes", "3"))
|
|---|
| 104 | assert c1.applies(getEnvironment("lanes:forward", "3"))
|
|---|
| 105 | assert c1.applies(getEnvironment("lanes:backward", "3"))
|
|---|
| 106 | assert !c1.applies(getEnvironment("lanes:foobar", "3"))
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | @Test
|
|---|
| 110 | public void testKeyKeyCondition() throws Exception {
|
|---|
| 111 | def c1 = (Condition.KeyValueCondition) new MapCSSParser(new StringReader("[foo = *bar]")).condition(Condition.Context.PRIMITIVE)
|
|---|
| 112 | def w1 = new Way()
|
|---|
| 113 | w1.put("foo", "123")
|
|---|
| 114 | w1.put("bar", "456")
|
|---|
| 115 | assert !c1.applies(new Environment().withPrimitive(w1))
|
|---|
| 116 | w1.put("bar", "123")
|
|---|
| 117 | assert c1.applies(new Environment().withPrimitive(w1))
|
|---|
| 118 | def c2 = (Condition.KeyValueCondition) new MapCSSParser(new StringReader("[foo =~ */bar/]")).condition(Condition.Context.PRIMITIVE)
|
|---|
| 119 | def w2 = new Way(w1)
|
|---|
| 120 | w2.put("bar", "[0-9]{3}")
|
|---|
| 121 | assert c2.applies(new Environment().withPrimitive(w2))
|
|---|
| 122 | w2.put("bar", "[0-9]")
|
|---|
| 123 | assert c2.applies(new Environment().withPrimitive(w2))
|
|---|
| 124 | w2.put("bar", "^[0-9]\$")
|
|---|
| 125 | assert !c2.applies(new Environment().withPrimitive(w2))
|
|---|
| 126 | }
|
|---|
| 127 | }
|
|---|