Signal a type-error if anything besides a symbol is used for a test name.
[lisp-unit.git] / lisp-unit.lisp
blob9ad2edb68b46511289fde4149bf66b6791c5efb7
1 ;;;-*- Mode: Lisp; Syntax: ANSI-Common-Lisp -*-
3 #|
4 Copyright (c) 2004-2005 Christopher K. Riesbeck
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files (the "Software"),
8 to deal in the Software without restriction, including without limitation
9 the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 and/or sell copies of the Software, and to permit persons to whom the
11 Software is furnished to do so, subject to the following conditions:
13 The above copyright notice and this permission notice shall be included
14 in all copies or substantial portions of the Software.
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 OTHER DEALINGS IN THE SOFTWARE.
25 How to use
26 ----------
28 1. Read the documentation at:
29 https://github.com/OdonataResearchLLC/lisp-unit/wiki
31 2. Make a file of DEFINE-TEST's. See exercise-tests.lisp for many
32 examples. If you want, start your test file with (REMOVE-TESTS :ALL)
33 to clear any previously defined tests.
35 3. Load this file.
37 4. (use-package :lisp-unit)
39 5. Load your code file and your file of tests.
41 6. Test your code with (RUN-TESTS '(test-name1 test-name2 ...)) or
42 simply (RUN-TESTS :ALL) to run all defined tests.
44 A summary of how many tests passed and failed will be printed.
46 NOTE: Nothing is compiled until RUN-TESTS is expanded. Redefining
47 functions or even macros does not require reloading any tests.
51 ;;; Packages
53 (in-package :cl-user)
55 (defpackage :lisp-unit
56 (:use :common-lisp)
57 ;; Print parameters
58 (:export :*print-summary*
59 :*print-failures*
60 :*print-errors*)
61 ;; Forms for assertions
62 (:export :assert-eq
63 :assert-eql
64 :assert-equal
65 :assert-equalp
66 :assert-equality
67 :assert-prints
68 :assert-expands
69 :assert-true
70 :assert-false
71 :assert-error)
72 ;; Functions for managing tests
73 (:export :define-test
74 :list-tests
75 :test-code
76 :test-documentation
77 :remove-tests
78 :run-tests
79 :use-debugger)
80 ;; Functions for managing tags
81 (:export :list-tags
82 :tagged-tests
83 :remove-tags
84 :run-tags)
85 ;; Functions for reporting test results
86 (:export :test-names
87 :failed-tests
88 :error-tests
89 :missing-tests
90 :print-failures
91 :print-errors
92 :summarize-results)
93 ;; Functions for extensibility via signals
94 (:export :signal-results
95 :test-run-complete
96 :results)
97 ;; Utility predicates
98 (:export :logically-equal :set-equal))
100 (in-package :lisp-unit)
102 ;;; Global counters
104 (defparameter *pass* 0
105 "The passed assertion results.")
107 (defparameter *fail* ()
108 "The failed assertion results.")
110 (defun reset-counters ()
111 "Reset the counters to empty lists."
112 (setf *pass* 0 *fail* ()))
114 ;;; Global options
116 (defparameter *print-summary* nil
117 "Print a summary of the pass, fail, and error count if non-nil.")
119 (defparameter *print-failures* nil
120 "Print failure messages if non-NIL.")
122 (defparameter *print-errors* nil
123 "Print error messages if non-NIL.")
125 (defparameter *use-debugger* nil
126 "If not NIL, enter the debugger when an error is encountered in an
127 assertion.")
129 (defparameter *signal-results* nil
130 "Signal the result if non NIL.")
132 (defun use-debugger-p (condition)
133 "Debug or ignore errors."
134 (cond
135 ((eq :ask *use-debugger*)
136 (y-or-n-p "~A -- debug?" condition))
137 (*use-debugger*)))
139 (defun use-debugger (&optional (flag t))
140 "Use the debugger when testing, or not."
141 (setq *use-debugger* flag))
143 (defun signal-results (&optional (flag t))
144 "Signal the results for extensibility."
145 (setq *signal-results* flag))
147 ;;; Global unit test database
149 (defparameter *test-db* (make-hash-table :test #'eq)
150 "The unit test database is simply a hash table.")
152 (defun package-table (package &optional create)
153 (cond
154 ((gethash (find-package package) *test-db*))
155 (create
156 (setf (gethash package *test-db*) (make-hash-table)))
157 (t (warn "No tests defined for package: ~S" package))))
159 ;;; Global tags database
161 (defparameter *tag-db* (make-hash-table :test #'eq)
162 "The tag database is simply a hash table.")
164 (defun package-tags (package &optional create)
165 "Return the tags DB for the package."
166 (cond
167 ((gethash (find-package package) *tag-db*))
168 (create
169 (setf (gethash package *tag-db*) (make-hash-table)))
170 (t (warn "No tags defined for package: ~S" package))))
172 ;;; Unit test definition
174 (defclass unit-test ()
175 ((doc
176 :type string
177 :initarg :doc
178 :reader doc)
179 (code
180 :type list
181 :initarg :code
182 :reader code))
183 (:default-initargs :doc "" :code ())
184 (:documentation
185 "Organize the unit test documentation and code."))
187 ;;; NOTE: Shamelessly taken from PG's analyze-body
188 (defun parse-body (body &optional doc tag)
189 "Separate the components of the body."
190 (let ((item (first body)))
191 (cond
192 ((and (listp item) (eq :tag (first item)))
193 (parse-body (rest body) doc (nconc (rest item) tag)))
194 ((and (stringp item) (not doc) (rest body))
195 (if tag
196 (values doc tag (rest body))
197 (parse-body (rest body) doc tag)))
198 (t (values doc tag body)))))
200 (defun test-name-error-report (test-name-error stream)
201 "Write the test-name-error to the stream."
202 (format stream "Test name ~S is not of type ~A."
203 (type-error-datum test-name-error)
204 (type-error-expected-type test-name-error)))
206 (define-condition test-name-error (type-error)
208 (:default-initargs :expected-type 'symbol)
209 (:report test-name-error-report)
210 (:documentation
211 "The test name error is a type error."))
213 (defun valid-test-name (name)
214 "Signal a type-error if the test name is not a symbol."
215 (if (symbolp name)
216 name
217 (error 'test-name-error :datum name)))
219 (defmacro define-test (name &body body)
220 "Store the test in the test database."
221 (let ((qname (gensym "NAME-")))
222 (multiple-value-bind (doc tag code) (parse-body body)
223 `(let* ((,qname ,(valid-test-name name))
224 (doc (or ,doc (string ,qname))))
225 (setf
226 ;; Unit test
227 (gethash ,qname (package-table *package* t))
228 (make-instance 'unit-test :doc doc :code ',code))
229 ;; Tags
230 (loop for tag in ',tag do
231 (pushnew
232 ,qname (gethash tag (package-tags *package* t))))
233 ;; Return the name of the test
234 ,qname))))
236 ;;; Manage tests
238 (defun list-tests (&optional (package *package*))
239 "Return a list of the tests in package."
240 (let ((table (package-table package)))
241 (when table
242 (loop for test-name being each hash-key in table
243 collect test-name))))
245 (defun test-documentation (name &optional (package *package*))
246 "Return the documentation for the test."
247 (let ((unit-test (gethash name (package-table package))))
248 (if (null unit-test)
249 (warn "No code defined for test ~A in package ~S."
250 name package)
251 (doc unit-test))))
253 (defun test-code (name &optional (package *package*))
254 "Returns the code stored for the test name."
255 (let ((unit-test (gethash name (package-table package))))
256 (if (null unit-test)
257 (warn "No code defined for test ~A in package ~S."
258 name package)
259 (code unit-test))))
261 (defun remove-tests (names &optional (package *package*))
262 "Remove individual tests or entire sets."
263 (if (eq :all names)
264 (if (null package)
265 (clrhash *test-db*)
266 (progn
267 (remhash (find-package package) *test-db*)
268 (remhash (find-package package) *tag-db*)))
269 (let ((table (package-table package)))
270 (unless (null table)
271 ;; Remove tests
272 (loop for name in names
273 unless (remhash name table) do
274 (warn "No test ~A in package ~A to remove."
275 name (package-name package)))
276 ;; Remove tests from tags
277 (loop with tags = (package-tags package)
278 for tag being each hash-key in tags
279 using (hash-value tagged-tests)
281 (setf
282 (gethash tag tags)
283 (set-difference tagged-tests names)))))))
285 ;;; Manage tags
287 (defun %tests-from-all-tags (&optional (package *package*))
288 "Return all of the tests that have been tagged."
289 (loop for tests being each hash-value in (package-tags package)
290 nconc (copy-list tests) into all-tests
291 finally (return (delete-duplicates all-tests))))
293 (defun %tests-from-tags (tags &optional (package *package*))
294 "Return the tests associated with the tags."
295 (loop with table = (package-tags package)
296 for tag in tags
297 as tests = (gethash tag table)
298 if (null tests) do (warn "No tests tagged with ~S." tag)
299 else nconc (copy-list tests) into all-tests
300 finally (return (delete-duplicates all-tests))))
302 (defun list-tags (&optional (package *package*))
303 "Return a list of the tags in package."
304 (let ((tags (package-tags package)))
305 (when tags
306 (loop for tag being each hash-key in tags collect tag))))
308 (defun tagged-tests (tags &optional (package *package*))
309 "Return a list of the tests associated with the tags."
310 (if (eq :all tags)
311 (%tests-from-all-tags package)
312 (%tests-from-tags tags package)))
314 (defun remove-tags (tags &optional (package *package*))
315 "Remove individual tags or entire sets."
316 (if (eq :all tags)
317 (if (null package)
318 (clrhash *tag-db*)
319 (remhash (find-package package) *tag-db*))
320 (let ((table (package-tags package)))
321 (unless (null table)
322 (loop for tag in tags
323 unless (remhash tag table) do
324 (warn "No tag ~A in package ~A to remove."
325 tag (package-name package)))))))
327 ;;; Assert macros
329 (defmacro assert-eq (expected form &rest extras)
330 "Assert whether expected and form are EQ."
331 `(expand-assert :equal ,form ,form ,expected ,extras :test #'eq))
333 (defmacro assert-eql (expected form &rest extras)
334 "Assert whether expected and form are EQL."
335 `(expand-assert :equal ,form ,form ,expected ,extras :test #'eql))
337 (defmacro assert-equal (expected form &rest extras)
338 "Assert whether expected and form are EQUAL."
339 `(expand-assert :equal ,form ,form ,expected ,extras :test #'equal))
341 (defmacro assert-equalp (expected form &rest extras)
342 "Assert whether expected and form are EQUALP."
343 `(expand-assert :equal ,form ,form ,expected ,extras :test #'equalp))
345 (defmacro assert-error (condition form &rest extras)
346 "Assert whether form signals condition."
347 `(expand-assert :error ,form (expand-error-form ,form)
348 ,condition ,extras))
350 (defmacro assert-expands (expansion form &rest extras)
351 "Assert whether form expands to expansion."
352 `(expand-assert :macro ,form
353 (expand-macro-form ,form nil)
354 ',expansion ,extras))
356 (defmacro assert-false (form &rest extras)
357 "Assert whether the form is false."
358 `(expand-assert :result ,form ,form nil ,extras))
360 (defmacro assert-equality (test expected form &rest extras)
361 "Assert whether expected and form are equal according to test."
362 `(expand-assert :equal ,form ,form ,expected ,extras :test ,test))
364 (defmacro assert-prints (output form &rest extras)
365 "Assert whether printing the form generates the output."
366 `(expand-assert :output ,form (expand-output-form ,form)
367 ,output ,extras))
369 (defmacro assert-true (form &rest extras)
370 "Assert whether the form is true."
371 `(expand-assert :result ,form ,form t ,extras))
373 (defmacro expand-assert (type form body expected extras &key (test '#'eql))
374 "Expand the assertion to the internal format."
375 `(internal-assert ,type ',form
376 (lambda () ,body)
377 (lambda () ,expected)
378 (expand-extras ,extras)
379 ,test))
381 (defmacro expand-error-form (form)
382 "Wrap the error assertion in HANDLER-CASE."
383 `(handler-case ,form
384 (condition (error) error)))
386 (defmacro expand-output-form (form)
387 "Capture the output of the form in a string."
388 (let ((out (gensym)))
389 `(let* ((,out (make-string-output-stream))
390 (*standard-output*
391 (make-broadcast-stream *standard-output* ,out)))
392 ,form
393 (get-output-stream-string ,out))))
395 (defmacro expand-macro-form (form env)
396 "Expand the macro form once."
397 `(let ((*gensym-counter* 1))
398 (macroexpand-1 ',form ,env)))
400 (defmacro expand-extras (extras)
401 "Expand extra forms."
402 `(lambda ()
403 (list ,@(mapcan (lambda (form) (list `',form form)) extras))))
405 (defclass failure-result ()
406 ((form
407 :initarg :form
408 :reader form)
409 (actual
410 :type list
411 :initarg :actual
412 :reader actual)
413 (expected
414 :type list
415 :initarg :expected
416 :reader expected)
417 (extras
418 :type list
419 :initarg :extras
420 :reader extras)
421 (test
422 :type function
423 :initarg :test
424 :reader test))
425 (:documentation
426 "Failure details of the assertion."))
428 (defclass equal-result (failure-result)
430 (:documentation
431 "Result of a failed equal assertion."))
433 (defun equal-result (test expected actual)
434 "Return the result of an equal assertion."
435 (and
436 (<= (length expected) (length actual))
437 (every test expected actual)))
439 (defclass error-result (failure-result)
441 (:documentation
442 "Result of a failed error assertion."))
444 (defun error-result (test expected actual)
445 "Return the result of an error assertion."
446 (declare (ignore test))
448 (eql (car actual) (car expected))
449 (typep (car actual) (car expected))))
451 (defclass macro-result (failure-result)
453 (:documentation
454 "Result of a failed macro expansion assertion."))
456 (defun %expansion-equal (form1 form2)
457 "Descend into the forms checking for equality."
458 (let ((item1 (first form1))
459 (item2 (first form2)))
460 (cond
461 ((and (null item1) (null item2)))
462 ((and (listp item1) (listp item2))
463 (and
464 (%expansion-equal item1 item2)
465 (%expansion-equal (rest form1) (rest form2))))
466 ((and (symbolp item1) (symbolp item2))
467 (and
468 (string= (symbol-name item1) (symbol-name item2))
469 (%expansion-equal (rest form1) (rest form2))))
470 (t (and
471 (equal item1 item2)
472 (%expansion-equal (rest form1) (rest form2)))))))
474 (defun macro-result (test expected actual)
475 "Return the result of a macro assertion."
476 (declare (ignore test))
477 (%expansion-equal (first expected) (first actual)))
479 (defclass boolean-result (failure-result)
481 (:documentation
482 "Result of a failed boolean assertion."))
484 (defun boolean-result (test expected actual)
485 "Return the result of a result assertion."
486 (declare (ignore test))
487 (logically-equal (car actual) (car expected)))
489 (defclass output-result (failure-result)
491 (:documentation
492 "Result of a failed output assertion."))
494 (defun output-result (test expected actual)
495 "Return the result of an output assertion."
496 (declare (ignore test))
497 (string=
498 (string-trim '(#\newline #\return #\space) (car actual))
499 (car expected)))
501 (defun assert-function (type)
502 "Return the function for the assertion type."
503 (ecase type
504 (:equal #'equal-result)
505 (:error #'error-result)
506 (:macro #'macro-result)
507 (:result #'boolean-result)
508 (:output #'output-result)))
510 (defun assert-class (type)
511 "Return the class for the assertion type."
512 (ecase type
513 (:equal 'equal-result)
514 (:error 'error-result)
515 (:macro 'macro-result)
516 (:result 'boolean-result)
517 (:output 'output-result)))
519 (defun internal-assert
520 (type form code-thunk expected-thunk extras test)
521 "Perform the assertion and record the results."
522 (let* ((actual (multiple-value-list (funcall code-thunk)))
523 (expected (multiple-value-list (funcall expected-thunk)))
524 (result
525 (funcall (assert-function type) test expected actual)))
526 (if result
527 (incf *pass*)
528 (push (make-instance
529 (assert-class type)
530 :form form
531 :actual actual
532 :expected expected
533 :extras (when extras (funcall extras))
534 :test test)
535 *fail*))
536 ;; Return the result
537 result))
539 ;;; Unit test results
541 (defclass test-result ()
542 ((name
543 :type symbol
544 :initarg :name
545 :reader name)
546 (pass
547 :type fixnum
548 :initarg :pass
549 :reader pass)
550 (fail
551 :type list
552 :initarg :fail
553 :reader fail)
554 (exerr
555 :initarg :exerr
556 :reader exerr)
557 (run-time
558 :initarg :run-time
559 :reader run-time
560 :documentation
561 "Test run time measured in internal time units"))
562 (:default-initargs :exerr nil)
563 (:documentation
564 "Store the results of the unit test."))
566 (defun print-summary (test-result &optional
567 (stream *standard-output*))
568 "Print a summary of the test result."
569 (format stream "~&~A: ~S assertions passed, ~S failed"
570 (name test-result)
571 (pass test-result)
572 (length (fail test-result)))
573 (if (exerr test-result)
574 (format stream ", and an execution error.")
575 (write-char #\. stream))
576 (terpri stream)
577 (terpri stream))
579 (defun run-code (code)
580 "Run the code to test the assertions."
581 (funcall (coerce `(lambda () ,@code) 'function)))
583 (defun run-test-thunk (name code)
584 (let ((*pass* 0)
585 (*fail* ())
586 (start (get-internal-run-time)))
587 (handler-bind
588 ((error
589 (lambda (condition)
590 (if (use-debugger-p condition)
591 condition
592 (return-from run-test-thunk
593 (make-instance
594 'test-result
595 :name name
596 :pass *pass*
597 :fail *fail*
598 :run-time (- (get-internal-run-time) start)
599 :exerr condition))))))
600 (run-code code))
601 ;; Return the result count
602 (make-instance
603 'test-result
604 :name name
605 :pass *pass*
606 :fail *fail*
607 :run-time (- (get-internal-run-time) start))))
609 ;;; Test results database
611 (defclass test-results-db ()
612 ((database
613 :type hash-table
614 :initform (make-hash-table :test #'eq)
615 :reader database)
616 (pass
617 :type fixnum
618 :initform 0
619 :accessor pass)
620 (fail
621 :type fixnum
622 :initform 0
623 :accessor fail)
624 (exerr
625 :type fixnum
626 :initform 0
627 :accessor exerr)
628 (failed-tests
629 :type list
630 :initform ()
631 :accessor failed-tests)
632 (error-tests
633 :type list
634 :initform ()
635 :accessor error-tests)
636 (missing-tests
637 :type list
638 :initform ()
639 :accessor missing-tests))
640 (:documentation
641 "Store the results of the tests for further evaluation."))
643 (defmethod print-object ((object test-results-db) stream)
644 "Print the summary counts with the object."
645 (let ((pass (pass object))
646 (fail (fail object))
647 (exerr (exerr object)))
648 (format
649 stream "#<~A Total(~D) Passed(~D) Failed(~D) Errors(~D)>~%"
650 (class-name (class-of object))
651 (+ pass fail) pass fail exerr)))
653 (defun test-names (test-results-db)
654 "Return a list of the test names in the database."
655 (loop for name being each hash-key in (database test-results-db)
656 collect name))
658 (defun record-result (test-name code results)
659 "Run the test code and record the result."
660 (let ((result (run-test-thunk test-name code)))
661 ;; Store the result
662 (setf (gethash test-name (database results)) result)
663 ;; Count passed tests
664 (when (plusp (pass result))
665 (incf (pass results) (pass result)))
666 ;; Count failed tests and record the name
667 (when (fail result)
668 (incf (fail results) (length (fail result)))
669 (push test-name (failed-tests results)))
670 ;; Count errors and record the name
671 (when (exerr result)
672 (incf (exerr results))
673 (push test-name (error-tests results)))
674 ;; Running output
675 (when *print-failures* (print-failures result))
676 (when *print-errors* (print-errors result))
677 (when (or *print-summary* *print-failures* *print-errors*)
678 (print-summary result))))
680 (defun summarize-results (results &optional
681 (stream *standard-output*))
682 "Print a summary of all results to the stream."
683 (let ((pass (pass results))
684 (fail (fail results)))
685 (format stream "~&Unit Test Summary~%")
686 (format stream " | ~D assertions total~%" (+ pass fail))
687 (format stream " | ~D passed~%" pass)
688 (format stream " | ~D failed~%" fail)
689 (format stream " | ~D execution errors~%" (exerr results))
690 (format stream " | ~D missing tests~2%"
691 (length (missing-tests results)))))
693 ;;; Run the tests
695 (define-condition test-run-complete ()
696 ((results
697 :type 'test-results-db
698 :initarg :results
699 :reader results))
700 (:documentation
701 "Signaled when a test run is finished."))
703 (defun %run-all-thunks (&optional (package *package*))
704 "Run all of the test thunks in the package."
705 (loop
706 with results = (make-instance 'test-results-db)
707 for test-name being each hash-key in (package-table package)
708 using (hash-value unit-test)
709 if unit-test do
710 (record-result test-name (code unit-test) results)
711 else do
712 (push test-name (missing-tests results))
713 ;; Summarize and return the test results
714 finally
715 (when *signal-results*
716 (signal 'test-run-complete :results results))
717 (summarize-results results)
718 (return results)))
720 (defun %run-thunks (test-names &optional (package *package*))
721 "Run the list of test thunks in the package."
722 (loop
723 with table = (package-table package)
724 and results = (make-instance 'test-results-db)
725 for test-name in test-names
726 as unit-test = (gethash test-name table)
727 if unit-test do
728 (record-result test-name (code unit-test) results)
729 else do
730 (push test-name (missing-tests results))
731 finally
732 (when *signal-results*
733 (signal 'test-run-complete :results results))
734 (summarize-results results)
735 (return results)))
737 (defun run-tests (test-names &optional (package *package*))
738 "Run the specified tests in package."
739 (reset-counters)
740 (if (eq :all test-names)
741 (%run-all-thunks package)
742 (%run-thunks test-names package)))
744 (defun run-tags (tags &optional (package *package*))
745 "Run the tests associated with the specified tags in package."
746 (reset-counters)
747 (%run-thunks (tagged-tests tags package) package))
749 ;;; Print failures
751 (defgeneric print-failures (result &optional stream)
752 (:documentation
753 "Report the results of the failed assertion."))
755 (defmethod print-failures :around ((result failure-result) &optional
756 (stream *standard-output*))
757 "Failure header and footer output."
758 (format stream "~& | Failed Form: ~S" (form result))
759 (call-next-method)
760 (when (extras result)
761 (format stream "~{~& | ~S => ~S~}~%" (extras result)))
762 (format stream "~& |~%"))
764 (defmethod print-failures ((result failure-result) &optional
765 (stream *standard-output*))
766 (format stream "~& | Expected ~{~S~^; ~} " (expected result))
767 (format stream "~<~% | ~:;but saw ~{~S~^; ~}~>" (actual result)))
769 (defmethod print-failures ((result error-result) &optional
770 (stream *standard-output*))
771 (format stream "~& | ~@[Should have signalled ~{~S~^; ~} but saw~]"
772 (expected result))
773 (format stream " ~{~S~^; ~}" (actual result)))
775 (defmethod print-failures ((result macro-result) &optional
776 (stream *standard-output*))
777 (format stream "~& | Should have expanded to ~{~S~^; ~} "
778 (expected result))
779 (format stream "~<~%~:;but saw ~{~S~^; ~}~>" (actual result)))
781 (defmethod print-failures ((result output-result) &optional
782 (stream *standard-output*))
783 (format stream "~& | Should have printed ~{~S~^; ~} "
784 (expected result))
785 (format stream "~<~%~:;but saw ~{~S~^; ~}~>"
786 (actual result)))
788 (defmethod print-failures ((result test-result) &optional
789 (stream *standard-output*))
790 "Print the failed assertions in the unit test."
791 (loop for fail in (fail result) do
792 (print-failures fail stream)))
794 (defmethod print-failures ((results test-results-db) &optional
795 (stream *standard-output*))
796 "Print all of the failure tests."
797 (loop with db = (database results)
798 for test in (failed-tests results)
799 as result = (gethash test db)
801 (print-failures result stream)
802 (print-summary result stream)))
804 ;;; Print errors
806 (defgeneric print-errors (result &optional stream)
807 (:documentation
808 "Print the error condition."))
810 (defmethod print-errors ((result test-result) &optional
811 (stream *standard-output*))
812 "Print the error condition."
813 (let ((exerr (exerr result))
814 (*print-escape* nil))
815 (when exerr
816 (format stream "~& | Execution error:~% | ~W" exerr)
817 (format stream "~& |~%"))))
819 (defmethod print-errors ((results test-results-db) &optional
820 (stream *standard-output*))
821 "Print all of the error tests."
822 (loop with db = (database results)
823 for test in (error-tests results)
824 as result = (gethash test db)
826 (print-errors result stream)
827 (print-summary result stream)))
829 ;;; Useful equality predicates for tests
831 (defun logically-equal (x y)
832 "Return true if x and y are both false or both true."
833 (eql (not x) (not y)))
835 (defun set-equal (list1 list2 &rest initargs &key key (test #'equal))
836 "Return true if every element of list1 is an element of list2 and
837 vice versa."
838 (declare (ignore key test))
839 (and
840 (listp list1)
841 (listp list2)
842 (apply #'subsetp list1 list2 initargs)
843 (apply #'subsetp list2 list1 initargs)))
845 (pushnew :lisp-unit common-lisp:*features*)