1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.tools.template_engine;
|
---|
3 |
|
---|
4 |
|
---|
5 | import java.util.Arrays;
|
---|
6 | import java.util.List;
|
---|
7 |
|
---|
8 | import org.junit.Assert;
|
---|
9 | import org.junit.BeforeClass;
|
---|
10 | import org.junit.Test;
|
---|
11 | import org.openstreetmap.josm.Main;
|
---|
12 | import org.openstreetmap.josm.actions.search.SearchCompiler;
|
---|
13 | import org.openstreetmap.josm.actions.search.SearchCompiler.Match;
|
---|
14 | import org.openstreetmap.josm.data.Preferences;
|
---|
15 | import org.openstreetmap.josm.data.osm.DatasetFactory;
|
---|
16 | import org.openstreetmap.josm.data.osm.Node;
|
---|
17 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
18 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
19 | import org.unitils.reflectionassert.ReflectionAssert;
|
---|
20 |
|
---|
21 | public class TemplateEngineTest {
|
---|
22 |
|
---|
23 | @BeforeClass
|
---|
24 | public static void before() {
|
---|
25 | Main.pref = new Preferences();
|
---|
26 | }
|
---|
27 |
|
---|
28 | @Test
|
---|
29 | public void testEmpty() throws ParseError {
|
---|
30 | TemplateParser parser = new TemplateParser("");
|
---|
31 | ReflectionAssert.assertReflectionEquals(new StaticText(""), parser.parse());
|
---|
32 | }
|
---|
33 |
|
---|
34 | @Test
|
---|
35 | public void testVariable() throws ParseError {
|
---|
36 | TemplateParser parser = new TemplateParser("abc{var}\\{ef\\$\\{g");
|
---|
37 | ReflectionAssert.assertReflectionEquals(CompoundTemplateEntry.fromArray(new StaticText("abc"), new Variable("var"), new StaticText("{ef${g")), parser.parse());
|
---|
38 | }
|
---|
39 |
|
---|
40 | @Test
|
---|
41 | public void testConditionWhitespace() throws ParseError {
|
---|
42 | TemplateParser parser = new TemplateParser("?{ '{name} {desc}' | '{name}' | '{desc}' }");
|
---|
43 | Condition condition = new Condition();
|
---|
44 | condition.getEntries().add(CompoundTemplateEntry.fromArray(new Variable("name"), new StaticText(" "), new Variable("desc")));
|
---|
45 | condition.getEntries().add(new Variable("name"));
|
---|
46 | condition.getEntries().add(new Variable("desc"));
|
---|
47 | ReflectionAssert.assertReflectionEquals(condition, parser.parse());
|
---|
48 | }
|
---|
49 |
|
---|
50 | @Test
|
---|
51 | public void testConditionNoWhitespace() throws ParseError {
|
---|
52 | TemplateParser parser = new TemplateParser("?{'{name} {desc}'|'{name}'|'{desc}'}");
|
---|
53 | Condition condition = new Condition();
|
---|
54 | condition.getEntries().add(CompoundTemplateEntry.fromArray(new Variable("name"), new StaticText(" "), new Variable("desc")));
|
---|
55 | condition.getEntries().add(new Variable("name"));
|
---|
56 | condition.getEntries().add(new Variable("desc"));
|
---|
57 | ReflectionAssert.assertReflectionEquals(condition, parser.parse());
|
---|
58 | }
|
---|
59 |
|
---|
60 | private static Match compile(String expression) throws org.openstreetmap.josm.actions.search.SearchCompiler.ParseError {
|
---|
61 | return SearchCompiler.compile(expression, false, false);
|
---|
62 | }
|
---|
63 |
|
---|
64 | @Test
|
---|
65 | public void testConditionSearchExpression() throws Exception {
|
---|
66 | TemplateParser parser = new TemplateParser("?{ admin_level = 2 'NUTS 1' | admin_level = 4 'NUTS 2' | '{admin_level}'}");
|
---|
67 | Condition condition = new Condition();
|
---|
68 | condition.getEntries().add(new SearchExpressionCondition(compile("admin_level = 2"), new StaticText("NUTS 1")));
|
---|
69 | condition.getEntries().add(new SearchExpressionCondition(compile("admin_level = 4"), new StaticText("NUTS 2")));
|
---|
70 | condition.getEntries().add(new Variable("admin_level"));
|
---|
71 | ReflectionAssert.assertReflectionEquals(condition, parser.parse());
|
---|
72 | }
|
---|
73 |
|
---|
74 | TemplateEngineDataProvider dataProvider = new TemplateEngineDataProvider() {
|
---|
75 | @Override
|
---|
76 | public Object getTemplateValue(String name, boolean special) {
|
---|
77 | if (special) {
|
---|
78 | if ("localName".equals(name))
|
---|
79 | return "localName";
|
---|
80 | else
|
---|
81 | return null;
|
---|
82 | } else {
|
---|
83 | if ("name".equals(name))
|
---|
84 | return "waypointName";
|
---|
85 | else if ("number".equals(name))
|
---|
86 | return 10;
|
---|
87 | else if ("special:key".equals(name))
|
---|
88 | return "specialKey";
|
---|
89 | else
|
---|
90 | return null;
|
---|
91 | }
|
---|
92 | }
|
---|
93 | @Override
|
---|
94 | public boolean evaluateCondition(Match condition) {
|
---|
95 | return true;
|
---|
96 | }
|
---|
97 | @Override
|
---|
98 | public List<String> getTemplateKeys() {
|
---|
99 | return Arrays.asList("name", "number");
|
---|
100 | }
|
---|
101 | };
|
---|
102 |
|
---|
103 | @Test
|
---|
104 | public void testFilling() throws Exception {
|
---|
105 | TemplateParser parser = new TemplateParser("{name} u{unknown}u i{number}i");
|
---|
106 | TemplateEntry entry = parser.parse();
|
---|
107 | StringBuilder sb = new StringBuilder();
|
---|
108 | entry.appendText(sb, dataProvider);
|
---|
109 | Assert.assertEquals("waypointName uu i10i", sb.toString());
|
---|
110 | }
|
---|
111 |
|
---|
112 | @Test
|
---|
113 | public void testFillingSearchExpression() throws Exception {
|
---|
114 | TemplateParser parser = new TemplateParser("?{ admin_level = 2 'NUTS 1' | admin_level = 4 'NUTS 2' | '{admin_level}'}");
|
---|
115 | TemplateEntry templateEntry = parser.parse();
|
---|
116 |
|
---|
117 | StringBuilder sb = new StringBuilder();
|
---|
118 | Relation r = new Relation();
|
---|
119 | r.put("admin_level", "2");
|
---|
120 | templateEntry.appendText(sb, r);
|
---|
121 | Assert.assertEquals("NUTS 1", sb.toString());
|
---|
122 |
|
---|
123 | sb.setLength(0);
|
---|
124 | r.put("admin_level", "5");
|
---|
125 | templateEntry.appendText(sb, r);
|
---|
126 | Assert.assertEquals("5", sb.toString());
|
---|
127 | }
|
---|
128 |
|
---|
129 | @Test
|
---|
130 | public void testPrintAll() throws Exception {
|
---|
131 | TemplateParser parser = new TemplateParser("{special:everything}");
|
---|
132 | TemplateEntry entry = parser.parse();
|
---|
133 | StringBuilder sb = new StringBuilder();
|
---|
134 | entry.appendText(sb, dataProvider);
|
---|
135 | Assert.assertEquals("name=waypointName, number=10", sb.toString());
|
---|
136 | }
|
---|
137 |
|
---|
138 | @Test
|
---|
139 | public void testPrintMultiline() throws Exception {
|
---|
140 | TemplateParser parser = new TemplateParser("{name}\\n{number}");
|
---|
141 | TemplateEntry entry = parser.parse();
|
---|
142 | StringBuilder sb = new StringBuilder();
|
---|
143 | entry.appendText(sb, dataProvider);
|
---|
144 | Assert.assertEquals("waypointName\n10", sb.toString());
|
---|
145 | }
|
---|
146 |
|
---|
147 | @Test
|
---|
148 | public void testSpecialVariable() throws Exception {
|
---|
149 | TemplateParser parser = new TemplateParser("{name}u{special:localName}u{special:special:key}");
|
---|
150 | TemplateEntry templateEntry = parser.parse();
|
---|
151 |
|
---|
152 | StringBuilder sb = new StringBuilder();
|
---|
153 | templateEntry.appendText(sb, dataProvider);
|
---|
154 | Assert.assertEquals("waypointNameulocalNameuspecialKey", sb.toString());
|
---|
155 | }
|
---|
156 |
|
---|
157 | @Test
|
---|
158 | public void testSearchExpression() throws Exception {
|
---|
159 | Match match = compile("(parent type=type1 type=parent1) | (parent type=type2 type=parent2)");
|
---|
160 | //"parent(type=type1,type=parent1) | (parent(type=type2,type=parent2)"
|
---|
161 | //TODO
|
---|
162 | }
|
---|
163 |
|
---|
164 | @Test
|
---|
165 | public void testSwitchContext() throws Exception {
|
---|
166 | TemplateParser parser = new TemplateParser("!{parent() type=parent2 '{name}'}");
|
---|
167 | DatasetFactory ds = new DatasetFactory();
|
---|
168 | Relation parent1 = ds.addRelation(1);
|
---|
169 | parent1.put("type", "parent1");
|
---|
170 | parent1.put("name", "name_parent1");
|
---|
171 | Relation parent2 = ds.addRelation(2);
|
---|
172 | parent2.put("type", "parent2");
|
---|
173 | parent2.put("name", "name_parent2");
|
---|
174 | Node child = ds.addNode(1);
|
---|
175 | parent1.addMember(new RelationMember("", child));
|
---|
176 | parent2.addMember(new RelationMember("", child));
|
---|
177 |
|
---|
178 | StringBuilder sb = new StringBuilder();
|
---|
179 | TemplateEntry entry = parser.parse();
|
---|
180 | entry.appendText(sb, child);
|
---|
181 |
|
---|
182 | Assert.assertEquals("name_parent2", sb.toString());
|
---|
183 | }
|
---|
184 |
|
---|
185 | @Test
|
---|
186 | public void testSetOr() throws ParseError {
|
---|
187 | TemplateParser parser = new TemplateParser("!{(parent(type=type1) type=parent1) | (parent type=type2 type=parent2) '{name}'}");
|
---|
188 | DatasetFactory ds = new DatasetFactory();
|
---|
189 | Relation parent1 = ds.addRelation(1);
|
---|
190 | parent1.put("type", "parent1");
|
---|
191 | parent1.put("name", "name_parent1");
|
---|
192 | Relation parent2 = ds.addRelation(2);
|
---|
193 | parent2.put("type", "parent2");
|
---|
194 | parent2.put("name", "name_parent2");
|
---|
195 | Node child1 = ds.addNode(1);
|
---|
196 | child1.put("type", "type1");
|
---|
197 | parent1.addMember(new RelationMember("", child1));
|
---|
198 | parent2.addMember(new RelationMember("", child1));
|
---|
199 | Node child2 = ds.addNode(2);
|
---|
200 | child2.put("type", "type2");
|
---|
201 | parent1.addMember(new RelationMember("", child2));
|
---|
202 | parent2.addMember(new RelationMember("", child2));
|
---|
203 |
|
---|
204 |
|
---|
205 | StringBuilder sb = new StringBuilder();
|
---|
206 | TemplateEntry entry = parser.parse();
|
---|
207 | entry.appendText(sb, child1);
|
---|
208 | entry.appendText(sb, child2);
|
---|
209 |
|
---|
210 | Assert.assertEquals("name_parent1name_parent2", sb.toString());
|
---|
211 | }
|
---|
212 |
|
---|
213 | @Test
|
---|
214 | public void testMultilevel() throws ParseError {
|
---|
215 | TemplateParser parser = new TemplateParser("!{(parent(parent(type=type1)) type=grandparent) | (parent type=type2 type=parent2) '{name}'}");
|
---|
216 | DatasetFactory ds = new DatasetFactory();
|
---|
217 | Relation parent1 = ds.addRelation(1);
|
---|
218 | parent1.put("type", "parent1");
|
---|
219 | parent1.put("name", "name_parent1");
|
---|
220 | Relation parent2 = ds.addRelation(2);
|
---|
221 | parent2.put("type", "parent2");
|
---|
222 | parent2.put("name", "name_parent2");
|
---|
223 | Node child1 = ds.addNode(1);
|
---|
224 | child1.put("type", "type1");
|
---|
225 | parent1.addMember(new RelationMember("", child1));
|
---|
226 | parent2.addMember(new RelationMember("", child1));
|
---|
227 | Node child2 = ds.addNode(2);
|
---|
228 | child2.put("type", "type2");
|
---|
229 | parent1.addMember(new RelationMember("", child2));
|
---|
230 | parent2.addMember(new RelationMember("", child2));
|
---|
231 | Relation grandParent = ds.addRelation(3);
|
---|
232 | grandParent.put("type", "grandparent");
|
---|
233 | grandParent.put("name", "grandparent_name");
|
---|
234 | grandParent.addMember(new RelationMember("", parent1));
|
---|
235 |
|
---|
236 |
|
---|
237 | StringBuilder sb = new StringBuilder();
|
---|
238 | TemplateEntry entry = parser.parse();
|
---|
239 | entry.appendText(sb, child1);
|
---|
240 | entry.appendText(sb, child2);
|
---|
241 |
|
---|
242 | Assert.assertEquals("grandparent_namename_parent2", sb.toString());
|
---|
243 | }
|
---|
244 |
|
---|
245 | @Test(expected=ParseError.class)
|
---|
246 | public void testErrorsNot() throws ParseError {
|
---|
247 | TemplateParser parser = new TemplateParser("!{-parent() '{name}'}");
|
---|
248 | parser.parse();
|
---|
249 | }
|
---|
250 |
|
---|
251 | @Test(expected=ParseError.class)
|
---|
252 | public void testErrorOr() throws ParseError {
|
---|
253 | TemplateParser parser = new TemplateParser("!{parent() | type=type1 '{name}'}");
|
---|
254 | parser.parse();
|
---|
255 | }
|
---|
256 |
|
---|
257 | @Test
|
---|
258 | public void testChild() throws ParseError {
|
---|
259 | TemplateParser parser = new TemplateParser("!{((child(type=type1) type=child1) | (child type=type2 type=child2)) type=child2 '{name}'}");
|
---|
260 | DatasetFactory ds = new DatasetFactory();
|
---|
261 | Relation parent1 = ds.addRelation(1);
|
---|
262 | parent1.put("type", "type1");
|
---|
263 | Relation parent2 = ds.addRelation(2);
|
---|
264 | parent2.put("type", "type2");
|
---|
265 | Node child1 = ds.addNode(1);
|
---|
266 | child1.put("type", "child1");
|
---|
267 | child1.put("name", "child1");
|
---|
268 | parent1.addMember(new RelationMember("", child1));
|
---|
269 | parent2.addMember(new RelationMember("", child1));
|
---|
270 | Node child2 = ds.addNode(2);
|
---|
271 | child2.put("type", "child2");
|
---|
272 | child2.put("name", "child2");
|
---|
273 | parent1.addMember(new RelationMember("", child2));
|
---|
274 | parent2.addMember(new RelationMember("", child2));
|
---|
275 |
|
---|
276 |
|
---|
277 | StringBuilder sb = new StringBuilder();
|
---|
278 | TemplateEntry entry = parser.parse();
|
---|
279 | entry.appendText(sb, parent2);
|
---|
280 |
|
---|
281 | Assert.assertEquals("child2", sb.toString());
|
---|
282 | }
|
---|
283 |
|
---|
284 | }
|
---|