Changeset 6376 in josm for trunk/src


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

see #9157 - check collection_times and service_times as well

And fix/add unit tests

File:
1 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
Note: See TracChangeset for help on using the changeset viewer.