source: josm/trunk/test/unit/org/openstreetmap/josm/gui/mappaint/mapcss/FunctionsTest.java@ 18829

Last change on this file since 18829 was 18829, checked in by taylor.smock, 9 months ago

Fix #16998: Add parent_osm_primitives and convert_primitives_to_string

  • Property svn:eol-style set to native
File size: 8.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.mappaint.mapcss;
3
4import static org.junit.jupiter.api.Assertions.assertAll;
5import static org.junit.jupiter.api.Assertions.assertEquals;
6import static org.junit.jupiter.api.Assertions.assertFalse;
7import static org.junit.jupiter.api.Assertions.assertNotNull;
8import static org.junit.jupiter.api.Assertions.assertNull;
9import static org.junit.jupiter.api.Assertions.assertSame;
10import static org.junit.jupiter.api.Assertions.assertTrue;
11import static org.openstreetmap.josm.data.osm.OsmPrimitiveType.NODE;
12
13import java.util.Arrays;
14import java.util.Collections;
15import java.util.List;
16import java.util.Objects;
17
18import org.junit.jupiter.api.Test;
19import org.openstreetmap.josm.TestUtils;
20import org.openstreetmap.josm.data.coor.LatLon;
21import org.openstreetmap.josm.data.osm.IPrimitive;
22import org.openstreetmap.josm.data.osm.Node;
23import org.openstreetmap.josm.data.osm.OsmPrimitive;
24import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
25import org.openstreetmap.josm.data.osm.Relation;
26import org.openstreetmap.josm.data.osm.RelationMember;
27import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
28import org.openstreetmap.josm.data.osm.User;
29import org.openstreetmap.josm.data.osm.Way;
30import org.openstreetmap.josm.data.preferences.NamedColorProperty;
31import org.openstreetmap.josm.gui.mappaint.Environment;
32import org.openstreetmap.josm.gui.util.GuiHelper;
33import org.openstreetmap.josm.spi.preferences.Config;
34import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
35import org.openstreetmap.josm.testutils.annotations.Projection;
36
37/**
38 * Unit tests of {@link Functions}.
39 */
40@BasicPreferences
41@Projection
42class FunctionsTest {
43 private static class EnvBuilder {
44 private final OsmPrimitive osm;
45
46 EnvBuilder(OsmPrimitiveType type) {
47 switch (type) {
48 case NODE : osm = TestUtils.newNode(""); break;
49 case WAY : osm = TestUtils.newWay(""); break;
50 case RELATION : osm = TestUtils.newRelation(""); break;
51 default: throw new IllegalArgumentException();
52 }
53 }
54
55 EnvBuilder setUser(User user) {
56 osm.setUser(user);
57 return this;
58 }
59
60 Environment build() {
61 return new Environment(osm);
62 }
63 }
64
65 /**
66 * Unit test of {@link Functions#title}.
67 */
68 @Test
69 void testTitle() {
70 assertNull(Functions.title(null));
71 assertEquals("", Functions.title(""));
72 assertEquals("I Am Fine", Functions.title("i am FINE"));
73 }
74
75 /**
76 * Unit test of {@link Functions#osm_user_name}.
77 */
78 @Test
79 void testOsmUserName() {
80 assertEquals("<anonymous>", Functions.osm_user_name(new EnvBuilder(NODE).setUser(User.getAnonymous()).build()));
81 }
82
83 /**
84 * Unit test of {@link Functions#osm_user_id}.
85 */
86 @Test
87 void testOsmUserId() {
88 assertEquals(-1, Functions.osm_user_id(new EnvBuilder(NODE).setUser(User.getAnonymous()).build()));
89 }
90
91 /**
92 * Unit test of {@link Functions#osm_version}.
93 */
94 @Test
95 void testOsmVersion() {
96 assertEquals(0, Functions.osm_version(new EnvBuilder(NODE).build()));
97 }
98
99 /**
100 * Unit test of {@link Functions#osm_changeset_id}.
101 */
102 @Test
103 void testOsmChangesetId() {
104 assertEquals(0, Functions.osm_changeset_id(new EnvBuilder(NODE).build()));
105 }
106
107 /**
108 * Unit test of {@link Functions#osm_timestamp}.
109 */
110 @Test
111 void testOsmTimestamp() {
112 assertEquals(0, Functions.osm_timestamp(new EnvBuilder(NODE).build()));
113 }
114
115 /**
116 * Test for {@link Functions#parent_way_angle(Environment)}
117 */
118 @Test
119 void testParentWayAngle() {
120 assertNull(Functions.parent_way_angle(new EnvBuilder(NODE).build()));
121 final Environment environment = new EnvBuilder(NODE).build();
122 ((Node) environment.osm).setCoor(LatLon.ZERO);
123 final Way parent = TestUtils.newWay("", new Node(new LatLon(-.1, 0)), (Node) environment.osm, new Node(new LatLon(.1, 0)));
124 environment.parent = parent;
125 Double actual = Functions.parent_way_angle(environment);
126 assertNotNull(actual);
127 assertEquals(Math.toRadians(0), actual, 1e-9);
128 // Reverse node order
129 Objects.requireNonNull(parent.firstNode()).setCoor(LatLon.NORTH_POLE);
130 Objects.requireNonNull(parent.lastNode()).setCoor(LatLon.SOUTH_POLE);
131 actual = Functions.parent_way_angle(environment);
132 assertNotNull(actual);
133 assertEquals(Math.toRadians(180), actual, 1e-9);
134 }
135
136 /**
137 * Unit test of {@code Functions#to_xxx}
138 */
139 @Test
140 void testParseFunctions() {
141 assertTrue(Functions.to_boolean("true"));
142 assertEquals(1, Functions.to_byte("1"));
143 assertEquals(1, Functions.to_short("1"));
144 assertEquals(1, Functions.to_int("1"));
145 assertEquals(1L, Functions.to_long("1"));
146 assertEquals(1f, Functions.to_float("1"), 1e-10);
147 assertEquals(1d, Functions.to_double("1"), 1e-10);
148 }
149
150 /**
151 * Unit test of {@link Functions#JOSM_pref}
152 */
153 @Test
154 void testPref() {
155 String key = "Functions.JOSM_pref";
156 Config.getPref().put(key, null);
157 assertEquals("foobar", Functions.JOSM_pref(null, key, "foobar"));
158 Config.getPref().put(key, "baz");
159 GuiHelper.runInEDTAndWait(() -> {
160 // await org.openstreetmap.josm.gui.mappaint.ElemStyles.clearCached
161 });
162 assertEquals("baz", Functions.JOSM_pref(null, key, "foobar"));
163 Config.getPref().put(key, null);
164 GuiHelper.runInEDTAndWait(() -> {
165 // await org.openstreetmap.josm.gui.mappaint.ElemStyles.clearCached
166 });
167 assertEquals("foobar", Functions.JOSM_pref(null, key, "foobar"));
168 Config.getPref().put(key, null);
169 }
170
171 /**
172 * Unit test of {@link Functions#JOSM_pref}, color handling
173 */
174 @Test
175 void testPrefColor() {
176 String key = "Functions.JOSM_pref";
177 String colorKey = NamedColorProperty.NAMED_COLOR_PREFIX + NamedColorProperty.COLOR_CATEGORY_MAPPAINT + ".unknown." + key;
178 Config.getPref().put(colorKey, null);
179 assertEquals("#000000", Functions.JOSM_pref(null, key, "#000000"));
180 Config.getPref().putList(colorKey, Collections.singletonList("#00FF00"));
181 GuiHelper.runInEDTAndWait(() -> {
182 // await org.openstreetmap.josm.gui.mappaint.ElemStyles.clearCached
183 });
184 assertEquals("#00FF00", Functions.JOSM_pref(null, key, "#000000"));
185 Config.getPref().put(colorKey, null);
186 GuiHelper.runInEDTAndWait(() -> {
187 // await org.openstreetmap.josm.gui.mappaint.ElemStyles.clearCached
188 });
189 assertEquals("#000000", Functions.JOSM_pref(null, key, "#000000"));
190 Config.getPref().put(colorKey, null);
191 }
192
193 @Test
194 void testConvertPrimitivesToString() {
195 assertEquals(Collections.singletonList("n1"), Functions.convert_primitives_to_string(
196 Collections.singleton(new SimplePrimitiveId(1, NODE))));
197 assertEquals(Arrays.asList("n1", "n9223372036854775807"), Functions.convert_primitives_to_string(
198 Arrays.asList(new SimplePrimitiveId(1, NODE), new SimplePrimitiveId(Long.MAX_VALUE, NODE))));
199 }
200
201 @Test
202 void testParentOsmPrimitives() {
203 final Environment env = new EnvBuilder(NODE).build();
204 final Relation relation1 = TestUtils.newRelation("", new RelationMember("", (Node) env.osm));
205 final Relation relation2 = TestUtils.newRelation("type=something", new RelationMember("", (Node) env.osm));
206 final Relation relation3 = TestUtils.newRelation("type=somethingelse", new RelationMember("", (Node) env.osm));
207
208 TestUtils.addFakeDataSet((Node) env.osm);
209 for (Relation relation : Arrays.asList(relation1, relation2, relation3)) {
210 ((Node) env.osm).getDataSet().addPrimitive(relation);
211 }
212
213 final List<IPrimitive> allReferrers = Functions.parent_osm_primitives(env);
214 assertAll(() -> assertEquals(3, allReferrers.size()),
215 () -> assertTrue(allReferrers.contains(relation1)),
216 () -> assertTrue(allReferrers.contains(relation2)),
217 () -> assertTrue(allReferrers.contains(relation3)));
218
219 final List<IPrimitive> typeReferrers = Functions.parent_osm_primitives(env, "type");
220 assertAll(() -> assertEquals(2, typeReferrers.size()),
221 () -> assertFalse(typeReferrers.contains(relation1)),
222 () -> assertTrue(typeReferrers.contains(relation2)),
223 () -> assertTrue(typeReferrers.contains(relation3)));
224
225 final List<IPrimitive> typeSomethingReferrers = Functions.parent_osm_primitives(env, "type", "something");
226 assertAll(() -> assertEquals(1, typeSomethingReferrers.size()),
227 () -> assertSame(relation2, typeSomethingReferrers.get(0)));
228
229 assertTrue(Functions.parent_osm_primitives(env, "type2").isEmpty());
230 }
231}
Note: See TracBrowser for help on using the repository browser.