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