-
Notifications
You must be signed in to change notification settings - Fork 56
/
AssertTest.cfc
596 lines (503 loc) · 19.5 KB
/
AssertTest.cfc
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
<!---
MXUnit TestCase Template
@author
@description
@history
--->
<cfcomponent extends="mxunit.framework.TestCase">
<!--- Begin Specific Test Cases --->
<cffunction name="testAssertEqualsCaseShouldPassOnSameStrings">
<cfscript>
var expected = 'a QuicK BrOWN foX';
var actual = 'a QuicK BrOWN foX';
assertEqualsCase( expected , actual );
</cfscript>
</cffunction>
<cffunction name="testAssertEqualsCaseShouldFailOnDifferentCasedStrings">
<cfscript>
var expected = 'a quick brown fox';
var actual = 'A QUICK BROWN FOX';
try{
assertEqualsCase( expected , actual );
fail('should not get here');
}
catch(mxunit.exception.AssertionFailedError e){
}
</cfscript>
</cffunction>
<cffunction name="testAssert">
<cfscript>
var foo = [1,2,{foo='bar'}];
assert(true,"should pass");
try{
assert (false);
}
catch(mxunit.exception.AssertionFailedError e){
//ok we're good
// caught expected exception : assert(false)
}
assert(1 eq 1);
try{
assert (1 eq 0);
}
catch(mxunit.exception.AssertionFailedError e){
//ok we're good
// caught expected exception : assert(1 eq 0)
}
try{
assert (foo);
}
catch(Application e){
// should not allow complex objects. But what's the exception type here, CF?
}
try{
assert ('some string');
}
catch(Expression e){
// should not allow strings, either;
}
// assert (_MIN lt _MAX , "@see setUp() for system MIN and MAX values.");
</cfscript>
</cffunction>
<cffunction name="testGetStringValue">
<cfscript>
var trace = "";
var foo = structNew();
var foo2 = structNew();
var a = arrayNew(1);
a[1] = myComponent3;
a[2] = myComponent4;
foo2.bar = a;
foo.bar = myComponent3;
foo.bar2 = myComponent4;
foo.bar3 = foo2;
writeoutput(getStringValue(foo)) ;
exp = "test stringValue output from ComparatorTestData.cfc";
assertEquals(exp, getStringValue(myComponent3), "getStringValue(myComponent3) problem");
</cfscript>
</cffunction>
<cffunction name="testAssertEqualsCFUnitFailure" returntype="void" hint="">
<cfset setTestStyle("cfunit") />
<cftry>
<!--- do something here to cause an error --->
<cfset assertEquals("message",1,2) />
<cfcatch type="mxunit.exception.AssertionFailedError"><!--- we want this! ---></cfcatch>
<cfcatch type="any"><cfrethrow></cfcatch>
</cftry>
</cffunction>
<cffunction name="testFailNotEqualsCFUnit" returntype="void" hint="">
<cfset setTestStyle("cfunit") />
<cftry>
<!--- do something here to cause an error --->
<cfset failNotEquals(1,2,"my message",false) />
<cfcatch type="mxunit.exception.AssertionFailedError">
<cfif not (
(find("[1]",cfcatch.message) OR find("[2]",cfcatch.message))
or (find("[1.0]",cfcatch.message) OR find("[2.0]",cfcatch.message))
)>
<cfset fail("Should've had [1] and [2] in the throw message but instead the message was #cfcatch.message#") />
</cfif>
</cfcatch>
</cftry>
</cffunction>
<cffunction name="testFailNotEquals">
<cftry>
<!--- do something here to cause an error --->
<cfset failNotEquals(1,2,"my message") />
<cfcatch type="mxunit.exception.AssertionFailedError">
<cfif not (
(find("[1]",cfcatch.message) OR find("[2]",cfcatch.message))
or (find("[1.0]",cfcatch.message) OR find("[2.0]",cfcatch.message))
)>
<cfset fail("Should've had [1] and [2] in the throw message but instead the message was #cfcatch.message#") />
</cfif>
</cfcatch>
</cftry>
</cffunction>
<cffunction name="failNotEqualsShouldAllowInvalidXMLCharsInActual">
<!--- this was added to test http://jira.mxunit.org/browse/MXUI-12
It passes using the html runner, but fails with an org.xml.sax.SAXParseException: Premature end of file.
with the Eclipse plugin --->
<cftry>
<cfset failNotEquals(1,chr(30),"my message") />
<cfcatch type="mxunit.exception.AssertionFailedError">
<cfif not (
find("[1]",cfcatch.message)
OR find("[1.0]",cfcatch.message)
) OR not find(chr(30),cfcatch.message)>
<cfset fail("Should've had [1] and #chr(30)# in the throw message but instead the message was #cfcatch.message#") />
</cfif>
</cfcatch>
</cftry>
</cffunction>
<cffunction name="testFailEquals" returntype="void" hint="tests error path">
<cftry>
<!--- do something here to cause an error --->
<cfset failEquals(1,2,"my message") />
<cfcatch type="mxunit.exception.AssertionFailedError">
<cfif not (
(find("[1]",cfcatch.message) OR find("[2]",cfcatch.message))
or (find("[1.0]",cfcatch.message) OR find("[2.0]",cfcatch.message))
)>
<cfset fail("Should've had [1] and [2] in the throw message but instead the message was #cfcatch.message#") />
</cfif>
</cfcatch>
</cftry>
</cffunction>
<cffunction name="testFailEqualsCFUnit" returntype="void" hint="tests error path">
<cfset setTestStyle("cfunit") />
<cftry>
<!--- do something here to cause an error --->
<cfset failEquals(1,2,false) />
<cfcatch type="mxunit.exception.AssertionFailedError">
<cfif not (
(find("[1]",cfcatch.message) OR find("[2]",cfcatch.message))
or (find("[1.0]",cfcatch.message) OR find("[2.0]",cfcatch.message))
)>
<cfset fail("Should've had [1] and [2] in the throw message but instead the message was #cfcatch.message#") />
</cfif>
</cfcatch>
</cftry>
</cffunction>
<cffunction name="testAssertEquals" access="public" returntype="void">
<cfscript>
assertEquals(myComponent4, myComponent3,"stringValue() implemented"); //
</cfscript>
</cffunction>
<cffunction name="whenAssertEqualsFailsForStringsExpectedAndActualValuesShouldBePopulated">
<cftry>
<cfset assertEquals("Please Excuse me sir","Please Excuse me madam") />
<cfcatch type="mxunit.exception.AssertionFailedError">
<cfset assertEquals("Please Excuse me sir", getExpected()) />
<cfset assertEquals("Please Excuse me madam", getActual()) />
</cfcatch>
</cftry>
</cffunction>
<cffunction name="whenAssertEqualsPassesExpectedAndActualValuesShouldBeEmpty">
<!--- trigger the assertion --->
<cfset assertEquals("one","one") />
<!--- now do the real assertions for this test --->
<cfset assertTrue(getExpected() eq "","getExpected() should have been empty but was #getExpected()#") />
<cfset assertTrue(getActual() eq "", "getActual() should have been empty but was #getActual()#") />
</cffunction>
<cffunction name="testAssertTrue" access="public" returntype="void">
<cfscript>assertTrue(true,"This test should pass.");</cfscript>
</cffunction>
<cffunction name="testAssertFalse" access="public" returntype="void"><cfset assertFalse(false,"this test should pass") /></cffunction>
<cffunction name="testAssertFalseFailure">
<cftry>
<cfset assertFalse(true,"this should fail") />
<cfthrow message="should not get here" />
<cfcatch type="mxunit.exception.AssertionFailedError"></cfcatch>
</cftry>
</cffunction>
<cffunction name="testAssertFailures" access="public" returntype="void">
<cftry>
<cfscript>
assertEquals(myComponent1,myComponent2,"This should fail because toString() or stringValue() not implemented.");
assertEquals(1,2,"This should fail");
</cfscript>
<cfcatch type="mxunit.exception.AssertionFailedError" />
</cftry>
</cffunction>
<cffunction name="testFailure" access="public" returntype="void">
This tests an intentional failure. It should catch the exception correctly and return true.
<cftry>
<cfset fail("Did not catch AssertionFailedError") />
<cfcatch type="mxunit.exception.AssertionFailedError" />
</cftry>
</cffunction>
<cffunction name="testAssertEqualsNumbers">
<!--- these should all fail if they are not equals --->
<cfset assertEquals(1,1,"boo") />
<cfset assertEquals(1.0,1) />
<cfset assertEquals(1000000000000.0,1000000000000) />
<cfset assertEquals(-5,-5.0) />
<cfset assertEquals(-100.222,-100.222) />
<cfset assertEquals("1",1) />
<cfset assertEquals(2,"2") />
<cfset assertEquals("2.222",2.222) />
</cffunction>
<cffunction name="testAssertEqualsNumbersFailures">
<cftry>
<cfset assertEquals(1,2) />
<cfthrow type="other" message="should throw an AssertionFailedError before this one" />
<cfcatch type="mxunit.exception.AssertionFailedError"></cfcatch>
</cftry>
<cftry>
<cfset assertEquals(121.000001,121.000) />
<cfthrow type="other" message="should throw an AssertionFailedError before this one" />
<cfcatch type="mxunit.exception.AssertionFailedError"></cfcatch>
</cftry>
<cftry>
<cfset assertEquals(1,"1.1") />
<cfthrow type="other" message="should throw an AssertionFailedError before this one" />
<cfcatch type="mxunit.exception.AssertionFailedError"></cfcatch>
</cftry>
<cftry>
<cfset assertEquals(-1,0) />
<cfthrow type="other" message="should throw an AssertionFailedError before this one" />
<cfcatch type="mxunit.exception.AssertionFailedError"></cfcatch>
</cftry>
<cftry>
<cfset assertEquals(-1,-.9999999999999) />
<cfthrow type="other" message="should throw an AssertionFailedError before this one" />
<cfcatch type="mxunit.exception.AssertionFailedError"></cfcatch>
</cftry>
</cffunction>
<cffunction name="testAssertEqualsStrings" access="public" returntype="void">
<cfset debug(getTestStyle()) />
<cfset assertEquals("one","one" , "Should pass!") />
<cfset assertEquals("#repeatString('aaaaaa ',50)#","#repeatString('aaaaaa ',50)#") />
<cfset assertEquals("One","ONE","case sensitivity shouldn't matter when comparing strings") />
</cffunction>
<cffunction name="testAssertEqualsStringsFailures">
<cftry>
<cfset assertEquals("ONE","ONE ") />
<cfthrow type="other" message="should throw an AssertionFailedError before this one" />
<cfcatch type="mxunit.exception.AssertionFailedError"></cfcatch>
</cftry>
<cftry>
<cfset assertEquals("ONE"," ONE") />
<cfthrow type="other" message="should throw an AssertionFailedError before this one" />
<cfcatch type="mxunit.exception.AssertionFailedError"></cfcatch>
</cftry>
</cffunction>
<cffunction name="testAssertEqualsStructs">
<cfset var s1 = StructNew() />
<cfset var s2 = StructNew() />
<cfset assertEquals(s1,s2) />
<cfset s1.one = "one" />
<cfset s2.one = "one" />
<cfset assertEquals(s1,s2) />
<cfset s1.two = ArrayNew(1) />
<cfset s2.two = ArrayNew(1) />
<cfset assertEquals(s1,s2) />
<cfset s1.two[1] = "one" />
<cfset s2.two[1] = "one" />
<cfset assertEquals(s1,s2) />
</cffunction>
<cffunction name="testAssertEqualsStructsDeepComparison">
<cfset var s1 = StructNew() />
<cfset var s2 = StructNew() />
<cfset var rand = "" />
<cfset var i = "" />
<cfset var key = "" />
<cfloop from="1" to="100" index="i"><cfset s1["a"&i] = "b_#i#" />
<cfset s2["a"&i] = "b_#i#" /></cfloop>
<cfset assertEquals(s1,s2) />
<cfset rand = getTickCount() />
<cfloop collection="#s1#" item="key">
<cfset s1[key] = StructNew() />
<cfset s1[key][key] = rand />
<cfset s2[key] = StructNew() />
<cfset s2[key][key] = rand />
</cfloop>
<cfset assertEquals(s1,s2) />
<cfset s2[key][key] = rand & "boo" />
<cftry>
<cfset assertEquals(s1,s2) />
<cfthrow type="other" message="should throw an AssertionFailedError before this one" />
<cfcatch type="mxunit.exception.AssertionFailedError"></cfcatch>
</cftry>
</cffunction>
<cffunction name="testAssertEqualsStructsFailures">
<cfset var s1 = StructNew() />
<cfset var s2 = StructNew() />
<cfset s1.one = "one" />
<cfset s2.one = "two" />
<cftry>
<cfset assertEquals(s1,s2) />
<cfthrow type="other" message="should throw an AssertionFailedError before this one" />
<cfcatch type="mxunit.exception.AssertionFailedError"></cfcatch>
</cftry>
<cfset s1.two = ArrayNew(1) />
<cfset s2.two = ArrayNew(1) />
<cftry>
<cfset assertEquals(s1,s2) />
<cfthrow type="other" message="should throw an AssertionFailedError before this one" />
<cfcatch type="mxunit.exception.AssertionFailedError"></cfcatch>
</cftry>
<cfset s1.two[1] = "one" />
<cfset s2.two[1] = "two" />
<cftry>
<cfset assertEquals(s1,s2) />
<cfthrow type="other" message="should throw an AssertionFailedError before this one" />
<cfcatch type="mxunit.exception.AssertionFailedError"></cfcatch>
</cftry>
</cffunction>
<cffunction name="assertStructEquals_succeeds_for_matching_nested_structs" returntype="void">
<cfset var nested = {one=1, 2="two" }>
<cfset var struct1 = { one="one", two="two", three="3", nested = nested }>
<cfset var struct2 = duplicate( struct1 )>
<cfset assertEquals( struct1, struct2 )>
</cffunction>
<cffunction name="assertStructEquals_fails_for_mismatching_simple_structs" returntype="void">
<cfset var nested = {one=1, 2="too" }>
<cfset var struct1 = { one="one", two="two", three="3", nested=nested }>
<cfset var struct2 = duplicate( struct1 )>
<cfset struct2.one = 1>
<cfset struct2.nested["2"] = "two">
<cftry>
<cfset assertEquals( struct1, struct2 )>
<cfcatch type="mxunit.exception.AssertionFailedError">
<!--- we want this failure --->
<cfset debug(cfcatch)>
</cfcatch>
</cftry>
</cffunction>
<cffunction name="assertStructEquals_fails_for_mismatching_nested_structs" returntype="void">
<cfset var nested = {one=1, 2="two" }>
<cfset var struct1 = { one="one", two="two", three="3", nested=nested }>
<cfset var struct2 = duplicate( struct1 )>
<cfset struct2.nested.one = "one">
<cftry>
<cfset assertEquals( struct1, struct2 )>
<cfcatch type="mxunit.exception.AssertionFailedError">
<!--- we want this failure --->
<cfset debug(cfcatch)>
</cfcatch>
</cftry>
</cffunction>
<cffunction name="assertQueryEquals_succeeds_for_equal_queries" returntype="void">
<cfset var q1 = "">
<cfset var q2 = "">
<cf_querysim>
q1
col1,col2,col3,col4
1|1.2|1.3|1.4
2|2.2|2.3|2.4
3|3.2|3.3|3.4
</cf_querysim>
<cf_querysim>
q2
col4,col2,col3,col1
1.4|1.2|1.3|1
2.4|2.2|2.3|2
3.4|3.2|3.3|3
</cf_querysim>
<cfset assertEquals( q1, q2, "These queries should have been equal even though they were created with columns in a different order")>
</cffunction>
<cffunction name="assertQueryEquals_fails_for_mismatching_queries" returntype="void">
<cfset var q1 = "">
<cfset var q2 = "">
<cf_querysim>
q1
col1,col2,col3,col4
1|1.2|1.3|1.4
2|2.2|2.3|2.4
3|3.2|3.3|3.4
</cf_querysim>
<cf_querysim>
q2
col1,col2,col3,col4
4|1.2|1.3|1.a
6|2.2|2.3|2.b
9|3.2|3.3|3.c
10|3.6|3.0|3.d
</cf_querysim>
<cftry>
<cfset assertEquals( q1, q2, "These queries should have been equal even though they were created with columns in a different order")>
<cfcatch type="mxunit.exception.AssertionFailedError" >
<!--- we want this failure --->
<cfset debug(cfcatch)>
</cfcatch>
</cftry>
</cffunction>
<cffunction name="testNormalizeArgumentsDefaultEquals">
<cfset var asserttype = "equals" />
<cfset var args = structnew() />
<cfset var newargs = "" />
<cfset args.expected = "1" />
<cfset args.actual = "2" />
<cfset args.message = "message" />
<cfset newargs = normalizeArguments(asserttype,args) />
<cfset assertTrue(args.expected eq newargs.expected) />
<cfset assertTrue(args.actual eq newargs.actual) />
<cfset assertTrue(args.message eq newargs.message) />
</cffunction>
<cffunction name="testNormalizeArgumentsDefaultTrue">
<cfset var asserttype = "true" />
<cfset var args = structnew() />
<cfset var newargs = "" />
<cfset args.condition = false />
<cfset args.message = "message" />
<cfset newargs = normalizeArguments(asserttype,args) />
<cfset assertTrue(args.condition eq newargs.condition) />
<cfset assertTrue(args.message eq newargs.message) />
</cffunction>
<cffunction name="testNormalizeArgumentsDefaultXMLDoc">
<cfset var asserttype = "isxmldoc" />
<cfset var args = structnew() />
<cfset var newargs = "" />
<cfset args.xml = "<myxml>blah</myxml>" />
<cfset args.message = "message" />
<cfset newargs = normalizeArguments(asserttype,args) />
<cfset assertTrue(args.xml eq newargs.xml) />
<cfset assertTrue(args.message eq newargs.message) />
</cffunction>
<cffunction name="testNormalizeArgumentsDefaultEmptyArray">
<cfset var asserttype = "isemptyarray" />
<cfset var args = structnew() />
<cfset var newargs = "" />
<cfset args.a = ArrayNew(1) />
<cfset args.message = "message" />
<cfset newargs = normalizeArguments(asserttype,args) />
<cfset assertTrue(ArrayLen(args.a) eq ArrayLen(newargs.a)) />
<cfset assertTrue(args.message eq newargs.message) />
</cffunction>
<cffunction name="testNormalizeArgumentsCFUnitTrue">
<cfset var asserttype = "true" />
<cfset var args = structnew() />
<cfset var newargs = "" />
<cfset setTestStyle("cfunit") />
<cfset args.condition = "message" />
<cfset args.message = false />
<cfset newargs = normalizeArguments(asserttype,args) />
<cfset assertTrue(args.condition eq newargs.message) />
<cfset assertTrue(args.message eq newargs.condition) />
</cffunction>
<cffunction name="testNormalizeArgumentsCFUnitEquals">
<cfset var asserttype = "equals" />
<cfset var args = structnew() />
<cfset var newargs = "" />
<cfset var expected=2 />
<cfset var actual=2 />
<cfset setTestStyle("cfunit") />
<cfset args.expected = "1" />
<cfset args.actual = "2" />
<cfset args.message = "message" />
<cfset newargs = normalizeArguments(asserttype,args) />
<cfset assertTrue(args.expected neq newargs.expected) />
<cfset assertTrue(args.actual neq newargs.actual) />
<cfset assertTrue(args.message neq newargs.message) />
<!--- here's the tale of the tape, in a way: if this doesn't work, it'll throw an error --->
<cfset assertEquals("message",expected,actual) />
</cffunction>
<cffunction name="testDoubleHashError">
<!--- see http://jira.mxunit.org/browse/MXUI-13 --->
<cftry>
<cfset assertTrue(false, "This -> ## <- should not cause an error") />
<cfcatch type="mxunit.exception.AssertionFailedError">
<cfset assertEquals("This -> ## <- should not cause an error", cfcatch.message) />
</cfcatch>
</cftry>
</cffunction>
<!---End Specific Test Cases --->
<cffunction name="setUp" access="public" returntype="void">
<!--- doing this because some tests need to test the cfunit-style behavior and we want
to ensure that we set it back for the other tests --->
<cfset this.setTestStyle('default') />
<!--- Place additional setUp and initialization code here --->
<cfscript>
myComponent1 = createObject("component","mxunit.tests.framework.fixture.NewCFComponent");
myComponent2 = createObject("component","mxunit.tests.framework.fixture.NewCFComponent");
//below implement consistent stringValue()
myComponent3 = createObject("component","mxunit.tests.framework.fixture.ComparatorTestData");
myComponent4 = createObject("component","mxunit.tests.framework.fixture.ComparatorTestData");
_MIN = createObject("java","java.lang.Integer").MIN_VALUE;
_MAX = createObject("java","java.lang.Integer").MAX_VALUE;
</cfscript>
</cffunction>
<cffunction name="tearDown" access="public" returntype="void"><!--- Place tearDown/clean up code here ---></cffunction>
</cfcomponent>