Changeset 6376 in josm
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/validation/tests/OpeningHourTest.java
r6373 r6376 54 54 if (ENGINE != null) { 55 55 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);};"); 57 59 } else { 58 60 Main.warn("Unable to initialize OpeningHourTest because no JavaScript engine has been found"); … … 60 62 } 61 63 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); 64 75 } 65 76 … … 104 115 * @return a list of {@link TestError} or an empty list 105 116 */ 106 public List<TestError> checkOpeningHourSyntax(final String value ) {117 public List<TestError> checkOpeningHourSyntax(final String value, CheckMode mode) { 107 118 if (ENGINE == null || value == null || value.trim().isEmpty()) { 108 119 return Collections.emptyList(); 109 120 } 110 121 try { 111 final Object r = parse(value );122 final Object r = parse(value, mode); 112 123 final List<TestError> errors = new ArrayList<TestError>(); 113 124 for (final Object i : getList(((Invocable) ENGINE).invokeMethod(r, "getWarnings"))) { … … 117 128 } catch (ScriptException ex) { 118 129 final String message = ex.getMessage() 119 .replace ("sun.org.mozilla.javascript.internal.JavaScriptException: ", "opening_hours - ")130 .replaceAll("[^:]*Exception: ", "opening_hours - ") 120 131 .replaceAll("\\(<Unknown source.*", "") 121 132 .trim(); … … 126 137 } 127 138 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)) { 130 145 e.setPrimitives(Collections.singletonList(p)); 131 146 errors.add(e); … … 134 149 135 150 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); 140 154 } 141 155 -
trunk/test/unit/org/openstreetmap/josm/data/validation/tests/OpeningHourTestTest.java
r6370 r6376 61 61 assertThat(OPENING_HOUR_TEST.checkOpeningHourSyntax("badtext").size(), is(1)); 62 62 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)); 64 88 } 65 89 }
Note:
See TracChangeset
for help on using the changeset viewer.