Ticket #19508: 19508v2.patch

File 19508v2.patch, 3.2 KB (added by gaben, 6 years ago)

Suggestions applied, improved test cases

  • src/org/openstreetmap/josm/actions/corrector/ReverseWayTagCorrector.java

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    201201    }
    202202
    203203    /**
    204      * Inverts sign of a numeric value.
     204     * Inverts sign of a numeric value and converts decimal number to use decimal point.
    205205     * @param value numeric value
    206206     * @return opposite numeric value
    207207     */
    208208    public static String invertNumber(String value) {
    209         Pattern pattern = Pattern.compile("^([+-]?)(\\d.*)$", Pattern.CASE_INSENSITIVE);
     209        Pattern pattern = Pattern.compile("^([+-]?)(\\d*[,.]?\\d*)(.*)$", Pattern.CASE_INSENSITIVE);
    210210        Matcher matcher = pattern.matcher(value);
    211211        if (!matcher.matches()) return value;
    212212        String sign = matcher.group(1);
    213         String rest = matcher.group(2);
     213        String number = matcher.group(2);
     214        String symbol = matcher.group(3);
    214215        sign = "-".equals(sign) ? "" : "-";
    215         return sign + rest;
     216
     217        if (!number.isEmpty()) {
     218            String fixedNum = number.replace(",", ".");
     219            double parsed = Double.parseDouble(fixedNum);
     220            if (parsed != 0) {
     221                return sign + fixedNum + symbol;
     222            }
     223        }
     224
     225        return value;
    216226    }
    217227
    218228    static List<TagCorrection> getTagCorrections(Tagged way) {
  • test/unit/org/openstreetmap/josm/actions/corrector/ReverseWayTagCorrectorTest.java

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    5858            assertSwitch(new Tag(k, "down"), new Tag(k, "up"));
    5959            assertSwitch(new Tag(k, "something"), new Tag(k, "something"));
    6060        }
     61        // numbered incline (see #19508)
     62        assertSwitch(new Tag("incline", "0%"), new Tag("incline", "0%"));
     63        assertSwitch(new Tag("incline", ".1%"), new Tag("incline", "-.1%"));
     64        assertSwitch(new Tag("incline", "-10.0%"), new Tag("incline", "10.0%"));
     65        assertSwitch(new Tag("incline", "0,6°"), new Tag("incline", "-0.6°"));
    6166        // direction=forward/backward/...
    6267        assertSwitch(new Tag("direction", "forward"), new Tag("direction", "backward"));
    6368        assertSwitch(new Tag("direction", "backward"), new Tag("direction", "forward"));
     
    99104    }
    100105
    101106    private void assertSwitch(Tag oldTag, Tag newTag) {
    102         Assert.assertEquals(ReverseWayTagCorrector.TagSwitcher.apply(oldTag), newTag);
     107        Assert.assertEquals(newTag, ReverseWayTagCorrector.TagSwitcher.apply(oldTag));
    103108    }
    104109
    105110    private Map<OsmPrimitive, List<TagCorrection>> getTagCorrectionsForWay(String middleNodeTags) {