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