| 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.MultiCascade
|
|---|
| 11 | import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.MapCSSParser
|
|---|
| 12 |
|
|---|
| 13 | class MapCSSParserTest {
|
|---|
| 14 |
|
|---|
| 15 | protected static OsmPrimitive getPrimitive(String key, String value) {
|
|---|
| 16 | def w = new Way()
|
|---|
| 17 | w.put(key, value)
|
|---|
| 18 | return w
|
|---|
| 19 | }
|
|---|
| 20 |
|
|---|
| 21 | protected static Environment getEnvironment(String key, String value) {
|
|---|
| 22 | return new Environment().withPrimitive(getPrimitive(key, value))
|
|---|
| 23 | }
|
|---|
| 24 |
|
|---|
| 25 | protected static MapCSSParser getParser(String stringToParse) {
|
|---|
| 26 | return new MapCSSParser(new StringReader(stringToParse));
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | @Before
|
|---|
| 30 | public void setUp() throws Exception {
|
|---|
| 31 | Main.pref = new Preferences()
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | @Test
|
|---|
| 35 | public void testKothicStylesheets() throws Exception {
|
|---|
| 36 | new MapCSSParser(new URL("http://kothic.googlecode.com/hg/src/styles/default.mapcss").openStream(), "UTF-8")
|
|---|
| 37 | new MapCSSParser(new URL("http://kothic.googlecode.com/hg/src/styles/mapink.mapcss").openStream(), "UTF-8")
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | @Test
|
|---|
| 41 | public void testDeclarations() {
|
|---|
| 42 | getParser("{ opacity: 0.5; color: rgb(1.0, 0.0, 0.0); }").declaration()
|
|---|
| 43 | getParser("{ set tag=value; }").declaration() //set a tag
|
|---|
| 44 | getParser("{ set tag; }").declaration() // set a tag to 'yes'
|
|---|
| 45 | getParser("{ opacity: eval(\"tag('population')/100000\"); }").declaration()
|
|---|
| 46 | getParser("{ set width_in_metres=eval(\"tag('lanes')*3\"); }").declaration()
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | @Test
|
|---|
| 50 | public void testClassCondition() throws Exception {
|
|---|
| 51 | def conditions = ((Selector.GeneralSelector) getParser("way[name=X].highway:closed").selector()).conds
|
|---|
| 52 | assert conditions.get(0) instanceof Condition.KeyValueCondition
|
|---|
| 53 | assert conditions.get(0).applies(getEnvironment("name", "X"))
|
|---|
| 54 | assert conditions.get(1) instanceof Condition.ClassCondition
|
|---|
| 55 | assert conditions.get(2) instanceof Condition.PseudoClassCondition
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | @Test
|
|---|
| 59 | public void testClassMatching() throws Exception {
|
|---|
| 60 | def css = new MapCSSStyleSource("")
|
|---|
| 61 | getParser("" +
|
|---|
| 62 | "way[highway=footway] { set path; color: #FF6644; width: 2; }\n" +
|
|---|
| 63 | "way[highway=path] { set path; color: brown; width: 2; }\n" +
|
|---|
| 64 | "way[\"set\"=escape] { }\n" +
|
|---|
| 65 | "way.path { text:auto; text-color: green; text-position: line; text-offset: 5; }\n" +
|
|---|
| 66 | "way!.path { color: orange; }\n"
|
|---|
| 67 | ).sheet(css)
|
|---|
| 68 | assert css.getErrors().isEmpty()
|
|---|
| 69 | def mc1 = new MultiCascade()
|
|---|
| 70 | css.apply(mc1, getPrimitive("highway", "path"), 1, null, false);
|
|---|
| 71 | assert "green".equals(mc1.getCascade("default").get("text-color", null, String.class))
|
|---|
| 72 | assert "brown".equals(mc1.getCascade("default").get("color", null, String.class))
|
|---|
| 73 | def mc2 = new MultiCascade()
|
|---|
| 74 | css.apply(mc2, getPrimitive("highway", "residential"), 1, null, false);
|
|---|
| 75 | assert "orange".equals(mc2.getCascade("default").get("color", null, String.class))
|
|---|
| 76 | assert mc2.getCascade("default").get("text-color", null, String.class) == null
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | @Test
|
|---|
| 80 | public void testEqualCondition() throws Exception {
|
|---|
| 81 | def condition = (Condition.KeyValueCondition) getParser("[surface=paved]").condition(Condition.Context.PRIMITIVE)
|
|---|
| 82 | assert condition instanceof Condition.KeyValueCondition
|
|---|
| 83 | assert Condition.Op.EQ.equals(condition.op)
|
|---|
| 84 | assert "surface".equals(condition.k)
|
|---|
| 85 | assert "paved".equals(condition.v)
|
|---|
| 86 | assert condition.applies(getEnvironment("surface", "paved"))
|
|---|
| 87 | assert !condition.applies(getEnvironment("surface", "unpaved"))
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | @Test
|
|---|
| 91 | public void testNotEqualCondition() throws Exception {
|
|---|
| 92 | def condition = (Condition.KeyValueCondition) getParser("[surface!=paved]").condition(Condition.Context.PRIMITIVE)
|
|---|
| 93 | assert Condition.Op.NEQ.equals(condition.op)
|
|---|
| 94 | assert !condition.applies(getEnvironment("surface", "paved"))
|
|---|
| 95 | assert condition.applies(getEnvironment("surface", "unpaved"))
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | @Test
|
|---|
| 99 | public void testRegexCondition() throws Exception {
|
|---|
| 100 | def condition = (Condition.KeyValueCondition) getParser("[surface=~/paved|unpaved/]").condition(Condition.Context.PRIMITIVE)
|
|---|
| 101 | assert Condition.Op.REGEX.equals(condition.op)
|
|---|
| 102 | assert condition.applies(getEnvironment("surface", "unpaved"))
|
|---|
| 103 | assert !condition.applies(getEnvironment("surface", "grass"))
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | @Test
|
|---|
| 107 | public void testNegatedRegexCondition() throws Exception {
|
|---|
| 108 | def condition = (Condition.KeyValueCondition) getParser("[surface!~/paved|unpaved/]").condition(Condition.Context.PRIMITIVE)
|
|---|
| 109 | assert Condition.Op.NREGEX.equals(condition.op)
|
|---|
| 110 | assert !condition.applies(getEnvironment("surface", "unpaved"))
|
|---|
| 111 | assert condition.applies(getEnvironment("surface", "grass"))
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | @Test
|
|---|
| 115 | public void testStandardKeyCondition() throws Exception {
|
|---|
| 116 | def c1 = (Condition.KeyCondition) getParser("[ highway ]").condition(Condition.Context.PRIMITIVE)
|
|---|
| 117 | assert c1.matchType == null
|
|---|
| 118 | assert c1.applies(getEnvironment("highway", "unclassified"))
|
|---|
| 119 | assert !c1.applies(getEnvironment("railway", "rail"))
|
|---|
| 120 | def c2 = (Condition.KeyCondition) getParser("[\"/slash/\"]").condition(Condition.Context.PRIMITIVE)
|
|---|
| 121 | assert c2.matchType == null
|
|---|
| 122 | assert c2.applies(getEnvironment("/slash/", "yes"))
|
|---|
| 123 | assert !c2.applies(getEnvironment("\"slash\"", "no"))
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | @Test
|
|---|
| 127 | public void testYesNoKeyCondition() throws Exception {
|
|---|
| 128 | def c1 = (Condition.KeyCondition) getParser("[oneway?]").condition(Condition.Context.PRIMITIVE)
|
|---|
| 129 | def c2 = (Condition.KeyCondition) getParser("[oneway?!]").condition(Condition.Context.PRIMITIVE)
|
|---|
| 130 | def c3 = (Condition.KeyCondition) getParser("[!oneway?]").condition(Condition.Context.PRIMITIVE)
|
|---|
| 131 | def c4 = (Condition.KeyCondition) getParser("[!oneway?!]").condition(Condition.Context.PRIMITIVE)
|
|---|
| 132 | def yes = getEnvironment("oneway", "yes")
|
|---|
| 133 | def no = getEnvironment("oneway", "no")
|
|---|
| 134 | def none = getEnvironment("no-oneway", "foo")
|
|---|
| 135 | assert c1.applies(yes)
|
|---|
| 136 | assert !c1.applies(no)
|
|---|
| 137 | assert !c1.applies(none)
|
|---|
| 138 | assert !c2.applies(yes)
|
|---|
| 139 | assert c2.applies(no)
|
|---|
| 140 | assert !c2.applies(none)
|
|---|
| 141 | assert !c3.applies(yes)
|
|---|
| 142 | assert c3.applies(no)
|
|---|
| 143 | assert c3.applies(none)
|
|---|
| 144 | assert c4.applies(yes)
|
|---|
| 145 | assert !c4.applies(no)
|
|---|
| 146 | assert c4.applies(none)
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | @Test
|
|---|
| 150 | public void testRegexKeyCondition() throws Exception {
|
|---|
| 151 | def c1 = (Condition.KeyCondition) getParser("[/.*:(backward|forward)\$/]").condition(Condition.Context.PRIMITIVE)
|
|---|
| 152 | assert Condition.KeyMatchType.REGEX.equals(c1.matchType)
|
|---|
| 153 | assert !c1.applies(getEnvironment("lanes", "3"))
|
|---|
| 154 | assert c1.applies(getEnvironment("lanes:forward", "3"))
|
|---|
| 155 | assert c1.applies(getEnvironment("lanes:backward", "3"))
|
|---|
| 156 | assert !c1.applies(getEnvironment("lanes:foobar", "3"))
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | @Test
|
|---|
| 160 | public void testKeyKeyCondition() throws Exception {
|
|---|
| 161 | def c1 = (Condition.KeyValueCondition) getParser("[foo = *bar]").condition(Condition.Context.PRIMITIVE)
|
|---|
| 162 | def w1 = new Way()
|
|---|
| 163 | w1.put("foo", "123")
|
|---|
| 164 | w1.put("bar", "456")
|
|---|
| 165 | assert !c1.applies(new Environment().withPrimitive(w1))
|
|---|
| 166 | w1.put("bar", "123")
|
|---|
| 167 | assert c1.applies(new Environment().withPrimitive(w1))
|
|---|
| 168 | def c2 = (Condition.KeyValueCondition) getParser("[foo =~ */bar/]").condition(Condition.Context.PRIMITIVE)
|
|---|
| 169 | def w2 = new Way(w1)
|
|---|
| 170 | w2.put("bar", "[0-9]{3}")
|
|---|
| 171 | assert c2.applies(new Environment().withPrimitive(w2))
|
|---|
| 172 | w2.put("bar", "[0-9]")
|
|---|
| 173 | assert c2.applies(new Environment().withPrimitive(w2))
|
|---|
| 174 | w2.put("bar", "^[0-9]\$")
|
|---|
| 175 | assert !c2.applies(new Environment().withPrimitive(w2))
|
|---|
| 176 | }
|
|---|
| 177 | }
|
|---|