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