author | Claus Gittinger <cg@exept.de> |
Thu, 13 Dec 2001 23:11:20 +0100 | |
changeset 50 | 6db52a1a4543 |
parent 40 | b27893ae4b99 |
child 63 | 4414cf7a4473 |
permissions | -rw-r--r-- |
0 | 1 |
"{ Package: 'stx:goodies/sunit' }" |
2 |
||
3 |
Object subclass:#TestCase |
|
4 |
instanceVariableNames:'testSelector' |
|
5 |
classVariableNames:'' |
|
6 |
poolDictionaries:'' |
|
6
78bb1397e43d
added rerun-defect tests; fixed button enable bug
Claus Gittinger <cg@exept.de>
parents:
4
diff
changeset
|
7 |
category:'SUnit-Base' |
0 | 8 |
! |
9 |
||
12 | 10 |
|
2 | 11 |
!TestCase class methodsFor:'initialization'! |
12 |
||
13 |
initialize |
|
14 |
"ensure, that the sunit extensions are loaded" |
|
15 |
||
16 |
(Class implements:#sunitName) ifFalse:[ |
|
36 | 17 |
Smalltalk fileIn:'extensions.st' inPackage:(self package) |
2 | 18 |
]. |
4 | 19 |
TestFailure isLoaded ifFalse:[ |
35 | 20 |
TestFailure autoload |
4 | 21 |
]. |
2 | 22 |
|
23 |
" |
|
24 |
self initialize |
|
4 | 25 |
" |
26 |
! ! |
|
2 | 27 |
|
50 | 28 |
!TestCase class methodsFor:'Accessing'! |
29 |
||
30 |
allTestSelectors |
|
31 |
^self sunitAllSelectors select: [:each | 'test*' match: each] |
|
32 |
! |
|
33 |
||
34 |
resources |
|
35 |
^#() |
|
36 |
! |
|
37 |
||
38 |
testSelectors |
|
39 |
^self sunitSelectors select: [:each | 'test*' match: each] |
|
40 |
! ! |
|
41 |
||
42 |
!TestCase class methodsFor:'Building Suites'! |
|
43 |
||
44 |
buildSuite |
|
45 |
||
46 |
| suite | |
|
47 |
^self isAbstract |
|
48 |
ifTrue: |
|
49 |
[suite := TestSuite new. |
|
50 |
suite name: self name asString. |
|
51 |
self allSubclasses do: [:each | each isAbstract ifFalse: [suite addTest: each buildSuiteFromSelectors]]. |
|
52 |
suite] |
|
53 |
ifFalse: [self buildSuiteFromSelectors] |
|
54 |
! |
|
55 |
||
56 |
buildSuiteFromAllSelectors |
|
57 |
^self buildSuiteFromMethods: self allTestSelectors |
|
58 |
! |
|
59 |
||
60 |
buildSuiteFromLocalSelectors |
|
61 |
^self buildSuiteFromMethods: self testSelectors |
|
62 |
! |
|
63 |
||
64 |
buildSuiteFromMethods: testMethods |
|
65 |
^testMethods |
|
66 |
inject: ((TestSuite new) |
|
67 |
name: self name asString; |
|
68 |
yourself) |
|
69 |
into: |
|
70 |
[:suite :selector | |
|
71 |
suite |
|
72 |
addTest: (self selector: selector); |
|
73 |
yourself] |
|
74 |
! |
|
75 |
||
76 |
buildSuiteFromSelectors |
|
77 |
^self shouldInheritSelectors |
|
78 |
ifTrue: [self buildSuiteFromAllSelectors] |
|
79 |
ifFalse: [self buildSuiteFromLocalSelectors] |
|
80 |
! ! |
|
81 |
||
0 | 82 |
!TestCase class methodsFor:'Instance Creation'! |
83 |
||
84 |
debug: aSymbol |
|
6
78bb1397e43d
added rerun-defect tests; fixed button enable bug
Claus Gittinger <cg@exept.de>
parents:
4
diff
changeset
|
85 |
^(self selector: aSymbol) debug |
78bb1397e43d
added rerun-defect tests; fixed button enable bug
Claus Gittinger <cg@exept.de>
parents:
4
diff
changeset
|
86 |
! |
0 | 87 |
|
88 |
run: aSymbol |
|
6
78bb1397e43d
added rerun-defect tests; fixed button enable bug
Claus Gittinger <cg@exept.de>
parents:
4
diff
changeset
|
89 |
^(self selector: aSymbol) run |
78bb1397e43d
added rerun-defect tests; fixed button enable bug
Claus Gittinger <cg@exept.de>
parents:
4
diff
changeset
|
90 |
! |
0 | 91 |
|
92 |
selector: aSymbol |
|
6
78bb1397e43d
added rerun-defect tests; fixed button enable bug
Claus Gittinger <cg@exept.de>
parents:
4
diff
changeset
|
93 |
^self new setTestSelector: aSymbol |
78bb1397e43d
added rerun-defect tests; fixed button enable bug
Claus Gittinger <cg@exept.de>
parents:
4
diff
changeset
|
94 |
! |
0 | 95 |
|
96 |
suite |
|
50 | 97 |
"/ ^self buildSuite |
37 | 98 |
| testSelectors result | |
99 |
testSelectors := self sunitSelectors select: [:each | 'test*' match: each]. |
|
100 |
testSelectors sort. |
|
101 |
result := TestSuite new. |
|
102 |
result name:self name. |
|
103 |
testSelectors do: [:each | result addTest: (self selector: each)]. |
|
104 |
^result |
|
0 | 105 |
|
106 |
"Modified: / 21.6.2000 / 10:05:24 / Sames" |
|
107 |
! ! |
|
108 |
||
50 | 109 |
!TestCase class methodsFor:'Testing'! |
110 |
||
111 |
isAbstract |
|
112 |
"Override to true if a TestCase subclass is Abstract and should not have |
|
113 |
TestCase instances built from it" |
|
114 |
^self name = #TestCase. |
|
115 |
! |
|
116 |
||
117 |
shouldInheritSelectors |
|
118 |
"answer true to inherit selectors from superclasses" |
|
119 |
||
120 |
^true |
|
121 |
! ! |
|
122 |
||
0 | 123 |
!TestCase methodsFor:'Accessing'! |
124 |
||
125 |
assert: aBoolean |
|
3 | 126 |
"fail, if the argument is not true" |
127 |
||
50 | 128 |
"check the testCase itself" |
129 |
(aBoolean isBoolean) ifFalse:[ self error:'non boolean assertion' ]. |
|
3 | 130 |
aBoolean ifFalse: [self signalFailure: 'Assertion failed'] |
0 | 131 |
|
132 |
"Modified: / 21.6.2000 / 10:00:05 / Sames" |
|
133 |
! |
|
134 |
||
27 | 135 |
assertFalse:aBoolean |
136 |
^ self assert:aBoolean not |
|
137 |
! |
|
138 |
||
139 |
assertFalse:aBoolean named:testName |
|
140 |
^ self assert:aBoolean not |
|
141 |
! |
|
142 |
||
143 |
assertTrue:aBoolean |
|
144 |
^ self assert:aBoolean |
|
145 |
! |
|
146 |
||
147 |
assertTrue:aBoolean named:testName |
|
148 |
^ self assert:aBoolean |
|
149 |
! |
|
150 |
||
0 | 151 |
deny: aBoolean |
3 | 152 |
"fail, if the argument is not false" |
153 |
||
154 |
self assert: aBoolean not |
|
155 |
! |
|
0 | 156 |
|
50 | 157 |
resources |
158 |
^self class resources |
|
159 |
! |
|
160 |
||
161 |
selector |
|
162 |
^testSelector |
|
163 |
! |
|
164 |
||
0 | 165 |
should: aBlock |
3 | 166 |
"fail, if the block does not evaluate to true" |
167 |
||
168 |
self assert: aBlock value |
|
169 |
! |
|
0 | 170 |
|
171 |
should: aBlock raise: anExceptionalEvent |
|
3 | 172 |
"fail, if the block does not raise the given event" |
173 |
||
174 |
^self assert: (self executeShould: aBlock inScopeOf: anExceptionalEvent) |
|
0 | 175 |
|
176 |
"Modified: / 21.6.2000 / 10:01:08 / Sames" |
|
177 |
! |
|
178 |
||
179 |
shouldnt: aBlock |
|
3 | 180 |
"fail, if the block does evaluate to true" |
181 |
||
182 |
self deny: aBlock value |
|
183 |
! |
|
0 | 184 |
|
185 |
shouldnt: aBlock raise: anExceptionalEvent |
|
3 | 186 |
"fail, if the block does raise the given event" |
187 |
||
188 |
^self assert: (self executeShould: aBlock inScopeOf: anExceptionalEvent) not |
|
0 | 189 |
|
190 |
"Modified: / 21.6.2000 / 10:01:16 / Sames" |
|
191 |
! |
|
192 |
||
193 |
signalFailure: aString |
|
3 | 194 |
"/ TestResult failure sunitSignalWith: aString |
195 |
TestResult failure raiseErrorString:aString in:thisContext sender sender . |
|
0 | 196 |
|
3 | 197 |
"Modified: / 21.6.2000 / 10:01:34 / Sames" |
198 |
! ! |
|
0 | 199 |
|
200 |
!TestCase methodsFor:'Dependencies'! |
|
201 |
||
202 |
addDependentToHierachy: anObject |
|
6
78bb1397e43d
added rerun-defect tests; fixed button enable bug
Claus Gittinger <cg@exept.de>
parents:
4
diff
changeset
|
203 |
"an empty method. for Composite compability with TestSuite" |
78bb1397e43d
added rerun-defect tests; fixed button enable bug
Claus Gittinger <cg@exept.de>
parents:
4
diff
changeset
|
204 |
! |
0 | 205 |
|
206 |
removeDependentFromHierachy: anObject |
|
6
78bb1397e43d
added rerun-defect tests; fixed button enable bug
Claus Gittinger <cg@exept.de>
parents:
4
diff
changeset
|
207 |
"an empty method. for Composite compability with TestSuite" |
78bb1397e43d
added rerun-defect tests; fixed button enable bug
Claus Gittinger <cg@exept.de>
parents:
4
diff
changeset
|
208 |
! ! |
0 | 209 |
|
210 |
!TestCase methodsFor:'Printing'! |
|
211 |
||
37 | 212 |
name |
213 |
^ self class name. |
|
214 |
! |
|
215 |
||
0 | 216 |
printOn: aStream |
217 |
aStream nextPutAll: self class name. |
|
218 |
aStream nextPutAll: '>>'. |
|
219 |
aStream nextPutAll: testSelector |
|
220 |
||
221 |
"Modified: / 4.4.2000 / 18:59:53 / Sames" |
|
222 |
! ! |
|
223 |
||
224 |
!TestCase methodsFor:'Private'! |
|
225 |
||
226 |
executeShould: aBlock inScopeOf: anExceptionalEvent |
|
35 | 227 |
[[aBlock value] |
228 |
on: anExceptionalEvent |
|
229 |
do: [:ex | ^true]] |
|
230 |
on: TestResult error |
|
231 |
do: [:ex | ^false]. |
|
232 |
^false. |
|
0 | 233 |
|
234 |
"Modified: / 21.6.2000 / 10:03:03 / Sames" |
|
235 |
! |
|
236 |
||
50 | 237 |
performTest |
238 |
self perform: testSelector asSymbol |
|
239 |
! |
|
240 |
||
0 | 241 |
setTestSelector: aSymbol |
6
78bb1397e43d
added rerun-defect tests; fixed button enable bug
Claus Gittinger <cg@exept.de>
parents:
4
diff
changeset
|
242 |
testSelector := aSymbol |
78bb1397e43d
added rerun-defect tests; fixed button enable bug
Claus Gittinger <cg@exept.de>
parents:
4
diff
changeset
|
243 |
! ! |
0 | 244 |
|
245 |
!TestCase methodsFor:'Running'! |
|
246 |
||
247 |
debug |
|
50 | 248 |
self debugUsing:#runCase. |
249 |
"/ (self class selector: testSelector) runCase |
|
6
78bb1397e43d
added rerun-defect tests; fixed button enable bug
Claus Gittinger <cg@exept.de>
parents:
4
diff
changeset
|
250 |
! |
0 | 251 |
|
252 |
debugAsFailure |
|
50 | 253 |
self debugUsing: #runCaseAsFailure |
254 |
"/ (self class selector: testSelector) runCaseAsFailure |
|
255 |
! |
|
256 |
||
257 |
debugUsing: aSymbol |
|
258 |
self areAllResourcesAvailable ifFalse: [^TestResult signalErrorWith: 'Resource could not be initialized']. |
|
259 |
[(self class selector: testSelector) perform: aSymbol] ensure: [self resources do: [:each | each reset]] |
|
6
78bb1397e43d
added rerun-defect tests; fixed button enable bug
Claus Gittinger <cg@exept.de>
parents:
4
diff
changeset
|
260 |
! |
0 | 261 |
|
262 |
openDebuggerOnFailingTestMethod |
|
35 | 263 |
"SUnit has halted one step in front of the failing test method. |
264 |
Step over the 'self halt' and send into 'self perform: testSelector' |
|
265 |
to see the failure from the beginning" |
|
0 | 266 |
|
35 | 267 |
"/ self halt. |
268 |
self perform: testSelector asSymbol |
|
0 | 269 |
|
6
78bb1397e43d
added rerun-defect tests; fixed button enable bug
Claus Gittinger <cg@exept.de>
parents:
4
diff
changeset
|
270 |
"Modified: / 21.6.2000 / 10:03:37 / Sames" |
78bb1397e43d
added rerun-defect tests; fixed button enable bug
Claus Gittinger <cg@exept.de>
parents:
4
diff
changeset
|
271 |
! |
0 | 272 |
|
273 |
run |
|
274 |
| result | |
|
275 |
result := TestResult new. |
|
276 |
self run: result. |
|
6
78bb1397e43d
added rerun-defect tests; fixed button enable bug
Claus Gittinger <cg@exept.de>
parents:
4
diff
changeset
|
277 |
^result |
78bb1397e43d
added rerun-defect tests; fixed button enable bug
Claus Gittinger <cg@exept.de>
parents:
4
diff
changeset
|
278 |
! |
0 | 279 |
|
280 |
run: aResult |
|
6
78bb1397e43d
added rerun-defect tests; fixed button enable bug
Claus Gittinger <cg@exept.de>
parents:
4
diff
changeset
|
281 |
aResult runCase: self |
78bb1397e43d
added rerun-defect tests; fixed button enable bug
Claus Gittinger <cg@exept.de>
parents:
4
diff
changeset
|
282 |
! |
0 | 283 |
|
40 | 284 |
run: aResult afterEachDo:block2 |
285 |
aResult runCase: self. |
|
286 |
block2 value:self value:aResult. |
|
287 |
! |
|
288 |
||
0 | 289 |
runCase |
50 | 290 |
self setUp. |
291 |
[self performTest "self perform: testSelector asSymbol"] ensure: [self tearDown] |
|
0 | 292 |
|
293 |
"Modified: / 21.6.2000 / 10:04:18 / Sames" |
|
294 |
! |
|
295 |
||
296 |
runCaseAsFailure |
|
50 | 297 |
self setUp. |
298 |
[[self openDebuggerOnFailingTestMethod] ensure: [self tearDown]] fork |
|
0 | 299 |
|
300 |
"Modified: / 21.6.2000 / 10:04:33 / Sames" |
|
301 |
! |
|
302 |
||
6
78bb1397e43d
added rerun-defect tests; fixed button enable bug
Claus Gittinger <cg@exept.de>
parents:
4
diff
changeset
|
303 |
setUp |
78bb1397e43d
added rerun-defect tests; fixed button enable bug
Claus Gittinger <cg@exept.de>
parents:
4
diff
changeset
|
304 |
! |
0 | 305 |
|
6
78bb1397e43d
added rerun-defect tests; fixed button enable bug
Claus Gittinger <cg@exept.de>
parents:
4
diff
changeset
|
306 |
tearDown |
78bb1397e43d
added rerun-defect tests; fixed button enable bug
Claus Gittinger <cg@exept.de>
parents:
4
diff
changeset
|
307 |
! ! |
0 | 308 |
|
50 | 309 |
!TestCase methodsFor:'Testing'! |
310 |
||
311 |
areAllResourcesAvailable |
|
312 |
^self resources |
|
313 |
inject: true |
|
314 |
into: [:total :each | each isAvailable & total] |
|
315 |
! ! |
|
316 |
||
12 | 317 |
!TestCase class methodsFor:'documentation'! |
318 |
||
319 |
version |
|
50 | 320 |
^ '$Header: /cvs/stx/stx/goodies/sunit/TestCase.st,v 1.17 2001-12-13 22:11:20 cg Exp $' |
12 | 321 |
! ! |
2 | 322 |
TestCase initialize! |