Changeset 6376 in josm


Ignore:
Timestamp:
2013-11-07T21:28:12+01:00 (6 years ago)
Author:
simon04
Message:

see #9157 - check collection_times and service_times as well

And fix/add unit tests

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/validation/tests/OpeningHourTest.java

    r6373 r6376  
    5454        if (ENGINE != null) {
    5555            ENGINE.eval(new InputStreamReader(new MirroredInputStream("resource://data/opening_hours.js"), "UTF-8"));
    56             ENGINE.eval("var oh = function (x, y) {return new opening_hours(x, y);};");
     56            // fake country/state to not get errors on holidays
     57            ENGINE.eval("var nominatiomJSON = {address: {state: 'Bayern', country_code: 'de'}};");
     58            ENGINE.eval("var oh = function (value, mode) {return new opening_hours(value, nominatiomJSON, mode);};");
    5759        } else {
    5860            Main.warn("Unable to initialize OpeningHourTest because no JavaScript engine has been found");
     
    6062    }
    6163
    62     protected Object parse(String value) throws ScriptException, NoSuchMethodException {
    63         return ((Invocable) ENGINE).invokeFunction("oh", value);
     64    static enum CheckMode {
     65        TIME_RANGE(0), POINTS_IN_TIME(1), BOTH(2);
     66        final int code;
     67
     68        CheckMode(int code) {
     69            this.code = code;
     70        }
     71    }
     72
     73    protected Object parse(String value, CheckMode mode) throws ScriptException, NoSuchMethodException {
     74        return ((Invocable) ENGINE).invokeFunction("oh", value, mode.code);
    6475    }
    6576
     
    104115     * @return a list of {@link TestError} or an empty list
    105116     */
    106     public List<TestError> checkOpeningHourSyntax(final String value) {
     117    public List<TestError> checkOpeningHourSyntax(final String value, CheckMode mode) {
    107118        if (ENGINE == null || value == null || value.trim().isEmpty()) {
    108119            return Collections.emptyList();
    109120        }
    110121        try {
    111             final Object r = parse(value);
     122            final Object r = parse(value, mode);
    112123            final List<TestError> errors = new ArrayList<TestError>();
    113124            for (final Object i : getList(((Invocable) ENGINE).invokeMethod(r, "getWarnings"))) {
     
    117128        } catch (ScriptException ex) {
    118129            final String message = ex.getMessage()
    119                     .replace("sun.org.mozilla.javascript.internal.JavaScriptException: ", "opening_hours - ")
     130                    .replaceAll("[^:]*Exception: ", "opening_hours - ")
    120131                    .replaceAll("\\(<Unknown source.*", "")
    121132                    .trim();
     
    126137    }
    127138
    128     protected void check(final OsmPrimitive p, final String tagValue) {
    129         for (TestError e : checkOpeningHourSyntax(tagValue)) {
     139    public List<TestError> checkOpeningHourSyntax(final String value) {
     140        return checkOpeningHourSyntax(value, CheckMode.TIME_RANGE);
     141    }
     142
     143    protected void check(final OsmPrimitive p, final String tagValue, CheckMode mode) {
     144        for (TestError e : checkOpeningHourSyntax(tagValue, mode)) {
    130145            e.setPrimitives(Collections.singletonList(p));
    131146            errors.add(e);
     
    134149
    135150    protected void check(final OsmPrimitive p) {
    136         check(p, p.get("opening_hours"));
    137         // unsupported, cf. https://github.com/AMDmi3/opening_hours.js/issues/12
    138         //check(p, p.get("collection_times"));
    139         //check(p, p.get("service_times"));
     151        check(p, p.get("opening_hours"), CheckMode.TIME_RANGE);
     152        check(p, p.get("collection_times"), CheckMode.BOTH);
     153        check(p, p.get("service_times"), CheckMode.BOTH);
    140154    }
    141155
  • trunk/test/unit/org/openstreetmap/josm/data/validation/tests/OpeningHourTestTest.java

    r6370 r6376  
    6161        assertThat(OPENING_HOUR_TEST.checkOpeningHourSyntax("badtext").size(), is(1));
    6262        assertThat(OPENING_HOUR_TEST.checkOpeningHourSyntax("badtext").get(0).getMessage(),
    63                 is("ba <--- (Unexpected token: \"b\" This means that the syntax is not valid at that point or it is currently not supported.)"));
     63                is("opening_hours - ba <--- (Unexpected token: \"b\" This means that the syntax is not valid at that point or it is currently not supported.)"));
     64    }
     65
     66    @Test
     67    public void testCheckOpeningHourSyntax6() throws Exception {
     68        assertThat(OPENING_HOUR_TEST.checkOpeningHourSyntax("PH open \"always open on public holidays\"").isEmpty(), is(true));
     69    }
     70
     71    @Test
     72    public void testCheckServiceTimeSyntax1() throws Exception {
     73        // frequently used tags according to http://taginfo.openstreetmap.org/keys/service_times#values
     74        assertThat(OPENING_HOUR_TEST.checkOpeningHourSyntax("Su 10:00", OpeningHourTest.CheckMode.BOTH).isEmpty(), is(true));
     75        assertThat(OPENING_HOUR_TEST.checkOpeningHourSyntax("automatic", OpeningHourTest.CheckMode.BOTH).isEmpty(), is(false));
     76        assertThat(OPENING_HOUR_TEST.checkOpeningHourSyntax("Mo-Sa 09:00-18:00", OpeningHourTest.CheckMode.BOTH).isEmpty(), is(true));
     77        assertThat(OPENING_HOUR_TEST.checkOpeningHourSyntax("Su 09:30; We 19:30", OpeningHourTest.CheckMode.BOTH).isEmpty(), is(true));
     78        assertThat(OPENING_HOUR_TEST.checkOpeningHourSyntax("Mo-Fr 0:00-0:30,4:00-00:30; Sa,Su,PH 0:00-24:00", OpeningHourTest.CheckMode.BOTH).isEmpty(), is(true));
     79    }
     80
     81    @Test
     82    public void testCheckCollectionTimeSyntax1() throws Exception {
     83        // frequently used tags according to http://taginfo.openstreetmap.org/keys/collection_times#values
     84        assertThat(OPENING_HOUR_TEST.checkOpeningHourSyntax("Mo-Sa 09:00", OpeningHourTest.CheckMode.BOTH).isEmpty(), is(true));
     85        assertThat(OPENING_HOUR_TEST.checkOpeningHourSyntax("fixme", OpeningHourTest.CheckMode.BOTH).isEmpty(), is(false));
     86        assertThat(OPENING_HOUR_TEST.checkOpeningHourSyntax("daily", OpeningHourTest.CheckMode.BOTH).isEmpty(), is(false));
     87        assertThat(OPENING_HOUR_TEST.checkOpeningHourSyntax("Mo-Fr 13:30, 17:45, 19:00; Sa 15:00; Su 11:00", OpeningHourTest.CheckMode.BOTH).isEmpty(), is(true));
    6488    }
    6589}
Note: See TracChangeset for help on using the changeset viewer.