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