forked from OdonataResearchLLC/lisp-unit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlisp-unit.lisp
1054 lines (907 loc) · 33.4 KB
/
lisp-unit.lisp
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
;;;-*- Mode: Lisp; Syntax: ANSI-Common-Lisp -*-
#|
Copyright (c) 2004-2005, Christopher K. Riesbeck
Copyright (c) 2009-2016, Thomas M. Hermann
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
How to use
----------
1. Read the documentation at:
https://github.com/OdonataResearchLLC/lisp-unit/wiki
2. Make a file of DEFINE-TEST's. See exercise-tests.lisp for many
examples. If you want, start your test file with (REMOVE-TESTS :ALL)
to clear any previously defined tests.
3. Load this file.
4. (use-package :lisp-unit)
5. Load your code file and your file of tests.
6. Test your code with (RUN-TESTS '(test-name1 test-name2 ...)) or
simply (RUN-TESTS :ALL) to run all defined tests.
A summary of how many tests passed and failed will be printed.
NOTE: Nothing is compiled until RUN-TESTS is expanded. Redefining
functions or even macros does not require reloading any tests.
|#
;;; Packages
(in-package :cl-user)
(defpackage :lisp-unit
(:use :common-lisp)
;; Print parameters
(:export :*print-summary*
:*print-failures*
:*print-errors*
:*summarize-results*)
;; Forms for assertions
(:export :assert-eq
:assert-eql
:assert-equal
:assert-equalp
:assert-equality
:assert-prints
:assert-expands
:assert-true
:assert-test
:assert-false
:assert-nil
:assert-error)
;; Functions for managing tests
(:export :define-test
:list-tests
:test-code
:test-documentation
:remove-tests
:run-tests
:use-debugger)
;; Functions for managing tags
(:export :list-tags
:tagged-tests
:remove-tags
:run-tags)
;; Functions for reporting test results
(:export :test-names
:failed-tests
:error-tests
:missing-tests
:print-failures
:print-errors
:summarize-results)
;; Functions for test results
(:export :reduce-test-results-dbs)
;; Functions for extensibility via signals
(:export :signal-results
:test-run-complete
:results)
;; Utility predicates
(:export :logically-equal :set-equal))
(in-package :lisp-unit)
;;; Global counters
(defparameter *pass* 0
"The passed assertion results.")
(defparameter *fail* ()
"The failed assertion results.")
(defun reset-counters ()
"Reset the counters to empty lists."
(setf *pass* 0 *fail* ()))
;;; Global options
(defparameter *print-summary* nil
"Print a summary of the pass, fail, and error count if non-nil.")
(defparameter *print-failures* nil
"Print failure messages if non-NIL.")
(defparameter *print-errors* nil
"Print error messages if non-NIL.")
(defparameter *summarize-results* t
"Summarize all of the unit test results.")
(defparameter *use-debugger* nil
"If not NIL, enter the debugger when an error is encountered in an
assertion.")
(defun use-debugger (&optional (flag t))
"Use the debugger when testing, or not."
(setq *use-debugger* flag))
(defun use-debugger-p (condition)
"Debug or ignore errors."
(cond
((eq :ask *use-debugger*)
(y-or-n-p "~A -- debug?" condition))
(*use-debugger*)))
(defparameter *signal-results* nil
"Signal the result if non NIL.")
(defun signal-results (&optional (flag t))
"Signal the results for extensibility."
(setq *signal-results* flag))
;;; Global unit test database
(defparameter *test-db* (make-hash-table :test #'eq)
"The unit test database is simply a hash table.")
(defun null-tests-warning-report (null-tests-warning stream)
"Write the null-tests-warning to the stream."
(format stream "No tests defined for package ~A."
(tests-package-name null-tests-warning)))
(define-condition null-tests-warning (simple-warning)
((name
:type string
:initarg :name
:reader tests-package-name))
(:report null-tests-warning-report))
(defun package-table (package &optional create)
(let ((packobj (find-package package)))
(cond
((gethash packobj *test-db*))
(create
(setf (gethash packobj *test-db*) (make-hash-table)))
(t (warn 'null-tests-warning :name (package-name package))))))
(defmacro with-package-table ((table
&optional (package *package*) create)
&body body)
"Execute the body only if the package table exists."
(let ((gtable (gensym "TABLE-")))
`(let* ((,gtable (package-table ,package ,create))
(,table ,gtable))
(when (hash-table-p ,gtable) ,@body))))
;;; Global tags database
(defparameter *tag-db* (make-hash-table :test #'eq)
"The tag database is simply a hash table.")
(defun null-tags-warning-report (null-tags-warning stream)
"Write the null-tags-warning to the stream."
(format stream "No tags defined for package ~A."
(tags-package-name null-tags-warning)))
(define-condition null-tags-warning (simple-warning)
((name
:type string
:initarg :name
:reader tags-package-name))
(:report null-tags-warning-report))
(defun package-tags (package &optional create)
"Return the tags DB for the package."
(let ((packobj (find-package package)))
(cond
((gethash packobj *tag-db*))
(create
(setf (gethash packobj *tag-db*) (make-hash-table)))
(t (warn 'null-tags-warning :name (package-name package))))))
(defmacro with-package-tags ((table
&optional (package *package*) create)
&body body)
"Execute the body only if the package tags exists."
(let ((gtable (gensym "TABLE-")))
`(let* ((,gtable (package-tags ,package ,create))
(,table ,gtable))
(when (hash-table-p ,gtable) ,@body))))
;;; Unit test definition
(defclass unit-test ()
((doc
:type string
:initarg :doc
:reader doc)
(code
:type list
:initarg :code
:reader code))
(:default-initargs :doc "" :code ())
(:documentation
"Organize the unit test documentation and code."))
;;; NOTE: Shamelessly taken from PG's analyze-body
(defun parse-body (body &optional doc tag)
"Separate the components of the body."
(let ((item (first body)))
(cond
((and (listp item) (eq :tag (first item)))
(parse-body (rest body) doc (nconc (rest item) tag)))
((and (stringp item) (not doc) (rest body))
(if tag
(values doc tag (rest body))
(parse-body (rest body) item tag)))
(t (values doc tag body)))))
(defun test-name-error-report (test-name-error stream)
"Write the test-name-error to the stream."
(format stream "Test name ~S is not of type ~A."
(type-error-datum test-name-error)
(type-error-expected-type test-name-error)))
(define-condition test-name-error (type-error)
()
(:default-initargs :expected-type 'symbol)
(:report test-name-error-report)
(:documentation
"The test name error is a type error."))
(defun valid-test-name (name)
"Signal a type-error if the test name is not a symbol."
(if (symbolp name)
name
(error 'test-name-error :datum name)))
(defun test-package (name)
"Return the package for storing the test."
(multiple-value-bind (symbol status)
(find-symbol (symbol-name name))
(declare (ignore symbol))
(ecase status
((:internal :external nil)
(symbol-package name))
(:inherited *package*))))
(defmacro define-test (name &body body)
"Store the test in the test database."
(let ((qname (gensym "NAME-")))
(multiple-value-bind (doc tag code) (parse-body body)
`(let* ((,qname (valid-test-name ',name))
(doc (or ,doc (symbol-name ,qname)))
(package (test-package ,qname)))
(setf
;; Unit test
(gethash ,qname (package-table package t))
(make-instance 'unit-test :doc doc :code ',code))
;; Tags
(loop
for tag in ',tag do
(pushnew ,qname (gethash tag (package-tags package t))))
;; Return the name of the test
,qname))))
;;; Manage tests
(defun list-tests (&optional (package *package*))
"Return a list of the tests in package."
(with-package-table (table package)
(loop for test-name being each hash-key in table
collect test-name)))
(defun test-documentation (name &optional (package *package*))
"Return the documentation for the test."
(with-package-table (table package)
(let ((unit-test (gethash name table)))
(if (null unit-test)
(warn "No test ~A in package ~A."
name (package-name package))
(doc unit-test)))))
(defun test-code (name &optional (package *package*))
"Returns the code stored for the test name."
(with-package-table (table package)
(let ((unit-test (gethash name table)))
(if (null unit-test)
(warn "No test ~A in package ~A."
name (package-name package))
(code unit-test)))))
(defun remove-tests (&optional (names :all) (package *package*))
"Remove individual tests or entire sets."
(if (eq :all names)
(if (null package)
(clrhash *test-db*)
(progn
(remhash (find-package package) *test-db*)
(remhash (find-package package) *tag-db*)))
(progn
;; Remove tests
(with-package-table (table package)
(loop for name in names
unless (remhash name table) do
(warn "No test ~A in package ~A to remove."
name (package-name package))))
;; Remove tests from tags
(with-package-tags (tags package)
(loop for tag being each hash-key in tags
using (hash-value tagged-tests)
do
(setf
(gethash tag tags)
(set-difference tagged-tests names)))))))
;;; Manage tags
(defun %tests-from-all-tags (&optional (package *package*))
"Return all of the tests that have been tagged."
(with-package-tags (table package)
(loop for tests being each hash-value in table
nconc (copy-list tests) into all-tests
finally (return (delete-duplicates all-tests)))))
(defun %tests-from-tags (tags &optional (package *package*))
"Return the tests associated with the tags."
(with-package-tags (table package)
(loop for tag in tags
as tests = (gethash tag table)
if (null tests) do (warn "No tests tagged with ~S." tag)
else nconc (copy-list tests) into all-tests
finally (return (delete-duplicates all-tests)))))
(defun list-tags (&optional (package *package*))
"Return a list of the tags in package."
(with-package-tags (table package)
(loop for tag being each hash-key in table collect tag)))
(defun tagged-tests (&optional (tags :all) (package *package*))
"Return a list of the tests associated with the tags."
(if (eq :all tags)
(%tests-from-all-tags package)
(%tests-from-tags tags package)))
(defun remove-tags (&optional (tags :all) (package *package*))
"Remove individual tags or entire sets."
(if (eq :all tags)
(if (null package)
(clrhash *tag-db*)
(remhash (find-package package) *tag-db*))
(with-package-tags (tag-table package)
(loop for tag in tags
unless (remhash tag tag-table) do
(warn "No tag ~A in package ~A to remove."
tag (package-name package))))))
;;; Assert macros
(defmacro assert-eq (expected form &rest extras)
"Assert whether expected and form are EQ."
`(expand-assert :equal ,form ,form ,expected ,extras :test #'eq))
(defmacro assert-eql (expected form &rest extras)
"Assert whether expected and form are EQL."
`(expand-assert :equal ,form ,form ,expected ,extras :test #'eql))
(defmacro assert-equal (expected form &rest extras)
"Assert whether expected and form are EQUAL."
`(expand-assert :equal ,form ,form ,expected ,extras :test #'equal))
(defmacro assert-equalp (expected form &rest extras)
"Assert whether expected and form are EQUALP."
`(expand-assert :equal ,form ,form ,expected ,extras :test #'equalp))
(defmacro assert-error (condition form &rest extras)
"Assert whether form signals condition."
`(expand-assert :error ,form (expand-error-form ,form)
,condition ,extras))
(defmacro assert-expands (expansion form &rest extras)
"Assert whether form expands to expansion."
`(expand-assert :macro ,form
(expand-macro-form ,form nil)
',expansion ,extras))
(defmacro assert-equality (test expected form &rest extras)
"Assert whether expected and form are equal according to test."
`(expand-assert :equal ,form ,form ,expected ,extras :test ,test))
(defmacro assert-prints (output form &rest extras)
"Assert whether printing the form generates the output."
`(expand-assert :output ,form (expand-output-form ,form)
,output ,extras))
(defmacro assert-nil (form &rest extras)
"Assert whether the form is false."
(if (atom form)
`(expand-assert :result ,form ,form nil ,extras)
`(expand-t-or-f nil ,form ,extras)))
(defmacro assert-false (form &rest extras)
"Assert whether the form is false."
(if (atom form)
`(expand-assert :result ,form ,form nil ,extras)
`(expand-t-or-f nil ,form ,extras)))
(defmacro assert-true (form &rest extras)
"Assert whether the form is true."
(if (atom form)
`(expand-assert :result ,form ,form t ,extras)
`(expand-t-or-f t ,form ,extras)))
(defmacro expand-t-or-f (t-or-f form extras)
"Expand the true/false assertions to report the arguments."
(let ((fname (gensym))
(args (gensym)))
`(let ((,fname ',(car form))
(,args (list ,@(cdr form))))
(if (macro-function ,fname)
;; Do not report macro arguments
(internal-assert
:result ',form
(lambda () ,form)
(lambda () ,t-or-f)
;; Concatenate the args with the extras
;; FIXME: Need to test whether the args are expanded at
;; the right time
(lambda ()
(nconc
(mapcan #'list ',(cdr form) ,args)
(funcall (expand-extras ,extras))))
#'eql)
;; Report function arguments
(internal-assert
:result ',form
(lambda () (apply ,fname ,args)) ; Evaluate the form
(lambda () ,t-or-f)
;; Concatenate the args with the extras
(lambda ()
(nconc
(mapcan #'list ',(cdr form) ,args)
(funcall (expand-extras ,extras))))
#'eql)))))
(defmacro expand-assert (type form body expected extras &key (test '#'eql))
"Expand the assertion to the internal format."
`(internal-assert ,type ',form
(lambda () ,body)
(lambda () ,expected)
(expand-extras ,extras)
,test))
(defmacro expand-error-form (form)
"Wrap the error assertion in HANDLER-CASE."
`(handler-case ,form
(condition (error) error)))
(defmacro expand-output-form (form)
"Capture the output of the form in a string."
(let ((out (gensym)))
`(let* ((,out (make-string-output-stream))
(*standard-output*
(make-broadcast-stream *standard-output* ,out)))
,form
(get-output-stream-string ,out))))
(defmacro expand-macro-form (form env)
"Expand the macro form once."
`(let ((*gensym-counter* 1))
(macroexpand-1 ',form ,env)))
(defmacro expand-extras (extras)
"Expand extra forms."
`(lambda ()
(list ,@(mapcan (lambda (form) (list `',form form)) extras))))
(defgeneric assert-result (type test expected actual)
(:documentation
"Return the result of the assertion."))
(defgeneric record-failure (type form actual expected extras test)
(:documentation
"Record the details of the failure."))
(defclass failure-result ()
((form
:initarg :form
:reader form)
(actual
:type list
:initarg :actual
:reader actual)
(expected
:type list
:initarg :expected
:reader expected)
(extras
:type list
:initarg :extras
:reader extras)
(test
:type function
:initarg :test
:reader test))
(:documentation
"Failure details of the assertion."))
(defun %record-failure (class form actual expected extras test)
"Return an instance of the failure result."
(make-instance class
:form form
:actual actual
:expected expected
:extras extras
:test test))
(defclass equal-result (failure-result)
()
(:documentation
"Result of a failed equal assertion."))
(defmethod assert-result ((type (eql :equal)) test expected actual)
"Return the result of an equal assertion."
(and
(<= (length expected) (length actual))
(every test expected actual)))
(defmethod record-failure ((type (eql :equal))
form actual expected extras test)
"Return an instance of an equal failure result."
(%record-failure 'equal-result form actual expected extras test))
(defclass error-result (failure-result)
()
(:documentation
"Result of a failed error assertion."))
(defmethod assert-result ((type (eql :error)) test expected actual)
"Return the result of an error assertion."
(declare (ignore test))
(or
(eql (car actual) (car expected))
(typep (car actual) (car expected))))
(defmethod record-failure ((type (eql :error))
form actual expected extras test)
"Return an instance of an error failure result."
(%record-failure 'error-result form actual expected extras test))
(defclass macro-result (failure-result)
()
(:documentation
"Result of a failed macro expansion assertion."))
(defun %expansion-equal (form1 form2)
"Descend into the forms checking for equality."
(let ((item1 (first form1))
(item2 (first form2)))
(cond
((and (null item1) (null item2)))
((and (listp item1) (listp item2))
(and
(%expansion-equal item1 item2)
(%expansion-equal (rest form1) (rest form2))))
((and (symbolp item1) (symbolp item2))
(and
(string= (symbol-name item1) (symbol-name item2))
(%expansion-equal (rest form1) (rest form2))))
(t (and
(equal item1 item2)
(%expansion-equal (rest form1) (rest form2)))))))
(defmethod assert-result ((type (eql :macro)) test expected actual)
"Return the result of a macro assertion."
(declare (ignore test))
(%expansion-equal (first expected) (first actual)))
(defmethod record-failure ((type (eql :macro))
form actual expected extras test)
"Return an instance of a macro failure result."
(%record-failure 'macro-result form actual expected extras test))
(defclass boolean-result (failure-result)
()
(:documentation
"Result of a failed boolean assertion."))
(defmethod assert-result ((type (eql :result)) test expected actual)
"Return the result of a result assertion."
(declare (ignore test))
(logically-equal (car actual) (car expected)))
(defmethod record-failure ((type (eql :result))
form actual expected extras test)
"Return an instance of a boolean failure result."
(%record-failure 'boolean-result form actual expected extras test))
(defclass output-result (failure-result)
()
(:documentation
"Result of a failed output assertion."))
(defmethod assert-result ((type (eql :output)) test expected actual)
"Return the result of an output assertion."
(declare (ignore test))
(string=
(string-trim '(#\newline #\return #\space) (car actual))
(car expected)))
(defmethod record-failure ((type (eql :output))
form actual expected extras test)
"Return an instance of an output failure result."
(%record-failure 'output-result form actual expected extras test))
(defun internal-assert
(type form code-thunk expected-thunk extras test)
"Perform the assertion and record the results."
(let* ((actual (multiple-value-list (funcall code-thunk)))
(expected (multiple-value-list (funcall expected-thunk)))
(result (assert-result type test expected actual)))
(if result
(incf *pass*)
(push
(record-failure
type form actual expected
(when extras (funcall extras)) test)
*fail*))
;; Return the result
result))
;;; Unit test results
(defclass test-result ()
((name
:type symbol
:initarg :name
:reader name)
(pass
:type fixnum
:initarg :pass
:reader pass)
(fail
:type list
:initarg :fail
:reader fail)
(exerr
:initarg :exerr
:reader exerr)
(run-time
:initarg :run-time
:reader run-time
:documentation
"Test run time measured in internal time units"))
(:default-initargs :exerr nil)
(:documentation
"Store the results of the unit test."))
(defun print-summary (test-result &optional
(stream *standard-output*))
"Print a summary of the test result."
(format stream "~&~A: ~S assertions passed, ~S failed"
(name test-result)
(pass test-result)
(length (fail test-result)))
(if (exerr test-result)
(format stream ", and an execution error.")
(write-char #\. stream))
(terpri stream)
(terpri stream))
(defun run-code (code)
"Run the code to test the assertions."
(funcall (coerce `(lambda () ,@code) 'function)))
(defun run-test-thunk (name code)
(let ((*pass* 0)
(*fail* ())
(start (get-internal-run-time)))
(handler-bind
((error
(lambda (condition)
(if (use-debugger-p condition)
condition
(return-from run-test-thunk
(make-instance
'test-result
:name name
:pass *pass*
:fail *fail*
:run-time (- (get-internal-run-time) start)
:exerr condition))))))
(run-code code))
;; Return the result count
(make-instance
'test-result
:name name
:pass *pass*
:fail *fail*
:run-time (- (get-internal-run-time) start))))
;;; Test results database
(defclass test-results-db ()
((database
:type hash-table
:initform (make-hash-table :test #'eq)
:reader database)
(pass
:type fixnum
:initform 0
:accessor pass)
(fail
:type fixnum
:initform 0
:accessor fail)
(exerr
:type fixnum
:initform 0
:accessor exerr)
(failed-tests
:type list
:initform ()
:accessor failed-tests)
(error-tests
:type list
:initform ()
:accessor error-tests)
(missing-tests
:type list
:initform ()
:accessor missing-tests))
(:documentation
"Store the results of the tests for further evaluation."))
(defmethod print-object ((object test-results-db) stream)
"Print the summary counts with the object."
(let ((pass (pass object))
(fail (fail object))
(exerr (exerr object)))
(format
stream "#<~A Total(~D) Passed(~D) Failed(~D) Errors(~D)>~%"
(class-name (class-of object))
(+ pass fail) pass fail exerr)))
(defun test-names (test-results-db)
"Return a list of the test names in the database."
(loop for name being each hash-key in (database test-results-db)
collect name))
(defun record-result (test-name code results)
"Run the test code and record the result."
(let ((result (run-test-thunk test-name code)))
;; Store the result
(setf (gethash test-name (database results)) result)
;; Count passed tests
(when (plusp (pass result))
(incf (pass results) (pass result)))
;; Count failed tests and record the name
(when (fail result)
(incf (fail results) (length (fail result)))
(push test-name (failed-tests results)))
;; Count errors and record the name
(when (exerr result)
(incf (exerr results))
(push test-name (error-tests results)))
;; Running output
(when *print-failures* (print-failures result))
(when *print-errors* (print-errors result))
(when (or *print-summary* *print-failures* *print-errors*)
(print-summary result))))
(defun summarize-results (results &optional
(stream *standard-output*))
"Print a summary of all results to the stream."
(let ((pass (pass results))
(fail (fail results)))
(format stream "~&Unit Test Summary~%")
(format stream " | ~D assertions total~%" (+ pass fail))
(format stream " | ~D passed~%" pass)
(format stream " | ~D failed~%" fail)
(format stream " | ~D execution errors~%" (exerr results))
(format stream " | ~D missing tests~2%"
(length (missing-tests results)))))
(defun default-db-merge-function (results new-results)
"Signal an error by default if a merge is required."
(lambda (key value1 value2)
(error
"Cannot merge TEST-RESULTS-DB instances ~A and ~A as key ~A has
two values, ~A and ~A"
results new-results key value1 value2)))
(defun nappend-test-results-db (results new-results &key merge)
"Merge the results of NEW-RESULTS in to RESULTS. Any conflicts
between RESULTS and NEW-RESULTS are handled by the function MERGE.
The lambda list for the MERGE functions is
(key results-value new-results-value)
where:
KEY is the key which appears in RESULTS and NEW-RESULTS.
RESULTS-VALUE is the value appearing RESULTS.
NEW-RESULTS-VALUE is the value appearing in NEW-RESULTS.
If MERGE is NIL, then an error is signalled when a conflict occurs.
"
(check-type results test-results-db)
(check-type new-results test-results-db)
(check-type merge (or null function))
(loop
with results-db = (database results)
with new-results-db = (database new-results)
with merge =
(or merge (default-db-merge-function results new-results))
;; Merge test databases
for key being each hash-key in new-results-db
using (hash-value new-results-value)
do
(multiple-value-bind (results-value presentp)
(gethash key results-db)
(setf
(gethash key results-db)
(if presentp
(funcall merge key results-value new-results-value)
new-results-value)))
finally
;; Update counters
(incf (pass results) (pass new-results))
(incf (fail results) (fail new-results))
(incf (exerr results) (exerr new-results))
;; Merge failures, errors, and missing test details
(setf
;; Failures
(failed-tests results)
(append (failed-tests results) (failed-tests new-results))
;; Errors
(error-tests results)
(append (error-tests results) (error-tests new-results))
;; Missing tests
(missing-tests results)
(append (missing-tests results) (missing-tests new-results))))
;; Return the merged results
results)
(defun reduce-test-results-dbs (all-results &key merge)
"Return a new instance of TEST-RESULTS-DB which contains all of the
results in the sequence RESULTS. Any conflicts are handled by the
function MERGE.
The lambda list for the MERGE function is
(key value-1 value-2)
where:
KEY is the key which appears at least twice in the sequence RESULTS.
VALUE-1 and VALUE-2 are the conflicting values for the given KEY.
If MERGE is NIL, then an error is signalled when a conflict occurs."
(loop
with accumulated-test-results-db = (make-instance 'test-results-db)
for new-results in all-results do
(nappend-test-results-db
accumulated-test-results-db new-results :merge merge)
finally (return accumulated-test-results-db)))
;;; Run the tests
(define-condition test-run-complete ()
((results
:type 'test-results-db
:initarg :results
:reader results))
(:documentation
"Signaled when a test run is finished."))
(defun %run-all-thunks (&optional (package *package*))
"Run all of the test thunks in the package."
(with-package-table (table package)
(loop
with results = (make-instance 'test-results-db)
for test-name being each hash-key in table
using (hash-value unit-test)
if unit-test do
(record-result test-name (code unit-test) results)
else do
(push test-name (missing-tests results))
;; Summarize and return the test results
finally
(when *signal-results*
(signal 'test-run-complete :results results))
(when *summarize-results*
(summarize-results results))
(return results))))
(defun %run-thunks (test-names &optional (package *package*))
"Run the list of test thunks in the package."
(with-package-table (table package)
(loop
with results = (make-instance 'test-results-db)
for test-name in test-names
as unit-test = (gethash test-name table)
if unit-test do
(record-result test-name (code unit-test) results)
else do
(push test-name (missing-tests results))
finally
(when *signal-results*
(signal 'test-run-complete :results results))
(when *summarize-results*
(summarize-results results))
(return results))))
(defun run-tests (&optional (test-names :all) (package *package*))
"Run the specified tests in package."
(reset-counters)
(if (eq :all test-names)
(%run-all-thunks package)
(%run-thunks test-names package)))
(defun run-tags (&optional (tags :all) (package *package*))
"Run the tests associated with the specified tags in package."
(reset-counters)
(%run-thunks (tagged-tests tags package) package))
;;; Print failures
(defgeneric print-failures (result &optional stream)
(:documentation
"Report the results of the failed assertion."))
(defmethod print-failures :around ((result failure-result) &optional
(stream *standard-output*))
"Failure header and footer output."
(format stream "~& | Failed Form: ~S" (form result))
(call-next-method)
(when (extras result)
(format stream "~{~& | ~S => ~S~}~%" (extras result)))
(format stream "~& |~%"))
(defmethod print-failures ((result failure-result) &optional
(stream *standard-output*))
(format stream "~& | Expected ~{~S~^; ~} " (expected result))
(format stream "~<~% | ~:;but saw ~{~S~^; ~}~>" (actual result)))
(defmethod print-failures ((result error-result) &optional
(stream *standard-output*))
(format stream "~& | ~@[Should have signalled ~{~S~^; ~} but saw~]"
(expected result))
(format stream " ~{~S~^; ~}" (actual result)))
(defmethod print-failures ((result macro-result) &optional
(stream *standard-output*))
(format stream "~& | Should have expanded to ~{~S~^; ~} "
(expected result))
(format stream "~<~%~:;but saw ~{~S~^; ~}~>" (actual result)))
(defmethod print-failures ((result output-result) &optional
(stream *standard-output*))
(format stream "~& | Should have printed ~{~S~^; ~} "
(expected result))
(format stream "~<~%~:;but saw ~{~S~^; ~}~>"
(actual result)))
(defmethod print-failures ((result test-result) &optional
(stream *standard-output*))