708
|
1 |
"{ Encoding: utf8 }"
|
|
2 |
|
222
|
3 |
"{ Package: 'stx:goodies/sunit' }"
|
|
4 |
|
647
|
5 |
"{ NameSpace: Smalltalk }"
|
|
6 |
|
222
|
7 |
Object subclass:#TestAsserter
|
376
|
8 |
instanceVariableNames:''
|
222
|
9 |
classVariableNames:''
|
|
10 |
poolDictionaries:''
|
|
11 |
category:'SUnit-Base'
|
|
12 |
!
|
|
13 |
|
|
14 |
TestAsserter comment:''
|
|
15 |
!
|
|
16 |
|
|
17 |
|
|
18 |
!TestAsserter class methodsFor:'asserting'!
|
|
19 |
|
|
20 |
assert: aBoolean description: aString
|
687
|
21 |
"Minimal clone of the instance-side assert protocol so that class-side methods can use it."
|
222
|
22 |
|
687
|
23 |
aBoolean ifFalse: [
|
|
24 |
self logFailure: aString.
|
|
25 |
TestResult failure sunitSignalWith: aString
|
|
26 |
].
|
|
27 |
|
|
28 |
"Modified (format): / 13-07-2017 / 15:03:51 / cg"
|
222
|
29 |
! !
|
|
30 |
|
|
31 |
!TestAsserter class methodsFor:'logging'!
|
|
32 |
|
|
33 |
failureLog
|
|
34 |
^SUnitNameResolver defaultLogDevice
|
|
35 |
!
|
|
36 |
|
|
37 |
isLogging
|
|
38 |
"By default, we're not logging failures. Override in subclasses as desired."
|
|
39 |
|
|
40 |
^false
|
|
41 |
!
|
|
42 |
|
|
43 |
logFailure: aString
|
|
44 |
self isLogging ifTrue:
|
|
45 |
[self failureLog cr; nextPutAll: aString; flush].
|
551
|
46 |
!
|
|
47 |
|
|
48 |
logSkipped: aString
|
|
49 |
self isLogging ifTrue:
|
|
50 |
[self failureLog cr; nextPutAll: aString; flush].
|
222
|
51 |
! !
|
|
52 |
|
687
|
53 |
!TestAsserter class methodsFor:'private'!
|
|
54 |
|
|
55 |
comparingCollectionBetween: left and: right
|
|
56 |
"helper to generate a nice description, if
|
|
57 |
a collection is not the expected one."
|
|
58 |
|
|
59 |
| additionalLeft additionalRight sortBlock|
|
|
60 |
|
|
61 |
"use a very slow sort block"
|
|
62 |
sortBlock := [ :a :b | a asString <= b asString ].
|
|
63 |
additionalLeft := (left difference: right) sorted: sortBlock.
|
|
64 |
additionalRight := (right difference: left) sorted: sortBlock.
|
|
65 |
|
|
66 |
^ String
|
|
67 |
streamContents: [:stream |
|
|
68 |
stream
|
|
69 |
nextPutAll: 'Given Collections do not match.'; lf;
|
|
70 |
tab; nextPutAll: 'Got left := '; print: left; nextPut: $.; lf;
|
|
71 |
nextPutAll: ' instead of '; lf;
|
|
72 |
tab; nextPutAll: 'right :='; print: right; nextPut: $.; lf.
|
|
73 |
|
|
74 |
left size = right size ifFalse: [
|
|
75 |
stream
|
|
76 |
nextPutAll: 'Collection size does not match: left=';
|
|
77 |
print: left size;
|
|
78 |
nextPutAll: ' vs. right=';
|
|
79 |
print: right size; lf
|
|
80 |
].
|
|
81 |
additionalLeft notEmpty ifTrue:[
|
|
82 |
stream
|
|
83 |
nextPutAll:'Got ';
|
|
84 |
print:additionalLeft size;
|
|
85 |
nextPutAll:' additional element(s) in the left collection: ';
|
|
86 |
tab;
|
|
87 |
print:additionalLeft
|
|
88 |
].
|
|
89 |
additionalRight notEmpty ifTrue:[
|
|
90 |
stream
|
|
91 |
nextPutAll:'Got ';
|
|
92 |
print:additionalRight size;
|
|
93 |
nextPutAll:' additional element(s) in the right collection: ';
|
|
94 |
tab;
|
|
95 |
print:additionalRight
|
|
96 |
]
|
|
97 |
]
|
|
98 |
|
|
99 |
"
|
|
100 |
self basicNew comparingCollectionBetween:#(1 2 3) and:#(1 2 3 4)
|
|
101 |
"
|
|
102 |
|
|
103 |
"Modified: / 13-07-2017 / 14:13:15 / cg"
|
|
104 |
! !
|
|
105 |
|
222
|
106 |
!TestAsserter methodsFor:'asserting'!
|
|
107 |
|
687
|
108 |
assert: aBooleanOrBlock
|
|
109 |
"fail the testcase if aBooleanOrBlock evaluates to false"
|
566
|
110 |
|
222
|
111 |
<resource: #skipInDebuggersWalkBack>
|
|
112 |
|
687
|
113 |
aBooleanOrBlock value ifFalse:[
|
|
114 |
self logFailure: 'Assertion failed'.
|
|
115 |
TestResult failure sunitSignalWith: 'Assertion failed'
|
|
116 |
].
|
222
|
117 |
|
687
|
118 |
"Modified: / 05-12-2009 / 18:14:08 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
119 |
"Modified: / 13-07-2017 / 13:50:13 / cg"
|
|
120 |
"Modified (comment): / 13-07-2017 / 15:05:33 / cg"
|
222
|
121 |
!
|
|
122 |
|
687
|
123 |
assert:aBooleanOrBlock description:aString
|
|
124 |
"fail the testCase if aBooleanOrBlock evaluates to false,
|
|
125 |
and report aStringOrBlock's value as failure-description."
|
222
|
126 |
|
708
|
127 |
<resource: #skipInDebuggersWalkBack>
|
222
|
128 |
|
692
|
129 |
^ self assert:aBooleanOrBlock description:aString resumable: false.
|
687
|
130 |
|
|
131 |
"Modified: / 11-09-2010 / 15:34:11 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
692
|
132 |
"Modified (format): / 09-11-2017 / 10:03:58 / cg"
|
222
|
133 |
!
|
|
134 |
|
687
|
135 |
assert:aBooleanOrBlock description:aStringOrBlock resumable:resumableBoolean
|
|
136 |
"fail the testCase if aBooleanOrBlock evaluates to false,
|
|
137 |
and report aStringOrBlock's value as failure-description.
|
|
138 |
If resumableBoolean is true, the test can be resumed in the debugger (if it was started by 'Debug')"
|
222
|
139 |
|
|
140 |
<resource: #skipInDebuggersWalkBack>
|
|
141 |
|
687
|
142 |
| exception |
|
|
143 |
|
|
144 |
aBooleanOrBlock value ifFalse:[
|
|
145 |
|string|
|
222
|
146 |
|
687
|
147 |
string := aStringOrBlock value.
|
|
148 |
self logFailure: string.
|
|
149 |
exception := resumableBoolean
|
|
150 |
ifTrue: [TestResult resumableFailure]
|
|
151 |
ifFalse: [TestResult failure].
|
|
152 |
exception sunitSignalWith: string
|
|
153 |
].
|
|
154 |
|
|
155 |
"Modified: / 05-12-2009 / 18:15:22 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
156 |
"Modified: / 13-07-2017 / 13:49:38 / cg"
|
|
157 |
"Modified (comment): / 13-07-2017 / 15:07:33 / cg"
|
222
|
158 |
!
|
|
159 |
|
687
|
160 |
deny: aBooleanOrBlock
|
|
161 |
"fail the testcase if aBooleanOrBlock evaluates to true"
|
222
|
162 |
|
|
163 |
<resource: #skipInDebuggersWalkBack>
|
566
|
164 |
|
687
|
165 |
self assert:(aBooleanOrBlock value not).
|
222
|
166 |
|
687
|
167 |
"Modified: / 05-12-2009 / 18:16:47 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
168 |
"Modified (comment): / 13-07-2017 / 15:06:17 / cg"
|
222
|
169 |
!
|
|
170 |
|
687
|
171 |
deny: aBooleanOrBlock description: aString
|
|
172 |
"fail the testCase if aBooleanOrBlock evaluates to true,
|
|
173 |
and report aStringOrBlock's value as failure-description."
|
222
|
174 |
|
|
175 |
<resource: #skipInDebuggersWalkBack>
|
|
176 |
|
687
|
177 |
self assert:(aBooleanOrBlock value not) description:aString.
|
|
178 |
|
|
179 |
"Modified: / 05-12-2009 / 18:17:02 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
180 |
"Modified (comment): / 13-07-2017 / 15:06:30 / cg"
|
|
181 |
!
|
|
182 |
|
|
183 |
deny:aBooleanOrBlock description:aString resumable:resumableBoolean
|
|
184 |
"fail the testCase if aBooleanOrBlock evaluates to true,
|
|
185 |
and report aStringOrBlock's value as failure-description.
|
|
186 |
If resumableBoolean is true, the test can be resumed in the debugger (if it was started by 'Debug')"
|
|
187 |
|
|
188 |
<resource: #skipInDebuggersWalkBack>
|
|
189 |
|
|
190 |
self assert:(aBooleanOrBlock value not) description:aString resumable:resumableBoolean.
|
222
|
191 |
|
|
192 |
"Modified: / 05-12-2009 / 18:17:17 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
687
|
193 |
"Modified (comment): / 13-07-2017 / 15:07:38 / cg"
|
222
|
194 |
!
|
|
195 |
|
|
196 |
should: aBlock raise: anExceptionalEvent
|
566
|
197 |
"during the execution of aBlock, the anExceptionalEvent should be raised"
|
222
|
198 |
|
|
199 |
<resource: #skipInDebuggersWalkBack>
|
|
200 |
|
|
201 |
self assert: (self executeShould: aBlock inScopeOf: anExceptionalEvent).
|
|
202 |
|
|
203 |
"Modified: / 05-12-2009 / 18:18:17 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
204 |
!
|
|
205 |
|
|
206 |
should: aBlock raise: anExceptionalEvent description: aString
|
566
|
207 |
"during the execution of aBlock, the anExceptionalEvent should be raised.
|
|
208 |
If not, report aString as failure-description."
|
222
|
209 |
|
|
210 |
<resource: #skipInDebuggersWalkBack>
|
|
211 |
|
|
212 |
self
|
566
|
213 |
assert: (self executeShould: aBlock inScopeOf: anExceptionalEvent)
|
|
214 |
description: aString.
|
222
|
215 |
|
|
216 |
"Modified: / 05-12-2009 / 18:18:08 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
217 |
!
|
|
218 |
|
|
219 |
should: aBlock raise: anExceptionalEvent suchThat: condBlock
|
566
|
220 |
"during the execution of aBlock, the anExceptionalEvent should be raised,
|
|
221 |
and condBlock should return true on the exception instance"
|
222
|
222 |
|
|
223 |
<resource: #skipInDebuggersWalkBack>
|
|
224 |
|
|
225 |
self assert: (self executeShould: aBlock inScopeOf: anExceptionalEvent suchThat: condBlock).
|
|
226 |
|
|
227 |
"Created: / 05-05-2011 / 20:14:14 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
228 |
!
|
|
229 |
|
|
230 |
should: aBlock raise: anExceptionalEvent suchThat: condBlock description: description
|
566
|
231 |
"during the execution of aBlock, the anExceptionalEvent should be raised,
|
|
232 |
and condBlock should return true on the exception instance.
|
|
233 |
If not, report aString as failure-description."
|
222
|
234 |
|
|
235 |
<resource: #skipInDebuggersWalkBack>
|
|
236 |
|
|
237 |
self assert: (self executeShould: aBlock inScopeOf: anExceptionalEvent suchThat: condBlock)
|
566
|
238 |
description: description
|
222
|
239 |
|
|
240 |
"Created: / 05-05-2011 / 20:14:55 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
241 |
!
|
|
242 |
|
566
|
243 |
should: aBlock raise: anExceptionalEvent whoseDescriptionIncludes: aStringFragment
|
|
244 |
"during the execution of aBlock, the anExceptionalEvent should be raised,
|
|
245 |
and aStringFragment should be contained in the exception-instances description."
|
|
246 |
|
|
247 |
<resource: #skipInDebuggersWalkBack>
|
|
248 |
|
|
249 |
^ self should: aBlock
|
|
250 |
raise: anExceptionalEvent
|
|
251 |
suchThat:[:ex | ex description includesString: aStringFragment]
|
|
252 |
!
|
|
253 |
|
|
254 |
should:aBlock raise:anExceptionalEvent whoseDescriptionIncludes:aStringFragment description:aString
|
|
255 |
"during the execution of aBlock, the anExceptionalEvent should be raised,
|
|
256 |
and aStringFragment should be contained in the exception-instances description.
|
|
257 |
If not, report aString as failure-description."
|
|
258 |
|
|
259 |
<resource: #skipInDebuggersWalkBack>
|
|
260 |
|
|
261 |
^ self should: aBlock
|
|
262 |
raise: anExceptionalEvent
|
|
263 |
suchThat:[:ex | ex description includesString: aStringFragment]
|
|
264 |
description: aString.
|
|
265 |
!
|
|
266 |
|
718
|
267 |
should:aBlock raise:anExceptionalEvent withExceptionDo:exCheckAction
|
|
268 |
"during the execution of aBlock, the anExceptionalEvent should be raised,
|
|
269 |
then exCheckAction is evaluated on the exception (which usually includes more checks).
|
|
270 |
If not, report aString as failure-description."
|
|
271 |
|
|
272 |
<resource: #skipInDebuggersWalkBack>
|
|
273 |
|
|
274 |
^ self should: aBlock
|
|
275 |
raise: anExceptionalEvent
|
|
276 |
suchThat:[:ex | exCheckAction value:ex. true]
|
|
277 |
|
|
278 |
"should fail:
|
|
279 |
|
|
280 |
self new
|
|
281 |
should:[ 1 / Float zero]
|
|
282 |
raise:ZeroDivide
|
|
283 |
withExceptionDo:[:ex |
|
|
284 |
ex description should equal:'hello world'
|
|
285 |
].
|
|
286 |
|
|
287 |
should fail:
|
|
288 |
self new
|
|
289 |
should:[ 1 / Float zero]
|
|
290 |
raise:ZeroDivide
|
|
291 |
withExceptionDo:[:ex |
|
|
292 |
ex description should equal:'hello world'
|
|
293 |
].
|
|
294 |
|
|
295 |
should pass:
|
|
296 |
self new
|
|
297 |
should:[ 1 / Float zero]
|
|
298 |
raise:ZeroDivide
|
|
299 |
withExceptionDo:[:ex |
|
|
300 |
ex description should equal:'hello world'
|
|
301 |
].
|
|
302 |
"
|
|
303 |
|
|
304 |
"Created: / 01-07-2018 / 12:18:18 / Claus Gittinger"
|
|
305 |
!
|
|
306 |
|
222
|
307 |
shouldnt: aBlock raise: anExceptionalEvent
|
566
|
308 |
"during the execution of aBlock, the anExceptionalEvent should NOT be raised"
|
222
|
309 |
|
|
310 |
<resource: #skipInDebuggersWalkBack>
|
|
311 |
|
|
312 |
self
|
566
|
313 |
assert: (self executeShould: aBlock inScopeOf: anExceptionalEvent) not.
|
222
|
314 |
|
|
315 |
"Modified: / 05-12-2009 / 18:18:35 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
316 |
!
|
|
317 |
|
|
318 |
shouldnt: aBlock raise: anExceptionalEvent description: aString
|
566
|
319 |
"during the execution of aBlock, the anExceptionalEvent should NOT be raised.
|
|
320 |
If not, report aString as failure-description."
|
222
|
321 |
|
|
322 |
<resource: #skipInDebuggersWalkBack>
|
|
323 |
|
|
324 |
self
|
566
|
325 |
assert: (self executeShould: aBlock inScopeOf: anExceptionalEvent) not
|
|
326 |
description: aString.
|
222
|
327 |
|
|
328 |
"Modified: / 05-12-2009 / 18:18:53 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
551
|
329 |
!
|
|
330 |
|
647
|
331 |
skip
|
|
332 |
"skip this test"
|
|
333 |
|
650
|
334 |
self skipIf:true description:'Test Skipped'.
|
649
|
335 |
!
|
|
336 |
|
674
|
337 |
skip:messageString
|
649
|
338 |
"skip this test"
|
|
339 |
|
674
|
340 |
self skipIf:true description:messageString
|
|
341 |
|
|
342 |
"Modified (format): / 12-02-2017 / 22:44:11 / cg"
|
647
|
343 |
!
|
|
344 |
|
674
|
345 |
skipIf:aCondition description:messageString
|
647
|
346 |
"skip this test, if some condition is met"
|
|
347 |
|
|
348 |
aCondition value ifTrue: [
|
674
|
349 |
self logSkipped: 'Skipped - ',messageString.
|
|
350 |
TestResult skipped sunitSignalWith: messageString
|
647
|
351 |
].
|
674
|
352 |
|
|
353 |
"Modified (format): / 12-02-2017 / 22:44:17 / cg"
|
222
|
354 |
! !
|
|
355 |
|
|
356 |
!TestAsserter methodsFor:'convenience'!
|
|
357 |
|
687
|
358 |
assert:anObject equals:anotherObject
|
|
359 |
^ self
|
|
360 |
assert: anObject = anotherObject
|
|
361 |
description: [ anObject printString, ' is not equal to ', anotherObject printString ].
|
|
362 |
|
|
363 |
"Modified: / 13-07-2017 / 15:12:26 / cg"
|
|
364 |
!
|
|
365 |
|
|
366 |
assertCollection:actual equals:expected
|
|
367 |
"specialized test method that generates a proper error message for collection"
|
|
368 |
|
|
369 |
^ self
|
|
370 |
assert:(expected = actual)
|
|
371 |
description:[ self class comparingCollectionBetween:actual and:expected ]
|
|
372 |
|
|
373 |
"
|
|
374 |
self basicNew assertCollection:#(1 2 3) equals:#(1 2 3)
|
|
375 |
self basicNew assertCollection:#(1 2 3) equals:#(1 2 3 4)
|
|
376 |
"
|
|
377 |
|
|
378 |
"Created: / 13-07-2017 / 13:46:51 / cg"
|
|
379 |
"Modified (comment): / 13-07-2017 / 15:03:28 / cg"
|
222
|
380 |
! !
|
|
381 |
|
|
382 |
!TestAsserter methodsFor:'logging'!
|
|
383 |
|
|
384 |
logFailure: aString
|
|
385 |
self class logFailure: aString.
|
551
|
386 |
!
|
|
387 |
|
|
388 |
logSkipped: aString
|
|
389 |
self class logSkipped: aString.
|
222
|
390 |
! !
|
|
391 |
|
|
392 |
!TestAsserter methodsFor:'private'!
|
|
393 |
|
|
394 |
executeShould: execBlock inScopeOf: exceptionalEvent
|
|
395 |
|
|
396 |
<resource: #skipInDebuggersWalkBack>
|
|
397 |
|
|
398 |
^ self
|
|
399 |
executeShould: execBlock
|
|
400 |
inScopeOf: exceptionalEvent
|
|
401 |
suchThat: [:ex | true ]
|
|
402 |
|
|
403 |
"Modified: / 05-05-2011 / 20:23:56 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
404 |
!
|
|
405 |
|
|
406 |
executeShould: execBlock inScopeOf: exceptionalEvent suchThat: conditionBlock
|
|
407 |
^[execBlock value.
|
|
408 |
false]
|
|
409 |
sunitOn: exceptionalEvent
|
|
410 |
do:
|
|
411 |
[:ex |
|
|
412 |
ex sunitExitWith:(conditionBlock value: ex)]
|
|
413 |
|
|
414 |
"Created: / 05-05-2011 / 20:08:46 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
415 |
! !
|
|
416 |
|
|
417 |
!TestAsserter class methodsFor:'documentation'!
|
|
418 |
|
286
|
419 |
version
|
647
|
420 |
^ '$Header$'
|
286
|
421 |
!
|
|
422 |
|
223
|
423 |
version_CVS
|
647
|
424 |
^ '$Header$'
|
223
|
425 |
!
|
|
426 |
|
222
|
427 |
version_SVN
|
647
|
428 |
^ '$Id$'
|
222
|
429 |
! !
|
551
|
430 |
|