/
conditions_spec.ik
837 lines (703 loc) · 22.3 KB
/
conditions_spec.ik
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
use("ispec")
describe(DefaultBehavior,
describe("FlowControl",
describe("ignoreErrors",
it("should evaluate the code and return the value",
x = 42
y = 55
ignoreErrors(x+y) should == 97
)
it("should return nil of any kind of error condition is signalled in the code",
ignoreErrors(10/0) should be nil
)
it("should not fail when another condition is signalled",
ignoreErrors(
signal!("Hello World!")
42) should == 42
)
it("should be possible to define a value to return when an error is encountered",
ignoreErrors(
10/0,
:failure) should == :failure
)
it("should only evaluate the second part when an error actually happens",
called = false
ignoreErrors(
42,
called = true
nil) should == 42
called should be false
)
)
describe("ensure",
it("should work with just main code",
ensure(10+10) should == 20
)
it("should execute all the ensure code after the main code",
Ground ensureResults = []
ensure(
ensureResults << :mainCode,
ensureResults << :ensureBlock1,
ensureResults << :ensureBlock2)
ensureResults should == [:mainCode, :ensureBlock1, :ensureBlock2]
)
it("should execute nested ensure blocks",
Ground ensureResults = []
ensure(
ensure(
ensure(
ensureResults << :mainCode,
ensureResults << :ensureBlockInner),
ensureResults << :ensureBlockOuter),
ensureResults << :ensureBlockOutmost)
ensureResults should == [:mainCode, :ensureBlockInner, :ensureBlockOuter, :ensureBlockOutmost]
)
it("should return the result of the main code even though there are ensure blocks",
ensure(
42,
43) should == 42
ensure(
ensure(
42,
44,
45),
43) should == 42
)
it("should execute the ensure code if a restart is invoked outside",
Ground ensureResults = []
bind(restart(outside, fn),
ensure(
invokeRestart(:outside),
ensureResults << :invoked))
ensureResults should == [:invoked]
)
it("should execute the ensure code if a condition is rescued outside",
Ground ensureResults = []
bind(rescue(Condition, fn(c, nil)),
ensure(
signal!(Condition),
ensureResults << :invoked))
ensureResults should == [:invoked]
)
)
)
describe("Conditions",
describe("error!",
it("should signal a Condition Error Default by default",
x = bind(
rescue(fn(c, c)),
error!("something"))
x text should == "something"
x should have kind("Condition Error Default")
)
it("should take an existing condition",
c1 = Condition mimic
cx = nil
bind(
rescue(fn(c, cx = c)),
error!(c1))
cx should == c1
)
it("should take a condition mimic and a set of keyword parameters",
cx = bind(
rescue(fn(c, c)),
error!(Condition, foo: "bar"))
cx foo should == "bar"
cx should not == Condition
)
)
describe("warn!",
it("should signal a Condition Warning Default by default",
x = bind(
rescue(fn(c, c)),
warn!("something"))
x text should == "something"
x should have kind("Condition Warning Default")
)
it("should take an existing condition",
c1 = Condition mimic
cx = nil
bind(
rescue(fn(c, cx = c)),
warn!(c1))
cx should == c1
)
it("should take a condition mimic and a set of keyword parameters",
cx = bind(
rescue(fn(c, c)),
warn!(Condition, foo: "bar"))
cx foo should == "bar"
cx should not == Condition
)
it("should establish an ignore-restart",
gah = nil
bind(
rescue(fn(c, gah)),
handle(fn(c, gah = findRestart(:ignore))),
warn!("something")) should not be nil
)
)
describe("signal!",
it("should take an existing condition",
c1 = Condition mimic
cx = nil
bind(
rescue(fn(c, cx = c)),
signal!(c1))
cx should == c1
)
it("should take a condition mimic and a set of keyword parameters",
cx = bind(
rescue(fn(c, c)),
signal!(Condition, foo: "bar"))
cx foo should == "bar"
cx should not == Condition
)
it("should not execute a handler that's not applicable",
x = 1
C1 = Condition mimic
bind(
handle(C1, fn(c, x = 42)),
signal!("foo"))
x should == 1
)
it("should execute one applicable handler",
x = 1
bind(
handle(fn(c, x = 42)),
signal!("foo"))
x should == 42
)
it("should execute two applicable handler, among some non-applicable",
x = []
C1 = Condition mimic
bind(
handle(C1, fn(c, x << 13)),
bind(
handle(fn(c, x << 15)),
bind(
handle(C1, fn(c, x << 17)),
bind(
handle(Condition, fn(c, x << 19)),
signal!("foo")))))
x should == [19, 15]
)
it("should not unwind the stack when invoking handlers",
x = []
bind(
handle(fn(c, x << 2)),
x << 1
signal!("foo")
x << 3
)
x should == [1,2,3]
)
it("should only invoke handlers up to the limit of the first applicable rescue",
x = []
bind(
handle(fn(c, x << 1)),
handle(fn(c, x << 2)),
rescue(fn(c, x << 3)),
handle(fn(c, x << 4)),
bind(
handle(fn(c, x << 5)),
handle(fn(c, x << 6)),
bind(
handle(fn(c, x << 7)),
signal!("Foo"))))
x should == [7, 6, 5, 4, 3]
)
it("should do nothing if no rescue has been registered for it",
x = 1
signal!("foo")
x++
x should == 2
x = 1
C2 = Condition mimic
bind(
rescue(C2, fn(e, x = 42)),
x++
signal!("something")
x++)
x should == 3
)
it("should transfer control if the condition is matched",
x = 1
bind(
rescue(fn(e, x = 42)),
signal!("something")
x = 13)
x should == 42
)
it("should transfer control to the innermost handler that matches",
x = 1
C1 = Condition mimic
C2 = Condition mimic
bind(
rescue(C1, fn(e, x = 42)),
bind(
rescue(fn(e, x = 444)),
bind(
rescue(C2, fn(e, x = 222)),
signal!("something"))))
x should == 444
)
it("should invoke the handler with the signalled condition",
x = 1
bind(
rescue(fn(e, x = e text)),
signal!("something")
x = 13)
x should == "something"
)
it("should return the value of the handler from the bind of the rescue in question",
bind(
rescue(fn(e, 42)),
signal!("something")
x = 13) should == 42
)
)
describe("handle",
it("should take only one argument, and in that case catch all Conditions",
handle(fn(e, 42)) handler call(1) should == 42
handle(fn) conditions should == [Condition]
)
it("should take one or more Conditions to catch",
c1 = Condition mimic
c2 = Condition mimic
handle(c1, c2, fn(e, 42)) conditions should == [c1, c2]
)
it("should return something that has kind Handler",
handle(fn) should have kind("Handler")
)
)
describe("rescue",
it("should take only one argument, and in that case catch all Conditions",
rescue(fn(e, 42)) handler call(1) should == 42
rescue(fn) conditions should == [Condition]
)
it("should take one or more Conditions to catch",
c1 = Condition mimic
c2 = Condition mimic
rescue(c1, c2, fn(e, 42)) conditions should == [c1, c2]
)
it("should return something that has kind Rescue",
rescue(fn) should have kind("Rescue")
)
)
describe("restart",
it("should take an optional unevaluated name as first argument",
restart(blub, fn) name should == :blub
)
it("should return something that has kind Restart",
restart(fn) should have kind("Restart")
)
it("should take an optional report: argument",
rp = fn("report" println)
restart(report: rp, fn) report should == rp
)
it("should take an optional test: argument",
t1 = fn("test" println)
restart(test: t1, fn) test should == t1
)
it("should take a code argument",
restart(fn(32+43)) code call should == 75
)
)
describe("bind",
it("should evaluate it's last argument and return the result of that",
bind() should be nil
bind(42) should == 42
bind(
restart(fn),
restart(fn),
restart(fn),
42+43
10+12) should == 22
)
it("should fail if any argument except the last doesn't evaluate to a restart",
fn(bind(10, 10)) should signal(Condition Error Type IncorrectType)
)
)
describe("availableRestarts",
it("should return the available restarts",
r = restart(fox, fn)
bind(r,
availableRestarts[0] should == r)
)
it("should use the test of a restart to see if it's correct",
r = restart(fox, fn)
Ground Cond1 = Condition mimic
Ground calledRTest = false
r test = fn(c, Ground calledRTest = true. c == Cond1)
bind(r,
availableRestarts[0] should not == r)
calledRTest should be true
)
it("should get the restarts applicable",
r = restart(fox, fn)
Ground Cond1 = Condition mimic
r test = fn(c, c == Cond1)
bind(r,
availableRestarts(Cond1)[0] should == r)
)
)
describe("findRestart",
it("should return nil if it can't find the named restart",
findRestart(:foo) should be nil
bind(
restart(bar, fn),
findRestart(:foo)) should be nil
)
it("should return the restart if found",
bind(
restart(foo, fn),
findRestart(:foo)) should not be nil
re = restart(foo, fn)
bind(
re,
findRestart(:foo)) should == re
)
it("should return the innermost restart for the name",
re1 = restart(foo, fn)
re2 = restart(foo, fn)
re3 = restart(foo, fn)
bind(
re1,
bind(
re2,
bind(
re3,
findRestart(:foo)))) should == re3
re1 = restart(foo, fn)
re2 = restart(foo, fn)
re3 = restart(foo, fn)
bind(
re1,
bind(
re2,
bind(
re3,
bind(
restart(bar, fn),
findRestart(:foo))))) should == re3
)
it("should fail when given nil",
fn(findRestart(nil)) should signal(Condition Error Type IncorrectType)
fn(bind(
restart,
findRestart(nil))) should signal(Condition Error Invocation TooFewArguments)
fn(bind(
restart(foo, fn),
findRestart(nil))) should signal(Condition Error Type IncorrectType)
)
it("should take a restart as argument and return it when that restart is active",
re = restart(foo, fn)
bind(
restart(foo, fn),
bind(
re,
bind(
restart(foo, fn),
findRestart(re)))) should == re
)
it("should take a restart as argument and return nil when that restart is not active",
re = restart(foo, fn)
bind(
restart(foo, fn),
bind(
restart(foo, fn),
findRestart(re))) should be nil
)
)
describe("invokeRestart",
it("should fail if no restarts of the name is active",
fn(invokeRestart(:bar)) should signal(Condition Error RestartNotActive)
fn(bind(
restart(foo, fn()),
invokeRestart(:bar))) should signal(Condition Error RestartNotActive)
fn(bind(
restart(foo, fn()),
bind(
restart(foo, fn()),
invokeRestart(:bar)))) should signal(Condition Error RestartNotActive)
)
it("should fail if no restarts of the restart is active",
fn(re = restart(bar, fn)
invokeRestart(re)) should signal(Condition Error RestartNotActive)
fn(re = restart(bar, fn)
bind(
restart(foo, fn),
invokeRestart(re))) should signal(Condition Error RestartNotActive)
fn(re = restart(bar, fn)
bind(
restart(foo, fn),
bind(
restart(foo, fn),
invokeRestart(re)))) should signal(Condition Error RestartNotActive)
)
it("should invoke a restart when given the name",
x = 1
bind(
restart(foo, fn(x = 42. 13)),
invokeRestart(:foo)) should == 13
x should == 42
)
it("should invoke a restart when given the restart",
x = 1
re = restart(foo, fn(x = 42. 13))
bind(
re,
invokeRestart(re)) should == 13
x should == 42
)
it("should invoke the innermost restart when given the name",
x = 1
invoked = 0
bind(
restart(foo, fn(invoked++. x = 42. 13)),
bind(
restart(foo, fn(invoked++. x = 43. 14)),
bind(
restart(foo, fn(invoked++. x = 44. 15)),
invokeRestart(:foo)))) should == 15
x should == 44
invoked should == 1
)
it("should invoke the right restart when given an instance",
x = 1
invoked = 0
re = restart(foo, fn(invoked++. x=24. 16))
bind(
restart(foo, fn(invoked++. x = 42. 13)),
bind(
re,
bind(
restart(foo, fn(invoked++. x = 43. 14)),
bind(
restart(foo, fn(invoked++. x = 44. 15)),
invokeRestart(re))))) should == 16
x should == 24
invoked should == 1
)
it("should take arguments and pass these along to the restart",
bind(
restart(foo, fn(x, x)),
invokeRestart(:foo, 13)) should == 13
bind(
restart(foo, fn(x, y, x)),
invokeRestart(:foo, 13, 15)) should == 13
bind(
restart(foo, fn(x, y, y)),
invokeRestart(:foo, 13, 15)) should == 15
)
)
)
)
describe(Restart,
it("should have a name",
Restart name should be nil
)
it("should have a report cell",
Restart report should have kind("LexicalBlock")
)
it("should have a test cell",
Restart test should have kind("LexicalBlock")
)
it("should have a code cell",
Restart code should have kind("LexicalBlock")
)
)
describe(Condition,
it("should have the right kind",
Condition should have kind("Condition")
)
describe("ignore",
it("should evaluate the code and return the value",
x = 42
y = 55
Condition ignore(x+y) should == 97
)
it("should return nil if any kind of condition is signalled in the code",
Condition ignore(10/0) should be nil
Condition ignore(signal!("blah")) should be nil
)
it("should not fail when another condition is signalled",
Condition Error ignore(
signal!("Hello World!")
42) should == 42
)
it("should be possible to define a value to return when an error is encountered",
Condition ignore(
10/0,
:failure) should == :failure
)
it("should only evaluate the second part when an error actually happens",
called = false
Condition ignore(
42,
called = true
nil) should == 42
called should be false
)
it("should only ignore the specific condition",
cc = Condition mimic
cc ignore(
signal!("BLAH")
43) should == 43
cc ignore(
signal!(cc)
43) should be nil
)
)
describe(Condition Default,
it("should have the right kind",
Condition Default should have kind("Condition Default")
)
)
describe(Condition Warning,
it("should have the right kind",
Condition Warning should have kind("Condition Warning")
)
describe(Condition Warning Default,
it("should have the right kind",
Condition Warning Default should have kind("Condition Warning Default")
)
)
)
describe(Condition Error,
it("should have the right kind",
Condition Error should have kind("Condition Error")
)
describe(Condition Error Default,
it("should have the right kind",
Condition Error Default should have kind("Condition Error Default")
)
)
describe(Condition Error Load,
it("should have the right kind",
Condition Error Load should have kind("Condition Error Load")
)
)
describe(Condition Error IO,
it("should have the right kind",
Condition Error IO should have kind("Condition Error IO")
)
)
describe(Condition Error Arithmetic,
it("should have the right kind",
Condition Error Arithmetic should have kind("Condition Error Arithmetic")
)
describe(Condition Error Arithmetic DivisionByZero,
it("should have the right kind",
Condition Error Arithmetic DivisionByZero should have kind("Condition Error Arithmetic DivisionByZero")
)
)
describe(Condition Error Arithmetic NotParseable,
it("should have the right kind",
Condition Error Arithmetic NotParseable should have kind("Condition Error Arithmetic NotParseable")
)
)
)
describe(Condition Error ModifyOnFrozen,
it("should have the right kind",
Condition Error ModifyOnFrozen should have kind("Condition Error ModifyOnFrozen")
)
)
describe(Condition Error NoSuchCell,
it("should have the right kind",
Condition Error NoSuchCell should have kind("Condition Error NoSuchCell")
)
)
describe(Condition Error Invocation,
it("should have the right kind",
Condition Error Invocation should have kind("Condition Error Invocation")
)
describe(Condition Error Invocation NotActivatable,
it("should have the right kind",
Condition Error Invocation NotActivatable should have kind("Condition Error Invocation NotActivatable")
)
)
describe(Condition Error Invocation ArgumentWithoutDefaultValue,
it("should have the right kind",
Condition Error Invocation ArgumentWithoutDefaultValue should have kind("Condition Error Invocation ArgumentWithoutDefaultValue")
)
)
describe(Condition Error Invocation TooManyArguments,
it("should have the right kind",
Condition Error Invocation TooManyArguments should have kind("Condition Error Invocation TooManyArguments")
)
)
describe(Condition Error Invocation TooFewArguments,
it("should have the right kind",
Condition Error Invocation TooFewArguments should have kind("Condition Error Invocation TooFewArguments")
)
)
describe(Condition Error Invocation MismatchedKeywords,
it("should have the right kind",
Condition Error Invocation MismatchedKeywords should have kind("Condition Error Invocation MismatchedKeywords")
)
)
describe(Condition Error Invocation NotSpreadable,
it("should have the right kind",
Condition Error Invocation NotSpreadable should have kind("Condition Error Invocation NotSpreadable")
)
)
describe(Condition Error Invocation NoMatch,
it("should have the right kind",
Condition Error Invocation NoMatch should have kind("Condition Error Invocation NoMatch")
)
)
)
describe(Condition Error CantMimicOddball,
it("should have the right kind",
Condition Error CantMimicOddball should have kind("Condition Error CantMimicOddball")
)
)
describe(Condition Error Index,
it("should have the right kind",
Condition Error Index should have kind("Condition Error Index")
)
)
describe(Condition Error RestartNotActive,
it("should have the right kind",
Condition Error RestartNotActive should have kind("Condition Error RestartNotActive")
)
)
describe(Condition Error NativeException,
it("should have the right kind",
Condition Error NativeException should have kind("Condition Error NativeException")
)
)
describe(Condition Error Parser,
it("should have the right kind",
Condition Error Parser should have kind("Condition Error Parser")
)
describe(Condition Error Parser OpShuffle,
it("should have the right kind",
Condition Error Parser OpShuffle should have kind("Condition Error Parser OpShuffle")
)
)
)
describe(Condition Error CommandLine,
it("should have the right kind",
Condition Error CommandLine should have kind("Condition Error CommandLine")
)
describe(Condition Error CommandLine DontUnderstandOption,
it("should have the right kind",
Condition Error CommandLine DontUnderstandOption should have kind("Condition Error CommandLine DontUnderstandOption")
)
)
)
describe(Condition Error Type,
it("should have the right kind",
Condition Error Type should have kind("Condition Error Type")
)
describe(Condition Error Type IncorrectType,
it("should have the right kind",
Condition Error Type IncorrectType should have kind("Condition Error Type IncorrectType")
)
)
)
)
)