-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask21.java
More file actions
489 lines (413 loc) · 15.2 KB
/
Task21.java
File metadata and controls
489 lines (413 loc) · 15.2 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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
package st;
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;
public class Task1 {
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);
}
//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);
}
//Additional Tests for extra coverage
//tests for equals on template engine.
@Test
public void twoequalTemplates(){
TemplateEngine.Template a = engine.new Template(null,null,null);
Boolean result = a.equals(a);
assertEquals(true,result);
}
@Test
public void nulltwo(){
TemplateEngine.Template a = engine.new Template(null,null,null);
Boolean result = a.equals(null);
assertEquals(false,result);
}
@Test
public void twoUnequalTemplates(){
TemplateEngine.Template a = engine.new Template(null,null,null);
TemplateEngine.Template b = engine.new Template(null,10,null);
Boolean result = a.equals(b);
assertEquals(false,result);
}
@Test
public void twoUnequalTemplates2(){
TemplateEngine.Template a = engine.new Template(null,10,null);
TemplateEngine.Template b = engine.new Template(null,null,null);
Boolean result = a.equals(b);
assertEquals(false,result);
}
@Test
public void twoUnequalTemplates3(){
TemplateEngine.Template a = engine.new Template(10,10,null);
TemplateEngine.Template b = engine.new Template(10,null,null);
Boolean result = a.equals(b);
assertEquals(false,result);
}
@Test
public void twoUnequalTemplates4(){
TemplateEngine.Template a = engine.new Template(null,null,null);
TemplateEngine.Template b = engine.new Template(10,null,null);
Boolean result = a.equals(b);
assertEquals(false,result);
}
@Test
public void twoequalTemplates5(){
TemplateEngine.Template a = engine.new Template(null,null,null);
TemplateEngine.Template b = engine.new Template(null,null,null);
Boolean result = a.equals(b);
assertEquals(true,result);
}
@Test
public void twoequalTemplates6(){
TemplateEngine.Template a = engine.new Template(null,null,"hey");
TemplateEngine.Template b = engine.new Template(null,null,"hey");
Boolean result = a.equals(b);
assertEquals(true,result);
}
@Test
public void twounequalTemplates7(){
TemplateEngine.Template a = engine.new Template(null,null,null);
TemplateEngine.Template b = engine.new Template(null,null,"hey");
Boolean result = a.equals(b);
assertEquals(false,result);
}
@Test
public void twoequalTemplates8(){
TemplateEngine.Template a = engine.new Template(null,5,null);
TemplateEngine.Template b = engine.new Template(null,5,null);
Boolean result = a.equals(b);
assertEquals(true,result);
}
@Test
public void twoUnequalIndices(){
TemplateEngine.Template a = engine.new Template(5,null,null);
TemplateEngine.Template b = engine.new Template(15,null,null);
Boolean result = a.equals(b);
assertEquals(false,result);
}
@Test
public void oneTemplateCompNontemplate(){
TemplateEngine.Template a = engine.new Template(null,null,null);
String b = "hello";
Boolean result = a.equals(b);
assertEquals(false,result);
}
//Entry map equals
@Test
public void twoequalEntrys(){
EntryMap.Entry a = map.new Entry("hey","adam",null);
Boolean result = a.equals(a);
assertEquals(true,result);
}
@Test
public void nulltwoEntrys(){
EntryMap.Entry a = map.new Entry(null,null,null);
Boolean result = a.equals(null);
assertEquals(false,result);
}
@Test
public void twoUnequalEntrys(){
EntryMap.Entry a = map.new Entry("hey","adam",null);
EntryMap.Entry b = map.new Entry("hey","bob",null);
Boolean result = a.equals(b);
assertEquals(false,result);
}
@Test
public void twoUnequalEntrys2(){
EntryMap.Entry a = map.new Entry("heythere","bob",null);
EntryMap.Entry b = map.new Entry("hey","bob",null);
Boolean result = a.equals(b);
assertEquals(false,result);
}
@Test
public void twoUnequalEntrys3(){
EntryMap.Entry a = map.new Entry("hey","bob",null);
EntryMap.Entry b = map.new Entry("hey","bob",true);
Boolean result = a.equals(b);
assertEquals(false,result);
}
@Test
public void twoUnequalEntrys4(){
EntryMap.Entry a = map.new Entry("hey","bob",null);
EntryMap.Entry b = map.new Entry("hey","bob",null);
Boolean result = a.equals(b);
assertEquals(true,result);
}
@Test
public void oneEntryCompNonEntry(){
EntryMap.Entry a = map.new Entry("heythere","bob",null);
String b = "hello";
Boolean result = a.equals(b);
assertEquals(false,result);
}
//tests for hashcode
@Test
public void hashcodetest(){
TemplateEngine.Template a = engine.new Template(null,null,null);
int x = a.hashCode();
Boolean result = x==0;
assertEquals(true,result);
}
@Test
public void hashcodetest2(){
EntryMap.Entry a = map.new Entry("hey","adam",null);
int x = a.hashCode();
Boolean result = x==0;
assertEquals(false,result);
}
@Test
public void gettemplatesreplaced(){
TemplateEngine.Result a = engine.new Result(null,5);
int x = a.getTemplatesReplaced();
Boolean result = x==5;
assertEquals(true,result);
}
}