-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathREADME
2899 lines (2196 loc) · 88.6 KB
/
README
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
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Try Manual
###### \[in package TRY\]
## The try ASDF System
- Version: 0.0.1
- Description: Try is an extensible test framework with equal support
for interactive and non-interactive workflows.
- Long Description: Try stays as close to normal Lisp evaluation
rules as possible. Tests are functions that record the checks they
perform as events. These events provide the means of customization
of what to debug, print, rerun. There is a single fundamental check,
the extensible IS macro. Everything else is built on top.
- Licence: MIT, see COPYING.
- Author: Gábor Melis
- Mailto: [[email protected]](mailto:[email protected])
- Homepage: [http://melisgl.github.io/try](http://melisgl.github.io/try)
- Bug tracker: [https://github.com/melisgl/try/issues](https://github.com/melisgl/try/issues)
- Source control: [GIT](https://github.com/melisgl/try.git)
## Links
Here is the [official repository](https://github.com/melisgl/try)
and the [HTML
documentation](http://melisgl.github.io/mgl-pax-world/try-manual.html)
for the latest version.
## Tutorial
Try is a library for unit testing with equal support for
interactive and non-interactive workflows. Tests are functions, and
almost everything else is a condition, whose types feature
prominently in parameterization.
Try is is what we get if we make tests functions and build a test
framework on top of the condition system as
[Stefil](https://common-lisp.net/project/stefil/index-old.shtml) did
but also address the issue of rerunning and replaying, make the
IS check more capable, use the types of the condition hierarchy
to parameterize what to debug, print, rerun, and finally document
the whole thing.
##### Looking for Truth
@TRY/IS is a replacement for CL:ASSERT, that can capture values of
subforms to provide context to failures:
```common-lisp
(is (= (1+ 5) 0))
.. debugger invoked on UNEXPECTED-RESULT-FAILURE:
.. UNEXPECTED-FAILURE in check:
.. (IS (= #1=(1+ 5) 0))
.. where
.. #1# = 6
```
This is a PAX transcript,
output is prefixed with `..`. Readable and unreadable return values
are prefixed with `=>` and `==>`, respectively.
Note the `#N#` syntax due to *PRINT-CIRCLE*.
##### Checking Multiple Values
IS automatically captures values of
arguments to functions like `1+` in the above example. Values of
other interesting subforms can be explicitly
captured. IS supports capturing multiple
values and can be taught how to deal with macros. The combination of these
features allows MATCH-VALUES to be implementable as tiny extension:
```common-lisp
(is (match-values (values (1+ 5) "sdf")
(= * 0)
(string= * "sdf")))
.. debugger invoked on UNEXPECTED-RESULT-FAILURE:
.. UNEXPECTED-FAILURE in check:
.. (IS
.. (MATCH-VALUES #1=(VALUES (1+ 5) #2="sdf")
.. (= * 0)
.. (STRING= * "sdf")))
.. where
.. #1# == 6
.. #2#
```
In the body of MATCH-VALUES, `*` is bound to
successive return values of some form, here `(VALUES (1+ 5) "sdf")`.
MATCH-VALUES comes with an automatic rewrite rule that captures the
values of this form, which are printed above as `#1# == 6 #2#`. IS
is flexible enough that all other checks (SIGNALS, SIGNALS-NOT,
INVOKES-DEBUGGER, INVOKES-DEBUGGER-NOT, FAILS, and IN-TIME are built
on top of it.
##### Writing Tests
Beyond IS, a fancy ASSERT, Try provides tests, which are Lisp
functions that record their execution in TRIAL objects. Let's define
a test and run it:
```common-lisp
(deftest should-work ()
(is t))
(should-work)
.. SHOULD-WORK ; TRIAL-START
.. ⋅ (IS T) ; EXPECTED-RESULT-SUCCESS
.. ⋅ SHOULD-WORK ⋅1 ; EXPECTED-VERDICT-SUCCESS
..
==> #<TRIAL (SHOULD-WORK) EXPECTED-SUCCESS 0.000s ⋅1>
```
Try is driven by conditions, and the comments to the right give the
type of the condition that is printed on that line. The `⋅`
character marks successes.
We could have run our test with `(TRY 'SHOULD-WORK)` as well, which
does pretty much the same thing except it defaults to never entering
the debugger, whereas calling a test function directly enters the
debugger on events whose type matches the type in the variable
*DEBUG*.
```common-lisp
(try 'should-work)
.. SHOULD-WORK
.. ⋅ (IS T)
.. ⋅ SHOULD-WORK ⋅1
..
==> #<TRIAL (SHOULD-WORK) EXPECTED-SUCCESS 0.000s ⋅1>
```
##### Test Suites
Test suites are just tests that call other tests.
```common-lisp
(deftest my-suite ()
(should-work)
(is (= (foo) 5)))
(defun foo ()
4)
(try 'my-suite)
.. MY-SUITE ; TRIAL-START
.. SHOULD-WORK ; TRIAL-START
.. ⋅ (IS T) ; EXPECTED-RESULT-SUCCESS
.. ⋅ SHOULD-WORK ⋅1 ; EXPECTED-VERDICT-SUCCESS
.. ⊠ (IS (= #1=(FOO) 5)) ; UNEXPECTED-RESULT-FAILURE
.. where
.. #1# = 4
.. ⊠ MY-SUITE ⊠1 ⋅1 ; UNEXPECTED-VERDICT-FAILURE
..
==> #<TRIAL (MY-SUITE) UNEXPECTED-FAILURE 0.000s ⊠1 ⋅1>
```
`⊠` marks UNEXPECTED-FAILUREs. Note how the failure of `(IS (= (FOO)
5))` caused `MY-SUITE` to fail as well. Finally, the `⊠1` and the
`⋅1` in the TRIAL's printed representation are the event
counts.
##### Filtering Output
To focus on the important bits, we can print only the UNEXPECTED
events:
```common-lisp
(try 'my-suite :print 'unexpected)
.. MY-SUITE
.. ⊠ (IS (= #1=(FOO) 5))
.. where
.. #1# = 4
.. ⊠ MY-SUITE ⊠1 ⋅1
..
==> #<TRIAL (MY-SUITE) UNEXPECTED-FAILURE 0.000s ⊠1 ⋅1>
```
Note that `SHOULD-WORK` is still run, and its check's success is
counted as evidenced by`⋅1`. The above effect can also be achieved
without running the tests again with REPLAY-EVENTS.
##### Debugging
Let's figure out what went wrong:
```
(my-suite)
;;; Here the debugger is invoked:
UNEXPECTED-FAILURE in check:
(IS (= #1=(FOO) 5))
where
#1# = 4
Restarts:
0: [RECORD-EVENT] Record the event and continue.
1: [FORCE-EXPECTED-SUCCESS] Change outcome to TRY:EXPECTED-RESULT-SUCCESS.
2: [FORCE-UNEXPECTED-SUCCESS] Change outcome to TRY:UNEXPECTED-RESULT-SUCCESS.
3: [FORCE-EXPECTED-FAILURE] Change outcome to TRY:EXPECTED-RESULT-FAILURE.
4: [ABORT-CHECK] Change outcome to TRY:RESULT-ABORT*.
5: [SKIP-CHECK] Change outcome to TRY:RESULT-SKIP.
6: [RETRY-CHECK] Retry check.
7: [ABORT-TRIAL] Record the event and abort trial TRY::MY-SUITE.
8: [SKIP-TRIAL] Record the event and skip trial TRY::MY-SUITE.
9: [RETRY-TRIAL] Record the event and retry trial TRY::MY-SUITE.
10: [SET-TRY-DEBUG] Supply a new value for :DEBUG of TRY:TRY.
11: [RETRY] Retry SLIME interactive evaluation request.
```
In the [SLIME](https://common-lisp.net/project/slime/doc/html/)
debugger, we press `v` on the frame of the call to `MY-SUITE` to
navigate to its definition, realize what the problem is and fix
`FOO`:
```common-lisp
(defun foo ()
5)
```
Now, we select the `RETRY-TRIAL` restart, and on the retry
`MY-SUITE` passes. The full output is:
```
MY-SUITE
SHOULD-WORK
⋅ (IS T)
⋅ SHOULD-WORK ⋅1
WARNING: redefining TRY::FOO in DEFUN
⊠ (IS (= #1=(FOO) 5))
where
#1# = 4
MY-SUITE retry #1
SHOULD-WORK
⋅ (IS T)
⋅ SHOULD-WORK ⋅1
⋅ (IS (= (FOO) 5))
⋅ MY-SUITE ⋅2
```
##### Rerunning Stuff
Instead of working interactively, one can fix the failing test and
rerun it. Now, let's fix `MY-SUITE` and rerun it:
```common-lisp
(deftest my-suite ()
(should-work)
(is nil))
(try 'my-suite)
.. MY-SUITE
.. SHOULD-WORK
.. ⋅ (IS T)
.. ⋅ SHOULD-WORK ⋅1
.. ⊠ (IS NIL)
.. ⊠ MY-SUITE ⊠1 ⋅1
..
==> #<TRIAL (MY-SUITE) UNEXPECTED-FAILURE 0.000s ⊠1 ⋅1>
(deftest my-suite ()
(should-work)
(is t))
(try !)
.. MY-SUITE
.. ⋅ (IS T)
.. ⋅ MY-SUITE ⋅1
..
==> #<TRIAL (MY-SUITE) EXPECTED-SUCCESS 0.004s ⋅1>
```
Here, `!` refers to the most recent TRIAL returned by TRY. When a
trial is passed to TRY or is `FUNCALL`ed, trials in it that match
the type in TRY's RERUN argument are rerun (here, UNEXPECTED by
default). `SHOULD-WORK` and its check are `EXPECTED-SUCCESS`es,
hence they don't match UNEXPECTED and are not rerun.
##### Conditional Execution
Conditional execution can be achieved simply testing the TRIAL
object returned by @TRY/TESTS.
```
(deftest my-suite ()
(when (passedp (should-work))
(is t :msg "a test that depends on SHOULD-WORK")
(when (is nil)
(is nil :msg "never run"))))
```
##### Skipping
Sometimes, we do not know up front that a test should not be
executed. Calling SKIP-TRIAL unwinds from the CURRENT-TRIAL and sets
it skipped.
```common-lisp
(deftest my-suite ()
(is t)
(skip-trial)
(is nil))
(my-suite)
==> #<TRIAL (MY-SUITE) SKIP 0.000s ⋅1>
```
In the above, `(IS T)` was executed, but `(IS NIL)` was not.
##### Expecting Outcomes
```common-lisp
(deftest known-broken ()
(with-failure-expected (t)
(is nil)))
(known-broken)
.. KNOWN-BROKEN
.. × (IS NIL)
.. ⋅ KNOWN-BROKEN ×1
..
==> #<TRIAL (KNOWN-BROKEN) EXPECTED-SUCCESS 0.000s ×1>
```
`×` marks EXPECTED-FAILUREs. `(WITH-SKIP (T) ...)` makes all checks
successes and failures EXPECTED, which are counted in their own
*CATEGORIES* by default but don't make the enclosing tests to fail.
Also see WITH-EXPECTED-OUTCOME.
##### Running Tests on Definition
With *RUN-DEFTEST-WHEN*, tests on in various EVAL-WHEN situations.
To run tests on evaluation, as in SLIME `C-M-x`, `slime-eval-defun`:
```common-lisp
(setq *run-deftest-when* :execute)
(deftest some-test ()
(is t))
.. SOME-TEST
.. ⋅ (IS T)
.. ⋅ SOME-TEST ⋅1
..
=> SOME-TEST
(setq *run-deftest-when* nil)
```
##### Fixtures
There is no direct support for fixtures in Try. One can easily write
macros like the following.
```
(defvar *server* nil)
(defmacro with-xxx (&body body)
`(flet ((,with-xxx-body ()
,@body))
(if *server*
(with-xxx-body)
(with-server (make-expensive-server)
(with-xxx-body)))))
```
Plus, with support for selectively @TRY/RERUN, the need for fixtures
is lessened.
##### Packages
The suggested way of writing tests is to call test functions
explicitly:
```
(defpackage :some-test-package
(:use #:common-lisp #:try))
(in-package :some-test-package)
(deftest test-all ()
(test-this)
(test-that))
(deftest test-this ()
(test-this/more))
(deftest test-this/more ()
(is t))
(deftest test-that ()
(is t))
(deftest not-called ()
(is t))
(defun test ()
(warn-on-tests-not-run ((find-package :some-test-package))
(try 'test-all)))
(test)
.. TEST-ALL
.. TEST-THIS
.. TEST-THIS/MORE
.. ⋅ (IS T)
.. ⋅ TEST-THIS/MORE ⋅1
.. ⋅ TEST-THIS ⋅1
.. TEST-THAT
.. ⋅ (IS T)
.. ⋅ TEST-THAT ⋅1
.. ⋅ TEST-ALL ⋅2
.. WARNING: Test NOT-CALLED not run.
==> #<TRIAL (TEST-ALL) EXPECTED-SUCCESS 0.012s ⋅2>
```
Note how the TEST function uses WARN-ON-TESTS-NOT-RUN to catch any
tests defined in `SOME-TEST-PACKAGE` that were not run. Tests can be
deleted by FMAKUNBOUND, UNINTERN, or by redefining the function with
DEFUN. Tests defined in a given package can be listed with
LIST-PACKAGE-TESTS.
This style allows higher level tests to establish the dynamic
environment necessary for lower level tests.
## Emacs Integration
The Elisp `mgl-try` interactive command runs a Try test and
displays its output in a `lisp-mode` buffer with minor modes
`outline-mode` and `mgl-try-mode`. It is assumed that the lisp is
running under [Slime](https://slime.common-lisp.dev/). In the
buffer,
- use `M-.` to visit a test function;
- move between UNEXPECTED events with keys `p` and `n`;
- move between events which are not EXPECTED-SUCCESSes with `P`
and `N`;
- rerun the most recent trial (TRY:!) with `r` (subject to the
filtering described @TRY/RERUN);
- rerun the most recently finished test with `R` (and all tests it
calls);
- run an arbitrary test with `t` (defaults to symbol under point);
- some low-level outline mode commands are also given convenient
bindings:
<tab> outline-cycle
C-p outline-previous-visible-heading
C-n outline-next-visible-heading
U outline-up-heading
### Emacs Setup
Load `src/mgl-try.el` in Emacs.
If you installed Try with Quicklisp, the location of `mgl-try.el`
may change with updates, and you may want to copy the current
version of `mgl-try.el` to a stable location:
(try:install-try-elisp "~/quicklisp/")
Then, assuming the Elisp file is in the quicklisp directory, add
something like this to your `.emacs`:
```elisp
(load "~/quicklisp/mgl-try.el")
```
- [function] INSTALL-TRY-ELISP TARGET-DIR
Copy `mgl-try.el` distributed with this package to TARGET-DIR.
## Events
Try is built around events implemented as CONDITIONs.
Matching the types of events to *DEBUG*, *COUNT*, *COLLECT*, *RERUN*,
*PRINT*, and *DESCRIBE* is what gives Try its flexibility.
### Middle Layer of Events
The event hierarchy is fairly involved, so let's start in the middle.
The condition EVENT has 4 disjoint subclasses:
- TRIAL-START, which corresponds to the entry to a test (see
@TRY/TESTS),
- VERDICT, the OUTCOME of a TRIAL,
- RESULT, the OUTCOME of a check (see @TRY/CHECKS), and
- ERROR\*, an unexpected CL:ERROR or unadorned non-local exit.
```common-lisp
(let (;; We don't want to debug nor print a backtrace for the error below.
(*debug* nil)
(*describe* nil))
;; signals TRIAL-START / VERDICT-ABORT* on entry / exit
(with-test (demo)
;; signals EXPECTED-RESULT-SUCCESS
(is t)
;; signals UNHANDLED-ERROR with a nested CL:ERROR
(error "xxx")))
.. DEMO ; TRIAL-START
.. ⋅ (IS T) ; EXPECTED-RESULT-SUCCESS (⋅)
.. ⊟ "xxx" (SIMPLE-ERROR) ; UNHANDLED-ERROR (⊟)
.. ⊟ DEMO ⊟1 ⋅1 ; VERDICT-ABORT* (⊟)
..
==> #<TRIAL (WITH-TEST (DEMO)) ABORT* 0.004s ⊟1 ⋅1>
```
### Concrete Events
The non-abstract condition classes of events that are actually
signalled are called concrete.
TRIAL-START is a concrete event class. RESULTs and VERDICTs have six
concrete subclasses:
- EXPECTED-RESULT-SUCCESS, UNEXPECTED-RESULT-SUCCESS,
EXPECTED-RESULT-FAILURE, UNEXPECTED-RESULT-FAILURE,
RESULT-SKIP, RESULT-ABORT\*
- EXPECTED-VERDICT-SUCCESS, UNEXPECTED-VERDICT-SUCCESS,
EXPECTED-VERDICT-FAILURE, UNEXPECTED-VERDICT-FAILURE,
VERDICT-SKIP, VERDICT-ABORT\*
ERROR\* is an abstract class with two concrete subclasses:
- UNHANDLED-ERROR, signalled when a CL:ERROR reaches the handler set
up by DEFTEST or WITH-TEST, or when the debugger is invoked.
- NLX, signalled when no error was detected by the handler, but the
trial finishes with a non-local exit.
These are the 15 concrete event classes.
### Event Glue
These condition classes group various bits of the
@TRY/CONCRETE-EVENTS and the @TRY/MIDDLE-LAYER-OF-EVENTS for ease of
reference.
Concrete event classes except TRIAL-START are subclasses of
hyphen-separated words in their name. For example,
UNEXPECTED-RESULT-FAILURE inherits from UNEXPECTED, RESULT, and
FAILURE, so it matches types such as UNEXPECTED or `(AND UNEXPECTED
RESULT)`.
- [condition] EVENT
Common abstract superclass of all events in Try.
- [condition] EXPECTED EVENT
Concrete condition classes with EXPECTED in their
name are subclasses of EXPECTED. SKIP is also a subclass of
EXPECTED.
- [condition] UNEXPECTED EVENT
Concrete condition classes with UNEXPECTED in their
name are subclasses of UNEXPECTED. ABORT\* is also a subclass of
UNEXPECTED.
- [condition] SUCCESS EVENT
See @TRY/CHECKS and @TRY/TRIAL-VERDICTS for how
SUCCESS or FAILURE is decided.
- [condition] FAILURE EVENT
See SUCCESS.
- [condition] DISMISSAL EVENT
The third possibility after SUCCESS and FAILURE.
Either SKIP or ABORT\*.
- [condition] ABORT* UNEXPECTED
`RESULT-ABORT*`, `VERDICT-ABORT*` or ERROR\*.
- [condition] SKIP EXPECTED DISMISSAL
RESULT-SKIP or VERDICT-SKIP.
- [condition] LEAF EVENT
RESULT or ERROR\*.
- [type] EXPECTED-SUCCESS
A shorthand for `(AND EXPECTED SUCCESS)`.
- [type] UNEXPECTED-SUCCESS
A shorthand for `(AND UNEXPECTED SUCCESS)`.
- [type] EXPECTED-FAILURE
A shorthand for `(AND EXPECTED FAILURE)`.
- [type] UNEXPECTED-FAILURE
A shorthand for `(AND UNEXPECTED FAILURE)`.
- [type] PASS
An OUTCOME that's not an ABORT\* or an UNEXPECTED FAILURE.
- [type] FAIL
An ABORT\* or an UNEXPECTED FAILURE.
### Printing Events
- [variable] *EVENT-PRINT-BINDINGS* ((\*PRINT-CIRCLE\* T))
EVENTs are conditions signalled in code that may change printer
variables such as *PRINT-CIRCLE*, *PRINT-LENGTH*, etc. To control
how events are printed, the list of variable bindings in
*EVENT-PRINT-BINDINGS* is established whenever an EVENT is printed
as if with:
```
(progv (mapcar #'first *event-print-bindings*)
(mapcar #'second *event-print-bindings*)
...)
```
The default value ensures that shared structure is recognized (see
@TRY/CAPTURES). If the `#N#` syntax feels cumbersome, then change
this variable.
### Event Restarts
Only RECORD-EVENT is applicable to all EVENTs. See
@TRY/CHECK-RESTARTS, @TRY/TRIAL-RESTARTS for more.
- [function] RECORD-EVENT &OPTIONAL CONDITION
This restart is always the first restart available when an EVENT is
signalled running under TRY (i.e. there is a CURRENT-TRIAL). TRY
always invokes RECORD-EVENT when handling events.
### Outcomes
- [condition] OUTCOME EVENT
An OUTCOME is the resolution of either a TRIAL or a
check (see @TRY/CHECKS), corresponding to subclasses VERDICT and
RESULT.
- [macro] WITH-EXPECTED-OUTCOME (EXPECTED-TYPE) &BODY BODY
When an OUTCOME is to be signalled, EXPECTED-TYPE determines
whether it's going to be EXPECTED. The concrete OUTCOME classes are
`{EXPECTED,UNEXPECTED}-{RESULT,VERDICT}-{SUCCESS,FAILURE}` (see
@TRY/EVENTS), of which RESULT or VERDICT and SUCCESS or FAILURE are
already known. If a RESULT FAILURE is to be signalled, then the
moral equivalent of `(SUBTYPEP '(AND RESULT FAILURE) EXPECTED-TYPE)`
is evaluated and depending on whether it's true,
EXPECTED-RESULT-FAILURE or UNEXPECTED-RESULT-FAILURE is signalled.
By default, SUCCESS is expected. The following example shows how to
expect both SUCCESS and FAILURE for RESULTs, while requiring
VERDICTs to succeed:
```common-lisp
(let ((*debug* nil))
(with-expected-outcome ('(or result (and verdict success)))
(with-test (t1)
(is nil))))
.. T1
.. × (IS NIL)
.. ⋅ T1 ×1
..
==> #<TRIAL (WITH-TEST (T1)) EXPECTED-SUCCESS 0.000s ×1>
```
This is equivalent to `(WITH-FAILURE-EXPECTED () ...)`. To make
result failures expected but result successes unexpected:
```common-lisp
(let ((*debug* nil))
(with-expected-outcome ('(or (and result failure) (and verdict success)))
(with-test (t1)
(is t)
(is nil))))
.. T1
.. ⊡ (IS T)
.. × (IS NIL)
.. ⋅ T1 ⊡1 ×1
..
==> #<TRIAL (WITH-TEST (T1)) EXPECTED-SUCCESS 0.000s ⊡1 ×1>
```
This is equivalent to `(WITH-FAILURE-EXPECTED ('FAILURE) ...)`. The
final example leaves result failures unexpected but makes both
verdict successes and failures expected:
```common-lisp
(let ((*debug* nil))
(with-expected-outcome ('(or (and result success) verdict))
(with-test (t1)
(is nil))))
.. T1
.. ⊠ (IS NIL)
.. × T1 ⊠1
..
==> #<TRIAL (WITH-TEST (T1)) EXPECTED-FAILURE 0.004s ⊠1>
```
- [macro] WITH-FAILURE-EXPECTED (&OPTIONAL (RESULT-EXPECTED-TYPE T) (VERDICT-EXPECTED-TYPE ''SUCCESS)) &BODY BODY
A convenience macro on top of WITH-EXPECTED-OUTCOME,
WITH-FAILURE-EXPECTED expects VERDICTs to have VERDICT-EXPECTED-TYPE
and RESULTs to have RESULT-EXPECTED-TYPE. A simple
`(WITH-FAILURE-EXPECTED () ...)` makes all RESULT SUCCESSes and
FAILUREs EXPECTED. `(WITH-FAILURE-EXPECTED ('FAILURE) ..)` expects
FAILUREs only, and any SUCCESSes will be UNEXPECTED.
- [macro] WITH-SKIP (&OPTIONAL (SKIP T)) &BODY BODY
WITH-SKIP skips checks and trials. It forces an immediate
SKIP-TRIAL whenever a trial is started (which turns into a
VERDICT-SKIP) and makes checks (without intervening trials, of
course) evaluate normally but signal RESULT-SKIP. SKIP is NIL
cancels the effect of any enclosing WITH-SKIP with SKIP true.
#### Outcome Restarts
- [function] FORCE-EXPECTED-SUCCESS &OPTIONAL CONDITION
Change the type of the OUTCOME being signalled to EXPECTED and
SUCCESS. If the original condition is a RESULT, then this will be
EXPECTED-RESULT-SUCCESS, if it is a VERDICT, then
EXPECTED-VERDICT-SUCCESS.
- [function] FORCE-UNEXPECTED-SUCCESS &OPTIONAL CONDITION
Change the type of OUTCOME being signalled to UNEXPECTED and
SUCCESS.
- [function] FORCE-EXPECTED-FAILURE &OPTIONAL CONDITION
Change the type of OUTCOME being signalled to EXPECTED and
FAILURE.
- [function] FORCE-UNEXPECTED-FAILURE &OPTIONAL CONDITION
Change the type of OUTCOME being signalled to UNEXPECTED and
FAILURE.
#### Checks
Checks are like CL:ASSERTs, they check whether some condition holds
and signal an OUTCOME. The outcome signalled for checks is a
subclass of RESULT.
Take, for example, `(IS (= X 5))`. Depending on whether `X` is
indeed 5, some kind of RESULT SUCCESS or FAILURE will be signalled.
WITH-EXPECTED-OUTCOME determines whether it's EXPECTED or
UNEXPECTED, and we have one of EXPECTED-RESULT-SUCCESS,
UNEXPECTED-RESULT-SUCCESS, EXPECTED-RESULT-FAILURE,
UNEXPECTED-RESULT-FAILURE to signal. Furthermore, if WITH-SKIP is in
effect, then RESULT-SKIP is signalled.
The result is signalled with `#'SIGNAL` if it is a PASS, else it's
signalled with `#'ERROR`. This distinction matters only if the event
is not handled, which is never the case in a TRIAL. Standalone
checks though - those that are not enclosed by a trial - invoke the
debugger on RESULTs which are not of type PASS.
The signalled RESULT is not final until RECORD-EVENT is invoked on
it, and it can be changed with the @TRY/OUTCOME-RESTARTS and the
@TRY/CHECK-RESTARTS.
- [condition] RESULT LEAF OUTCOME
- [condition] EXPECTED-RESULT-SUCCESS EXPECTED RESULT SUCCESS
- [condition] UNEXPECTED-RESULT-SUCCESS UNEXPECTED RESULT SUCCESS
- [condition] EXPECTED-RESULT-FAILURE EXPECTED RESULT FAILURE
- [condition] UNEXPECTED-RESULT-FAILURE UNEXPECTED RESULT FAILURE
- [condition] RESULT-SKIP RESULT SKIP
- [condition] RESULT-ABORT* RESULT ABORT\* DISMISSAL
##### Check Restarts
- [function] ABORT-CHECK &OPTIONAL CONDITION
Change the OUTCOME of the check being signalled to `RESULT-ABORT*`.
`RESULT-ABORT*`, being `(NOT PASS)`, will cause the check to return
NIL if RECORD-EVENT is invoked on it.
- [function] SKIP-CHECK &OPTIONAL CONDITION
Change the OUTCOME of the check being signalled to RESULT-SKIP.
RESULT-SKIP, being a PASS, will cause the check to return T if
CONTINUE or RECORD-EVENT is invoked on it.
- [function] RETRY-CHECK &OPTIONAL CONDITION
Initiate a non-local exit to go reevaluate the forms
wrapped by the check without signalling an OUTCOME.
#### Trials
- [class] TRIAL FUNCALLABLE-STANDARD-OBJECT
Trials are records of calls to tests (see
@TRY/COUNT, @TRY/COLLECT). Their behaviour as @FUNCALLABLE-INSTANCEs
is explained in @TRY/RERUN.
There are three ways to acquire a TRIAL object: by calling
CURRENT-TRIAL, through the lexical binding of the symbol that names
the test or through the return value of a test:
```common-lisp
(deftest xxx ()
(prin1 xxx))
(xxx)
.. #<TRIAL (XXX) RUNNING>
==> #<TRIAL (XXX) EXPECTED-SUCCESS 0.000s>
```
WITH-TRIAL can also provide access to its TRIAL:
```common-lisp
(with-test (t0)
(prin1 t0))
.. #<TRIAL (WITH-TEST (T0)) RUNNING>
==> #<TRIAL (WITH-TEST (T0)) EXPECTED-SUCCESS 0.000s>
```
TRIALs are not to be instantiated by client code.
- [function] CURRENT-TRIAL
TRIALs, like the calls to tests they stand for, nest. CURRENT-TRIAL
returns the innermost trial. If there is no currently running test,
then an error is signalled. The returned trial is RUNNINGP.
##### Trial Events
- [condition] TRIAL-EVENT EVENT
A TRIAL-EVENT is either a TRIAL-START or a
VERDICT.
- [reader] TRIAL TRIAL-EVENT (:TRIAL)
- [condition] TRIAL-START TRIAL-EVENT
TRIAL-START is signalled when a test function
(see @TRY/TESTS) is entered and a TRIAL is started, it is already
the CURRENT-TRIAL, and the @TRY/TRIAL-RESTARTS are available. It is
also signalled when a trial is retried:
```common-lisp
(let ((*print* nil)
(n 0))
(with-test ()
(handler-bind ((trial-start (lambda (c)
(format t "TRIAL-START for ~S retry#~S~%"
(test-name (trial c))
(n-retries (trial c))))))
(with-test (this)
(incf n)
(when (< n 3)
(retry-trial))))))
.. TRIAL-START for THIS retry#0
.. TRIAL-START for THIS retry#1
.. TRIAL-START for THIS retry#2
..
```
The matching of TRIAL-START events is less straightforward than that
of other EVENTs.
- When a TRIAL-START event matches the `COLLECT` type (see
@TRY/COLLECT), its TRIAL is collected.
- Similarly, when a TRIAL-START matches the PRINT
type (see @TRY/PRINT), it is printed immediately, and its trial's
VERDICT will be printed too regardless of whether it matches
PRINT. If TRIAL-START does not match
PRINT, it may still be printed if for example
*PRINT-PARENT* requires it.
- When a TRIAL-START matches the `RERUN` type (see @TRY/RERUN), its
TRIAL may be rerun.
- Also, see WITH-SKIP.
- [condition] VERDICT TRIAL-EVENT OUTCOME
A VERDICT is the OUTCOME of a TRIAL. It is one of
`{EXPECTED,UNEXPECTED}-VERDICT-{SUCCESS,FAILURE}`, VERDICT-SKIP and
VERDICT-ABORT\*. Regarding how the verdict type is determined, see
@TRY/TRIAL-VERDICTS.
Verdicts are signalled while their TRIAL is
still the CURRENT-TRIAL, and @TRY/TRIAL-RESTARTS are still
available.
```common-lisp
(try (lambda ()
(handler-bind (((and verdict failure) #'retry-trial))
(with-test (this)
(is (zerop (random 2)))))))
.. (TRY #<FUNCTION (LAMBDA ()) {53038ADB}>)
.. THIS
.. ⊠ (IS (ZEROP #1=(RANDOM 2)))
.. where
.. #1# = 1
.. THIS retry #1
.. ⋅ (IS (ZEROP (RANDOM 2)))
.. ⋅ THIS ⋅1
.. ⋅ (TRY #<FUNCTION (LAMBDA ()) {53038ADB}>) ⋅1
..
==> #<TRIAL (TRY #<FUNCTION (LAMBDA ()) {53038ADB}>) EXPECTED-SUCCESS 0.000s ⋅1>
```
- [condition] EXPECTED-VERDICT-SUCCESS EXPECTED VERDICT SUCCESS
- [condition] UNEXPECTED-VERDICT-SUCCESS UNEXPECTED VERDICT SUCCESS
- [condition] EXPECTED-VERDICT-FAILURE EXPECTED VERDICT FAILURE
- [condition] UNEXPECTED-VERDICT-FAILURE UNEXPECTED VERDICT FAILURE
- [condition] VERDICT-SKIP VERDICT SKIP
- [condition] VERDICT-ABORT* VERDICT ABORT\* DISMISSAL
##### Trial Verdicts
When a trial finished, a VERDICT is signalled. The verdict's type
is determined as follows.
- It is a VERDICT-SKIP if
- SKIP-TRIAL was called on the trial, or
- ABORT-TRIAL, SKIP-TRIAL, or RETRY-TRIAL was called on an
enclosing trial, and
- these were not overruled by a later ABORT-TRIAL or RETRY-TRIAL
on the trial.
- It is a VERDICT-ABORT\* if ABORT-TRIAL was called on the trial, and
it wasn't overruled by a later SKIP-TRIAL or RETRY-TRIAL.
- If all children (including those not collected in CHILDREN) of the
trial PASS, then the verdict will be a SUCCESS, else it will be a
FAILURE.
- Subject to the WITH-EXPECTED-OUTCOME in effect,
`{EXPECTED,UNEXPECTED}-VERDICT-{SUCCESS,FAILURE}` is the type of
the verdict which will be signalled.
The verdict of this type is signalled, but its type can be changed
by the @TRY/OUTCOME-RESTARTS or the @TRY/TRIAL-RESTARTS before
RECORD-EVENT is invoked on it.
- [reader] VERDICT TRIAL (= NIL)
The VERDICT EVENT signalled when this
TRIAL finished or NIL if it has not finished yet.
- [function] RUNNINGP TRIAL
See if the function call associated with TRIAL has not returned yet.
Trials that are not running have a VERDICT and are said to be
finished.
- [function] PASSEDP TRIAL
See if TRIAL has finished and its VERDICT is a
PASS.
- [function] FAILEDP TRIAL
See if TRIAL has finished and its VERDICT is a
FAIL.
##### Trial Restarts