-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTemplateEngineTest(John).java
More file actions
211 lines (180 loc) · 7.05 KB
/
TemplateEngineTest(John).java
File metadata and controls
211 lines (180 loc) · 7.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import org.junit.Before;
import org.junit.Test;
import st.EntryMap;
import st.TemplateEngine;
public class TemplateEngineTest {
private EntryMap map;
private TemplateEngine engine;
@Before
public void setUp() throws Exception {
map = new EntryMap();
engine = new TemplateEngine();
}
@Test
public void Test1() {
map.store("name", "Adam", false);
map.store("surname", "Dykes", false);
String result = engine.evaluate("Hello ${name} ${surname}", map,"delete-unmatched");
assertEquals("Hello Adam Dykes", result);
}
@Test
public void Test2() {
map.store("name", "Adam", false);
map.store("surname", "Dykes", false);
map.store("age", "29", false);
String result = engine.evaluate("Hello ${name}, is your age ${age ${symbol}}", map,"delete-unmatched");
assertEquals("Hello Adam, is your age 29", result);
}
//////////////////////////////////////////////////////////////////////////////////
@Test
public void SpecAllOK(){
//OK get message right
map.store("name", "Adam", true);
map.store("Name", "Dykes", true);
String result = engine.evaluate("Hello ${name} ${Name ${type}}", map,"keep-unmatched");
assertEquals("Hello Adam ${Name ${type}}",result);
}
// Spec 1
@Test
public void Spec1TempNull(){
// Template can be NULL
// Unchanged string should be returned (in this case null is returned back)
map.store("name", "Adam", true);
map.store("Name", "Dykes", true);
String result = engine.evaluate(null, map,"delete-unmatched");
assertEquals(null,result);
}
@Test
public void Spec1TempEmpty(){
// Template can be empty
// Unchanged string should be returned (in this case <empty string> returned back)
map.store("name", "Adam", true);
map.store("Name", "Dykes", true);
String result = engine.evaluate("", map,"delete-unmatched");
assertEquals("",result);
}
// Spec 2
@Test
public void Spec2MapNull(){
// Entry Map can be NULL
// Unchanged string should be returned.
String result = engine.evaluate("Hello ${name} ${Name}", null,"delete-unmatched");
assertEquals("Hello ${name} ${Name}",result);
}
// Spec 3
// See SpecAllOK test case to see how it looks like how the keep-unmatched string looks like
// In that case name is not matched but in delete-unmatched case everything is matched perfectly
@Test
public void Spec3MatchNull(){
// Matching mode cannot be NULL -> default = delete-unmatched
map.store("name", "Adam", true);
map.store("Name", "Dykes", true);
String result = engine.evaluate("Hello ${name} ${Name ${type}}", map,null);
assertEquals("Hello Adam Dykes",result);
}
@Test
public void Spec3MatchEmpty(){
// Matching mode cannot be NULL -> default = delete-unmatched
map.store("name", "Adam", true);
map.store("Name", "Dykes", true);
String result = engine.evaluate("Hello ${name} ${Name ${type}}", map,"");
assertEquals("Hello Adam Dykes",result);
}
@Test
public void Spec3MatchWrongText(){
// Matching mode cannot be NULL -> default = delete-unmatched
map.store("name", "Adam", true);
map.store("Name", "Dykes", true);
String result = engine.evaluate("Hello ${name} ${Name ${type}}", map,"delete-unmched");
//Saving them as case insensitive these 2 are considered the same
assertEquals("Hello Adam Dykes",result);
}
// Spec 4
@Test
public void Spec4(){
// Everything should be between ${ and }
// Templates boundaries are omitted
map.store("name", "Adam", true);
map.store("Name", "Dykes", false);
String result = engine.evaluate("Hello ${name} ${Name}", map,"keep-unmatched");
assertEquals("Hello Adam Dykes",result);
}
// Spec 5
@Test
public void Spec5IgnoreNonChar(){
// ${middle name} = ${middlename}
map.store("name", "Adam", true);
map.store("Name suname", "Dykes", false);
String result = engine.evaluate("Hello ${name} ${Name surname}", map,"keep-unmatched");
assertEquals("Hello Adam Dykes",result);
}
// Spec 6
@Test
public void Spec6(){
// Nested ${${}} check
map.store("name", "Adam", true);
map.store("sur Brown orange name", "Dykes", true);
map.store("colour2","orange", false);
map.store("colour","Brown", false);
String result = engine.evaluate("Hello ${name} ${sur ${colour} ${colour2} name}", map,"keep-unmatched");
assertEquals("Hello Adam Dykes",result);
}
// Spec 7
@Test
public void Spec7(){
// Shorter templates are processed first
// Template order:
// 1. ${n}
// 2.${sur${n} (left-right)
// 3.${bur${n}} (left-right)
//
// n - gets replaced first with ns
//
// News Templates:
// 1. ${surns} (left-right)
// 2. ${burns} (left-right)
//
// surns - gets replaced with Jameson
// burns - gets replaced with Browns
//
// If it was any other order (left-right or big-small) sur would get
// replaced first with Smith and the new template to be matched would
// have to be Smithns. Which proves that the order is small to big.
map.store("surns","Jameson",false);
map.store("sur","Smith",false);
map.store("bur","Brown", false);
map.store("buns","Browns", false);
map.store("n","ns", false);
String result = engine.evaluate("Hello ${sur${n}} ${bu${n}}", map,"delete-unmatched");
assertEquals("Hello Jameson Browns",result);
}
// Spec 8
@Test
public void Spec8KeepUnmatched(){
// Engine processes one template at a time and attempts to match it against the keys of the entry map
// until there is a match or the entry list is exhausted
map.store("name", "Adam", true);
map.store("sur Brown", "Dykes", true);
map.store("type","Brown", false);
//map.store(pattern, value, caseSensitive);
String result = engine.evaluate("Hello ${name} ${sur ${type}} ${type} ${tt}", map,"keep-unmatched");
assertEquals("Hello Adam Dykes Brown ${tt}",result);
}
@Test
public void Spec8DeleteUnmatched(){
// Engine processes one template at a time and attempts to match it against the keys of the entry map
// until there is a match or the entry list is exhausted
map.store("name", "Adam", true);
map.store("sur Brown", "Dykes", true);
map.store("type","Brown", false);
//map.store(pattern, value, caseSensitive);
String result = engine.evaluate("Hello ${name} ${sur ${type}} ${type} ${tt}", map,"delete-unmatched");
assertEquals("Hello Adam Dykes Brown ",result);
}
// Spec EMpty
@Test
public void SpecEmpty(){
}
}