Changeset 15981 in josm for trunk/src/org
- Timestamp:
- 2020-03-01T23:35:51+01:00 (5 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/data/validation/tests
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/validation/tests/MapCSSTagChecker.java
r15980 r15981 22 22 import java.util.Optional; 23 23 import java.util.Set; 24 import java.util.function.Consumer; 24 25 import java.util.function.Predicate; 25 26 import java.util.regex.Matcher; … … 288 289 * Is evaluated on the matching primitive to give the error message. Map is checked to contain exactly one element. */ 289 290 protected final Map<Instruction.AssignmentInstruction, Severity> errors = new HashMap<>(); 290 /** Unit tests */291 protected final Map<String, Boolean> assertions = new HashMap<>();292 291 /** MapCSS Classes to set on matching primitives */ 293 292 protected final Set<String> setClassExpressions = new HashSet<>(); … … 303 302 private static final String POSSIBLE_THROWS = "throwError/throwWarning/throwOther"; 304 303 305 static TagCheck ofMapCSSRule(final GroupedMapCSSRule rule ) throws IllegalDataException {304 static TagCheck ofMapCSSRule(final GroupedMapCSSRule rule, AssertionConsumer assertionConsumer) throws IllegalDataException { 306 305 final TagCheck check = new TagCheck(rule); 306 final Map<String, Boolean> assertions = new HashMap<>(); 307 307 for (Instruction i : rule.declaration.instructions) { 308 308 if (i instanceof Instruction.AssignmentInstruction) { … … 346 346 check.alternatives.add(val); 347 347 } else if (val != null && "assertMatch".equals(ai.key)) { 348 check.assertions.put(val, Boolean.TRUE);348 assertions.put(val, Boolean.TRUE); 349 349 } else if (val != null && "assertNoMatch".equals(ai.key)) { 350 check.assertions.put(val, Boolean.FALSE);350 assertions.put(val, Boolean.FALSE); 351 351 } else if (val != null && "group".equals(ai.key)) { 352 352 check.group = val; … … 369 369 + rule.selectors); 370 370 } 371 if (assertionConsumer != null) { 372 MapCSSTagCheckerAsserts.checkAsserts(check, assertions, assertionConsumer); 373 } 371 374 return check; 372 375 } 373 376 374 377 static ParseResult readMapCSS(Reader css) throws ParseException { 375 return readMapCSS(css, "" );376 } 377 378 static ParseResult readMapCSS(Reader css, String url ) throws ParseException {378 return readMapCSS(css, "", null); 379 } 380 381 static ParseResult readMapCSS(Reader css, String url, AssertionConsumer assertionConsumer) throws ParseException { 379 382 CheckParameterUtil.ensureParameterNotNull(css, "css"); 380 383 … … 401 404 try { 402 405 parseChecks.add(TagCheck.ofMapCSSRule( 403 new GroupedMapCSSRule(map.getValue(), map.getKey(), url) ));406 new GroupedMapCSSRule(map.getValue(), map.getKey(), url), assertionConsumer)); 404 407 } catch (IllegalDataException e) { 405 408 Logging.error("Cannot add MapCss rule: "+e.getMessage()); … … 781 784 782 785 /** 786 * A handler for assertion error messages (for not fulfilled "assertMatch", "assertNoMatch"). 787 */ 788 @FunctionalInterface 789 interface AssertionConsumer extends Consumer<String> { 790 } 791 792 /** 783 793 * Adds a new MapCSS config file from the given URL. 784 794 * @param url The unique URL of the MapCSS config file … … 789 799 */ 790 800 public synchronized ParseResult addMapCSS(String url) throws ParseException, IOException { 801 // Check assertions, useful for development of local files 802 final boolean checkAssertions = Config.getPref().getBoolean("validator.check_assert_local_rules", false) && Utils.isLocalUrl(url); 803 return addMapCSS(url, checkAssertions ? Logging::warn : null); 804 } 805 806 synchronized ParseResult addMapCSS(String url, AssertionConsumer assertionConsumer) throws ParseException, IOException { 791 807 CheckParameterUtil.ensureParameterNotNull(url, "url"); 792 808 ParseResult result; … … 797 813 if (zip != null) 798 814 I18n.addTexts(cache.getFile()); 799 result = TagCheck.readMapCSS(reader, url );815 result = TagCheck.readMapCSS(reader, url, assertionConsumer); 800 816 checks.remove(url); 801 817 checks.putAll(url, result.parseChecks); 802 818 indexData = null; 803 // Check assertions, useful for development of local files804 if (Config.getPref().getBoolean("validator.check_assert_local_rules", false) && Utils.isLocalUrl(url)) {805 for (String msg : MapCSSTagCheckerAsserts.checkAsserts(result.parseChecks)) {806 Logging.warn(msg);807 }808 }809 819 } 810 820 return result; … … 838 848 } 839 849 } 850 MapCSSTagCheckerAsserts.clear(); 840 851 } 841 852 -
trunk/src/org/openstreetmap/josm/data/validation/tests/MapCSSTagCheckerAsserts.java
r15979 r15981 8 8 import java.util.Collections; 9 9 import java.util.HashSet; 10 import java.util.LinkedHashSet;11 10 import java.util.List; 12 11 import java.util.Map; … … 36 35 37 36 /** 38 * Utility class for checking rule {@linkplain MapCSSTagChecker.TagCheck#assertions assertions}of {@link MapCSSTagChecker.TagCheck}.37 * Utility class for checking rule assertions of {@link MapCSSTagChecker.TagCheck}. 39 38 */ 40 class MapCSSTagCheckerAsserts { 39 final class MapCSSTagCheckerAsserts { 40 41 41 private MapCSSTagCheckerAsserts() { 42 42 // private constructor 43 43 } 44 44 45 private static final ArrayList<MapCSSTagChecker.TagCheck> previousChecks = new ArrayList<>(); 46 45 47 /** 46 48 * Checks that rule assertions are met for the given set of TagChecks. 47 * @param schecks The TagChecks for which assertions have to be checked 48 * @return A set of error messages, empty if all assertions are met 49 * @since 7356 49 * @param check The TagCheck for which assertions have to be checked 50 * @param assertionConsumer The handler for assertion error messages 50 51 */ 51 public static Set<String> checkAsserts(final Collection<MapCSSTagChecker.TagCheck> schecks) {52 Set<String> assertionErrors = new LinkedHashSet<>();52 static void checkAsserts(final MapCSSTagChecker.TagCheck check, final Map<String, Boolean> assertions, 53 final MapCSSTagChecker.AssertionConsumer assertionConsumer) { 53 54 final Method insideMethod = getFunctionMethod("inside"); 54 55 final DataSet ds = new DataSet(); 55 for (final MapCSSTagChecker.TagCheck check : schecks) { 56 Logging.debug("Check: {0}", check); 57 for (final Map.Entry<String, Boolean> i : check.assertions.entrySet()) { 58 Logging.debug("- Assertion: {0}", i); 59 final OsmPrimitive p = OsmUtils.createPrimitive(i.getKey(), getLocation(check, insideMethod), true); 60 // Build minimal ordered list of checks to run to test the assertion 61 List<Set<MapCSSTagChecker.TagCheck>> checksToRun = new ArrayList<>(); 62 Set<MapCSSTagChecker.TagCheck> checkDependencies = getTagCheckDependencies(check, schecks); 63 if (!checkDependencies.isEmpty()) { 64 checksToRun.add(checkDependencies); 56 Logging.debug("Check: {0}", check); 57 for (final Map.Entry<String, Boolean> i : assertions.entrySet()) { 58 Logging.debug("- Assertion: {0}", i); 59 final OsmPrimitive p = OsmUtils.createPrimitive(i.getKey(), getLocation(check, insideMethod), true); 60 // Build minimal ordered list of checks to run to test the assertion 61 List<Set<MapCSSTagChecker.TagCheck>> checksToRun = new ArrayList<>(); 62 Set<MapCSSTagChecker.TagCheck> checkDependencies = getTagCheckDependencies(check, previousChecks); 63 if (!checkDependencies.isEmpty()) { 64 checksToRun.add(checkDependencies); 65 } 66 checksToRun.add(Collections.singleton(check)); 67 // Add primitive to dataset to avoid DataIntegrityProblemException when evaluating selectors 68 addPrimitive(ds, p); 69 final Collection<TestError> pErrors = MapCSSTagChecker.getErrorsForPrimitive(p, true, checksToRun); 70 Logging.debug("- Errors: {0}", pErrors); 71 final boolean isError = pErrors.stream().anyMatch(e -> e.getTester() instanceof MapCSSTagChecker.MapCSSTagCheckerAndRule 72 && ((MapCSSTagChecker.MapCSSTagCheckerAndRule) e.getTester()).rule.equals(check.rule)); 73 if (isError != i.getValue()) { 74 assertionConsumer.accept(MessageFormat.format("Expecting test ''{0}'' (i.e., {1}) to {2} {3} (i.e., {4})", 75 check.getMessage(p), check.rule.selectors, i.getValue() ? "match" : "not match", i.getKey(), p.getKeys())); 76 } 77 if (isError) { 78 // Check that autofix works as expected 79 Command fix = check.fixPrimitive(p); 80 if (fix != null && fix.executeCommand() && !MapCSSTagChecker.getErrorsForPrimitive(p, true, checksToRun).isEmpty()) { 81 assertionConsumer.accept(MessageFormat.format("Autofix does not work for test ''{0}'' (i.e., {1})", 82 check.getMessage(p), check.rule.selectors)); 65 83 } 66 checksToRun.add(Collections.singleton(check));67 // Add primitive to dataset to avoid DataIntegrityProblemException when evaluating selectors68 addPrimitive(ds, p);69 final Collection<TestError> pErrors = MapCSSTagChecker.getErrorsForPrimitive(p, true, checksToRun);70 Logging.debug("- Errors: {0}", pErrors);71 final boolean isError = pErrors.stream().anyMatch(e -> e.getTester() instanceof MapCSSTagChecker.MapCSSTagCheckerAndRule72 && ((MapCSSTagChecker.MapCSSTagCheckerAndRule) e.getTester()).rule.equals(check.rule));73 if (isError != i.getValue()) {74 assertionErrors.add(MessageFormat.format("Expecting test ''{0}'' (i.e., {1}) to {2} {3} (i.e., {4})",75 check.getMessage(p), check.rule.selectors, i.getValue() ? "match" : "not match", i.getKey(), p.getKeys()));76 }77 if (isError) {78 // Check that autofix works as expected79 Command fix = check.fixPrimitive(p);80 if (fix != null && fix.executeCommand() && !MapCSSTagChecker.getErrorsForPrimitive(p, true, checksToRun).isEmpty()) {81 assertionErrors.add(MessageFormat.format("Autofix does not work for test ''{0}'' (i.e., {1})",82 check.getMessage(p), check.rule.selectors));83 }84 }85 ds.removePrimitive(p);86 84 } 85 ds.removePrimitive(p); 87 86 } 88 return assertionErrors; 87 previousChecks.add(check); 88 } 89 90 public static void clear() { 91 previousChecks.clear(); 92 previousChecks.trimToSize(); 89 93 } 90 94
Note:
See TracChangeset
for help on using the changeset viewer.