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.DatasetFactory;
|
---|
14 | import org.openstreetmap.josm.data.osm.Node;
|
---|
15 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
16 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
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 | }
|
---|
177 |
|
---|
178 | /**
|
---|
179 | * Test to print on several lines.
|
---|
180 | * @throws ParseError if the template cannot be parsed
|
---|
181 | */
|
---|
182 | @Test
|
---|
183 | public void testPrintMultiline() throws ParseError {
|
---|
184 | TemplateParser parser = new TemplateParser("{name}\\n{number}");
|
---|
185 | TemplateEntry entry = parser.parse();
|
---|
186 | StringBuilder sb = new StringBuilder();
|
---|
187 | entry.appendText(sb, dataProvider);
|
---|
188 | Assert.assertEquals("waypointName\n10", sb.toString());
|
---|
189 | }
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * Test to print special variables.
|
---|
193 | * @throws ParseError if the template cannot be parsed
|
---|
194 | */
|
---|
195 | @Test
|
---|
196 | public void testSpecialVariable() throws ParseError {
|
---|
197 | TemplateParser parser = new TemplateParser("{name}u{special:localName}u{special:special:key}");
|
---|
198 | TemplateEntry templateEntry = parser.parse();
|
---|
199 |
|
---|
200 | StringBuilder sb = new StringBuilder();
|
---|
201 | templateEntry.appendText(sb, dataProvider);
|
---|
202 | Assert.assertEquals("waypointNameulocalNameuspecialKey", sb.toString());
|
---|
203 | }
|
---|
204 |
|
---|
205 | @Test
|
---|
206 | public void testSearchExpression() throws Exception {
|
---|
207 | compile("(parent type=type1 type=parent1) | (parent type=type2 type=parent2)");
|
---|
208 | //"parent(type=type1,type=parent1) | (parent(type=type2,type=parent2)"
|
---|
209 | //TODO
|
---|
210 | }
|
---|
211 |
|
---|
212 | /**
|
---|
213 | * Test to switch context.
|
---|
214 | * @throws ParseError if the template cannot be parsed
|
---|
215 | */
|
---|
216 | @Test
|
---|
217 | public void testSwitchContext() throws ParseError {
|
---|
218 | TemplateParser parser = new TemplateParser("!{parent() type=parent2 '{name}'}");
|
---|
219 | DatasetFactory ds = new DatasetFactory();
|
---|
220 | Relation parent1 = ds.addRelation(1);
|
---|
221 | parent1.put("type", "parent1");
|
---|
222 | parent1.put("name", "name_parent1");
|
---|
223 | Relation parent2 = ds.addRelation(2);
|
---|
224 | parent2.put("type", "parent2");
|
---|
225 | parent2.put("name", "name_parent2");
|
---|
226 | Node child = ds.addNode(1);
|
---|
227 | parent1.addMember(new RelationMember("", child));
|
---|
228 | parent2.addMember(new RelationMember("", child));
|
---|
229 |
|
---|
230 | StringBuilder sb = new StringBuilder();
|
---|
231 | TemplateEntry entry = parser.parse();
|
---|
232 | entry.appendText(sb, child);
|
---|
233 |
|
---|
234 | Assert.assertEquals("name_parent2", sb.toString());
|
---|
235 | }
|
---|
236 |
|
---|
237 | @Test
|
---|
238 | public void testSetOr() throws ParseError {
|
---|
239 | TemplateParser parser = new TemplateParser("!{(parent(type=type1) type=parent1) | (parent type=type2 type=parent2) '{name}'}");
|
---|
240 | DatasetFactory ds = new DatasetFactory();
|
---|
241 | Relation parent1 = ds.addRelation(1);
|
---|
242 | parent1.put("type", "parent1");
|
---|
243 | parent1.put("name", "name_parent1");
|
---|
244 | Relation parent2 = ds.addRelation(2);
|
---|
245 | parent2.put("type", "parent2");
|
---|
246 | parent2.put("name", "name_parent2");
|
---|
247 | Node child1 = ds.addNode(1);
|
---|
248 | child1.put("type", "type1");
|
---|
249 | parent1.addMember(new RelationMember("", child1));
|
---|
250 | parent2.addMember(new RelationMember("", child1));
|
---|
251 | Node child2 = ds.addNode(2);
|
---|
252 | child2.put("type", "type2");
|
---|
253 | parent1.addMember(new RelationMember("", child2));
|
---|
254 | parent2.addMember(new RelationMember("", child2));
|
---|
255 |
|
---|
256 | StringBuilder sb = new StringBuilder();
|
---|
257 | TemplateEntry entry = parser.parse();
|
---|
258 | entry.appendText(sb, child1);
|
---|
259 | entry.appendText(sb, child2);
|
---|
260 |
|
---|
261 | Assert.assertEquals("name_parent1name_parent2", sb.toString());
|
---|
262 | }
|
---|
263 |
|
---|
264 | @Test
|
---|
265 | public void testMultilevel() throws ParseError {
|
---|
266 | TemplateParser parser = new TemplateParser(
|
---|
267 | "!{(parent(parent(type=type1)) type=grandparent) | (parent type=type2 type=parent2) '{name}'}");
|
---|
268 | DatasetFactory ds = new DatasetFactory();
|
---|
269 | Relation parent1 = ds.addRelation(1);
|
---|
270 | parent1.put("type", "parent1");
|
---|
271 | parent1.put("name", "name_parent1");
|
---|
272 | Relation parent2 = ds.addRelation(2);
|
---|
273 | parent2.put("type", "parent2");
|
---|
274 | parent2.put("name", "name_parent2");
|
---|
275 | Node child1 = ds.addNode(1);
|
---|
276 | child1.put("type", "type1");
|
---|
277 | parent1.addMember(new RelationMember("", child1));
|
---|
278 | parent2.addMember(new RelationMember("", child1));
|
---|
279 | Node child2 = ds.addNode(2);
|
---|
280 | child2.put("type", "type2");
|
---|
281 | parent1.addMember(new RelationMember("", child2));
|
---|
282 | parent2.addMember(new RelationMember("", child2));
|
---|
283 | Relation grandParent = ds.addRelation(3);
|
---|
284 | grandParent.put("type", "grandparent");
|
---|
285 | grandParent.put("name", "grandparent_name");
|
---|
286 | grandParent.addMember(new RelationMember("", parent1));
|
---|
287 |
|
---|
288 |
|
---|
289 | StringBuilder sb = new StringBuilder();
|
---|
290 | TemplateEntry entry = parser.parse();
|
---|
291 | entry.appendText(sb, child1);
|
---|
292 | entry.appendText(sb, child2);
|
---|
293 |
|
---|
294 | Assert.assertEquals("grandparent_namename_parent2", sb.toString());
|
---|
295 | }
|
---|
296 |
|
---|
297 | @Test(expected = ParseError.class)
|
---|
298 | public void testErrorsNot() throws ParseError {
|
---|
299 | TemplateParser parser = new TemplateParser("!{-parent() '{name}'}");
|
---|
300 | parser.parse();
|
---|
301 | }
|
---|
302 |
|
---|
303 | @Test(expected = ParseError.class)
|
---|
304 | public void testErrorOr() throws ParseError {
|
---|
305 | TemplateParser parser = new TemplateParser("!{parent() | type=type1 '{name}'}");
|
---|
306 | parser.parse();
|
---|
307 | }
|
---|
308 |
|
---|
309 | @Test
|
---|
310 | public void testChild() throws ParseError {
|
---|
311 | TemplateParser parser = new TemplateParser("!{((child(type=type1) type=child1) | (child type=type2 type=child2)) type=child2 '{name}'}");
|
---|
312 | DatasetFactory ds = new DatasetFactory();
|
---|
313 | Relation parent1 = ds.addRelation(1);
|
---|
314 | parent1.put("type", "type1");
|
---|
315 | Relation parent2 = ds.addRelation(2);
|
---|
316 | parent2.put("type", "type2");
|
---|
317 | Node child1 = ds.addNode(1);
|
---|
318 | child1.put("type", "child1");
|
---|
319 | child1.put("name", "child1");
|
---|
320 | parent1.addMember(new RelationMember("", child1));
|
---|
321 | parent2.addMember(new RelationMember("", child1));
|
---|
322 | Node child2 = ds.addNode(2);
|
---|
323 | child2.put("type", "child2");
|
---|
324 | child2.put("name", "child2");
|
---|
325 | parent1.addMember(new RelationMember("", child2));
|
---|
326 | parent2.addMember(new RelationMember("", child2));
|
---|
327 |
|
---|
328 |
|
---|
329 | StringBuilder sb = new StringBuilder();
|
---|
330 | TemplateEntry entry = parser.parse();
|
---|
331 | entry.appendText(sb, parent2);
|
---|
332 |
|
---|
333 | Assert.assertEquals("child2", sb.toString());
|
---|
334 | }
|
---|
335 | }
|
---|