Ticket #17995: 17995.3.patch

File 17995.3.patch, 2.9 KB (added by taylor.smock, 2 years ago)

Overload function so that it works with lists from other functions

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

     
    494494    }
    495495
    496496    /**
     497     * Sort an array of strings
     498     * @param placer A variable to make a distinct function (FIXME: Remove when mapcss functions can be overloaded)
     499     * @param sortables The array to sort
     500     * @return The sorted list
     501     * @since xxx
     502     */
     503    public static List<String> sort(String placer, String... sortables) {
     504        List<String> list = Arrays.asList(sortables);
     505        list.add(placer);
     506        return sort(list);
     507    }
     508
     509    /**
     510     * Sort an array of strings
     511     * @param sortables The list to sort
     512     * @return The sorted list
     513     * @since xxx
     514     */
     515    public static List<String> sort(List<String> sortables) {
     516        Collections.sort(sortables);
     517        return sortables;
     518    }
     519
     520    /**
    497521     * Returns the role of current object in parent relation, or role of child if current object is a relation.
    498522     * @param env the environment
    499523     * @return role of current object in parent relation, or role of child if current object is a relation
  • test/unit/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSParserTest.java

     
    99
    1010import java.awt.Color;
    1111import java.io.StringReader;
     12import java.util.Arrays;
    1213import java.util.List;
    1314
    1415import org.junit.Rule;
     
    407408    }
    408409
    409410    @Test
     411    public void testSort() throws Exception {
     412        assertEquals(Arrays.asList(new String[] {"alpha", "beta"}), Functions.sort("beta", "alpha"));
     413        Way way1 = TestUtils.newWay("highway=residential name=Alpha alt_name=Beta", new Node(new LatLon(0.001, 0.001)),
     414                new Node(new LatLon(0.002, 0.002)));
     415
     416        MapCSSStyleSource source = new MapCSSStyleSource("way[highway] {sorted: join_list(\",\", sort(tag(\"alt_name\"), tag(\"name\")));}");
     417        source.loadStyleSource();
     418        assertEquals(1, source.rules.size());
     419        Environment e = new Environment(way1, new MultiCascade(), Environment.DEFAULT_LAYER, null);
     420        assertTrue(source.rules.get(0).selector.matches(e));
     421        source.rules.get(0).declaration.execute(e);
     422        assertEquals(Functions.join(",", "Alpha", "Beta"), e.getCascade(Environment.DEFAULT_LAYER).get("sorted", null, String.class));
     423    }
     424
     425    @Test
    410426    public void testCountRoles() throws Exception {
    411427        DataSet ds = new DataSet();
    412428        Way way1 = TestUtils.newWay("highway=residential name=1",