1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.tools.template_engine;
|
---|
3 |
|
---|
4 | import java.util.Arrays;
|
---|
5 | import java.util.List;
|
---|
6 |
|
---|
7 | import org.junit.Assert;
|
---|
8 | import org.junit.BeforeClass;
|
---|
9 | import org.junit.Test;
|
---|
10 | import org.openstreetmap.josm.JOSMFixture;
|
---|
11 | import org.openstreetmap.josm.actions.search.SearchCompiler;
|
---|
12 | import org.openstreetmap.josm.actions.search.SearchCompiler.Match;
|
---|
13 | import org.openstreetmap.josm.data.osm.Node;
|
---|
14 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
15 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
16 | import org.openstreetmap.josm.testutils.DatasetFactory;
|
---|
17 | import org.unitils.reflectionassert.ReflectionAssert;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Unit tests of {@link TemplateParser} class.
|
---|
21 | */
|
---|
22 | public class TemplateParserTest {
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Setup test.
|
---|
26 | */
|
---|
27 | @BeforeClass
|
---|
28 | public static void setUp() {
|
---|
29 | JOSMFixture.createUnitTestFixture().init();
|
---|
30 | }
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Test to parse an empty string.
|
---|
34 | * @throws ParseError if the template cannot be parsed
|
---|
35 | */
|
---|
36 | @Test
|
---|
37 | public void testEmpty() throws ParseError {
|
---|
38 | TemplateParser parser = new TemplateParser("");
|
---|
39 | ReflectionAssert.assertReflectionEquals(new StaticText(""), parser.parse());
|
---|
40 | }
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Test to parse a variable.
|
---|
44 | * @throws ParseError if the template cannot be parsed
|
---|
45 | */
|
---|
46 | @Test
|
---|
47 | public void testVariable() throws ParseError {
|
---|
48 | TemplateParser parser = new TemplateParser("abc{var}\\{ef\\$\\{g");
|
---|
49 | ReflectionAssert.assertReflectionEquals(CompoundTemplateEntry.fromArray(new StaticText("abc"),
|
---|
50 | new Variable("var"), new StaticText("{ef${g")), parser.parse());
|
---|
51 | }
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Test to parse a condition with whitespaces.
|
---|
55 | * @throws ParseError if the template cannot be parsed
|
---|
56 | */
|
---|
57 | @Test
|
---|
58 | public void testConditionWhitespace() throws ParseError {
|
---|
59 | TemplateParser parser = new TemplateParser("?{ '{name} {desc}' | '{name}' | '{desc}' }");
|
---|
60 | Condition condition = new Condition();
|
---|
61 | condition.getEntries().add(CompoundTemplateEntry.fromArray(new Variable("name"), new StaticText(" "), new Variable("desc")));
|
---|
62 | condition.getEntries().add(new Variable("name"));
|
---|
63 | condition.getEntries().add(new Variable("desc"));
|
---|
64 | ReflectionAssert.assertReflectionEquals(condition, parser.parse());
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Test to parse a condition without whitespace.
|
---|
69 | * @throws ParseError if the template cannot be parsed
|
---|
70 | */
|
---|
71 | @Test
|
---|
72 | public void testConditionNoWhitespace() throws ParseError {
|
---|
73 | TemplateParser parser = new TemplateParser("?{'{name} {desc}'|'{name}'|'{desc}'}");
|
---|
74 | Condition condition = new Condition();
|
---|
75 | condition.getEntries().add(CompoundTemplateEntry.fromArray(new Variable("name"), new StaticText(" "), new Variable("desc")));
|
---|
76 | condition.getEntries().add(new Variable("name"));
|
---|
77 | condition.getEntries().add(new Variable("desc"));
|
---|
78 | ReflectionAssert.assertReflectionEquals(condition, parser.parse());
|
---|
79 | }
|
---|
80 |
|
---|
81 | private static Match compile(String expression) throws SearchCompiler.ParseError {
|
---|
82 | return SearchCompiler.compile(expression);
|
---|
83 | }
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Test to parse a search expression condition.
|
---|
87 | * @throws ParseError if the template cannot be parsed
|
---|
88 | * @throws SearchCompiler.ParseError if an error has been encountered while compiling
|
---|
89 | */
|
---|
90 | @Test
|
---|
91 | public void testConditionSearchExpression() throws ParseError, SearchCompiler.ParseError {
|
---|
92 | TemplateParser parser = new TemplateParser("?{ admin_level = 2 'NUTS 1' | admin_level = 4 'NUTS 2' | '{admin_level}'}");
|
---|
93 | Condition condition = new Condition();
|
---|
94 | condition.getEntries().add(new SearchExpressionCondition(compile("admin_level = 2"), new StaticText("NUTS 1")));
|
---|
95 | condition.getEntries().add(new SearchExpressionCondition(compile("admin_level = 4"), new StaticText("NUTS 2")));
|
---|
96 | condition.getEntries().add(new Variable("admin_level"));
|
---|
97 | ReflectionAssert.assertReflectionEquals(condition, parser.parse());
|
---|
98 | }
|
---|
99 |
|
---|
100 | TemplateEngineDataProvider dataProvider = new TemplateEngineDataProvider() {
|
---|
101 | @Override
|
---|
102 | public Object getTemplateValue(String name, boolean special) {
|
---|
103 | if (special) {
|
---|
104 | if ("localName".equals(name))
|
---|
105 | return "localName";
|
---|
106 | else
|
---|
107 | return null;
|
---|
108 | } else {
|
---|
109 | if ("name".equals(name))
|
---|
110 | return "waypointName";
|
---|
111 | else if ("number".equals(name))
|
---|
112 | return 10;
|
---|
113 | else if ("special:key".equals(name))
|
---|
114 | return "specialKey";
|
---|
115 | else
|
---|
116 | return null;
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | @Override
|
---|
121 | public boolean evaluateCondition(Match condition) {
|
---|
122 | return true;
|
---|
123 | }
|
---|
124 |
|
---|
125 | @Override
|
---|
126 | public List<String> getTemplateKeys() {
|
---|
127 | return Arrays.asList("name", "number");
|
---|
128 | }
|
---|
129 | };
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * Test to fill a template.
|
---|
133 | * @throws ParseError if the template cannot be parsed
|
---|
134 | */
|
---|
135 | @Test
|
---|
136 | public void testFilling() throws ParseError {
|
---|
137 | TemplateParser parser = new TemplateParser("{name} u{unknown}u i{number}i");
|
---|
138 | TemplateEntry entry = parser.parse();
|
---|
139 | StringBuilder sb = new StringBuilder();
|
---|
140 | entry.appendText(sb, dataProvider);
|
---|
141 | Assert.assertEquals("waypointName uu i10i", sb.toString());
|
---|
142 | }
|
---|
143 |
|
---|
144 | /**
|
---|
145 | * Test to parse a search expression.
|
---|
146 | * @throws ParseError if the template cannot be parsed
|
---|
147 | */
|
---|
148 | @Test
|
---|
149 | public void testFillingSearchExpression() throws ParseError {
|
---|
150 | TemplateParser parser = new TemplateParser("?{ admin_level = 2 'NUTS 1' | admin_level = 4 'NUTS 2' | '{admin_level}'}");
|
---|
151 | TemplateEntry templateEntry = parser.parse();
|
---|
152 |
|
---|
153 | StringBuilder sb = new StringBuilder();
|
---|
154 | Relation r = new Relation();
|
---|
155 | r.put("admin_level", "2");
|
---|
156 | templateEntry.appendText(sb, r);
|
---|
157 | Assert.assertEquals("NUTS 1", sb.toString());
|
---|
158 |
|
---|
159 | sb.setLength(0);
|
---|
160 | r.put("admin_level", "5");
|
---|
161 | templateEntry.appendText(sb, r);
|
---|
162 | Assert.assertEquals("5", sb.toString());
|
---|
163 | }
|
---|
164 |
|
---|
165 | /**
|
---|
166 | * Test to print all.
|
---|
167 | * @throws ParseError if the template cannot be parsed
|
---|
168 | */
|
---|
169 | @Test
|
---|
170 | public void testPrintAll() throws ParseError {
|
---|
171 | TemplateParser parser = new TemplateParser("{special:everything}");
|
---|
172 | TemplateEntry entry = parser.parse();
|
---|
173 | StringBuilder sb = new StringBuilder();
|
---|
174 | entry.appendText(sb, dataProvider);
|
---|
175 | Assert.assertEquals("name=waypointName, number=10", sb.toString());
|
---|
176 | Assert.assertEquals("{special:everything}", entry.toString());
|
---|
177 | }
|
---|
178 |
|
---|
179 | /**
|
---|
180 | * Test to print on several lines.
|
---|
181 | * @throws ParseError if the template cannot be parsed
|
---|
182 | */
|
---|
183 | @Test
|
---|
184 | public void testPrintMultiline() throws ParseError {
|
---|
185 | TemplateParser parser = new TemplateParser("{name}\\n{number}");
|
---|
186 | TemplateEntry entry = parser.parse();
|
---|
187 | StringBuilder sb = new StringBuilder();
|
---|
188 | entry.appendText(sb, dataProvider);
|
---|
189 | Assert.assertEquals("waypointName\n10", sb.toString());
|
---|
190 | }
|
---|
191 |
|
---|
192 | /**
|
---|
193 | * Test to print special variables.
|
---|
194 | * @throws ParseError if the template cannot be parsed
|
---|
195 | */
|
---|
196 | @Test
|
---|
197 | public void testSpecialVariable() throws ParseError {
|
---|
198 | TemplateParser parser = new TemplateParser("{name}u{special:localName}u{special:special:key}");
|
---|
199 | TemplateEntry templateEntry = parser.parse();
|
---|
200 |
|
---|
201 | StringBuilder sb = new StringBuilder();
|
---|
202 | templateEntry.appendText(sb, dataProvider);
|
---|
203 | Assert.assertEquals("waypointNameulocalNameuspecialKey", sb.toString());
|
---|
204 | }
|
---|
205 |
|
---|
206 | @Test
|
---|
207 | public void testSearchExpression() throws Exception {
|
---|
208 | compile("(parent type=type1 type=parent1) | (parent type=type2 type=parent2)");
|
---|
209 | //"parent(type=type1,type=parent1) | (parent(type=type2,type=parent2)"
|
---|
210 | //TODO
|
---|
211 | }
|
---|
212 |
|
---|
213 | /**
|
---|
214 | * Test to switch context.
|
---|
215 | * @throws ParseError if the template cannot be parsed
|
---|
216 | */
|
---|
217 | @Test
|
---|
218 | public void testSwitchContext() throws ParseError {
|
---|
219 | TemplateParser parser = new TemplateParser("!{parent() type=parent2 '{name}'}");
|
---|
220 | DatasetFactory ds = new DatasetFactory();
|
---|
221 | Relation parent1 = ds.addRelation(1);
|
---|
222 | parent1.put("type", "parent1");
|
---|
223 | parent1.put("name", "name_parent1");
|
---|
224 | Relation parent2 = ds.addRelation(2);
|
---|
225 | parent2.put("type", "parent2");
|
---|
226 | parent2.put("name", "name_parent2");
|
---|
227 | Node child = ds.addNode(1);
|
---|
228 | parent1.addMember(new RelationMember("", child));
|
---|
229 | parent2.addMember(new RelationMember("", child));
|
---|
230 |
|
---|
231 | StringBuilder sb = new StringBuilder();
|
---|
232 | TemplateEntry entry = parser.parse();
|
---|
233 | entry.appendText(sb, child);
|
---|
234 |
|
---|
235 | Assert.assertEquals("name_parent2", sb.toString());
|
---|
236 | }
|
---|
237 |
|
---|
238 | @Test
|
---|
239 | public void testSetAnd() throws ParseError {
|
---|
240 | TemplateParser parser = new TemplateParser("!{(parent(type=child) type=parent) & (parent type=child subtype=parent) '{name}'}");
|
---|
241 | DatasetFactory ds = new DatasetFactory();
|
---|
242 | Relation parent1 = ds.addRelation(1);
|
---|
243 | parent1.put("type", "parent");
|
---|
244 | parent1.put("subtype", "parent");
|
---|
245 | parent1.put("name", "name_parent1");
|
---|
246 | Node child = ds.addNode(1);
|
---|
247 | child.put("type", "child");
|
---|
248 | parent1.addMember(new RelationMember("", child));
|
---|
249 |
|
---|
250 | StringBuilder sb = new StringBuilder();
|
---|
251 | TemplateEntry entry = parser.parse();
|
---|
252 | entry.appendText(sb, child);
|
---|
253 |
|
---|
254 | Assert.assertEquals("name_parent1", sb.toString());
|
---|
255 | }
|
---|
256 |
|
---|
257 | @Test
|
---|
258 | public void testSetOr() throws ParseError {
|
---|
259 | TemplateParser parser = new TemplateParser("!{(parent(type=type1) type=parent1) | (parent type=type2 type=parent2) '{name}'}");
|
---|
260 | DatasetFactory ds = new DatasetFactory();
|
---|
261 | Relation parent1 = ds.addRelation(1);
|
---|
262 | parent1.put("type", "parent1");
|
---|
263 | parent1.put("name", "name_parent1");
|
---|
264 | Relation parent2 = ds.addRelation(2);
|
---|
265 | parent2.put("type", "parent2");
|
---|
266 | parent2.put("name", "name_parent2");
|
---|
267 | Node child1 = ds.addNode(1);
|
---|
268 | child1.put("type", "type1");
|
---|
269 | parent1.addMember(new RelationMember("", child1));
|
---|
270 | parent2.addMember(new RelationMember("", child1));
|
---|
271 | Node child2 = ds.addNode(2);
|
---|
272 | child2.put("type", "type2");
|
---|
273 | parent1.addMember(new RelationMember("", child2));
|
---|
274 | parent2.addMember(new RelationMember("", child2));
|
---|
275 |
|
---|
276 | StringBuilder sb = new StringBuilder();
|
---|
277 | TemplateEntry entry = parser.parse();
|
---|
278 | entry.appendText(sb, child1);
|
---|
279 | entry.appendText(sb, child2);
|
---|
280 |
|
---|
281 | Assert.assertEquals("name_parent1name_parent2", sb.toString());
|
---|
282 | }
|
---|
283 |
|
---|
284 | @Test
|
---|
285 | public void testMultilevel() throws ParseError {
|
---|
286 | TemplateParser parser = new TemplateParser(
|
---|
287 | "!{(parent(parent(type=type1)) type=grandparent) | (parent type=type2 type=parent2) '{name}'}");
|
---|
288 | DatasetFactory ds = new DatasetFactory();
|
---|
289 | Relation parent1 = ds.addRelation(1);
|
---|
290 | parent1.put("type", "parent1");
|
---|
291 | parent1.put("name", "name_parent1");
|
---|
292 | Relation parent2 = ds.addRelation(2);
|
---|
293 | parent2.put("type", "parent2");
|
---|
294 | parent2.put("name", "name_parent2");
|
---|
295 | Node child1 = ds.addNode(1);
|
---|
296 | child1.put("type", "type1");
|
---|
297 | parent1.addMember(new RelationMember("", child1));
|
---|
298 | parent2.addMember(new RelationMember("", child1));
|
---|
299 | Node child2 = ds.addNode(2);
|
---|
300 | child2.put("type", "type2");
|
---|
301 | parent1.addMember(new RelationMember("", child2));
|
---|
302 | parent2.addMember(new RelationMember("", child2));
|
---|
303 | Relation grandParent = ds.addRelation(3);
|
---|
304 | grandParent.put("type", "grandparent");
|
---|
305 | grandParent.put("name", "grandparent_name");
|
---|
306 | grandParent.addMember(new RelationMember("", parent1));
|
---|
307 |
|
---|
308 |
|
---|
309 | StringBuilder sb = new StringBuilder();
|
---|
310 | TemplateEntry entry = parser.parse();
|
---|
311 | entry.appendText(sb, child1);
|
---|
312 | entry.appendText(sb, child2);
|
---|
313 |
|
---|
314 | Assert.assertEquals("grandparent_namename_parent2", sb.toString());
|
---|
315 | }
|
---|
316 |
|
---|
317 | @Test(expected = ParseError.class)
|
---|
318 | public void testErrorsNot() throws ParseError {
|
---|
319 | TemplateParser parser = new TemplateParser("!{-parent() '{name}'}");
|
---|
320 | parser.parse();
|
---|
321 | }
|
---|
322 |
|
---|
323 | @Test(expected = ParseError.class)
|
---|
324 | public void testErrorOr() throws ParseError {
|
---|
325 | TemplateParser parser = new TemplateParser("!{parent() | type=type1 '{name}'}");
|
---|
326 | parser.parse();
|
---|
327 | }
|
---|
328 |
|
---|
329 | @Test
|
---|
330 | public void testChild() throws ParseError {
|
---|
331 | TemplateParser parser = new TemplateParser("!{((child(type=type1) type=child1) | (child type=type2 type=child2)) type=child2 '{name}'}");
|
---|
332 | DatasetFactory ds = new DatasetFactory();
|
---|
333 | Relation parent1 = ds.addRelation(1);
|
---|
334 | parent1.put("type", "type1");
|
---|
335 | Relation parent2 = ds.addRelation(2);
|
---|
336 | parent2.put("type", "type2");
|
---|
337 | Node child1 = ds.addNode(1);
|
---|
338 | child1.put("type", "child1");
|
---|
339 | child1.put("name", "child1");
|
---|
340 | parent1.addMember(new RelationMember("", child1));
|
---|
341 | parent2.addMember(new RelationMember("", child1));
|
---|
342 | Node child2 = ds.addNode(2);
|
---|
343 | child2.put("type", "child2");
|
---|
344 | child2.put("name", "child2");
|
---|
345 | parent1.addMember(new RelationMember("", child2));
|
---|
346 | parent2.addMember(new RelationMember("", child2));
|
---|
347 |
|
---|
348 |
|
---|
349 | StringBuilder sb = new StringBuilder();
|
---|
350 | TemplateEntry entry = parser.parse();
|
---|
351 | entry.appendText(sb, parent2);
|
---|
352 |
|
---|
353 | Assert.assertEquals("child2", sb.toString());
|
---|
354 | }
|
---|
355 |
|
---|
356 | @Test
|
---|
357 | public void testToStringCanBeParsedAgain() throws Exception {
|
---|
358 | final String s1 = "?{ '{name} ({desc})' | '{name} ({cmt})' | '{name}' | '{desc}' | '{cmt}' }";
|
---|
359 | final String s2 = new TemplateParser(s1).parse().toString();
|
---|
360 | final String s3 = new TemplateParser(s2).parse().toString();
|
---|
361 | Assert.assertEquals(s1, s2);
|
---|
362 | Assert.assertEquals(s2, s3);
|
---|
363 | }
|
---|
364 | }
|
---|