forked from rcdmk/classicAspUnit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aspunit.class.asp
851 lines (652 loc) · 18.5 KB
/
aspunit.class.asp
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
<%
' aspUnit testing framework class v0.9
' By RCDMK - rcdmk@rcdmk.com
'
' The MIT License (MIT)
' Copyright (c) 2012 RCDMK - rcdmk@rcdmk.com
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software
' and associated documentation files (the "Software"), to deal in the Software without restriction,
' including without limitation the rights to use, copy, modify, merge, publish, distribute,
' sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
' furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or
' substantial portions of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
' BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
' NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
' DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
' OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
' Constants
const AU_ASSERT_TRUE = 0
const AU_ASSERT_FALSE = 1
const AU_ASSERT_EXISTS = 2
const AU_ASSERT_NULL = 3
const AU_ASSERT_NOT_NULL = 4
const AU_ASSERT_EMPTY = 5
const AU_ASSERT_NOT_EMPTY = 6
const AU_ASSERT_EQUALS = 7
const AU_ASSERT_NOT_EQUALS = 8
const AU_ASSERT_IS_A = 9
const AU_ASSERT_IS_VALID = 10
const AU_ASSERT_IS_NOT_VALID = 11
const AU_ERROR_TEST_CASE_ALREADY_EXISTS = &h800a01c9
const AU_ERROR_TEST_ALREADY_EXISTS = &h800a01c9
const AU_ERROR_TEST_METHOD_ALREADY_EXISTS = &h800a01c9
' Classes
class aspUnit
' fields
dim dTestCases
' constructor and destructor
private sub class_initialize()
set dTestCases = createObject("Scripting.Dictionary")
end sub
private sub class_terminate()
dim testCase
for each testCase in dTestCases.keys
set dTestCases(testCase) = nothing
dTestCases.remove testCase
next
set dTestCases = nothing
end sub
' public methods
public function AddTestCase(byval testCaseName)
dim testCase
if not dTestCases.Exists(testCaseName) then
set testCase = new aspUnitTestCase
testCase.Name = testCaseName
dTestCases.Add testCaseName, testCase
else
err.Raise AU_ERROR_TEST_CASE_ALREADY_EXISTS, "Test Case already exists", "A test case with this name aready exists: """ & testCaseName & """"
end if
set AddTestCase = testCase
end function
public function Run()
dim results, testCase
set results = new aspUnitTestResult
for each testCase in dTestCases.Items
testCase.Run
results.TestCases.Add testCase
next
set Run = results
end function
end class
class aspUnitTestCase
' fields
dim sName, sStatus
dim dTests
dim setupCode, tearDowncode
' properties
public property get Name()
Name = sName
end property
public property let Name(value)
sName = value
end property
public property get Status()
Status = sStatus
end property
public property get Tests()
set Tests = dTests
end property
' constructor and destructor
private sub class_initialize()
set dTests = createObject("Scripting.Dictionary")
sStatus = "Inconclusive"
end sub
private sub class_terminate()
dim test
for each test in dTests.keys
set dTests(test) = nothing
dTests.remove test
next
set dTests = nothing
end sub
' public methods
public sub Setup(byval setupCallbackCode)
setupCode = setupCallbackCode
end sub
public sub Teardown(byval terardownCallbackCode)
tearDowncode = terardownCallbackCode
end sub
public function AddTest(byval testName)
dim test
set test = new aspUnitTestMethod
test.Name = testName
if not dTests.exists(testName) then
dTests.add testName, test
else
err.Raise AU_ERROR_TEST_ALREADY_EXISTS, "Test already exists", "A test with this name already exists: """ & testName & """"
end if
set AddTest = test
end function
public sub Run()
dim passed, testResult
passed = false
if dTests.Count > 0 then
sStatus = "Passed"
on error resume next
for each test in dTests.Items
if setupCode <> "" then execute setupCode
if test.Assertions.Count > 0 then
testResult = test.Run()
if isnull(testResult) then
sStatus = "Error"
elseif not testResult then
sStatus = "Failed"
end if
elseif sStatus <> "Failed" then
sStatus = "Inconclusive"
end if
if tearDowncode <> "" then execute tearDowncode
if err <> 0 then
sStatus = "Error"
err.clear
end if
next
on error goto 0
end if
end sub
end class
class aspUnitTestMethod
' fields
dim sName, sStatus
dim cAssertions, cErrors
' properties
public property get Name()
Name = sName
end property
public property let Name(value)
sName = value
end property
public property get Status()
Status = sStatus
end property
public property get Output()
dim sOutput
if cAssertions.Count > 0 then
if cErrors.Count > 0 then
sOutput = "<li>" & join(cErrors, "</li><li>") & "</li>"
else
sOutput = "OK"
end if
else
sOutput = "Untested"
end if
Output = sOutput
end property
public property get Assertions()
set Assertions = cAssertions
end property
' constructor and destructor
private sub class_initialize()
sStatus = "Inconclusive"
set cAssertions = new aspUnitCollection
set cErrors = new aspUnitCollection
end sub
private sub class_terminate()
cAssertions.Clear
cErrors.Clear
set cAssertions = nothing
set cErrors = nothing
end sub
' public methods
public sub AssertTrue(byref obj, byval message)
addAssertion AU_ASSERT_TRUE, obj, null, message
end sub
public sub AssertFalse(byref obj, byval message)
addAssertion AU_ASSERT_FALSE, obj, null, message
end sub
public sub AssertExists(byref obj, byval message)
addAssertion AU_ASSERT_EXISTS, obj, null, message
end sub
public sub AssertNull(byref obj, byval message)
addAssertion AU_ASSERT_NULL, obj, null, message
end sub
public sub AssertNotNull(byref obj, byval message)
addAssertion AU_ASSERT_NOT_NULL, obj, null, message
end sub
public sub AssertEmpty(byref obj, byval message)
addAssertion AU_ASSERT_EMPTY, obj, null, message
end sub
public sub AssertNotEmpty(byref obj, byval message)
addAssertion AU_ASSERT_NOT_EMPTY, obj, null, message
end sub
public sub AssertIsA(byref obj, byval typeName, byval message)
addAssertion AU_ASSERT_IS_A, obj, typeName, message
end sub
public sub AssertEquals(byref obj, byref obj2, byval message)
addAssertion AU_ASSERT_EQUALS, obj, obj2, message
end sub
public sub AssertNotEquals(byref obj, byref obj2, byval message)
addAssertion AU_ASSERT_NOT_EQUALS, obj, obj2, message
end sub
public sub AssertIsValid(byref obj, byref obj2, byval message)
addAssertion AU_ASSERT_NOT_EQUALS, obj, obj2, message
end sub
public function Run()
dim assertion, assertionResult, passed, msg
passed = false
on error resume next
if cAssertions.Count > 0 then
passed = true
sStatus = "Passed"
for each assertion in cAssertions.Items
assertionResult = assertion.Run()
if err.number <> 0 then
passed = null
sStatus = "Error"
cErrors.Add Err.Source & ": " & Err.Description
err.clear
elseif not assertionResult then
passed = false
sStatus = "Failed"
cErrors.Add assertion.Message
end if
next
end if
on error goto 0
Run = passed
end function
' private methods
private sub addAssertion(byval mode, byref obj1, byref obj2, byval msg)
dim assertion
set assertion = new aspUnitAssertion
assertion.Mode = mode
assertion.Message = msg
if isObject(obj1) then
set assertion.Obj1 = obj1
else
assertion.Obj1 = obj1
end if
if isObject(obj2) then
set assertion.Obj2 = obj2
else
assertion.Obj2 = obj2
end if
cAssertions.Add assertion
end sub
end class
class aspUnitAssertion
' fields
dim iMode, sMessage, oObj1, oObj2
' properties
public property get Mode()
Mode = iMode
end property
public property let Mode(value)
iMode = value
end property
public property get Message()
Message = sMessage
end property
public property let Message(value)
sMessage = value
end property
public property get Obj1()
if isObject(oObj1) then
set Obj1 = oObj1
else
Obj1 = oObj1
end if
end property
public property let Obj1(value)
oObj1 = value
end property
public property set Obj1(value)
set oObj1 = value
end property
public property get Obj2()
if isObject(oObj2) then
set Obj2 = oObj2
else
Obj2 = oObj2
end if
end property
public property let Obj2(value)
oObj2 = value
end property
public property set Obj2(value)
set oObj2 = value
end property
' public methods
public function Run()
dim passed, msg, val1, val2
val1 = objectValue(oObj1)
val2 = objectValue(oObj2)
passed = false
select case iMode
case AU_ASSERT_TRUE, AU_ASSERT_FALSE:
if oObj1 = true then
passed = true
end if
if iMode = AU_ASSERT_FALSE then
passed = not passed
if not passed then
if sMessage = "" or isnull(sMessage) then
msg = "Should be FALSE"
else
msg = replace(sMessage, "{1}", val1)
end if
end if
else
if not passed then
if sMessage = "" or isnull(sMessage) then
msg = "Should be TRUE"
else
msg = replace(sMessage, "{1}", val1)
end if
end if
end if
case AU_ASSERT_EXISTS:
if isObject(oObj1) then
if typeName(oObj1) <> "Nothing" then
passed = true
end if
elseif not isnull(oObj1) then
if oObj1 <> "" then passed = true
end if
if not passed then
if sMessage = "" or isnull(sMessage) then
msg = "Object doesn't exists (" & val1 & ")"
else
msg = replace(sMessage, "{1}", val1)
end if
end if
case AU_ASSERT_IS_VALID, AU_ASSERT_IS_NOT_VALID:
if isEmpty(oObj1) or isnull(oObj1) then
passed = true
elseif isObject(oObj1) or isArray(oObj1) then
if typeName(oObj1) = "Nothing" then passed = true
elseif trim(oObj1) = "" then
passed = true
end if
if iMode = AU_ASSERT_IS_VALID then
passed = not passed
if not passed then
if sMessage = "" or isnull(sMessage) then
msg = """" & val1 & """ should have a valid value (eg.: not empty, not null nor blank)"
else
msg = replace(sMessage, "{1}", val1)
end if
end if
else
if not passed then
if sMessage = "" or isnull(sMessage) then
msg = """" & val1 & """ should not have a valid value (eg.: empty, null or blank)"
else
msg = replace(sMessage, "{1}", val1)
end if
end if
end if
case AU_ASSERT_EMPTY, AU_ASSERT_NOT_EMPTY:
if isEmpty(oObj1) then
passed = true
elseif not isObject(oObj1) then
if oObj1 = "" then passed = true
end if
if iMode = AU_ASSERT_NOT_EMPTY then
passed = not passed
if not passed then
if sMessage = "" or isnull(sMessage) then
msg = """" & val1 & """ should not be empty"
else
msg = replace(sMessage, "{1}", val1)
end if
end if
else
if not passed then
if sMessage = "" or isnull(sMessage) then
msg = """" & val1 & """ should be empty"
else
msg = replace(sMessage, "{1}", val1)
end if
end if
end if
case AU_ASSERT_NULL, AU_ASSERT_NOT_NULL:
if isNull(oObj1) then passed = true
if iMode = AU_ASSERT_NOT_NULL then
passed = not passed
if not passed then
if sMessage = "" or isnull(sMessage) then
msg = """" & val1 & """ should not be null"
else
msg = replace(sMessage, "{1}", val1)
end if
end if
else
if not passed then
if sMessage = "" or isnull(sMessage) then
msg = """" & val1 & """ should be null"
else
msg = replace(sMessage, "{1}", val1)
end if
end if
end if
case AU_ASSERT_IS_A:
if typeName(oObj1) = oObj2 then
passed = true
else
if sMessage = "" or isnull(sMessage) then
msg = """" & val1 & """ is not of type """ & val2 & """"
else
msg = replace(replace(sMessage, "{1}", val1), "{2}", val2)
end if
end if
case AU_ASSERT_EQUALS, AU_ASSERT_NOT_EQUALS:
if isObject(oObj1) or isObject(oObj2) then
if isObject(oObj1) and isObject(oObj2) then
if oObj1 is oObj2 then passed = true
end if
elseif isArray(oObj1) or isArray(oObj2) then
if isArray(oObj1) and isArray(oObj2) then
dim dimensions1, dimensions2
dimensions1 = numDimensions(oObj1)
dimensions2 = numDimensions(oObj2)
if dimensions1 = dimensions2 then
dim i, tmp
tmp = true
if dimensions1 > 1 then
dim j
for i = 0 to ubound(oObj1, 2)
for j = 0 to ubound(oObj1, 1)
if oObj1(j, i) <> oObj2(j, i) then tmp = false
next
next
elseif ubound(oObj1) = ubound(oObj2) then
for i = 0 to ubound(oObj1)
if oObj1(i) = oObj2(i) then tmp = false
next
end if
if tmp then passed = true
end if
end if
elseif oObj1 = oObj2 then
passed = true
end if
if iMode = AU_ASSERT_NOT_EQUALS then
passed = not passed
if not passed then
if sMessage = "" or isnull(sMessage) then
msg = "Should not be equal to """ & val2 & """"
else
msg = replace(replace(sMessage, "{1}", val1), "{2}", val2)
end if
end if
else
if not passed then
if sMessage = "" or isnull(sMessage) then
msg = """" & val1 & """ should be equal to """ & val2 & """"
else
msg = replace(replace(sMessage, "{1}", val1), "{2}", val2)
end if
end if
end if
case default
msg = "Invalid assertion mode"
end select
if not passed then sMessage = msg
Run = passed
end function
private function objectValue(byref obj)
dim name, result
name = typeName(obj)
if isObject(obj) or name = "Empty" then
result = name
elseif name = "Variant()" then
dim dimensions, i, j
dimensions = numDimensions(obj)
if dimensions > 1 then
for j = 0 to ubound(obj, 2)
if j > 0 then result = result & ", "
redim cols(ubound(obj, 1))
for i = 0 to ubound(obj, 1)
cols(i) = objectValue(obj(i, j))
next
result = result & "[" & join(cols, ", ") & "]"
next
else
redim lines(ubound(obj))
for i = 0 to ubound(obj)
lines(i) = objectValue(obj(i))
next
result = "[" & join(obj, ", ") & "]"
end if
else
result = obj
end if
objectValue = result
end function
private function numDimensions(byref arr)
dim dimensions
dimensions = 0
on error resume next
do while err.number = 0
dimensions = dimensions + 1
ubound arr, dimensions
loop
on error goto 0
NumDimensions = dimensions - 1
end function
end class
class aspUnitTestResult
' fields
dim iTests, iPassed, iFailed, iErrors
dim cTestCases
' properties
public property get Tests()
Tests = iTests
end property
public property get Passed()
Passed = iPassed
end property
public property get Failed()
Failed = iFailed
end property
public property get Errors()
Errors = iErrors
end property
public property get TestCases()
set TestCases = cTestCases
end property
' constructor and desctructor
private sub class_initialize()
iTests = 0
iPassed = 0
iFailed = 0
iErrors = 0
set cTestCases = new aspUnitCollection
end sub
private sub class_terminate()
cTestCases.clear()
set cTestCases = nothing
end sub
' public methods
public sub Update()
dim testCase, test
for each testCase in cTestCases.Items
for each test in testCase.Tests.Items
iTests = iTests + 1
select case test.Status
case "Passed"
iPassed = iPassed + 1
case "Failed"
iFailed = iFailed + 1
case "Error"
iErrors = iErrors + 1
end select
next
next
end sub
end class
class aspUnitCollection
' fields
dim aCollection()
' properties
public default property get Items()
Items = aCollection
end property
public property get Count()
Count = ubound(aCollection) + 1
end property
' constructor
private sub class_initialize()
redim aCollection(-1)
end sub
private sub class_terminate()
Clear
end sub
' public methods
public sub Add(byref value)
redim preserve aCollection(ubound(aCollection) + 1)
if isobject(value) then
set aCollection(ubound(aCollection)) = value
else
aCollection(ubound(aCollection)) = value
end if
end sub
public function Remove(obj)
dim i, index, total, result
i = 0
total = ubound(aCollection)
result = false
index = getIndex(obj)
' If the object was found
if index >= 0 then
'Destroy the object
set aCollection(index) = nothing
' Shifts the objecs above this index one index less
for i = index to total
set aCollection(i) = aCollection(i + 1)
next
' Destroy the las item to be removed
set aCollection(total) = nothing
' Shorten the array, removing the last item
redim preserve aCollection(total - 1)
result = true
End If
Remove = result
end function
public sub Clear()
dim obj
for each obj in aCollection
Remove obj
next
end sub
' private methods
private function getIndex(obj)
dim i, index
index = -1
do while i < total
if aCollection(i) = obj then
index = i
exit do
end if
i = i + 1
loop
getIndex = index
end function
end class
%>