Changeset 15279 in josm


Ignore:
Timestamp:
2019-08-04T22:09:54+02:00 (5 years ago)
Author:
Don-vip
Message:

fix #17995 - Add sort method for mapcss (patch by taylor.smock)

Location:
trunk
Files:
2 edited

Legend:

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

    r15275 r15279  
    495495
    496496    /**
     497     * Sort an array of strings
     498     * @param sortables The array to sort
     499     * @return The sorted list
     500     * @since 15279
     501     */
     502    public static List<String> sort(String... sortables) {
     503        Arrays.parallelSort(sortables);
     504        return Arrays.asList(sortables);
     505    }
     506
     507    /**
     508     * Sort a list of strings
     509     * @param sortables The list to sort
     510     * @return The sorted list
     511     * @since 15279
     512     */
     513    public static List<String> sort_list(List<String> sortables) {
     514        Collections.sort(sortables);
     515        return sortables;
     516    }
     517
     518    /**
    497519     * Returns the role of current object in parent relation, or role of child if current object is a relation.
    498520     * @param env the environment
  • trunk/test/unit/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSParserTest.java

    r15275 r15279  
    1010import java.awt.Color;
    1111import java.io.StringReader;
     12import java.util.Arrays;
    1213import java.util.List;
    1314
     
    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 ref=\"A9;A8\"", 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        source = new MapCSSStyleSource("way[ref] {sorted: join_list(\",\", sort_list(split(\";\", tag(\"ref\"))));}");
     425        source.loadStyleSource();
     426        e = new Environment(way1, new MultiCascade(), Environment.DEFAULT_LAYER, null);
     427        assertTrue(source.rules.get(0).selector.matches(e));
     428        source.rules.get(0).declaration.execute(e);
     429        assertEquals(Functions.join(",", "A8", "A9"), e.getCascade(Environment.DEFAULT_LAYER).get("sorted", null, String.class));
     430    }
     431
     432    @Test
    410433    public void testCountRoles() throws Exception {
    411434        DataSet ds = new DataSet();
Note: See TracChangeset for help on using the changeset viewer.