Changeset 7167 in josm for trunk


Ignore:
Timestamp:
2014-05-22T17:13:04+02:00 (7 years ago)
Author:
simon04
Message:

MapCSS: simplify ~= implementation, add unit tests

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/Condition.java

    r7099 r7167  
    33
    44import java.text.MessageFormat;
     5import java.util.Arrays;
    56import java.util.EnumSet;
    67import java.util.Objects;
     
    9596                return REGEX.equals(this) ? contains : !contains;
    9697            case ONE_OF:
    97                 String[] parts = testString.split(";");
    98                 for (String part : parts) {
    99                     if (Objects.equals(prototypeString, part.trim()))
    100                         return true;
    101                 }
    102                 return false;
     98                return Arrays.asList(testString.split("\\s*;\\s*")).contains(prototypeString);
    10399            case BEGINS_WITH:
    104100                return testString.startsWith(prototypeString);
  • trunk/test/unit/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSParserTest.groovy

    r7166 r7167  
    122122
    123123    @Test
     124    public void testBeginsEndsWithCondition() throws Exception {
     125        def condition = (Condition.KeyValueCondition) getParser('[foo ^= bar]').condition(Condition.Context.PRIMITIVE)
     126        assert Condition.Op.BEGINS_WITH.equals(condition.op)
     127        assert condition.applies(getEnvironment("foo", "bar123"))
     128        assert !condition.applies(getEnvironment("foo", "123bar"))
     129        assert !condition.applies(getEnvironment("foo", "123bar123"))
     130        condition = (Condition.KeyValueCondition) getParser('[foo $= bar]').condition(Condition.Context.PRIMITIVE)
     131        assert Condition.Op.ENDS_WITH.equals(condition.op)
     132        assert !condition.applies(getEnvironment("foo", "bar123"))
     133        assert condition.applies(getEnvironment("foo", "123bar"))
     134        assert !condition.applies(getEnvironment("foo", "123bar123"))
     135    }
     136
     137    @Test
     138    public void testOneOfCondition() throws Exception {
     139        def condition = getParser('[vending~=stamps]').condition(Condition.Context.PRIMITIVE)
     140        assert condition.applies(getEnvironment("vending", "stamps"))
     141        assert condition.applies(getEnvironment("vending", "bar;stamps;foo"))
     142        assert !condition.applies(getEnvironment("vending", "every;thing;else"))
     143        assert !condition.applies(getEnvironment("vending", "or_nothing"))
     144    }
     145
     146    @Test
    124147    public void testStandardKeyCondition() throws Exception {
    125148        def c1 = (Condition.KeyCondition) getParser("[ highway ]").condition(Condition.Context.PRIMITIVE)
Note: See TracChangeset for help on using the changeset viewer.