Ticket #25026: #25026-Test-Add-unit-tests-for-TagCheckerTest.2.patch

File #25026-Test-Add-unit-tests-for-TagCheckerTest.2.patch, 5.2 KB (added by DanProgs <Abenteurer.2901@…>, 3 weeks ago)
  • test/unit/org/openstreetmap/josm/data/validation/tests/TagCheckerTest.java

    ### Eclipse Workspace Patch 1.0
    #P josm
    diff --git test/unit/org/openstreetmap/josm/data/validation/tests/TagCheckerTest.java test/unit/org/openstreetmap/josm/data/validation/tests/TagCheckerTest.java
    index a4185d3..6898333 100644
     
    374374            originalBusStop.data.remove(duplicateKey);
    375375        }
    376376    }
     377   
     378    /**
     379     * Tests value harmonization (e.g. case normalization and trimming) resulting in a valid preset match.
     380     *
     381     * @throws IOException if an I/O error occurs
     382     */
     383    @Test
     384    void testTryGuessHarmonizedMatch() throws IOException {
     385        final List<TestError> errors = test(OsmUtils.createPrimitive("way highway=\"Residential \""));
     386        assertEquals(1, errors.size());
     387        final TestError error = errors.get(0);
     388        assertEquals(Severity.WARNING, error.getSeverity());
     389        assertEquals(TagChecker.MISSPELLED_VALUE, error.getCode());
     390        assertTrue(error.getDescription().contains("maybe 'residential' is meant?"));
     391    }
     392
     393    /**
     394     * Tests a single unambiguous typo corrected via Levenshtein distance.
     395     *
     396     * @throws IOException if an I/O error occurs
     397     */
     398    @Test
     399    void testTryGuessSingleLevenshteinMatch() throws IOException {
     400        final List<TestError> errors = test(OsmUtils.createPrimitive("way highway=residentail"));
     401        assertEquals(1, errors.size());
     402        final TestError error = errors.get(0);
     403        assertEquals(Severity.WARNING, error.getSeverity());
     404        assertEquals(TagChecker.MISSPELLED_VALUE, error.getCode());
     405        assertTrue(error.getDescription().contains("maybe 'residential' is meant?"));
     406    }
     407
     408    /**
     409     * Tests a misspelled value that has multiple equally close preset candidates.
     410     *
     411     * @throws IOException if an I/O error occurs
     412     */
     413    @Test
     414    void testTryGuessMultipleLevenshteinMatches() throws IOException {
     415        // "radh" (length 4) has a Levenshtein distance of 2 to both "path" and "road"
     416        final List<TestError> errors = test(OsmUtils.createPrimitive("way highway=radh"));
     417        assertEquals(1, errors.size());
     418        final TestError error = errors.get(0);
     419        assertEquals(Severity.WARNING, error.getSeverity());
     420        assertEquals(TagChecker.MISSPELLED_VALUE_NO_FIX, error.getCode());
     421        assertTrue(error.getDescription().contains("maybe one of ["));
     422    }
     423
     424    /**
     425     * Tests that short values do not trigger false typo suggestions when length differences are too large.
     426     *
     427     * @throws IOException if an I/O error occurs
     428     */
     429    @Test
     430    void testTryGuessShortValueGuard() throws IOException {
     431        final List<TestError> errors = test(OsmUtils.createPrimitive("way surface=u"));
     432        assertEquals(1, errors.size());
     433        final TestError error = errors.get(0);
     434        assertEquals(Severity.OTHER, error.getSeverity());
     435        assertEquals(TagChecker.INVALID_VALUE, error.getCode());
     436    }
     437
     438    /**
     439     * Tests that short values with no matching characters are reported as unknown instead of misspelled.
     440     *
     441     * @throws IOException if an I/O error occurs
     442     */
     443    @Test
     444    void testTryGuessCompletelyDifferentShortValue() throws IOException {
     445        final List<TestError> errors = test(OsmUtils.createPrimitive("node amenity=parking fee=10"));
     446        assertEquals(1, errors.size());
     447        final TestError error = errors.get(0);
     448        assertEquals(Severity.OTHER, error.getSeverity());
     449        assertEquals(TagChecker.INVALID_VALUE, error.getCode());
     450    }
     451
     452    /**
     453     * Tests an unknown tag value that does not match any preset within the allowed Levenshtein distance.
     454     *
     455     * @throws IOException if an I/O error occurs
     456     */
     457    @Test
     458    void testTryGuessUnknownValue() throws IOException {
     459        final List<TestError> errors = test(OsmUtils.createPrimitive("way highway=completely_unknown_value"));
     460        assertEquals(1, errors.size());
     461        final TestError error = errors.get(0);
     462        assertEquals(Severity.OTHER, error.getSeverity());
     463        assertEquals(TagChecker.INVALID_VALUE, error.getCode());
     464    }
     465
     466    /**
     467     * Test that keys listed in {@code ignoreForLevenshtein} do not trigger a typo suggestion,
     468     * even if the unknown value has a small Levenshtein distance to a preset value.
     469     *
     470     * @throws IOException if an I/O error occurs
     471     */
     472    @Test
     473    void testTryGuessIgnoredKey() throws IOException {
     474        // 'couplings' has numerical preset values (e.g. 1, 2) and is in ignoreForLevenshtein.
     475        // Value "11" has Levenshtein distance 1 to "1", so no typo suggestion should be made.
     476        final List<TestError> ignoredErrors = test(OsmUtils.createPrimitive("node emergency=fire_hydrant couplings=11"));
     477        assertEquals(1, ignoredErrors.size());
     478        final TestError error = ignoredErrors.get(0);
     479        assertEquals(Severity.OTHER, error.getSeverity());
     480        assertEquals(TagChecker.INVALID_VALUE, error.getCode());
     481        assertEquals("Presets do not contain property value", error.getMessage());
     482        assertEquals("Value '11' for key 'couplings' not in presets.", error.getDescription());
     483    }
    377484}