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.Relation;
|
---|
16 | import org.unitils.reflectionassert.ReflectionAssert;
|
---|
17 |
|
---|
18 | public class TemplateEngineTest {
|
---|
19 |
|
---|
20 | @BeforeClass
|
---|
21 | public static void before() {
|
---|
22 | Main.pref = new Preferences();
|
---|
23 | }
|
---|
24 |
|
---|
25 | @Test
|
---|
26 | public void testEmpty() throws ParseError {
|
---|
27 | TemplateParser parser = new TemplateParser("");
|
---|
28 | ReflectionAssert.assertReflectionEquals(new StaticText(""), parser.parse());
|
---|
29 | }
|
---|
30 |
|
---|
31 | @Test
|
---|
32 | public void testVariable() throws ParseError {
|
---|
33 | TemplateParser parser = new TemplateParser("abc{var}\\{ef\\$\\{g");
|
---|
34 | ReflectionAssert.assertReflectionEquals(CompoundTemplateEntry.fromArray(new StaticText("abc"), new Variable("var"), new StaticText("{ef${g")), parser.parse());
|
---|
35 | }
|
---|
36 |
|
---|
37 | @Test
|
---|
38 | public void testConditionWhitespace() throws ParseError {
|
---|
39 | TemplateParser parser = new TemplateParser("?{ '{name} {desc}' | '{name}' | '{desc}' }");
|
---|
40 | Condition condition = new Condition();
|
---|
41 | condition.getEntries().add(CompoundTemplateEntry.fromArray(new Variable("name"), new StaticText(" "), new Variable("desc")));
|
---|
42 | condition.getEntries().add(new Variable("name"));
|
---|
43 | condition.getEntries().add(new Variable("desc"));
|
---|
44 | ReflectionAssert.assertReflectionEquals(condition, parser.parse());
|
---|
45 | }
|
---|
46 |
|
---|
47 | @Test
|
---|
48 | public void testConditionNoWhitespace() throws ParseError {
|
---|
49 | TemplateParser parser = new TemplateParser("?{'{name} {desc}'|'{name}'|'{desc}'}");
|
---|
50 | Condition condition = new Condition();
|
---|
51 | condition.getEntries().add(CompoundTemplateEntry.fromArray(new Variable("name"), new StaticText(" "), new Variable("desc")));
|
---|
52 | condition.getEntries().add(new Variable("name"));
|
---|
53 | condition.getEntries().add(new Variable("desc"));
|
---|
54 | ReflectionAssert.assertReflectionEquals(condition, parser.parse());
|
---|
55 | }
|
---|
56 |
|
---|
57 | private static Match compile(String expression) throws org.openstreetmap.josm.actions.search.SearchCompiler.ParseError {
|
---|
58 | return SearchCompiler.compile(expression, false, false);
|
---|
59 | }
|
---|
60 |
|
---|
61 | @Test
|
---|
62 | public void testConditionSearchExpression() throws Exception {
|
---|
63 | TemplateParser parser = new TemplateParser("?{ admin_level = 2 'NUTS 1' | admin_level = 4 'NUTS 2' | '{admin_level}'}");
|
---|
64 | Condition condition = new Condition();
|
---|
65 | condition.getEntries().add(new SearchExpressionCondition(compile("admin_level = 2"), new StaticText("NUTS 1")));
|
---|
66 | condition.getEntries().add(new SearchExpressionCondition(compile("admin_level = 4"), new StaticText("NUTS 2")));
|
---|
67 | condition.getEntries().add(new Variable("admin_level"));
|
---|
68 | ReflectionAssert.assertReflectionEquals(condition, parser.parse());
|
---|
69 | }
|
---|
70 |
|
---|
71 | TemplateEngineDataProvider dataProvider = new TemplateEngineDataProvider() {
|
---|
72 | @Override
|
---|
73 | public Object getTemplateValue(String name, boolean special) {
|
---|
74 | if (special) {
|
---|
75 | if ("localName".equals(name))
|
---|
76 | return "localName";
|
---|
77 | else
|
---|
78 | return null;
|
---|
79 | } else {
|
---|
80 | if ("name".equals(name))
|
---|
81 | return "waypointName";
|
---|
82 | else if ("number".equals(name))
|
---|
83 | return 10;
|
---|
84 | else if ("special:key".equals(name))
|
---|
85 | return "specialKey";
|
---|
86 | else
|
---|
87 | return null;
|
---|
88 | }
|
---|
89 | }
|
---|
90 | @Override
|
---|
91 | public boolean evaluateCondition(Match condition) {
|
---|
92 | return true;
|
---|
93 | }
|
---|
94 | @Override
|
---|
95 | public List<String> getTemplateKeys() {
|
---|
96 | return Arrays.asList("name", "number");
|
---|
97 | }
|
---|
98 | };
|
---|
99 |
|
---|
100 | @Test
|
---|
101 | public void testFilling() throws Exception {
|
---|
102 | TemplateParser parser = new TemplateParser("{name} u{unknown}u i{number}i");
|
---|
103 | TemplateEntry entry = parser.parse();
|
---|
104 | StringBuilder sb = new StringBuilder();
|
---|
105 | entry.appendText(sb, dataProvider);
|
---|
106 | Assert.assertEquals("waypointName uu i10i", sb.toString());
|
---|
107 | }
|
---|
108 |
|
---|
109 | @Test
|
---|
110 | public void testFillingSearchExpression() throws Exception {
|
---|
111 | TemplateParser parser = new TemplateParser("?{ admin_level = 2 'NUTS 1' | admin_level = 4 'NUTS 2' | '{admin_level}'}");
|
---|
112 | TemplateEntry templateEntry = parser.parse();
|
---|
113 |
|
---|
114 | StringBuilder sb = new StringBuilder();
|
---|
115 | Relation r = new Relation();
|
---|
116 | r.put("admin_level", "2");
|
---|
117 | templateEntry.appendText(sb, r);
|
---|
118 | Assert.assertEquals("NUTS 1", sb.toString());
|
---|
119 |
|
---|
120 | sb.setLength(0);
|
---|
121 | r.put("admin_level", "5");
|
---|
122 | templateEntry.appendText(sb, r);
|
---|
123 | Assert.assertEquals("5", sb.toString());
|
---|
124 | }
|
---|
125 |
|
---|
126 | @Test
|
---|
127 | public void testPrintAll() throws Exception {
|
---|
128 | TemplateParser parser = new TemplateParser("{special:everything}");
|
---|
129 | TemplateEntry entry = parser.parse();
|
---|
130 | StringBuilder sb = new StringBuilder();
|
---|
131 | entry.appendText(sb, dataProvider);
|
---|
132 | Assert.assertEquals("name=waypointName, number=10", sb.toString());
|
---|
133 | }
|
---|
134 |
|
---|
135 | @Test
|
---|
136 | public void testPrintMultiline() throws Exception {
|
---|
137 | TemplateParser parser = new TemplateParser("{name}\\n{number}");
|
---|
138 | TemplateEntry entry = parser.parse();
|
---|
139 | StringBuilder sb = new StringBuilder();
|
---|
140 | entry.appendText(sb, dataProvider);
|
---|
141 | Assert.assertEquals("waypointName\n10", sb.toString());
|
---|
142 | }
|
---|
143 |
|
---|
144 | @Test
|
---|
145 | public void testSpecialVariable() throws Exception {
|
---|
146 | TemplateParser parser = new TemplateParser("{name}u{special:localName}u{special:special:key}");
|
---|
147 | TemplateEntry templateEntry = parser.parse();
|
---|
148 |
|
---|
149 | StringBuilder sb = new StringBuilder();
|
---|
150 | templateEntry.appendText(sb, dataProvider);
|
---|
151 | Assert.assertEquals("waypointNameulocalNameuspecialKey", sb.toString());
|
---|
152 |
|
---|
153 | }
|
---|
154 |
|
---|
155 |
|
---|
156 | }
|
---|