Changeset 14064 in josm


Ignore:
Timestamp:
2018-07-28T23:18:31+02:00 (6 years ago)
Author:
Don-vip
Message:

see #16498 - convert last unit tests to Java

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  
    11// License: GPL. For details, see LICENSE file.
    2 package org.openstreetmap.josm.gui.mappaint.mapcss
     2package org.openstreetmap.josm.gui.mappaint.mapcss;
    33
    4 import java.util.logging.Logger
     4import static org.junit.Assert.assertEquals;
     5import static org.junit.Assert.assertFalse;
     6import static org.junit.Assert.assertTrue;
    57
    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
     8import java.io.FileInputStream;
     9import java.util.Arrays;
    1910
    20 class ChildOrParentSelectorTest {
    21     static private Logger logger = Logger.getLogger(ChildOrParentSelectorTest.class.getName());
     11import org.junit.Before;
     12import org.junit.Ignore;
     13import org.junit.Rule;
     14import org.junit.Test;
     15import org.openstreetmap.josm.data.coor.LatLon;
     16import org.openstreetmap.josm.data.osm.DataSet;
     17import org.openstreetmap.josm.data.osm.Node;
     18import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
     19import org.openstreetmap.josm.data.osm.Relation;
     20import org.openstreetmap.josm.data.osm.RelationMember;
     21import org.openstreetmap.josm.data.osm.Way;
     22import org.openstreetmap.josm.gui.mappaint.Environment;
     23import org.openstreetmap.josm.gui.mappaint.MultiCascade;
     24import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.ChildOrParentSelector;
     25import org.openstreetmap.josm.io.OsmReader;
     26import org.openstreetmap.josm.testutils.JOSMTestRules;
    2227
    23     def DataSet ds;
     28import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
    2429
    25     @BeforeClass
    26     public static void createJOSMFixture(){
    27         JOSMFixture.createUnitTestFixture().init()
     30/**
     31 * Unit tests of {@link ChildOrParentSelector}.
     32 */
     33public 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();
    2850    }
    2951
    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;
    3356    }
    3457
    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;
    3963    }
    4064
    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;
    4669    }
    4770
    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;
    5976    }
    6077
     
    6279    @Ignore
    6380    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);
    6883
    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);
    7388
    74         assert selector.matches(e)
     89        assertTrue(selector.matches(e));
    7590    }
    7691
     
    7893    @Ignore
    7994    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);
    8497
    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);
    89102
    90         assert selector.matches(e)
     103        assertTrue(selector.matches(e));
    91104    }
    92105
     
    94107    @Ignore
    95108    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);
    100111
    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);
    105116
    106         assert !selector.matches(e)
     117        assertFalse(selector.matches(e));
    107118    }
    108119
     
    110121    @Ignore
    111122    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);
    117126
    118127    }
     128
    119129    @Test
    120130    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);
    126134
    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)));
    130138
    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)));
    133141
    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)));
    136144
    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));
    141149
    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));
    144152
    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));
    149157    }
    150158
    151159    @Test
    152160    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);
    157163
    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)));
    161167
    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)));
    164170
    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)));
    167173
    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));
    171177
    172         Environment e = new Environment(w1)
    173         assert !selector.matches(e)
     178        Environment e = new Environment(w1);
     179        assertFalse(selector.matches(e));
    174180
    175         e = new Environment(w2)
    176         assert !selector.matches(e)
     181        e = new Environment(w2);
     182        assertFalse(selector.matches(e));
    177183
    178         e = new Environment(w3)
    179         assert selector.matches(e)
     184        e = new Environment(w3);
     185        assertTrue(selector.matches(e));
    180186    }
    181187
    182188    @Test
    183189    public void testContains() throws Exception {
    184         def 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))));
    188194    }
    189195}
  • trunk/test/unit/org/openstreetmap/josm/gui/mappaint/mapcss/KeyConditionTest.java

    r14051 r14064  
    22package org.openstreetmap.josm.gui.mappaint.mapcss;
    33
    4 import static groovy.test.GroovyAssert.shouldFail
    5 import static org.junit.Assert.*
     4import static org.junit.Assert.assertFalse;
     5import static org.junit.Assert.assertTrue;
     6import static org.junit.Assert.fail;
    67
    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
     8import org.junit.Before;
     9import org.junit.Rule;
     10import org.junit.Test;
     11import org.openstreetmap.josm.data.coor.LatLon;
     12import org.openstreetmap.josm.data.osm.DataSet;
     13import org.openstreetmap.josm.data.osm.Node;
     14import org.openstreetmap.josm.data.osm.Relation;
     15import org.openstreetmap.josm.data.osm.RelationMember;
     16import org.openstreetmap.josm.gui.mappaint.Environment;
     17import org.openstreetmap.josm.gui.mappaint.mapcss.Condition.Context;
     18import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.KeyCondition;
     19import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.KeyMatchType;
     20import org.openstreetmap.josm.testutils.JOSMTestRules;
     21import org.openstreetmap.josm.tools.Logging;
    1722
    18 class KeyConditionTest {
     23import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
    1924
    20     def DataSet ds;
     25/**
     26 * Unit tests of {@link KeyCondition}.
     27 */
     28public class KeyConditionTest {
    2129
    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();
    2545    }
    2646
    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;
    3051    }
    3152
    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;
    3658    }
    3759
    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        }
    4367    }
    4468
     69    /**
     70     * Test {@link ConditionFactory#createKeyCondition}.
     71     */
    4572    @Test
    4673    public void create() {
    4774
    4875        // ["a label"]
    49         Condition c = ConditionFactory.createKeyCondition("a key", false, KeyMatchType.FALSE, Context.PRIMITIVE)
     76        ConditionFactory.createKeyCondition("a key", false, KeyMatchType.FALSE, Context.PRIMITIVE);
    5077        // ["a label"?]
    51         c = ConditionFactory.createKeyCondition("a key", false, KeyMatchType.TRUE, Context.PRIMITIVE)
     78        ConditionFactory.createKeyCondition("a key", false, KeyMatchType.TRUE, Context.PRIMITIVE);
    5279        // [!"a label"]
    53         c = ConditionFactory.createKeyCondition("a key", true, KeyMatchType.FALSE, Context.PRIMITIVE)
     80        ConditionFactory.createKeyCondition("a key", true, KeyMatchType.FALSE, Context.PRIMITIVE);
    5481        // [!"a label"?]
    55         c = ConditionFactory.createKeyCondition("a key", true, KeyMatchType.TRUE, Context.PRIMITIVE)
     82        ConditionFactory.createKeyCondition("a key", true, KeyMatchType.TRUE, Context.PRIMITIVE);
    5683
    5784        // ["a label"]
    58         c = ConditionFactory.createKeyCondition("a key", false, null, Context.LINK)
     85        ConditionFactory.createKeyCondition("a key", false, null, Context.LINK);
    5986        // [!"a label"]
    60         c = ConditionFactory.createKeyCondition("a key", true, null, Context.LINK)
     87        ConditionFactory.createKeyCondition("a key", true, null, Context.LINK);
    6188
    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        );
    6693
    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        );
    7198    }
    7299
    73100    @Test
    74101    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));
    78105
    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();
    80107
    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));
    83110
    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));
    86113    }
    87114
    88115    @Test
    89116    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));
    93120
    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();
    95122
    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));
    98125
    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));
    101128    }
    102129}
  • trunk/test/unit/org/openstreetmap/josm/gui/mappaint/mapcss/KeyValueConditionTest.java

    r14051 r14064  
    22package org.openstreetmap.josm.gui.mappaint.mapcss;
    33
    4 import static groovy.test.GroovyAssert.shouldFail
     4import static org.junit.Assert.assertFalse;
     5import static org.junit.Assert.assertTrue;
     6import static org.junit.Assert.fail;
    57
    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
     8import java.io.StringReader;
    189
    19 class KeyValueConditionTest {
     10import org.junit.Before;
     11import org.junit.Rule;
     12import org.junit.Test;
     13import org.openstreetmap.josm.data.coor.LatLon;
     14import org.openstreetmap.josm.data.osm.DataSet;
     15import org.openstreetmap.josm.data.osm.Node;
     16import org.openstreetmap.josm.data.osm.OsmUtils;
     17import org.openstreetmap.josm.data.osm.Relation;
     18import org.openstreetmap.josm.data.osm.RelationMember;
     19import org.openstreetmap.josm.gui.mappaint.Environment;
     20import org.openstreetmap.josm.gui.mappaint.mapcss.Condition.Context;
     21import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.KeyValueCondition;
     22import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.Op;
     23import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.MapCSSParser;
     24import org.openstreetmap.josm.testutils.JOSMTestRules;
     25import org.openstreetmap.josm.tools.Logging;
    2026
    21     def DataSet ds;
     27import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
    2228
    23     @BeforeClass
    24     public static void createJOSMFixture(){
    25         JOSMFixture.createUnitTestFixture().init()
     29/**
     30 * Unit tests of {@link KeyValueCondition}.
     31 */
     32public 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();
    2649    }
    2750
    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;
    3155    }
    3256
    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;
    3762    }
    3863
    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        }
    4471    }
    4572
    4673    @Test
    4774    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);
    4976
    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);
    5279
    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        );
    5683    }
    5784
    5885    @Test
    5986    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));
    6390
    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());
    6592
    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));
    6895
    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));
    7198    }
    7299
    73100    @Test
    74101    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));
    78105
    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();
    80107
    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));
    83110
    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));
    86113    }
    87114
    88115    @Test
    89116    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"))));
    101128    }
    102129
     
    104131    public void testValueFive() throws Exception {
    105132        // 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"))));
    109136    }
    110137
     
    112139    public void testValueZero() throws Exception {
    113140        // 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"))));
    117144    }
    118145}
  • 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.
     2package org.openstreetmap.josm.gui.mappaint.mapcss;
     3
     4import static org.junit.Assert.assertEquals;
     5import static org.junit.Assert.assertFalse;
     6import static org.junit.Assert.assertNull;
     7import static org.junit.Assert.assertTrue;
     8
     9import java.awt.Color;
     10import java.io.StringReader;
     11import java.net.URL;
     12import java.util.List;
     13
     14import org.junit.Rule;
     15import org.junit.Test;
     16import org.openstreetmap.josm.data.coor.LatLon;
     17import org.openstreetmap.josm.data.osm.DataSet;
     18import org.openstreetmap.josm.data.osm.Node;
     19import org.openstreetmap.josm.data.osm.OsmUtils;
     20import org.openstreetmap.josm.data.osm.Way;
     21import org.openstreetmap.josm.gui.mappaint.Environment;
     22import org.openstreetmap.josm.gui.mappaint.MultiCascade;
     23import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.ClassCondition;
     24import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.KeyCondition;
     25import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.KeyMatchType;
     26import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.KeyValueCondition;
     27import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.Op;
     28import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.PseudoClassCondition;
     29import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.SimpleKeyValueCondition;
     30import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.ChildOrParentSelector;
     31import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.MapCSSParser;
     32import org.openstreetmap.josm.testutils.JOSMTestRules;
     33import org.openstreetmap.josm.tools.ColorHelper;
     34
     35import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
     36
     37/**
     38 * Unit tests of {@link MapCSSParser}.
     39 */
     40public class MapCSSParserTest {
    2641
    2742    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));
    2944    }
    3045
     
    3348    }
    3449
    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();
    3956
    4057    @Test
    4158    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 tag
    50         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();
    5370    }
    5471
    5572    @Test
    5673    public void testClassCondition() throws Exception {
    57         def conditions = ((Selector.GeneralSelector) getParser("way[name=X].highway:closed").selector()).conds
    58         assert conditions.get(0) instanceof SimpleKeyValueCondition
    59         assert conditions.get(0).applies(getEnvironment("name", "X"))
    60         assert conditions.get(1) instanceof ClassCondition
    61         assert conditions.get(2) instanceof PseudoClassCondition
    62         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")));
    6380    }
    6481
    6582    @Test
    6683    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());
    7390    }
    7491
    7592    @Test
    7693    public void testClassMatching() throws Exception {
    77         def css = new MapCSSStyleSource("" +
     94        MapCSSStyleSource css = new MapCSSStyleSource(
    7895                "way[highway=footway] { set .path; color: #FF6644; width: 2; }\n" +
    7996                "way[highway=path]    { set path; color: brown; width: 2; }\n" +
     
    8198                "way.path             { text:auto; text-color: green; text-position: line; text-offset: 5; }\n" +
    8299                "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();
    87104        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();
    91108        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) == null
    94         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();
    95112        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));
    97114    }
    98115
    99116    @Test
    100117    public void testEqualCondition() throws Exception {
    101         def condition = (SimpleKeyValueCondition) getParser("[surface=paved]").condition(Condition.Context.PRIMITIVE)
    102         assert condition instanceof SimpleKeyValueCondition
    103         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")));
    107124    }
    108125
    109126    @Test
    110127    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")));
    115132    }
    116133
    117134    @Test
    118135    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")));
    123140    }
    124141
    125142    @Test
    126143    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))")));
    131148    }
    132149
    133150    @Test
    134151    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")));
    139156    }
    140157
    141158    @Test
    142159    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")));
    153170    }
    154171
    155172    @Test
    156173    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")));
    162179    }
    163180
    164181    @Test
    165182    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")));
    174191    }
    175192
    176193    @Test
    177194    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));
    197214    }
    198215
    199216    @Test
    200217    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")));
    207224    }
    208225
    209226    @Test
    210227    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"))));
    218235    }
    219236
    220237    @Test
    221238    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)));
    237254    }
    238255
    239256    @Test
    240257    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)));
    264281    }
    265282
    266283    @Test
    267284    public void testTicket8568() throws Exception {
    268         def sheet = new MapCSSStyleSource("" +
     285        MapCSSStyleSource sheet = new MapCSSStyleSource(
    269286                "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") == 5
    275         sheet.apply(mc, OsmUtils.createPrimitive("way keyA=true"), 20, false)
    276         assert mc.getCascade(Environment.DEFAULT_LAYER).get("width") == 15
    277         sheet.apply(mc, OsmUtils.createPrimitive("way keyB=true"), 20, false)
    278         assert mc.getCascade(Environment.DEFAULT_LAYER).get("width") == 15
    279         sheet.apply(mc, OsmUtils.createPrimitive("way keyA=true keyB=true"), 20, false)
    280         assert mc.getCascade(Environment.DEFAULT_LAYER).get("width") == 15
     287                "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"));
    281298    }
    282299
    283300    @Test
    284301    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"));
    295312
    296313        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"));
    301318    }
    302319
    303320    @Test
    304321    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") == expected
     322        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"));
    309326    }
    310327
    311328    @Test
    312329    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") == expected
     330        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"));
    317334    }
    318335
    319336    @Test
    320337    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"));
    322339    }
    323340
    324341    @Test
    325342    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());
    328346    }
    329347
    330348    @Test
    331349    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)));
    353372    }
    354373
    355374    @Test
    356375    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() == 1
    376         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));
    380399    }
    381400
    382401    @Test
    383402    public void testSiblingSelectorInterpolation() throws Exception {
    384         def s1 = (Selector.ChildOrParentSelector) getParser(
     403        ChildOrParentSelector s1 = (Selector.ChildOrParentSelector) getParser(
    385404                "*[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)));
    410429    }
    411430
    412431    @Test
    413432    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"));
    418437    }
    419438
    420439    @Test
    421440    public void testMinMaxFunctions() throws Exception {
    422         def sheet = new MapCSSStyleSource("* {" +
     441        MapCSSStyleSource sheet = new MapCSSStyleSource("* {" +
    423442                "min_value: min(tag(x), tag(y), tag(z)); " +
    424443                "max_value: max(tag(x), tag(y), tag(z)); " +
    425444                "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.0f
    432         assert mc.getCascade(Environment.DEFAULT_LAYER).get("max_value", Float.NaN, Float.class) == 8.0f
    433 
    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) == 4
    436         assert mc.getCascade(Environment.DEFAULT_LAYER).get("max_value", -777f, Float.class) == 6
    437         assert mc.getCascade(Environment.DEFAULT_LAYER).get("max_split", -777f, Float.class) == 56
     445                "}");
     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));
    438457    }
    439458
    440459    @Test
    441460    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"))));
    450469    }
    451470}
  • trunk/test/unit/org/openstreetmap/josm/gui/mappaint/mapcss/ParsingLinkSelectorTest.java

    r14051 r14064  
    22package org.openstreetmap.josm.gui.mappaint.mapcss;
    33
    4 import static org.junit.Assert.*
     4import static org.junit.Assert.assertEquals;
    55
    6 import org.junit.*
    7 import org.openstreetmap.josm.JOSMFixture;
     6import org.junit.Rule;
     7import org.junit.Test;
     8import org.openstreetmap.josm.testutils.JOSMTestRules;
    89
     10import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
    911
    10 class ParsingLinkSelectorTest {
     12/**
     13 * Unit tests of {@code ParsingLinkSelector}.
     14 */
     15public class ParsingLinkSelectorTest {
    1116
    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();
    1623
    1724    @Test
    1825    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());
    2530    }
    2631
    2732    @Test
    2833    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());
    3538    }
    36 
    3739
    3840    @Test
    3941    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());
    4646    }
    4747
    4848    @Test
    4949    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());
    5654    }
    5755}
    58 
Note: See TracChangeset for help on using the changeset viewer.