Changeset 6512 in josm for trunk/test/unit/org/openstreetmap


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

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.