author | Claus Gittinger <cg@exept.de> |
Tue, 26 Feb 2002 11:30:47 +0100 | |
changeset 68 | 9fd111438d60 |
parent 67 | 7861684195ec |
child 69 | c5bff082e12f |
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 |
|
68 | 28 |
!TestCase class methodsFor:'instance creation'! |
0 | 29 |
|
30 |
debug: aSymbol |
|
6
78bb1397e43d
added rerun-defect tests; fixed button enable bug
Claus Gittinger <cg@exept.de>
parents:
4
diff
changeset
|
31 |
^(self selector: aSymbol) debug |
78bb1397e43d
added rerun-defect tests; fixed button enable bug
Claus Gittinger <cg@exept.de>
parents:
4
diff
changeset
|
32 |
! |
0 | 33 |
|
34 |
run: aSymbol |
|
6
78bb1397e43d
added rerun-defect tests; fixed button enable bug
Claus Gittinger <cg@exept.de>
parents:
4
diff
changeset
|
35 |
^(self selector: aSymbol) run |
78bb1397e43d
added rerun-defect tests; fixed button enable bug
Claus Gittinger <cg@exept.de>
parents:
4
diff
changeset
|
36 |
! |
0 | 37 |
|
38 |
selector: aSymbol |
|
6
78bb1397e43d
added rerun-defect tests; fixed button enable bug
Claus Gittinger <cg@exept.de>
parents:
4
diff
changeset
|
39 |
^self new setTestSelector: aSymbol |
78bb1397e43d
added rerun-defect tests; fixed button enable bug
Claus Gittinger <cg@exept.de>
parents:
4
diff
changeset
|
40 |
! |
0 | 41 |
|
42 |
suite |
|
50 | 43 |
"/ ^self buildSuite |
37 | 44 |
| testSelectors result | |
45 |
testSelectors := self sunitSelectors select: [:each | 'test*' match: each]. |
|
46 |
testSelectors sort. |
|
47 |
result := TestSuite new. |
|
48 |
result name:self name. |
|
49 |
testSelectors do: [:each | result addTest: (self selector: each)]. |
|
50 |
^result |
|
0 | 51 |
|
52 |
"Modified: / 21.6.2000 / 10:05:24 / Sames" |
|
53 |
! ! |
|
54 |
||
66 | 55 |
!TestCase class methodsFor:'accessing'! |
56 |
||
57 |
allTestSelectors |
|
58 |
^self sunitAllSelectors select: [:each | 'test*' match: each] |
|
59 |
! |
|
60 |
||
61 |
resources |
|
62 |
^#() |
|
63 |
! |
|
64 |
||
65 |
testSelectors |
|
66 |
^self sunitSelectors select: [:each | 'test*' match: each] |
|
67 |
! ! |
|
68 |
||
68 | 69 |
!TestCase class methodsFor:'building Suites'! |
70 |
||
71 |
buildSuite |
|
72 |
||
73 |
| suite | |
|
74 |
^self isAbstract |
|
75 |
ifTrue: |
|
76 |
[suite := TestSuite new. |
|
77 |
suite name: self name asString. |
|
78 |
self allSubclasses do: [:each | each isAbstract ifFalse: [suite addTest: each buildSuiteFromSelectors]]. |
|
79 |
suite] |
|
80 |
ifFalse: [self buildSuiteFromSelectors] |
|
81 |
! |
|
82 |
||
83 |
buildSuiteFromAllSelectors |
|
84 |
^self buildSuiteFromMethods: self allTestSelectors |
|
85 |
! |
|
86 |
||
87 |
buildSuiteFromLocalSelectors |
|
88 |
^self buildSuiteFromMethods: self testSelectors |
|
89 |
! |
|
90 |
||
91 |
buildSuiteFromMethods: testMethods |
|
92 |
^testMethods |
|
93 |
inject: ((TestSuite new) |
|
94 |
name: self name asString; |
|
95 |
yourself) |
|
96 |
into: |
|
97 |
[:suite :selector | |
|
98 |
suite |
|
99 |
addTest: (self selector: selector); |
|
100 |
yourself] |
|
101 |
! |
|
102 |
||
103 |
buildSuiteFromSelectors |
|
104 |
^self shouldInheritSelectors |
|
105 |
ifTrue: [self buildSuiteFromAllSelectors] |
|
106 |
ifFalse: [self buildSuiteFromLocalSelectors] |
|
107 |
! ! |
|
108 |
||
66 | 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 |
||
66 | 123 |
!TestCase methodsFor:'accessing'! |
124 |
||
125 |
assert: aBoolean |
|
126 |
"fail, if the argument is not true" |
|
127 |
||
128 |
"check the testCase itself" |
|
129 |
(aBoolean isBoolean) ifFalse:[ self error:'non boolean assertion' ]. |
|
130 |
aBoolean ifFalse: [self signalFailure: 'Assertion failed'] |
|
131 |
||
132 |
"Modified: / 21.6.2000 / 10:00:05 / Sames" |
|
133 |
! |
|
134 |
||
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 |
||
151 |
deny: aBoolean |
|
152 |
"fail, if the argument is not false" |
|
153 |
||
154 |
self assert: aBoolean not |
|
155 |
! |
|
156 |
||
157 |
resources |
|
158 |
^self class resources |
|
159 |
! |
|
160 |
||
161 |
selector |
|
162 |
^testSelector |
|
163 |
! |
|
164 |
||
165 |
should: aBlock |
|
166 |
"fail, if the block does not evaluate to true" |
|
167 |
||
168 |
self assert: aBlock value |
|
169 |
! |
|
170 |
||
171 |
should: aBlock raise: anExceptionalEvent |
|
172 |
"fail, if the block does not raise the given event" |
|
173 |
||
174 |
^self assert: (self executeShould: aBlock inScopeOf: anExceptionalEvent) |
|
175 |
||
176 |
"Modified: / 21.6.2000 / 10:01:08 / Sames" |
|
177 |
! |
|
178 |
||
179 |
shouldnt: aBlock |
|
180 |
"fail, if the block does evaluate to true" |
|
181 |
||
182 |
self deny: aBlock value |
|
183 |
! |
|
184 |
||
185 |
shouldnt: aBlock raise: anExceptionalEvent |
|
186 |
"fail, if the block does raise the given event" |
|
187 |
||
188 |
^self assert: (self executeShould: aBlock inScopeOf: anExceptionalEvent) not |
|
189 |
||
190 |
"Modified: / 21.6.2000 / 10:01:16 / Sames" |
|
191 |
! |
|
192 |
||
193 |
signalFailure: aString |
|
194 |
"/ TestResult failure sunitSignalWith: aString |
|
195 |
TestResult failure raiseErrorString:aString in:thisContext sender sender . |
|
196 |
||
197 |
"Modified: / 21.6.2000 / 10:01:34 / Sames" |
|
198 |
! ! |
|
199 |
||
67 | 200 |
!TestCase methodsFor:'dependencies'! |
201 |
||
202 |
addDependentToHierachy: anObject |
|
203 |
"an empty method. for Composite compability with TestSuite" |
|
204 |
! |
|
205 |
||
206 |
removeDependentFromHierachy: anObject |
|
207 |
"an empty method. for Composite compability with TestSuite" |
|
208 |
! ! |
|
209 |
||
210 |
!TestCase methodsFor:'printing'! |
|
211 |
||
212 |
name |
|
213 |
^ self class name. |
|
214 |
! |
|
215 |
||
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 |
|
227 |
[[aBlock value] |
|
228 |
on: anExceptionalEvent |
|
229 |
do: [:ex | ^true]] |
|
230 |
on: TestResult error |
|
231 |
do: [:ex | ^false]. |
|
232 |
^false. |
|
233 |
||
234 |
"Modified: / 21.6.2000 / 10:03:03 / Sames" |
|
235 |
! |
|
236 |
||
237 |
performTest |
|
238 |
self perform: testSelector asSymbol |
|
239 |
! |
|
240 |
||
241 |
setTestSelector: aSymbol |
|
242 |
testSelector := aSymbol |
|
243 |
! ! |
|
244 |
||
245 |
!TestCase methodsFor:'running'! |
|
246 |
||
247 |
debug |
|
248 |
self debugUsing:#runCase. |
|
249 |
"/ (self class selector: testSelector) runCase |
|
250 |
! |
|
251 |
||
252 |
debugAsFailure |
|
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]] |
|
260 |
! |
|
261 |
||
262 |
openDebuggerOnFailingTestMethod |
|
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" |
|
266 |
||
267 |
"/ self halt. |
|
268 |
self perform: testSelector asSymbol |
|
269 |
||
270 |
"Modified: / 21.6.2000 / 10:03:37 / Sames" |
|
271 |
! |
|
272 |
||
273 |
run |
|
274 |
| result | |
|
275 |
result := TestResult new. |
|
276 |
self run: result. |
|
277 |
^result |
|
278 |
! |
|
279 |
||
280 |
run: aResult |
|
281 |
aResult runCase: self |
|
282 |
! |
|
283 |
||
284 |
run: aResult afterEachDo:block2 |
|
285 |
aResult runCase: self. |
|
286 |
block2 value:self value:aResult. |
|
287 |
! |
|
288 |
||
289 |
run: aResult beforeEachDo:block1 afterEachDo:block2 |
|
290 |
block1 value:self value:aResult. |
|
291 |
aResult runCase: self. |
|
292 |
block2 value:self value:aResult. |
|
293 |
! |
|
294 |
||
295 |
runCase |
|
296 |
self setUp. |
|
297 |
[self performTest "self perform: testSelector asSymbol"] ensure: [self tearDown] |
|
298 |
||
299 |
"Modified: / 21.6.2000 / 10:04:18 / Sames" |
|
300 |
! |
|
301 |
||
302 |
runCaseAsFailure |
|
303 |
self setUp. |
|
304 |
[[self openDebuggerOnFailingTestMethod] ensure: [self tearDown]] fork |
|
305 |
||
306 |
"Modified: / 21.6.2000 / 10:04:33 / Sames" |
|
307 |
! |
|
308 |
||
309 |
setUp |
|
310 |
! |
|
311 |
||
312 |
tearDown |
|
313 |
! ! |
|
314 |
||
66 | 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 |
|
68 | 326 |
^ '$Header: /cvs/stx/stx/goodies/sunit/TestCase.st,v 1.22 2002-02-26 10:30:20 cg Exp $' |
12 | 327 |
! ! |
2 | 328 |
TestCase initialize! |