-
Notifications
You must be signed in to change notification settings - Fork 0
/
Task1 (copy).java
305 lines (267 loc) · 10 KB
/
Task1 (copy).java
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import static org.junit.Assert.*;
import java.util.ArrayList;
import static org.hamcrest.CoreMatchers.*;
import org.junit.Before;
import org.junit.Test;
import st.EntryMap;
import st.TemplateEngine;
import st.EntryMap;
import st.TemplateEngine;
public class Task2 {
private EntryMap map;
private TemplateEngine engine;
@Before
public void setUp() throws Exception {
map = new EntryMap();
engine = new TemplateEngine();
}
/*////////////////////////*/
//Spec1
@Test(expected = RuntimeException.class)
public void TestEmptyTemplateF() {
map.store("", "Jimmy", false);
}
@Test(expected = RuntimeException.class)
public void TestNullTemplateF() {
map.store(null, "Jimmy", false);
}
@Test(expected = RuntimeException.class)
public void TestEmptyTemplateT() {
map.store("", "Jimmy", true);
}
@Test(expected = RuntimeException.class)
public void TestNullTemplateT() {
map.store(null, "Jimmy", true);
}
//Spec2
@Test(expected = RuntimeException.class)
public void TestNullReplaceF() {
map.store("name", null, false);
}
@Test(expected = RuntimeException.class)
public void TestNullReplaceT() {
map.store("name", null, true);
}
//Spec3
@Test
public void TestNullFlag() {
map.store("name", "Adam", null);
map.store("surname", "Dykes", null);
String result = engine.evaluate("Hello ${NaMe} ${SuRnAmE}", map,"delete-unmatched");
assertEquals("Hello Adam Dykes", result);
}
@Test
public void TestOptionalFlag() {
map.store("name", "Adam");
map.store("surname", "Dykes");
String result = engine.evaluate("Hello ${NaMe} ${SuRnAmE}", map,"delete-unmatched");
assertEquals("Hello Adam Dykes", result);
}
//Spec4
@Test
public void TestOrder(){
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);
}
//Spec5
@Test
public void TestDuplicateEntriesF() {
map.store("name", "Adam", false);
map.store("name", "Adam", false);
assertEquals(map.getEntries().size(), 1);
}
@Test
public void TestDuplicateEntriesT() {
map.store("name", "Adam", true);
map.store("name", "Adam", true);
assertEquals(map.getEntries().size(), 1);
}
@Test
public void TestDuplicateEntriesDifferentFlags() {
map.store("name", "Adam", false);
map.store("name", "Adam", true);
assertEquals(map.getEntries().size(), 2);
}
/////////////////////////////
//////////ENGINE/////////////
/////////////////////////////
// Spec 1
@Test
public void Spec1TempNullDU(){
// 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 Spec1TempEmptyDU(){
// 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);
}
@Test
public void Spec1TempNullKU(){
// 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,"keep-unmatched");
assertEquals(null,result);
}
@Test
public void Spec1TempEmptyKU(){
// 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,"keep-unmatched");
assertEquals("",result);
}
// Spec 2
@Test
public void Spec2MapNulDUl(){
// 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);
}
@Test
public void Spec2MapNulKUl(){
// Entry Map can be NULL
// Unchanged string should be returned.
String result = engine.evaluate("Hello ${name} ${Name}", null,"keep-unmatched");
assertEquals("Hello ${name} ${Name}",result);
}
// Spec 3
@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 EMPTY -> 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("$", "loves", true);
map.store("hahahaha hohohohoho", "cheesy", true);
map.store("\n", "bangos", false);
String result = engine.evaluate("${$} ${hahahaha hohohohoho} ${\n}", map,"keep-unmatched");
assertEquals("loves cheesy bangos",result);
}
@Test
public void Spec4_2(){
// 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(){
map.store("Name surname", "Dykes", false);
String result = engine.evaluate("${Name surname} ${Namesurname} ${Name surname}", map,"keep-unmatched");
assertEquals("Dykes Dykes Dykes",result);
}
// Spec 6
@Test
public void Spec6(){
// Everything should be between ${ and }
// Templates boundaries are omitted
map.store("${", "Adam", true);
map.store("$", "loves", true);
map.store("hahahaha hohohohoho", "cheesy", true);
map.store("\n", "bangos", false);
String result = engine.evaluate("${${} ${$} ${hahahaha hohohohoho} ${\n}", map,"keep-unmatched");
assertEquals("Adam loves cheesy bangos",result);
}
@Test
public void Spec6_2(){
// 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);
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);
String result = engine.evaluate("Hello ${name} ${sur ${type}} ${type} ${tt}", map,"delete-unmatched");
assertEquals("Hello Adam Dykes Brown ",result);
}
}