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