Changeset 13145 in josm


Ignore:
Timestamp:
2017-11-22T21:37:46+01:00 (6 years ago)
Author:
Don-vip
Message:

see #15582 - simplify our usage of opening_hours.js library (drop mode: it is deduced from tag key) + make the parse method public so it can be called from opening_hours plugin

Location:
trunk
Files:
3 edited

Legend:

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

    r13017 r13145  
    199199                    if (condition.matches(".*[0-9]:[0-9]{2}.*")) {
    200200                        final List<OpeningHourTest.OpeningHoursTestError> errors = openingHourTest.checkOpeningHourSyntax(
    201                                 "", condition, OpeningHourTest.CheckMode.TIME_RANGE, true, LanguageInfo.getJOSMLocaleCode());
     201                                "", condition, true, LanguageInfo.getJOSMLocaleCode());
    202202                        if (!errors.isEmpty()) {
    203203                            return errors.get(0).getMessage();
  • trunk/src/org/openstreetmap/josm/data/validation/tests/OpeningHourTest.java

    r12620 r13145  
    5757                ENGINE.eval("var nominatimJSON = {address: {state: 'Bayern', country_code: 'de'}};");
    5858                ENGINE.eval(
    59                         "var oh = function (value, mode, locale) {" +
     59                        "var oh = function (value, tag_key, locale) {" +
    6060                        " try {" +
    61                         "    var r = new opening_hours(value, nominatimJSON, {mode: mode, locale: locale});" +
     61                        "    var r = new opening_hours(value, nominatimJSON, {tag_key: tag_key, locale: locale});" +
    6262                        "    r.getErrors = function() {return [];};" +
    6363                        "    return r;" +
     
    7676    }
    7777
    78     enum CheckMode {
    79         TIME_RANGE(0), POINTS_IN_TIME(1), BOTH(2);
    80         private final int code;
    81 
    82         CheckMode(int code) {
    83             this.code = code;
    84         }
    85     }
    86 
    87     protected Object parse(String value, CheckMode mode, String locale) throws ScriptException, NoSuchMethodException {
    88         return ((Invocable) ENGINE).invokeFunction("oh", value, mode.code, locale);
     78    /**
     79     * Parses the opening hour syntax of the {@code value} given according to
     80     * <a href="https://github.com/ypid/opening_hours.js">opening_hours.js</a> and returns an object on which
     81     * methods can be called to extract information.
     82     * @param value the opening hour value to be checked
     83     * @param tagKey the OSM key (should be "opening_hours", "collection_times" or "service_times")
     84     * @param locale the locale code used for localizing messages
     85     * @return The value returned by the underlying method. Usually a {@code jdk.nashorn.api.scripting.ScriptObjectMirror}
     86     * @throws ScriptException if an error occurs during invocation of the underlying method
     87     * @throws NoSuchMethodException if underlying method with given name or matching argument types cannot be found
     88     * @since 13145
     89     */
     90    public Object parse(String value, String tagKey, String locale) throws ScriptException, NoSuchMethodException {
     91        return ((Invocable) ENGINE).invokeFunction("oh", value, tagKey, locale);
    8992    }
    9093
     
    177180     * @param key the OSM key (should be "opening_hours", "collection_times" or "service_times"). Used in error message
    178181     * @param value the opening hour value to be checked.
    179      * @param mode whether to validate {@code value} as a time range, or points in time, or both.
    180182     * @return a list of {@link TestError} or an empty list
    181183     */
    182     public List<OpeningHoursTestError> checkOpeningHourSyntax(final String key, final String value, CheckMode mode) {
    183         return checkOpeningHourSyntax(key, value, mode, false, LanguageInfo.getJOSMLocaleCode());
     184    public List<OpeningHoursTestError> checkOpeningHourSyntax(final String key, final String value) {
     185        return checkOpeningHourSyntax(key, value, false, LanguageInfo.getJOSMLocaleCode());
    184186    }
    185187
     
    190192     * @param key the OSM key (should be "opening_hours", "collection_times" or "service_times").
    191193     * @param value the opening hour value to be checked.
    192      * @param mode whether to validate {@code value} as a time range, or points in time, or both.
    193194     * @param ignoreOtherSeverity whether to ignore errors with {@link Severity#OTHER}.
    194195     * @param locale the locale code used for localizing messages
    195196     * @return a list of {@link TestError} or an empty list
    196197     */
    197     public List<OpeningHoursTestError> checkOpeningHourSyntax(final String key, final String value, CheckMode mode,
     198    public List<OpeningHoursTestError> checkOpeningHourSyntax(final String key, final String value,
    198199            boolean ignoreOtherSeverity, String locale) {
    199200        if (ENGINE == null || value == null || value.isEmpty()) {
     
    202203        final List<OpeningHoursTestError> errors = new ArrayList<>();
    203204        try {
    204             final Object r = parse(value, mode, locale);
     205            final Object r = parse(value, key, locale);
    205206            String prettifiedValue = null;
    206207            try {
     
    243244    }
    244245
    245     /**
    246      * Checks for a correct usage of the opening hour syntax of the {@code value} given, in time range mode, according to
    247      * <a href="https://github.com/ypid/opening_hours.js">opening_hours.js</a> and returns a list containing
    248      * validation errors or an empty list. Null values result in an empty list.
    249      * @param key the OSM key (should be "opening_hours", "collection_times" or "service_times"). Used in error message
    250      * @param value the opening hour value to be checked.
    251      * @return a list of {@link TestError} or an empty list
    252      */
    253     public List<OpeningHoursTestError> checkOpeningHourSyntax(final String key, final String value) {
    254         return checkOpeningHourSyntax(key, value, "opening_hours".equals(key) ? CheckMode.TIME_RANGE : CheckMode.BOTH);
    255     }
    256 
    257     protected void check(final OsmPrimitive p, final String key, CheckMode mode) {
    258         for (OpeningHoursTestError e : checkOpeningHourSyntax(key, p.get(key), mode)) {
     246    protected void check(final OsmPrimitive p, final String key) {
     247        for (OpeningHoursTestError e : checkOpeningHourSyntax(key, p.get(key))) {
    259248            errors.add(e.getTestError(p, key));
    260249        }
     
    263252    @Override
    264253    public void check(final OsmPrimitive p) {
    265         check(p, "opening_hours", CheckMode.TIME_RANGE);
    266         check(p, "collection_times", CheckMode.BOTH);
    267         check(p, "service_times", CheckMode.BOTH);
     254        check(p, "opening_hours");
     255        check(p, "collection_times");
     256        check(p, "service_times");
    268257    }
    269258}
  • trunk/test/unit/org/openstreetmap/josm/data/validation/tests/OpeningHourTestTest.java

    r11890 r13145  
    7777    @Test
    7878    public void testI18n() {
    79         assertTrue(openingHourTest.checkOpeningHourSyntax("opening_hours", ".", OpeningHourTest.CheckMode.POINTS_IN_TIME, false, "de")
     79        assertTrue(openingHourTest.checkOpeningHourSyntax("opening_hours", ".", false, "de")
    8080                .get(0).toString().contains("Unerwartetes Zeichen"));
    81         assertFalse(openingHourTest.checkOpeningHourSyntax("opening_hours", ".", OpeningHourTest.CheckMode.POINTS_IN_TIME, false, "en")
     81        assertFalse(openingHourTest.checkOpeningHourSyntax("opening_hours", ".", false, "en")
    8282                .get(0).toString().contains("Unerwartetes Zeichen"));
    8383    }
     
    121121        assertThat(openingHourTest.checkOpeningHourSyntax(null, null), isEmpty());
    122122        assertThat(openingHourTest.checkOpeningHourSyntax(null, ""), isEmpty());
    123         assertEquals("null - The value contains nothing meaningful which can be parsed.",
     123        assertEquals("opening_hours - The value contains nothing meaningful which can be parsed.",
     124                openingHourTest.checkOpeningHourSyntax("opening_hours", " ").get(0).getMessage());
     125        assertEquals("null - The optional_conf_parm[\"tag_key\"] parameter is of unknown type. Given object, expected string.",
    124126                openingHourTest.checkOpeningHourSyntax(null, " ").get(0).getMessage());
    125127    }
     
    180182        final String key = "service_times";
    181183        // frequently used tags according to https://taginfo.openstreetmap.org/keys/service_times#values
    182         assertThat(openingHourTest.checkOpeningHourSyntax(key, "Su 10:00", OpeningHourTest.CheckMode.BOTH), isEmpty());
    183         assertThat(openingHourTest.checkOpeningHourSyntax(key, "automatic", OpeningHourTest.CheckMode.BOTH), not(isEmpty()));
    184         assertThat(openingHourTest.checkOpeningHourSyntax(key, "Mo-Sa 09:00-18:00", OpeningHourTest.CheckMode.BOTH), isEmpty());
    185         assertThat(openingHourTest.checkOpeningHourSyntax(key, "Su 09:30; We 19:30", OpeningHourTest.CheckMode.BOTH), isEmpty());
    186         assertThat(openingHourTest.checkOpeningHourSyntax(key, "Mo-Fr 00:00-00:30,04:00-00:30; Sa,Su,PH 00:00-24:00",
    187                 OpeningHourTest.CheckMode.BOTH), isEmpty());
    188         assertThat(openingHourTest.checkOpeningHourSyntax(key, "Mo-Fr 0:00-0:30,4:00-00:30; Sa,Su,PH 0:00-24:00",
    189                 OpeningHourTest.CheckMode.BOTH), hasSize(1));
     184        assertThat(openingHourTest.checkOpeningHourSyntax(key, "Su 10:00"), isEmpty());
     185        assertThat(openingHourTest.checkOpeningHourSyntax(key, "automatic"), not(isEmpty()));
     186        assertThat(openingHourTest.checkOpeningHourSyntax(key, "Mo-Sa 09:00-18:00"), isEmpty());
     187        assertThat(openingHourTest.checkOpeningHourSyntax(key, "Su 09:30; We 19:30"), isEmpty());
     188        assertThat(openingHourTest.checkOpeningHourSyntax(key, "Mo-Fr 00:00-00:30,04:00-00:30; Sa,Su,PH 00:00-24:00"), isEmpty());
     189        assertThat(openingHourTest.checkOpeningHourSyntax(key, "Mo-Fr 0:00-0:30,4:00-00:30; Sa,Su,PH 0:00-24:00"), hasSize(1));
    190190        assertEquals("Mo-Fr 00:00-00:30,04:00-00:30; Sa,Su,PH 00:00-24:00",
    191                 openingHourTest.checkOpeningHourSyntax(key, "Mo-Fr 0:00-0:30,4:00-00:30; Sa,Su,PH 0:00-24:00",
    192                 OpeningHourTest.CheckMode.BOTH).get(0).getPrettifiedValue());
     191                openingHourTest.checkOpeningHourSyntax(key, "Mo-Fr 0:00-0:30,4:00-00:30; Sa,Su,PH 0:00-24:00").get(0).getPrettifiedValue());
    193192        assertEquals("Mo-Fr 00:00-00:30,04:00-00:30; Sa,Su,PH 00:00-24:00",
    194                 openingHourTest.checkOpeningHourSyntax(key, "Mo-Fr 0:00-0:30,4:00-00:30; Sa,Su,PH 0:00-24:00",
    195                 OpeningHourTest.CheckMode.BOTH).get(0).getPrettifiedValue());
     193                openingHourTest.checkOpeningHourSyntax(key, "Mo-Fr 0:00-0:30,4:00-00:30; Sa,Su,PH 0:00-24:00").get(0).getPrettifiedValue());
    196194    }
    197195
     
    203201        final String key = "collection_times";
    204202        // frequently used tags according to https://taginfo.openstreetmap.org/keys/collection_times#values
    205         assertThat(openingHourTest.checkOpeningHourSyntax(key, "Mo-Sa 09:00", OpeningHourTest.CheckMode.BOTH), isEmpty());
    206         assertThat(openingHourTest.checkOpeningHourSyntax(key, "fixme", OpeningHourTest.CheckMode.BOTH), not(isEmpty()));
    207         assertThat(openingHourTest.checkOpeningHourSyntax(key, "daily", OpeningHourTest.CheckMode.BOTH), not(isEmpty()));
    208         assertThat(openingHourTest.checkOpeningHourSyntax(key, "Mo-Fr 13:30,17:45,19:00; Sa 15:00; Su 11:00",
    209                 OpeningHourTest.CheckMode.BOTH), isEmpty());
    210         assertThat(openingHourTest.checkOpeningHourSyntax(key, "Mo-Fr 13:30, 17:45, 19:00; Sa 15:00; Su 11:00",
    211                 OpeningHourTest.CheckMode.BOTH), hasSize(1));
     203        assertThat(openingHourTest.checkOpeningHourSyntax(key, "Mo-Sa 09:00"), isEmpty());
     204        assertThat(openingHourTest.checkOpeningHourSyntax(key, "fixme"), not(isEmpty()));
     205        assertThat(openingHourTest.checkOpeningHourSyntax(key, "daily"), not(isEmpty()));
     206        assertThat(openingHourTest.checkOpeningHourSyntax(key, "Mo-Fr 13:30,17:45,19:00; Sa 15:00; Su 11:00"), isEmpty());
     207        assertThat(openingHourTest.checkOpeningHourSyntax(key, "Mo-Fr 13:30, 17:45, 19:00; Sa 15:00; Su 11:00"), hasSize(1));
    212208        assertEquals(Severity.OTHER,
    213                 openingHourTest.checkOpeningHourSyntax(key, "Mo-Fr 13:30, 17:45, 19:00; Sa 15:00; Su 11:00",
    214                 OpeningHourTest.CheckMode.BOTH).get(0).getSeverity());
     209                openingHourTest.checkOpeningHourSyntax(key, "Mo-Fr 13:30, 17:45, 19:00; Sa 15:00; Su 11:00").get(0).getSeverity());
    215210        assertEquals("Mo-Fr 13:30,17:45,19:00; Sa 15:00; Su 11:00",
    216                 openingHourTest.checkOpeningHourSyntax(key, "Mo-Fr 13:30, 17:45, 19:00; Sa 15:00; Su 11:00",
    217                 OpeningHourTest.CheckMode.BOTH).get(0).getPrettifiedValue());
     211                openingHourTest.checkOpeningHourSyntax(key, "Mo-Fr 13:30, 17:45, 19:00; Sa 15:00; Su 11:00").get(0).getPrettifiedValue());
    218212    }
    219213
Note: See TracChangeset for help on using the changeset viewer.