Ticket #8687: 8687_test.patch

File 8687_test.patch, 5.5 KB (added by simon04, 12 years ago)
  • data/tagchecker.cfg

    diff --git a/data/tagchecker.cfg b/data/tagchecker.cfg
    index dcb3fe9..397a617 100644
    a b  
    3535# The comment at the end of a rule is displayed in validator description
    3636#
    3737# Empty lines and space signs are ignored
     38#
     39# Lines starting with #TEST# add unit tests (cf. TagCheckerTest)
     40# Example: "#TEST# w+layer=1 # w-layer=10"
     41# Syntax:  "#TEST# [nwr][+-][key]=[value] # ..."
     42#                  [nwr] whether to test a node/way/relation
     43                        [+-] whether test is expected to succeed/fail
    3844
    3945way  : W : highway == * && name == /.* (Ave|Blvd|Cct|Cir|Cl|Cr|Crct|Cres|Crt|Ct|Dr|Drv|Esp|Espl|Hwy|Ln|Mw|Mwy|Pl|Rd|Qy|Qys|Sq|St|Str|Ter|Tce|Tr|Wy)\.?$/i               # abbreviated street name
    4046
    way : W : junction == * && highway != *  
    191197
    192198# measurement values and units warnings (ticket #8687)
    193199way : W : layer == * && layer != /^0$|^-?[1-5]$/                                                                         # layer should be between -5 and 5
     200#TEST# w+layer=1 # w+layer=5 # w+layer=-5 # w-layer=10
    194201*   : W : level == * && level != /^((([0-9]|-[1-9])|[1-9][0-9]*)(\.5)?)(;(([0-9]|-[1-9])|[1-9][0-9]*)(\.5)?)*$|^-0\.5$/  # level should be numbers with optional .5 increments
    195202*   : W : height == * && height != /^(([0-9]+\.?[0-9]*( m)?)|([1-9][0-9]*\'((10|11|[0-9])((\.[0-9]+)?)\")?))$/           # height: meters is default; period is separator; if units, put space then unit
    196203
    197204*   : W : maxheight == * && maxheight != /^(([1-9][0-9]*(\.[0-9]+)?( m)?)|([0-9]+\'([0-9]|10|11)(\.[0-9]*)?\"))$/        # maxheight: meters is default; period is separator; if units, put space then unit
    198205way : W : width == * && width != /^(([0-9]+\.?[0-9]*( [a-z]+)?)|([0-9]+\'[0-9]+\.?[0-9]*\"))$/                           # width: meters is default; period is separator; if units, put space then unit
    199206*   : W : maxwidth == * && maxwidth != /^(([0-9]+\.?[0-9]*( m)?)|([0-9]+\'[0-9]+\.?[0-9]*\"))$/                          # maxwidth: meters is default; period is separator; if units, put space then unit
     207#TEST# w+maxwidth=2 # w+maxwidth=2 m # w-maxwidth=2m # w+maxwidth=2.5 # w-maxwidth=2,5
    200208way : W : maxspeed == * && maxspeed != /^(signals|none|unposted|unknown|variable|walk|[1-9][0-9]*( [a-z]+)?|[A-Z][A-Z]:(urban|rural|living_street|motorway))$/  # unusual maxspeed format
    201209way : W : voltage == * && voltage == /(.*[A-Za-z].*)|.*,.*|.*( ).*/                                                      # voltage should be in volts with no units/delimiter/spaces
    202210# some users are using frequency for other purposes (not electromagnetic) with the values 'perennial' and 'intermittent'; the vast majority are 0, 16.7, 50 and 60
  • new file test/unit/org/openstreetmap/josm/data/validation/tests/TagCheckerTest.java

    diff --git a/test/unit/org/openstreetmap/josm/data/validation/tests/TagCheckerTest.java b/test/unit/org/openstreetmap/josm/data/validation/tests/TagCheckerTest.java
    new file mode 100644
    index 0000000..a14a32a
    - +  
     1// License: GPL. For details, see LICENSE file.
     2package org.openstreetmap.josm.data.validation.tests;
     3
     4import org.hamcrest.core.Is;
     5import org.junit.Assert;
     6import org.junit.Before;
     7import org.junit.Test;
     8import org.openstreetmap.josm.Main;
     9import org.openstreetmap.josm.data.Preferences;
     10import org.openstreetmap.josm.data.osm.Node;
     11import org.openstreetmap.josm.data.osm.OsmPrimitive;
     12import org.openstreetmap.josm.data.osm.Relation;
     13import org.openstreetmap.josm.data.osm.Way;
     14
     15import java.io.BufferedReader;
     16import java.io.IOException;
     17import java.io.InputStreamReader;
     18import java.util.Collections;
     19
     20public class TagCheckerTest {
     21
     22    @Before
     23    public void setUp() throws Exception {
     24        Main.pref = new Preferences();
     25        new TagChecker().initialize();
     26    }
     27
     28    @Test
     29    public void testTests() throws IOException {
     30        final BufferedReader reader = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream(
     31                TagChecker.DATA_FILE.substring("resource:/".length())), "UTF-8"));
     32        String line;
     33        while ((line = reader.readLine()) != null) {
     34            if (line.startsWith("#TEST#")) {
     35                for (final String test : line.substring("#TEST#".length()).split("\\s*#\\s*")) {
     36                    final String[] t = test.trim().split("=", 2);
     37                    if (t.length != 2) continue;
     38                    final String key = t[0].substring(2);
     39                    final String value = t[1];
     40                    final boolean expected = t[0].charAt(1) == '+';
     41                    final OsmPrimitive p = t[0].charAt(0) == 'n'
     42                            ? new Node()
     43                            :t[0].charAt(0) == 'w'
     44                            ? new Way()
     45                            : t[0].charAt(0) == 'r'
     46                            ? new Relation()
     47                            : null;
     48                    final boolean actual = matches(p, key, value);
     49                    System.out.println(test.trim() + " yields " + actual + ", expecting " + actual);
     50                    Assert.assertThat(test.trim(), actual, Is.is(expected));
     51                }
     52            }
     53        }
     54    }
     55
     56    protected boolean matches(final OsmPrimitive p, final String key, final String value) {
     57        boolean matchAll = true;
     58        for (final TagChecker.CheckerData check : TagChecker.checkerData) {
     59            if (check.match(p, Collections.singletonMap(key, value))) {
     60                matchAll = false;
     61                break;
     62            }
     63        }
     64        return matchAll;
     65    }
     66}