1 ;;;-*- Mode: Lisp; Syntax: ANSI-Common-Lisp -*-
5 Copyright
(c) 2004-
2005, Christopher K. Riesbeck
6 Copyright
(c) 2009-
2016, Thomas M. Hermann
8 Permission is hereby granted
, free of charge
, to any person obtaining
9 a copy of this software and associated documentation files
(the "Software"),
10 to deal in the Software without restriction
, including without limitation
11 the rights to use
, copy
, modify
, merge
, publish
, distribute
, sublicense
,
12 and
/or sell copies of the Software
, and to permit persons to whom the
13 Software is furnished to do so
, subject to the following conditions
:
15 The above copyright notice and this permission notice shall be included
16 in all copies or substantial portions of the Software.
18 THE SOFTWARE IS PROVIDED
"AS IS", WITHOUT WARRANTY OF ANY KIND
, EXPRESS
19 OR IMPLIED
, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY
,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM
, DAMAGES OR
22 OTHER LIABILITY
, WHETHER IN AN ACTION OF CONTRACT
, TORT OR OTHERWISE
,
23 ARISING FROM
, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 OTHER DEALINGS IN THE SOFTWARE.
29 1. Read the documentation at
:
30 https
://github.com
/OdonataResearchLLC
/lisp-unit
/wiki
32 2. Make a file of DEFINE-TEST
's. See exercise-tests.lisp for many
33 examples. If you want
, start your test file with
(REMOVE-TESTS :ALL
)
34 to clear any previously defined tests.
38 4.
(use-package :lisp-unit
)
40 5. Load your code file and your file of tests.
42 6. Test your code with
(RUN-TESTS '(test-name1 test-name2 ...
)) or
43 simply
(RUN-TESTS :ALL
) to run all defined tests.
45 A summary of how many tests passed and failed will be printed.
47 NOTE
: Nothing is compiled until RUN-TESTS is expanded. Redefining
48 functions or even macros does not require reloading any tests.
56 (defpackage :lisp-unit
59 (:export
:*print-summary
*
63 ;; Forms for assertions
76 ;; Functions for managing tests
84 ;; Functions for managing tags
89 ;; Functions for reporting test results
97 ;; Functions for test results
98 (:export
:reduce-test-results-dbs
)
99 ;; Functions for extensibility via signals
100 (:export
:signal-results
103 ;; Utility predicates
104 (:export
:logically-equal
:set-equal
))
106 (in-package :lisp-unit
)
110 (defparameter *pass
* 0
111 "The passed assertion results.")
113 (defparameter *fail
* ()
114 "The failed assertion results.")
116 (defun reset-counters ()
117 "Reset the counters to empty lists."
118 (setf *pass
* 0 *fail
* ()))
122 (defparameter *print-summary
* nil
123 "Print a summary of the pass, fail, and error count if non-nil.")
125 (defparameter *print-failures
* nil
126 "Print failure messages if non-NIL.")
128 (defparameter *print-errors
* nil
129 "Print error messages if non-NIL.")
131 (defparameter *summarize-results
* t
132 "Summarize all of the unit test results.")
134 (defparameter *use-debugger
* nil
135 "If not NIL, enter the debugger when an error is encountered in an
138 (defun use-debugger (&optional
(flag t
))
139 "Use the debugger when testing, or not."
140 (setq *use-debugger
* flag
))
142 (defun use-debugger-p (condition)
143 "Debug or ignore errors."
145 ((eq :ask
*use-debugger
*)
146 (y-or-n-p "~A -- debug?" condition
))
149 (defparameter *signal-results
* nil
150 "Signal the result if non NIL.")
152 (defun signal-results (&optional
(flag t
))
153 "Signal the results for extensibility."
154 (setq *signal-results
* flag
))
156 ;;; Global unit test database
158 (defparameter *test-db
* (make-hash-table :test
#'eq
)
159 "The unit test database is simply a hash table.")
161 (defun null-tests-warning-report (null-tests-warning stream
)
162 "Write the null-tests-warning to the stream."
163 (format stream
"No tests defined for package ~A."
164 (tests-package-name null-tests-warning
)))
166 (define-condition null-tests-warning
(simple-warning)
170 :reader tests-package-name
))
171 (:report null-tests-warning-report
))
173 (defun package-table (package &optional create
)
174 (let ((packobj (find-package package
)))
176 ((gethash packobj
*test-db
*))
178 (setf (gethash packobj
*test-db
*) (make-hash-table)))
179 (t (warn 'null-tests-warning
:name
(package-name package
))))))
181 (defmacro with-package-table
((table
182 &optional
(package *package
*) create
)
184 "Execute the body only if the package table exists."
185 (let ((gtable (gensym "TABLE-")))
186 `(let* ((,gtable
(package-table ,package
,create
))
188 (when (hash-table-p ,gtable
) ,@body
))))
190 ;;; Global tags database
192 (defparameter *tag-db
* (make-hash-table :test
#'eq
)
193 "The tag database is simply a hash table.")
195 (defun null-tags-warning-report (null-tags-warning stream
)
196 "Write the null-tags-warning to the stream."
197 (format stream
"No tags defined for package ~A."
198 (tags-package-name null-tags-warning
)))
200 (define-condition null-tags-warning
(simple-warning)
204 :reader tags-package-name
))
205 (:report null-tags-warning-report
))
207 (defun package-tags (package &optional create
)
208 "Return the tags DB for the package."
209 (let ((packobj (find-package package
)))
211 ((gethash packobj
*tag-db
*))
213 (setf (gethash packobj
*tag-db
*) (make-hash-table)))
214 (t (warn 'null-tags-warning
:name
(package-name package
))))))
216 (defmacro with-package-tags
((table
217 &optional
(package *package
*) create
)
219 "Execute the body only if the package tags exists."
220 (let ((gtable (gensym "TABLE-")))
221 `(let* ((,gtable
(package-tags ,package
,create
))
223 (when (hash-table-p ,gtable
) ,@body
))))
225 ;;; Unit test definition
227 (defclass unit-test
()
236 (:default-initargs
:doc
"" :code
())
238 "Organize the unit test documentation and code."))
240 ;;; NOTE: Shamelessly taken from PG's analyze-body
241 (defun parse-body (body &optional doc tag
)
242 "Separate the components of the body."
243 (let ((item (first body
)))
245 ((and (listp item
) (eq :tag
(first item
)))
246 (parse-body (rest body
) doc
(nconc (rest item
) tag
)))
247 ((and (stringp item
) (not doc
) (rest body
))
249 (values doc tag
(rest body
))
250 (parse-body (rest body
) item tag
)))
251 (t (values doc tag body
)))))
253 (defun test-name-error-report (test-name-error stream
)
254 "Write the test-name-error to the stream."
255 (format stream
"Test name ~S is not of type ~A."
256 (type-error-datum test-name-error
)
257 (type-error-expected-type test-name-error
)))
259 (define-condition test-name-error
(type-error)
261 (:default-initargs
:expected-type
'symbol
)
262 (:report test-name-error-report
)
264 "The test name error is a type error."))
266 (defun valid-test-name (name)
267 "Signal a type-error if the test name is not a symbol."
270 (error 'test-name-error
:datum name
)))
272 (defun test-package (name)
273 "Return the package for storing the test."
274 (multiple-value-bind (symbol status
)
275 (find-symbol (symbol-name name
))
276 (declare (ignore symbol
))
278 ((:internal
:external nil
)
279 (symbol-package name
))
280 (:inherited
*package
*))))
282 (defmacro define-test
(name &body body
)
283 "Store the test in the test database."
284 (let ((qname (gensym "NAME-")))
285 (multiple-value-bind (doc tag code
) (parse-body body
)
286 `(let* ((,qname
(valid-test-name ',name
))
287 (doc (or ,doc
(symbol-name ,qname
)))
288 (package (test-package ,qname
)))
291 (gethash ,qname
(package-table package t
))
292 (make-instance 'unit-test
:doc doc
:code
',code
))
296 (pushnew ,qname
(gethash tag
(package-tags package t
))))
297 ;; Return the name of the test
302 (defun list-tests (&optional
(package *package
*))
303 "Return a list of the tests in package."
304 (with-package-table (table package
)
305 (loop for test-name being each hash-key in table
308 (defun test-documentation (name &optional
(package *package
*))
309 "Return the documentation for the test."
310 (with-package-table (table package
)
311 (let ((unit-test (gethash name table
)))
313 (warn "No test ~A in package ~A."
314 name
(package-name package
))
317 (defun test-code (name &optional
(package *package
*))
318 "Returns the code stored for the test name."
319 (with-package-table (table package
)
320 (let ((unit-test (gethash name table
)))
322 (warn "No test ~A in package ~A."
323 name
(package-name package
))
326 (defun remove-tests (&optional
(names :all
) (package *package
*))
327 "Remove individual tests or entire sets."
332 (remhash (find-package package
) *test-db
*)
333 (remhash (find-package package
) *tag-db
*)))
336 (with-package-table (table package
)
337 (loop for name in names
338 unless
(remhash name table
) do
339 (warn "No test ~A in package ~A to remove."
340 name
(package-name package
))))
341 ;; Remove tests from tags
342 (with-package-tags (tags package
)
343 (loop for tag being each hash-key in tags
344 using
(hash-value tagged-tests
)
348 (set-difference tagged-tests names
)))))))
352 (defun %tests-from-all-tags
(&optional
(package *package
*))
353 "Return all of the tests that have been tagged."
354 (with-package-tags (table package
)
355 (loop for tests being each hash-value in table
356 nconc
(copy-list tests
) into all-tests
357 finally
(return (delete-duplicates all-tests
)))))
359 (defun %tests-from-tags
(tags &optional
(package *package
*))
360 "Return the tests associated with the tags."
361 (with-package-tags (table package
)
362 (loop for tag in tags
363 as tests
= (gethash tag table
)
364 if
(null tests
) do
(warn "No tests tagged with ~S." tag
)
365 else nconc
(copy-list tests
) into all-tests
366 finally
(return (delete-duplicates all-tests
)))))
368 (defun list-tags (&optional
(package *package
*))
369 "Return a list of the tags in package."
370 (with-package-tags (table package
)
371 (loop for tag being each hash-key in table collect tag
)))
373 (defun tagged-tests (&optional
(tags :all
) (package *package
*))
374 "Return a list of the tests associated with the tags."
376 (%tests-from-all-tags package
)
377 (%tests-from-tags tags package
)))
379 (defun remove-tags (&optional
(tags :all
) (package *package
*))
380 "Remove individual tags or entire sets."
384 (remhash (find-package package
) *tag-db
*))
385 (with-package-tags (tag-table package
)
386 (loop for tag in tags
387 unless
(remhash tag tag-table
) do
388 (warn "No tag ~A in package ~A to remove."
389 tag
(package-name package
))))))
393 (defmacro assert-eq
(expected form
&rest extras
)
394 "Assert whether expected and form are EQ."
395 `(expand-assert :equal
,form
,form
,expected
,extras
:test
#'eq
))
397 (defmacro assert-eql
(expected form
&rest extras
)
398 "Assert whether expected and form are EQL."
399 `(expand-assert :equal
,form
,form
,expected
,extras
:test
#'eql
))
401 (defmacro assert-equal
(expected form
&rest extras
)
402 "Assert whether expected and form are EQUAL."
403 `(expand-assert :equal
,form
,form
,expected
,extras
:test
#'equal
))
405 (defmacro assert-equalp
(expected form
&rest extras
)
406 "Assert whether expected and form are EQUALP."
407 `(expand-assert :equal
,form
,form
,expected
,extras
:test
#'equalp
))
409 (defmacro assert-error
(condition form
&rest extras
)
410 "Assert whether form signals condition."
411 `(expand-assert :error
,form
(expand-error-form ,form
)
414 (defmacro assert-expands
(expansion form
&rest extras
)
415 "Assert whether form expands to expansion."
416 `(expand-assert :macro
,form
417 (expand-macro-form ,form nil
)
418 ',expansion
,extras
))
420 (defmacro assert-equality
(test expected form
&rest extras
)
421 "Assert whether expected and form are equal according to test."
422 `(expand-assert :equal
,form
,form
,expected
,extras
:test
,test
))
424 (defmacro assert-prints
(output form
&rest extras
)
425 "Assert whether printing the form generates the output."
426 `(expand-assert :output
,form
(expand-output-form ,form
)
429 (defmacro assert-nil
(form &rest extras
)
430 "Assert whether the form is false."
432 `(expand-assert :result
,form
,form nil
,extras
)
433 `(expand-t-or-f nil
,form
,extras
)))
435 (defmacro assert-false
(form &rest extras
)
436 "Assert whether the form is false."
438 `(expand-assert :result
,form
,form nil
,extras
)
439 `(expand-t-or-f nil
,form
,extras
)))
441 (defmacro assert-true
(form &rest extras
)
442 "Assert whether the form is true."
444 `(expand-assert :result
,form
,form t
,extras
)
445 `(expand-t-or-f t
,form
,extras
)))
447 (defmacro expand-t-or-f
(t-or-f form extras
)
448 "Expand the true/false assertions to report the arguments."
449 (let ((fname (gensym))
451 `(let ((,fname
',(car form
))
452 (,args
(list ,@(cdr form
))))
453 (if (macro-function ,fname
)
454 ;; Do not report macro arguments
459 ;; Concatenate the args with the extras
460 ;; FIXME: Need to test whether the args are expanded at
464 (mapcan #'list
',(cdr form
) ,args
)
465 (funcall (expand-extras ,extras
))))
467 ;; Report function arguments
470 (lambda () (apply ,fname
,args
)) ; Evaluate the form
472 ;; Concatenate the args with the extras
475 (mapcan #'list
',(cdr form
) ,args
)
476 (funcall (expand-extras ,extras
))))
479 (defmacro expand-assert
(type form body expected extras
&key
(test '#'eql
))
480 "Expand the assertion to the internal format."
481 `(internal-assert ,type
',form
483 (lambda () ,expected
)
484 (expand-extras ,extras
)
487 (defmacro expand-error-form
(form)
488 "Wrap the error assertion in HANDLER-CASE."
490 (condition (error) error
)))
492 (defmacro expand-output-form
(form)
493 "Capture the output of the form in a string."
494 (let ((out (gensym)))
495 `(let* ((,out
(make-string-output-stream))
497 (make-broadcast-stream *standard-output
* ,out
)))
499 (get-output-stream-string ,out
))))
501 (defmacro expand-macro-form
(form env
)
502 "Expand the macro form once."
503 `(let ((*gensym-counter
* 1))
504 (macroexpand-1 ',form
,env
)))
506 (defmacro expand-extras
(extras)
507 "Expand extra forms."
509 (list ,@(mapcan (lambda (form) (list `',form form
)) extras
))))
511 (defgeneric assert-result
(type test expected actual
)
513 "Return the result of the assertion."))
515 (defgeneric record-failure
(type form actual expected extras test
)
517 "Record the details of the failure."))
519 (defclass failure-result
()
540 "Failure details of the assertion."))
542 (defun %record-failure
(class form actual expected extras test
)
543 "Return an instance of the failure result."
551 (defclass equal-result
(failure-result)
554 "Result of a failed equal assertion."))
556 (defmethod assert-result ((type (eql :equal
)) test expected actual
)
557 "Return the result of an equal assertion."
559 (<= (length expected
) (length actual
))
560 (every test expected actual
)))
562 (defmethod record-failure ((type (eql :equal
))
563 form actual expected extras test
)
564 "Return an instance of an equal failure result."
565 (%record-failure
'equal-result form actual expected extras test
))
567 (defclass error-result
(failure-result)
570 "Result of a failed error assertion."))
572 (defmethod assert-result ((type (eql :error
)) test expected actual
)
573 "Return the result of an error assertion."
574 (declare (ignore test
))
576 (eql (car actual
) (car expected
))
577 (typep (car actual
) (car expected
))))
579 (defmethod record-failure ((type (eql :error
))
580 form actual expected extras test
)
581 "Return an instance of an error failure result."
582 (%record-failure
'error-result form actual expected extras test
))
584 (defclass macro-result
(failure-result)
587 "Result of a failed macro expansion assertion."))
589 (defun %expansion-equal
(form1 form2
)
590 "Descend into the forms checking for equality."
591 (let ((item1 (first form1
))
592 (item2 (first form2
)))
594 ((and (null item1
) (null item2
)))
595 ((and (listp item1
) (listp item2
))
597 (%expansion-equal item1 item2
)
598 (%expansion-equal
(rest form1
) (rest form2
))))
599 ((and (symbolp item1
) (symbolp item2
))
601 (string= (symbol-name item1
) (symbol-name item2
))
602 (%expansion-equal
(rest form1
) (rest form2
))))
605 (%expansion-equal
(rest form1
) (rest form2
)))))))
607 (defmethod assert-result ((type (eql :macro
)) test expected actual
)
608 "Return the result of a macro assertion."
609 (declare (ignore test
))
610 (%expansion-equal
(first expected
) (first actual
)))
612 (defmethod record-failure ((type (eql :macro
))
613 form actual expected extras test
)
614 "Return an instance of a macro failure result."
615 (%record-failure
'macro-result form actual expected extras test
))
617 (defclass boolean-result
(failure-result)
620 "Result of a failed boolean assertion."))
622 (defmethod assert-result ((type (eql :result
)) test expected actual
)
623 "Return the result of a result assertion."
624 (declare (ignore test
))
625 (logically-equal (car actual
) (car expected
)))
627 (defmethod record-failure ((type (eql :result
))
628 form actual expected extras test
)
629 "Return an instance of a boolean failure result."
630 (%record-failure
'boolean-result form actual expected extras test
))
632 (defclass output-result
(failure-result)
635 "Result of a failed output assertion."))
637 (defmethod assert-result ((type (eql :output
)) test expected actual
)
638 "Return the result of an output assertion."
639 (declare (ignore test
))
641 (string-trim '(#\newline
#\return
#\space
) (car actual
))
644 (defmethod record-failure ((type (eql :output
))
645 form actual expected extras test
)
646 "Return an instance of an output failure result."
647 (%record-failure
'output-result form actual expected extras test
))
649 (defun internal-assert
650 (type form code-thunk expected-thunk extras test
)
651 "Perform the assertion and record the results."
652 (let* ((actual (multiple-value-list (funcall code-thunk
)))
653 (expected (multiple-value-list (funcall expected-thunk
)))
654 (result (assert-result type test expected actual
)))
659 type form actual expected
660 (when extras
(funcall extras
)) test
)
665 ;;; Unit test results
667 (defclass test-result
()
687 "Test run time measured in internal time units"))
688 (:default-initargs
:exerr nil
)
690 "Store the results of the unit test."))
692 (defun print-summary (test-result &optional
693 (stream *standard-output
*))
694 "Print a summary of the test result."
695 (format stream
"~&~A: ~S assertions passed, ~S failed"
698 (length (fail test-result
)))
699 (if (exerr test-result
)
700 (format stream
", and an execution error.")
701 (write-char #\. stream
))
705 (defun run-code (code)
706 "Run the code to test the assertions."
707 (funcall (coerce `(lambda () ,@code
) 'function
)))
709 (defun run-test-thunk (name code
)
712 (start (get-internal-run-time)))
716 (if (use-debugger-p condition
)
718 (return-from run-test-thunk
724 :run-time
(- (get-internal-run-time) start
)
725 :exerr condition
))))))
727 ;; Return the result count
733 :run-time
(- (get-internal-run-time) start
))))
735 ;;; Test results database
737 (defclass test-results-db
()
740 :initform
(make-hash-table :test
#'eq
)
757 :accessor failed-tests
)
761 :accessor error-tests
)
765 :accessor missing-tests
))
767 "Store the results of the tests for further evaluation."))
769 (defmethod print-object ((object test-results-db
) stream
)
770 "Print the summary counts with the object."
771 (let ((pass (pass object
))
773 (exerr (exerr object
)))
775 stream
"#<~A Total(~D) Passed(~D) Failed(~D) Errors(~D)>~%"
776 (class-name (class-of object
))
777 (+ pass fail
) pass fail exerr
)))
779 (defun test-names (test-results-db)
780 "Return a list of the test names in the database."
781 (loop for name being each hash-key in
(database test-results-db
)
784 (defun record-result (test-name code results
)
785 "Run the test code and record the result."
786 (let ((result (run-test-thunk test-name code
)))
788 (setf (gethash test-name
(database results
)) result
)
789 ;; Count passed tests
790 (when (plusp (pass result
))
791 (incf (pass results
) (pass result
)))
792 ;; Count failed tests and record the name
794 (incf (fail results
) (length (fail result
)))
795 (push test-name
(failed-tests results
)))
796 ;; Count errors and record the name
798 (incf (exerr results
))
799 (push test-name
(error-tests results
)))
801 (when *print-failures
* (print-failures result
))
802 (when *print-errors
* (print-errors result
))
803 (when (or *print-summary
* *print-failures
* *print-errors
*)
804 (print-summary result
))))
806 (defun summarize-results (results &optional
807 (stream *standard-output
*))
808 "Print a summary of all results to the stream."
809 (let ((pass (pass results
))
810 (fail (fail results
)))
811 (format stream
"~&Unit Test Summary~%")
812 (format stream
" | ~D assertions total~%" (+ pass fail
))
813 (format stream
" | ~D passed~%" pass
)
814 (format stream
" | ~D failed~%" fail
)
815 (format stream
" | ~D execution errors~%" (exerr results
))
816 (format stream
" | ~D missing tests~2%"
817 (length (missing-tests results
)))))
819 (defun default-db-merge-function (results new-results
)
820 "Signal an error by default if a merge is required."
821 (lambda (key value1 value2
)
823 "Cannot merge TEST-RESULTS-DB instances ~A and ~A as key ~A has
824 two values, ~A and ~A"
825 results new-results key value1 value2
)))
827 (defun nappend-test-results-db (results new-results
&key merge
)
828 "Merge the results of NEW-RESULTS in to RESULTS. Any conflicts
829 between RESULTS and NEW-RESULTS are handled by the function MERGE.
831 The lambda list for the MERGE functions is
833 (key results-value new-results-value)
836 KEY is the key which appears in RESULTS and NEW-RESULTS.
837 RESULTS-VALUE is the value appearing RESULTS.
838 NEW-RESULTS-VALUE is the value appearing in NEW-RESULTS.
840 If MERGE is NIL, then an error is signalled when a conflict occurs.
842 (check-type results test-results-db
)
843 (check-type new-results test-results-db
)
844 (check-type merge
(or null function
))
846 with results-db
= (database results
)
847 with new-results-db
= (database new-results
)
849 (or merge
(default-db-merge-function results new-results
))
850 ;; Merge test databases
851 for key being each hash-key in new-results-db
852 using
(hash-value new-results-value
)
854 (multiple-value-bind (results-value presentp
)
855 (gethash key results-db
)
857 (gethash key results-db
)
859 (funcall merge key results-value new-results-value
)
863 (incf (pass results
) (pass new-results
))
864 (incf (fail results
) (fail new-results
))
865 (incf (exerr results
) (exerr new-results
))
866 ;; Merge failures, errors, and missing test details
869 (failed-tests results
)
870 (append (failed-tests results
) (failed-tests new-results
))
872 (error-tests results
)
873 (append (error-tests results
) (error-tests new-results
))
875 (missing-tests results
)
876 (append (missing-tests results
) (missing-tests new-results
))))
877 ;; Return the merged results
880 (defun reduce-test-results-dbs (all-results &key merge
)
881 "Return a new instance of TEST-RESULTS-DB which contains all of the
882 results in the sequence RESULTS. Any conflicts are handled by the
885 The lambda list for the MERGE function is
887 (key value-1 value-2)
890 KEY is the key which appears at least twice in the sequence RESULTS.
891 VALUE-1 and VALUE-2 are the conflicting values for the given KEY.
893 If MERGE is NIL, then an error is signalled when a conflict occurs."
895 with accumulated-test-results-db
= (make-instance 'test-results-db
)
896 for new-results in all-results do
897 (nappend-test-results-db
898 accumulated-test-results-db new-results
:merge merge
)
899 finally
(return accumulated-test-results-db
)))
903 (define-condition test-run-complete
()
905 :type
'test-results-db
909 "Signaled when a test run is finished."))
911 (defun %run-all-thunks
(&optional
(package *package
*))
912 "Run all of the test thunks in the package."
913 (with-package-table (table package
)
915 with results
= (make-instance 'test-results-db
)
916 for test-name being each hash-key in table
917 using
(hash-value unit-test
)
919 (record-result test-name
(code unit-test
) results
)
921 (push test-name
(missing-tests results
))
922 ;; Summarize and return the test results
924 (when *signal-results
*
925 (signal 'test-run-complete
:results results
))
926 (when *summarize-results
*
927 (summarize-results results
))
930 (defun %run-thunks
(test-names &optional
(package *package
*))
931 "Run the list of test thunks in the package."
932 (with-package-table (table package
)
934 with results
= (make-instance 'test-results-db
)
935 for test-name in test-names
936 as unit-test
= (gethash test-name table
)
938 (record-result test-name
(code unit-test
) results
)
940 (push test-name
(missing-tests results
))
942 (when *signal-results
*
943 (signal 'test-run-complete
:results results
))
944 (when *summarize-results
*
945 (summarize-results results
))
948 (defun run-tests (&optional
(test-names :all
) (package *package
*))
949 "Run the specified tests in package."
951 (if (eq :all test-names
)
952 (%run-all-thunks package
)
953 (%run-thunks test-names package
)))
955 (defun run-tags (&optional
(tags :all
) (package *package
*))
956 "Run the tests associated with the specified tags in package."
958 (%run-thunks
(tagged-tests tags package
) package
))
962 (defgeneric print-failures
(result &optional stream
)
964 "Report the results of the failed assertion."))
966 (defmethod print-failures :around
((result failure-result
) &optional
967 (stream *standard-output
*))
968 "Failure header and footer output."
969 (format stream
"~& | Failed Form: ~S" (form result
))
971 (when (extras result
)
972 (format stream
"~{~& | ~S => ~S~}~%" (extras result
)))
973 (format stream
"~& |~%"))
975 (defmethod print-failures ((result failure-result
) &optional
976 (stream *standard-output
*))
977 (format stream
"~& | Expected ~{~S~^; ~} " (expected result
))
978 (format stream
"~<~% | ~:;but saw ~{~S~^; ~}~>" (actual result
)))
980 (defmethod print-failures ((result error-result
) &optional
981 (stream *standard-output
*))
982 (format stream
"~& | ~@[Should have signalled ~{~S~^; ~} but saw~]"
984 (format stream
" ~{~S~^; ~}" (actual result
)))
986 (defmethod print-failures ((result macro-result
) &optional
987 (stream *standard-output
*))
988 (format stream
"~& | Should have expanded to ~{~S~^; ~} "
990 (format stream
"~<~%~:;but saw ~{~S~^; ~}~>" (actual result
)))
992 (defmethod print-failures ((result output-result
) &optional
993 (stream *standard-output
*))
994 (format stream
"~& | Should have printed ~{~S~^; ~} "
996 (format stream
"~<~%~:;but saw ~{~S~^; ~}~>"
999 (defmethod print-failures ((result test-result
) &optional
1000 (stream *standard-output
*))
1001 "Print the failed assertions in the unit test."
1002 (loop for fail in
(fail result
) do
1003 (print-failures fail stream
)))
1005 (defmethod print-failures ((results test-results-db
) &optional
1006 (stream *standard-output
*))
1007 "Print all of the failure tests."
1008 (loop with db
= (database results
)
1009 for test in
(failed-tests results
)
1010 as result
= (gethash test db
)
1012 (print-failures result stream
)
1013 (print-summary result stream
)))
1017 (defgeneric print-errors
(result &optional stream
)
1019 "Print the error condition."))
1021 (defmethod print-errors ((result test-result
) &optional
1022 (stream *standard-output
*))
1023 "Print the error condition."
1024 (let ((exerr (exerr result
))
1025 (*print-escape
* nil
))
1027 (format stream
"~& | Execution error:~% | ~W" exerr
)
1028 (format stream
"~& |~%"))))
1030 (defmethod print-errors ((results test-results-db
) &optional
1031 (stream *standard-output
*))
1032 "Print all of the error tests."
1033 (loop with db
= (database results
)
1034 for test in
(error-tests results
)
1035 as result
= (gethash test db
)
1037 (print-errors result stream
)
1038 (print-summary result stream
)))
1040 ;;; Useful equality predicates for tests
1042 (defun logically-equal (x y
)
1043 "Return true if x and y are both false or both true."
1044 (eql (not x
) (not y
)))
1046 (defun set-equal (list1 list2
&rest initargs
&key key
(test #'equal
))
1047 "Return true if every element of list1 is an element of list2 and
1049 (declare (ignore key test
))
1053 (apply #'subsetp list1 list2 initargs
)
1054 (apply #'subsetp list2 list1 initargs
)))