/
BlockClosureTest.class.st
477 lines (381 loc) · 12.8 KB
/
BlockClosureTest.class.st
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
"
SUnit tests for BlockClosure
"
Class {
#name : #BlockClosureTest,
#superclass : #TestCase,
#instVars : [
'aBlockContext',
'contextOfaBlockContext'
],
#category : #'Kernel-Tests-Methods'
}
{ #category : #helpers }
BlockClosureTest >> blockWithNonLocalReturn: resultObject [
^[ ^resultObject ]
]
{ #category : #running }
BlockClosureTest >> setUp [
super setUp.
aBlockContext := [100@100 corner: 200@200].
contextOfaBlockContext := thisContext.
]
{ #category : #'tests - evaluating' }
BlockClosureTest >> testCull [
[ ] cull: 1.
[ :x | ] cull: 1.
self should: [ [ :x :y | ] cull: 1 ] raise: Error.
self should: [ [ :x :y :z | ] cull: 1 ] raise: Error.
self should: [ [ :x :y :z :a | ] cull: 1 ] raise: Error.
self should: [ [ :x :y :z :a :b | ] cull: 1 ] raise: Error.
self assert: ([ 0 ] cull: 1) = 0.
self assert: ([ :x | x ] cull: 1) = 1
]
{ #category : #'tests - evaluating' }
BlockClosureTest >> testCullCull [
[ ] cull: 1 cull: 2.
[ :x | ] cull: 1 cull: 2.
[ :x :y | ] cull: 1 cull: 2.
self should: [ [ :x :y :z | ] cull: 1 cull: 2 ] raise: Error.
self should: [ [ :x :y :z :a | ] cull: 1 cull: 2 ] raise: Error.
self should: [ [ :x :y :z :a :b | ] cull: 1 cull: 2 ] raise: Error.
self assert: ([ 0 ] cull: 1 cull: 2) = 0.
self assert: ([ :x | x ] cull: 1 cull: 2) = 1.
self assert: ([ :x :y | y ] cull: 1 cull: 2) = 2
]
{ #category : #'tests - evaluating' }
BlockClosureTest >> testCullCullCull [
[ ] cull: 1 cull: 2 cull: 3.
[ :x | ] cull: 1 cull: 2 cull: 3.
[ :x :y | ] cull: 1 cull: 2 cull: 3.
[ :x :y :z | ] cull: 1 cull: 2 cull: 3.
self should: [ [ :x :y :z :a | ] cull: 1 cull: 2 cull: 3 ] raise: Error.
self should: [ [ :x :y :z :a :b | ] cull: 1 cull: 2 cull: 3 ] raise: Error.
self assert: ([ 0 ] cull: 1 cull: 2 cull: 3) = 0.
self assert: ([ :x | x ] cull: 1 cull: 2 cull: 3) = 1.
self assert: ([ :x :y | y ] cull: 1 cull: 2 cull: 3) = 2.
self assert: ([ :x :y :z | z ] cull: 1 cull: 2 cull: 3) = 3
]
{ #category : #'tests - evaluating' }
BlockClosureTest >> testCullCullCullCull [
[ ]
cull: 1
cull: 2
cull: 3
cull: 4.
[ :x | ]
cull: 1
cull: 2
cull: 3
cull: 4.
[ :x :y | ]
cull: 1
cull: 2
cull: 3
cull: 4.
[ :x :y :z | ]
cull: 1
cull: 2
cull: 3
cull: 4.
[ :x :y :z :a | ]
cull: 1
cull: 2
cull: 3
cull: 4.
self
should: [
[ :x :y :z :a :b | ]
cull: 1
cull: 2
cull: 3
cull: 4 ]
raise: Error.
self
assert:
([ 0 ]
cull: 1
cull: 2
cull: 3
cull: 4) = 0.
self
assert:
([ :x | x ]
cull: 1
cull: 2
cull: 3
cull: 4) = 1.
self
assert:
([ :x :y | y ]
cull: 1
cull: 2
cull: 3
cull: 4) = 2.
self
assert:
([ :x :y :z | z ]
cull: 1
cull: 2
cull: 3
cull: 4) = 3.
self
assert:
([ :x :y :z :a | a ]
cull: 1
cull: 2
cull: 3
cull: 4) = 4
]
{ #category : #tests }
BlockClosureTest >> testNew [
self should: [Context new: 5] raise: Error.
[Context new: 5]
ifError: [:error | error = 'Error: Contexts must only be created with newForMethod:'].
[Context new]
ifError: [:error | error = 'Error: Contexts must only be created with newForMethod:'].
[Context basicNew]
ifError: [:error | error = 'Error: Contexts must only be created with newForMethod:'].
]
{ #category : #tests }
BlockClosureTest >> testNoArguments [
| block1 block2 |
"avoid compile error in GemStone"
block1 := [:arg | 1 + 2].
block2 := [:arg1 :arg2 | 1 + 2].
[10
timesRepeat: block1]
ifError: [:err | self deny: err = 'This block requires 1 arguments.'].
[10
timesRepeat: block2]
ifError: [:err | self deny: err = 'This block requires 2 arguments.']
]
{ #category : #'tests - on-fork' }
BlockClosureTest >> testOnFork [
"Test that if code runs without errors, there is no fork! "
| result1 result2 |
result2 := nil.
result1 := [ 1 ] on: Exception fork: [ result2 := 2 ].
Processor yield.
self assert: (result1 = 1).
self assert: (result2 isNil ).
]
{ #category : #'tests - on-fork' }
BlockClosureTest >> testOnForkErrorExecutesBlock [
"Test that if code runs with error, there is fork"
| result sema |
sema := Semaphore new.
result := nil.
[ 1/0 ] on: Exception fork: [ result := 2. sema signal].
sema wait.
"and of course result should be not nil "
self assert: result = 2.
]
{ #category : #'tests - on-fork' }
BlockClosureTest >> testOnForkErrorOnSeparateProcess [
"Test that if code runs with error, there is fork"
| result forkedProc sema |
sema := Semaphore new.
result := [ 1/0 ] on: Exception fork: [ forkedProc := Processor activeProcess. sema signal ].
sema wait.
self assert: (forkedProc ~~ Processor activeProcess).
]
{ #category : #'tests - on-fork' }
BlockClosureTest >> testOnForkErrorReturnsNil [
"Test that if code runs with error, there is fork"
| result sema |
sema := Semaphore new.
result := [ 1/0 ] on: Exception fork: [ sema signal. ].
sema wait.
"in case of error, evaluation result should be nil"
self assert: result isNil.
]
{ #category : #'tests - on-fork' }
BlockClosureTest >> testOnForkErrorTakesLessThanOneSecond [
"Test that if code runs with error, there is fork"
| sema timeout |
self flag: 'This test is too brittle, failing often on Windows CI'; skip.
self flag: 'The following line makes the test pass under headless linux. Everywhere else this test works'.
Smalltalk os isUnix
ifTrue: [ 1 milliSecond wait ].
sema := Semaphore new.
[ 1/0 ] on: Exception fork: [ sema signal ].
timeout := (sema waitTimeoutSeconds: 1).
self assert: timeout == false
]
{ #category : #'tests - on-fork' }
BlockClosureTest >> testOnForkSplit [
"Test that when forking, the stack are split correctly (there is no any contexts referenced by both processes)"
| sema timeout forkedContexts myContexts c |
self flag: 'This test is too brittle, failing often on CI'; skip.
sema := Semaphore new.
[ 1/0 ] on: Exception fork: [ | ctx |
forkedContexts := IdentitySet new.
ctx := thisContext.
[ ctx notNil ] whileTrue: [ forkedContexts add: ctx. ctx := ctx sender ].
sema signal ].
timeout := (sema waitTimeoutSeconds: 1).
self assert: timeout == false description: 'fork lasted more than one second'.
myContexts := IdentitySet new.
c := thisContext.
[ c notNil ] whileTrue: [ myContexts add: c. c := c sender ].
self assert: (myContexts noneSatisfy: [:b | forkedContexts includes: b ]) description: 'myContexts are not within forkedContexts'.
self assert: (forkedContexts noneSatisfy: [:b | myContexts includes: b ]) description: 'forkedContexts are not within myContexts'.
]
{ #category : #tests }
BlockClosureTest >> testOneArgument [
| c |
c := OrderedCollection new.
c add: 'hello'.
[c
do: [1 + 2]]
ifError: [:err | self deny: err = 'This block requires 0 arguments.'].
[c
do: [:arg1 :arg2 | 1 + 2]]
ifError: [:err | self deny: err = 'This block requires 2 arguments.']
]
{ #category : #'tests - printing' }
BlockClosureTest >> testPrintOn [
self assert: [ 1 + 2 ] printString equals: '[ 1 + 2 ]'
]
{ #category : #tests }
BlockClosureTest >> testSetUp [
"Note: In addition to verifying that the setUp worked the way it was expected to, testSetUp is used to illustrate the meaning of the simple access methods, methods that are not normally otherwise 'tested'"
self assert: aBlockContext home = contextOfaBlockContext.
self assert: aBlockContext receiver = self.
"Depending on the closure implementation, it's either a compiled block, a compiled method or nil."
self assert: (aBlockContext method isNil or: [aBlockContext method isKindOf: CompiledCode]).
]
{ #category : #testing }
BlockClosureTest >> testSupplyAnswerOfFillInTheBlank [
self should: ['blue' = ([UIManager default request: 'Your favorite color?']
valueSupplyingAnswer: #('Your favorite color?' 'blue'))]
]
{ #category : #testing }
BlockClosureTest >> testSupplyAnswerOfFillInTheBlankUsingDefaultAnswer [
self should: ['red' = ([UIManager default request: 'Your favorite color?' initialAnswer: 'red']
valueSupplyingAnswer: #('Your favorite color?' #default))]
]
{ #category : #tests }
BlockClosureTest >> testSupplyAnswerThroughNestedBlocks [
self should: [true = ([[self confirm: 'You like Smalltalk?']
valueSupplyingAnswer: #('Blub' false)] valueSupplyingAnswer: #('Smalltalk' true))]
]
{ #category : #tests }
BlockClosureTest >> testSupplyAnswerUsingOnlySubstringOfQuestion [
self should: [false = ([self confirm: 'You like Smalltalk?']
valueSupplyingAnswer: #('like' false))]
]
{ #category : #tests }
BlockClosureTest >> testSupplyAnswerUsingRegexMatchOfQuestion [
(String includesSelector: #matchesRegex:) ifFalse: [^ self].
self should: [true = ([self confirm: 'You like Smalltalk?']
valueSupplyingAnswer: #('.*Smalltalk\?' true))]
]
{ #category : #tests }
BlockClosureTest >> testSupplyAnswerUsingTraditionalMatchOfQuestion [
self should: [true = ([self confirm: 'You like Smalltalk?']
valueSupplyingAnswer: #('*Smalltalk#' true))]
]
{ #category : #tests }
BlockClosureTest >> testSupplySameAnswerToAllQuestions [
self should: [true = ([self confirm: 'You like Smalltalk?'] valueSupplyingAnswer: true)].
self should: [#(true true) = ([{self confirm: 'One'. self confirm: 'Two'}] valueSupplyingAnswer: true)].
]
{ #category : #tests }
BlockClosureTest >> testSupplySeveralAnswersToSeveralQuestions [
self should: [#(false true) = ([{self confirm: 'One'. self confirm: 'Two'}]
valueSupplyingAnswers: #( ('One' false) ('Two' true) ))].
self should: [#(true false) = ([{self confirm: 'One'. self confirm: 'Two'}]
valueSupplyingAnswers: #( ('One' true) ('Two' false) ))]
]
{ #category : #tests }
BlockClosureTest >> testSupplySpecificAnswerToQuestion [
self should: [false = ([self confirm: 'You like Smalltalk?']
valueSupplyingAnswer: #('You like Smalltalk?' false))]
]
{ #category : #tests }
BlockClosureTest >> testSuppressInform [
self should: [[nil inform: 'Should not see this message or this test failed!'] valueSuppressingAllMessages isNil]
]
{ #category : #tests }
BlockClosureTest >> testSuppressInformUsingStringMatchOptions [
self should: [([nil inform: 'Should not see this message or this test failed!'] valueSuppressingMessages: #('Should not see this message or this test failed!')) isNil].
self should: [([nil inform: 'Should not see this message or this test failed!'] valueSuppressingMessages: #('not see this message')) isNil].
self should: [([nil inform: 'Should not see this message or this test failed!'] valueSuppressingMessages: #('*message*failed#')) isNil].
]
{ #category : #'tests - evaluating' }
BlockClosureTest >> testValueWithArguments [
self shouldnt: [ aBlockContext valueWithArguments: #() ] raise: ArgumentsCountMismatch.
self should: [ aBlockContext valueWithArguments: #(1) ] raise: ArgumentsCountMismatch
withExceptionDo: [ :err |
self assert: err expectedArgumentsCount equals: 0.
self assert: err calledArgumentsCount equals: 1 ].
self should: [ [ :i | 3 + 4 ] valueWithArguments: #(1 2) ] raise: ArgumentsCountMismatch
withExceptionDo: [ :err |
self assert: err expectedArgumentsCount equals: 1.
self assert: err calledArgumentsCount equals: 2 ].
]
{ #category : #'tests - evaluating' }
BlockClosureTest >> testValueWithExitBreak [
| val |
[ :break |
1 to: 10 do: [ :i |
val := i.
i = 4 ifTrue: [break value].
]
] valueWithExit.
self assert: val = 4.
]
{ #category : #'tests - evaluating' }
BlockClosureTest >> testValueWithExitContinue [
| val last |
val := 0.
1 to: 10 do: [ :i |
[ :continue |
i = 4 ifTrue: [continue value].
val := val + 1.
last := i
] valueWithExit.
].
self assert: val = 9.
self assert: last = 10.
]
{ #category : #'tests - evaluating' }
BlockClosureTest >> testValueWithPossibleArgs [
| block blockWithArg blockWith2Arg |
block := [ 1 ].
blockWithArg := [ :arg | arg ].
blockWith2Arg := [ :arg1 :arg2 |
{arg1.
arg2} ].
self assert: (block valueWithPossibleArgs: #()) = 1.
self assert: (block valueWithPossibleArgs: #(1)) = 1.
self assert: (blockWithArg valueWithPossibleArgs: #()) isNil.
self assert: (blockWithArg valueWithPossibleArgs: #(1)) = 1.
self assert: (blockWithArg valueWithPossibleArgs: #(1 2)) = 1.
self
assert:
(blockWith2Arg valueWithPossibleArgs: #())
=
{nil.
nil}.
self
assert:
(blockWith2Arg valueWithPossibleArgs: #(1))
=
{1.
nil}.
self assert: (blockWith2Arg valueWithPossibleArgs: #(1 2)) = #(1 2).
self assert: (blockWith2Arg valueWithPossibleArgs: #(1 2 3)) = #(1 2)
]
{ #category : #'tests - evaluating' }
BlockClosureTest >> testValueWithPossibleArgument [
| block blockWithArg blockWith2Arg |
block := [1].
blockWithArg := [:arg | arg].
blockWith2Arg := [:arg1 :arg2 | {arg1. arg2}].
self assert: (block valueWithPossibleArgument: 1) = 1.
self assert: (blockWithArg valueWithPossibleArgument: 1) = 1.
self assert: (blockWith2Arg valueWithPossibleArgument: 1) = {1 . nil}.
]