Ticket #17845: 17845_varargs.patch

File 17845_varargs.patch, 3.0 KB (added by taylor.smock, 7 years ago)

Initial work on allowing varargs to be used in mapcss functions

  • src/org/openstreetmap/josm/gui/mappaint/mapcss/ExpressionFactory.java

     
    572572        }
    573573
    574574        /**
    575          * Returns true if role is in relation. Returns false if not a relation or it does not have the role.
     575         * Returns the number of primitives in a relation with the specified roles.
    576576         * @param env the environment
    577577         * @param roles The roles to count in the relation
    578578         * @return The number of relation members with the specified role
     
    13711371                convertedArgs = new Object[args.size()+1];
    13721372                convertedArgs[0] = env;
    13731373                for (int i = 1; i < convertedArgs.length; ++i) {
    1374                     convertedArgs[i] = Cascade.convertTo(args.get(i-1).evaluate(env), expectedParameterTypes[i]);
     1374                    if (!expectedParameterTypes[i].isArray()) {
     1375                        convertedArgs[i] = Cascade.convertTo(args.get(i-1).evaluate(env), expectedParameterTypes[i]);
     1376                    } else {
     1377                        Class<?> clazz = expectedParameterTypes[i].getComponentType();
     1378                        Object[] varargs = (Object[]) Array.newInstance(clazz, args.size() - i + 1);
     1379                        for (int j = 0; j < args.size() - i + 1; ++j) {
     1380                            varargs[j] = Cascade.convertTo(args.get(j + i - 1).evaluate(env), clazz);
     1381                        }
     1382                        convertedArgs[i] = expectedParameterTypes[i].cast(varargs);
     1383                        break;
     1384                    }
    13751385                    if (convertedArgs[i] == null && !nullable) {
    13761386                        return null;
    13771387                    }
  • test/unit/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSParserTest.java

     
    445445        /* Check with non-relation */
    446446        e = new Environment(way1, new MultiCascade(), Environment.DEFAULT_LAYER, null);
    447447        assertEquals(0, ExpressionFactory.Functions.count_roles(e, "from", "to"));
     448
     449        /* Check with actual call to mapcss functions */
     450        MapCSSStyleSource source = new MapCSSStyleSource("relation[type=destination_sign] {roles: count_roles(\"from\");}");
     451        source.loadStyleSource();
     452        assertEquals(1, source.rules.size());
     453        e = new Environment(rel1, new MultiCascade(), Environment.DEFAULT_LAYER, null);
     454        assertTrue(source.rules.get(0).selector.matches(e));
     455        source.rules.get(0).declaration.execute(e);
     456        assertEquals((Integer) 1, e.getCascade(Environment.DEFAULT_LAYER).get("roles", null, Integer.class));
     457
    448458    }
    449459
    450460    @Test