Changeset 6512 in josm


Ignore:
Timestamp:
2013-12-22T22:43:24+01:00 (6 years ago)
Author:
simon04
Message:

see #9414 - MapCSSTagChecker: parse and unit test match assertions (assertMatch, assertNoMatch)

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/data/validator/deprecated.mapcss

    r6506 r6512  
    44  fixAdd: "barrier=fence";
    55  fixAdd: "fence_type=chain_link";
     6  assertMatch: "way barrier=wire_fence";
     7  assertNoMatch: "way barrier=fence";
    68}
    79 
  • trunk/src/org/openstreetmap/josm/data/osm/Tag.java

    r6506 r6512  
    105105            return new Tag(x[0], x[1]);
    106106        } else {
    107             throw new IllegalArgumentException("String does not contain '='");
     107            throw new IllegalArgumentException("'" + s + "' does not contain '='");
    108108        }
    109109    }
  • trunk/src/org/openstreetmap/josm/data/validation/tests/MapCSSTagChecker.java

    r6511 r6512  
    5353        protected final List<Tag> alternatives = new ArrayList<Tag>();
    5454        protected final Map<String, Severity> errors = new HashMap<String, Severity>();
     55        protected final Map<String, Boolean> assertions = new HashMap<String, Boolean>();
    5556
    5657        TagCheck(List<Selector> selector) {
     
    8283                    } else if ("suggestAlternative".equals(ai.key) && val != null) {
    8384                        check.alternatives.add(val.contains("=") ? Tag.ofString(val) : new Tag(val));
     85                    } else if ("assertMatch".equals(ai.key) && val != null) {
     86                        check.assertions.put(val, true);
     87                    } else if ("assertNoMatch".equals(ai.key) && val != null) {
     88                        check.assertions.put(val, false);
    8489                    } else {
    8590                        throw new RuntimeException("Cannot add instruction " + ai.key + ": " + ai.val + "!");
  • trunk/test/unit/org/openstreetmap/josm/data/validation/tests/MapCSSTagCheckerTest.java

    r6506 r6512  
    66import org.openstreetmap.josm.data.Preferences;
    77import org.openstreetmap.josm.data.osm.Node;
     8import org.openstreetmap.josm.data.osm.OsmPrimitive;
     9import org.openstreetmap.josm.data.osm.Relation;
    810import org.openstreetmap.josm.data.osm.Tag;
     11import org.openstreetmap.josm.data.osm.Way;
    912
    1013import java.io.StringReader;
     14import java.util.LinkedHashSet;
    1115import java.util.List;
     16import java.util.Map;
    1217
    1318import static org.hamcrest.CoreMatchers.is;
     
    4954    }
    5055
     56    OsmPrimitive createPrimitiveForAssertion(String assertion) {
     57        final String[] x = assertion.split("\\s+");
     58        final OsmPrimitive p = "n".equals(x[0]) || "node".equals(x[0])
     59                ? new Node()
     60                : "w".equals(x[0]) || "way".equals(x[0])
     61                ? new Way()
     62                : "r".equals(x[0]) || "relation".equals(x[0])
     63                ? new Relation()
     64                : null;
     65        if (p == null) {
     66            throw new IllegalArgumentException("Expecting n/node/w/way/r/relation, but got " + x[0]);
     67        }
     68        for (int i = 1; i < x.length; i++) {
     69            final Tag tag = Tag.ofString(x[i]);
     70            p.put(tag.getKey(), tag.getValue());
     71        }
     72        return p;
     73    }
     74
     75    @Test
     76    public void testCreatePrimitiveForAssertion() throws Exception {
     77        final OsmPrimitive p = createPrimitiveForAssertion("way name=Foo railway=rail");
     78        assertTrue(p instanceof Way);
     79        assertThat(p.keySet().size(), is(2));
     80        assertThat(p.get("name"), is("Foo"));
     81        assertThat(p.get("railway"), is("rail"));
     82    }
     83
     84    @Test(expected = IllegalArgumentException.class)
     85    public void testCreatePrimitiveForAssertionFail() throws Exception {
     86        final OsmPrimitive p = createPrimitiveForAssertion("noway name=Foo");
     87    }
     88
    5189    @Test
    5290    public void testInit() throws Exception {
    5391        final MapCSSTagChecker c = new MapCSSTagChecker();
    5492        c.initialize();
     93
     94        LinkedHashSet<String> assertionErrors = new LinkedHashSet<String>();
     95        for (final MapCSSTagChecker.TagCheck check : c.checks) {
     96            for (final Map.Entry<String, Boolean> i : check.assertions.entrySet()) {
     97                if (check.matchesPrimitive(createPrimitiveForAssertion(i.getKey())) != i.getValue()) {
     98                    final String error = "Expecting test '" + check.getMessage() + "' to " + (i.getValue() ? "" : "not ") + "match " + i.getKey();
     99                    System.err.println(error);
     100                    assertionErrors.add(error);
     101                }
     102            }
     103        }
     104        assertTrue("not all assertions included in the tests are met", assertionErrors.isEmpty());
     105
    55106    }
    56107}
Note: See TracChangeset for help on using the changeset viewer.