Changeset 17769 in josm


Ignore:
Timestamp:
2021-04-13T21:12:08+02:00 (3 years ago)
Author:
simon04
Message:

see #20744 - MapCSS: fix unary minus

Regression of r17758.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/ExpressionFactory.java

    r17764 r17769  
    5555        }
    5656
    57         static Factory ofNumberVarArgs(DoubleBinaryOperator operator) {
    58             return args -> env -> args.stream()
     57        static Factory ofNumberVarArgs(double identity, DoubleUnaryOperator unaryOperator, DoubleBinaryOperator operator) {
     58            return args -> env -> args.isEmpty()
     59                    ? identity
     60                    : args.size() == 1
     61                    ? unaryOperator.applyAsDouble(Cascade.convertTo(args.get(0).evaluate(env), Double.class))
     62                    : args.stream()
    5963                    .map(arg -> Cascade.convertTo(arg.evaluate(env), Double.class))
    6064                    .filter(Objects::nonNull)
     
    162166        FACTORY_MAP.put("count_roles", Factory.ofStringVarargs(Functions::count_roles));
    163167        FACTORY_MAP.put("degree_to_radians", Factory.of(Functions::degree_to_radians));
    164         FACTORY_MAP.put("divided_by", Factory.ofNumberVarArgs(Functions::divided_by));
     168        FACTORY_MAP.put("divided_by", Factory.ofNumberVarArgs(1.0, DoubleUnaryOperator.identity(), Functions::divided_by));
    165169        FACTORY_MAP.put("equal", Factory.of(Object.class, Object.class, Functions::equal));
    166170        FACTORY_MAP.put("eval", Factory.of(Object.class, Functions::eval));
     
    189193        FACTORY_MAP.put("log", Factory.of(Math::log));
    190194        FACTORY_MAP.put("lower", Factory.of(String.class, Functions::lower));
    191         FACTORY_MAP.put("minus", Factory.ofNumberVarArgs(Functions::minus));
     195        FACTORY_MAP.put("minus", Factory.ofNumberVarArgs(0.0, v -> -v, Functions::minus));
    192196        FACTORY_MAP.put("mod", Factory.of(float.class, float.class, Functions::mod));
    193197        FACTORY_MAP.put("not", Factory.of(boolean.class, Functions::not));
     
    204208        FACTORY_MAP.put("parent_tag", Factory.ofEnv(String.class, Functions::parent_tag));
    205209        FACTORY_MAP.put("parent_tags", Factory.ofEnv(String.class, Functions::parent_tags));
    206         FACTORY_MAP.put("plus", Factory.ofNumberVarArgs(Functions::plus));
     210        FACTORY_MAP.put("plus", Factory.ofNumberVarArgs(0.0, DoubleUnaryOperator.identity(), Functions::plus));
    207211        FACTORY_MAP.put("print", Factory.of(Object.class, Functions::print));
    208212        FACTORY_MAP.put("println", Factory.of(Object.class, Functions::println));
     
    229233        FACTORY_MAP.put("tan", Factory.of(Math::tan));
    230234        FACTORY_MAP.put("tanh", Factory.of(Math::tanh));
    231         FACTORY_MAP.put("times", Factory.ofNumberVarArgs(Functions::times));
     235        FACTORY_MAP.put("times", Factory.ofNumberVarArgs(1.0, DoubleUnaryOperator.identity(), Functions::times));
    232236        FACTORY_MAP.put("title", Factory.of(String.class, Functions::title));
    233237        FACTORY_MAP.put("to_boolean", Factory.of(String.class, Functions::to_boolean));
  • trunk/test/unit/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSParserTest.java

    r17762 r17769  
    606606    @Test
    607607    void testMath() {
    608         MapCSSStyleSource source = new MapCSSStyleSource("node { add: 1 + 2 + 3 + 4; mul: 2 * 3 * 5 * 7; sub: 0 - 1 - 2 - 3; div: 360 / 15; }");
     608        MapCSSStyleSource source = new MapCSSStyleSource("node {" +
     609                "add: 1 + 2 + 3 + 4;" +
     610                "mul: 2 * 3 * 5 * 7;" +
     611                "sub: 0 - 1 - 2 - 3;" +
     612                "div: 360 / 15;" +
     613                "neg: -13;" +
     614                "not: !0;" +
     615                "}");
    609616        source.loadStyleSource();
    610617        MultiCascade mc = new MultiCascade();
     
    614621        assertEquals(-6.0, mc.getCascade(null).get("sub"));
    615622        assertEquals(24.0, mc.getCascade(null).get("div"));
     623        assertEquals(-13.0, mc.getCascade(null).get("neg"));
     624        assertEquals(true, mc.getCascade(null).get("not"));
    616625    }
    617626
Note: See TracChangeset for help on using the changeset viewer.