Fix issue 17 in remove-tags.
[lisp-unit.git] / lisp-unit.lisp
blobefaae1a49ea202223934bab5bfa4ebc159a2ffc8
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 null-tests-warning-report (null-tests-warning stream)
153 "Write the null-tests-warning to the stream."
154 (format stream "No tests defined for package ~A."
155 (tests-package-name null-tests-warning)))
157 (define-condition null-tests-warning (simple-warning)
158 ((name
159 :type string
160 :initarg :name
161 :reader tests-package-name))
162 (:report null-tests-warning-report))
164 (defun package-table (package &optional create)
165 (let ((packobj (find-package package)))
166 (cond
167 ((gethash packobj *test-db*))
168 (create
169 (setf (gethash packobj *test-db*) (make-hash-table)))
170 (t (warn 'null-tests-warning :name (package-name package))))))
172 (defmacro with-package-table ((table
173 &optional (package *package*) create)
174 &body body)
175 "Execute the body only if the package table exists."
176 (let ((gtable (gensym "TABLE-")))
177 `(let* ((,gtable (package-table ,package ,create))
178 (,table ,gtable))
179 (when (hash-table-p ,gtable) ,@body))))
181 ;;; Global tags database
183 (defparameter *tag-db* (make-hash-table :test #'eq)
184 "The tag database is simply a hash table.")
186 (defun null-tags-warning-report (null-tags-warning stream)
187 "Write the null-tags-warning to the stream."
188 (format stream "No tags defined for package ~A."
189 (tags-package-name null-tags-warning)))
191 (define-condition null-tags-warning (simple-warning)
192 ((name
193 :type string
194 :initarg :name
195 :reader tags-package-name))
196 (:report null-tags-warning-report))
198 (defun package-tags (package &optional create)
199 "Return the tags DB for the package."
200 (let ((packobj (find-package package)))
201 (cond
202 ((gethash packobj *tag-db*))
203 (create
204 (setf (gethash packobj *tag-db*) (make-hash-table)))
205 (t (warn 'null-tags-warning :name (package-name package))))))
207 (defmacro with-package-tags ((table
208 &optional (package *package*) create)
209 &body body)
210 "Execute the body only if the package tags exists."
211 (let ((gtable (gensym "TABLE-")))
212 `(let* ((,gtable (package-tags ,package ,create))
213 (,table ,gtable))
214 (when (hash-table-p ,gtable) ,@body))))
216 ;;; Unit test definition
218 (defclass unit-test ()
219 ((doc
220 :type string
221 :initarg :doc
222 :reader doc)
223 (code
224 :type list
225 :initarg :code
226 :reader code))
227 (:default-initargs :doc "" :code ())
228 (:documentation
229 "Organize the unit test documentation and code."))
231 ;;; NOTE: Shamelessly taken from PG's analyze-body
232 (defun parse-body (body &optional doc tag)
233 "Separate the components of the body."
234 (let ((item (first body)))
235 (cond
236 ((and (listp item) (eq :tag (first item)))
237 (parse-body (rest body) doc (nconc (rest item) tag)))
238 ((and (stringp item) (not doc) (rest body))
239 (if tag
240 (values doc tag (rest body))
241 (parse-body (rest body) doc tag)))
242 (t (values doc tag body)))))
244 (defun test-name-error-report (test-name-error stream)
245 "Write the test-name-error to the stream."
246 (format stream "Test name ~S is not of type ~A."
247 (type-error-datum test-name-error)
248 (type-error-expected-type test-name-error)))
250 (define-condition test-name-error (type-error)
252 (:default-initargs :expected-type 'symbol)
253 (:report test-name-error-report)
254 (:documentation
255 "The test name error is a type error."))
257 (defun valid-test-name (name)
258 "Signal a type-error if the test name is not a symbol."
259 (if (symbolp name)
260 name
261 (error 'test-name-error :datum name)))
263 (defmacro define-test (name &body body)
264 "Store the test in the test database."
265 (let ((qname (gensym "NAME-")))
266 (multiple-value-bind (doc tag code) (parse-body body)
267 `(let* ((,qname (valid-test-name ',name))
268 (doc (or ,doc (string ,qname))))
269 (setf
270 ;; Unit test
271 (gethash ,qname (package-table *package* t))
272 (make-instance 'unit-test :doc doc :code ',code))
273 ;; Tags
274 (loop for tag in ',tag do
275 (pushnew
276 ,qname (gethash tag (package-tags *package* t))))
277 ;; Return the name of the test
278 ,qname))))
280 ;;; Manage tests
282 (defun list-tests (&optional (package *package*))
283 "Return a list of the tests in package."
284 (with-package-table (table package)
285 (loop for test-name being each hash-key in table
286 collect test-name)))
288 (defun test-documentation (name &optional (package *package*))
289 "Return the documentation for the test."
290 (with-package-table (table package)
291 (let ((unit-test (gethash name table)))
292 (if (null unit-test)
293 (warn "No test ~A in package ~A."
294 name (package-name package))
295 (doc unit-test)))))
297 (defun test-code (name &optional (package *package*))
298 "Returns the code stored for the test name."
299 (with-package-table (table package)
300 (let ((unit-test (gethash name table)))
301 (if (null unit-test)
302 (warn "No test ~A in package ~A."
303 name (package-name package))
304 (code unit-test)))))
306 (defun remove-tests (&optional (names :all) (package *package*))
307 "Remove individual tests or entire sets."
308 (if (eq :all names)
309 (if (null package)
310 (clrhash *test-db*)
311 (progn
312 (remhash (find-package package) *test-db*)
313 (remhash (find-package package) *tag-db*)))
314 (progn
315 ;; Remove tests
316 (with-package-table (table package)
317 (loop for name in names
318 unless (remhash name table) do
319 (warn "No test ~A in package ~A to remove."
320 name (package-name package))))
321 ;; Remove tests from tags
322 (with-package-tags (tags package)
323 (loop for tag being each hash-key in tags
324 using (hash-value tagged-tests)
326 (setf
327 (gethash tag tags)
328 (set-difference tagged-tests names)))))))
330 ;;; Manage tags
332 (defun %tests-from-all-tags (&optional (package *package*))
333 "Return all of the tests that have been tagged."
334 (with-package-tags (table package)
335 (loop for tests being each hash-value in table
336 nconc (copy-list tests) into all-tests
337 finally (return (delete-duplicates all-tests)))))
339 (defun %tests-from-tags (tags &optional (package *package*))
340 "Return the tests associated with the tags."
341 (with-package-tags (table package)
342 (loop for tag in tags
343 as tests = (gethash tag table)
344 if (null tests) do (warn "No tests tagged with ~S." tag)
345 else nconc (copy-list tests) into all-tests
346 finally (return (delete-duplicates all-tests)))))
348 (defun list-tags (&optional (package *package*))
349 "Return a list of the tags in package."
350 (with-package-tags (table package)
351 (loop for tag being each hash-key in table collect tag)))
353 (defun tagged-tests (&optional (tags :all) (package *package*))
354 "Return a list of the tests associated with the tags."
355 (if (eq :all tags)
356 (%tests-from-all-tags package)
357 (%tests-from-tags tags package)))
359 (defun remove-tags (&optional (tags :all) (package *package*))
360 "Remove individual tags or entire sets."
361 (if (eq :all tags)
362 (if (null package)
363 (clrhash *tag-db*)
364 (remhash (find-package package) *tag-db*))
365 (with-package-tags (tag-table package)
366 (loop for tag in tags
367 unless (remhash tag tag-table) do
368 (warn "No tag ~A in package ~A to remove."
369 tag (package-name package))))))
371 ;;; Assert macros
373 (defmacro assert-eq (expected form &rest extras)
374 "Assert whether expected and form are EQ."
375 `(expand-assert :equal ,form ,form ,expected ,extras :test #'eq))
377 (defmacro assert-eql (expected form &rest extras)
378 "Assert whether expected and form are EQL."
379 `(expand-assert :equal ,form ,form ,expected ,extras :test #'eql))
381 (defmacro assert-equal (expected form &rest extras)
382 "Assert whether expected and form are EQUAL."
383 `(expand-assert :equal ,form ,form ,expected ,extras :test #'equal))
385 (defmacro assert-equalp (expected form &rest extras)
386 "Assert whether expected and form are EQUALP."
387 `(expand-assert :equal ,form ,form ,expected ,extras :test #'equalp))
389 (defmacro assert-error (condition form &rest extras)
390 "Assert whether form signals condition."
391 `(expand-assert :error ,form (expand-error-form ,form)
392 ,condition ,extras))
394 (defmacro assert-expands (expansion form &rest extras)
395 "Assert whether form expands to expansion."
396 `(expand-assert :macro ,form
397 (expand-macro-form ,form nil)
398 ',expansion ,extras))
400 (defmacro assert-false (form &rest extras)
401 "Assert whether the form is false."
402 `(expand-assert :result ,form ,form nil ,extras))
404 (defmacro assert-equality (test expected form &rest extras)
405 "Assert whether expected and form are equal according to test."
406 `(expand-assert :equal ,form ,form ,expected ,extras :test ,test))
408 (defmacro assert-prints (output form &rest extras)
409 "Assert whether printing the form generates the output."
410 `(expand-assert :output ,form (expand-output-form ,form)
411 ,output ,extras))
413 (defmacro assert-true (form &rest extras)
414 "Assert whether the form is true."
415 `(expand-assert :result ,form ,form t ,extras))
417 (defmacro expand-assert (type form body expected extras &key (test '#'eql))
418 "Expand the assertion to the internal format."
419 `(internal-assert ,type ',form
420 (lambda () ,body)
421 (lambda () ,expected)
422 (expand-extras ,extras)
423 ,test))
425 (defmacro expand-error-form (form)
426 "Wrap the error assertion in HANDLER-CASE."
427 `(handler-case ,form
428 (condition (error) error)))
430 (defmacro expand-output-form (form)
431 "Capture the output of the form in a string."
432 (let ((out (gensym)))
433 `(let* ((,out (make-string-output-stream))
434 (*standard-output*
435 (make-broadcast-stream *standard-output* ,out)))
436 ,form
437 (get-output-stream-string ,out))))
439 (defmacro expand-macro-form (form env)
440 "Expand the macro form once."
441 `(let ((*gensym-counter* 1))
442 (macroexpand-1 ',form ,env)))
444 (defmacro expand-extras (extras)
445 "Expand extra forms."
446 `(lambda ()
447 (list ,@(mapcan (lambda (form) (list `',form form)) extras))))
449 (defgeneric assert-result (type test expected actual)
450 (:documentation
451 "Return the result of the assertion."))
453 (defgeneric record-failure (type form actual expected extras test)
454 (:documentation
455 "Record the details of the failure."))
457 (defclass failure-result ()
458 ((form
459 :initarg :form
460 :reader form)
461 (actual
462 :type list
463 :initarg :actual
464 :reader actual)
465 (expected
466 :type list
467 :initarg :expected
468 :reader expected)
469 (extras
470 :type list
471 :initarg :extras
472 :reader extras)
473 (test
474 :type function
475 :initarg :test
476 :reader test))
477 (:documentation
478 "Failure details of the assertion."))
480 (defun %record-failure (class form actual expected extras test)
481 "Return an instance of the failure result."
482 (make-instance class
483 :form form
484 :actual actual
485 :expected expected
486 :extras extras
487 :test test))
489 (defclass equal-result (failure-result)
491 (:documentation
492 "Result of a failed equal assertion."))
494 (defmethod assert-result ((type (eql :equal)) test expected actual)
495 "Return the result of an equal assertion."
496 (and
497 (<= (length expected) (length actual))
498 (every test expected actual)))
500 (defmethod record-failure ((type (eql :equal))
501 form actual expected extras test)
502 "Return an instance of an equal failure result."
503 (%record-failure 'equal-result form actual expected extras test))
505 (defclass error-result (failure-result)
507 (:documentation
508 "Result of a failed error assertion."))
510 (defmethod assert-result ((type (eql :error)) test expected actual)
511 "Return the result of an error assertion."
512 (declare (ignore test))
514 (eql (car actual) (car expected))
515 (typep (car actual) (car expected))))
517 (defmethod record-failure ((type (eql :error))
518 form actual expected extras test)
519 "Return an instance of an error failure result."
520 (%record-failure 'error-result form actual expected extras test))
522 (defclass macro-result (failure-result)
524 (:documentation
525 "Result of a failed macro expansion assertion."))
527 (defun %expansion-equal (form1 form2)
528 "Descend into the forms checking for equality."
529 (let ((item1 (first form1))
530 (item2 (first form2)))
531 (cond
532 ((and (null item1) (null item2)))
533 ((and (listp item1) (listp item2))
534 (and
535 (%expansion-equal item1 item2)
536 (%expansion-equal (rest form1) (rest form2))))
537 ((and (symbolp item1) (symbolp item2))
538 (and
539 (string= (symbol-name item1) (symbol-name item2))
540 (%expansion-equal (rest form1) (rest form2))))
541 (t (and
542 (equal item1 item2)
543 (%expansion-equal (rest form1) (rest form2)))))))
545 (defmethod assert-result ((type (eql :macro)) test expected actual)
546 "Return the result of a macro assertion."
547 (declare (ignore test))
548 (%expansion-equal (first expected) (first actual)))
550 (defmethod record-failure ((type (eql :macro))
551 form actual expected extras test)
552 "Return an instance of a macro failure result."
553 (%record-failure 'macro-result form actual expected extras test))
555 (defclass boolean-result (failure-result)
557 (:documentation
558 "Result of a failed boolean assertion."))
560 (defmethod assert-result ((type (eql :result)) test expected actual)
561 "Return the result of a result assertion."
562 (declare (ignore test))
563 (logically-equal (car actual) (car expected)))
565 (defmethod record-failure ((type (eql :result))
566 form actual expected extras test)
567 "Return an instance of a boolean failure result."
568 (%record-failure 'boolean-result form actual expected extras test))
570 (defclass output-result (failure-result)
572 (:documentation
573 "Result of a failed output assertion."))
575 (defmethod assert-result ((type (eql :output)) test expected actual)
576 "Return the result of an output assertion."
577 (declare (ignore test))
578 (string=
579 (string-trim '(#\newline #\return #\space) (car actual))
580 (car expected)))
582 (defmethod record-failure ((type (eql :output))
583 form actual expected extras test)
584 "Return an instance of an output failure result."
585 (%record-failure 'output-result form actual expected extras test))
587 (defun internal-assert
588 (type form code-thunk expected-thunk extras test)
589 "Perform the assertion and record the results."
590 (let* ((actual (multiple-value-list (funcall code-thunk)))
591 (expected (multiple-value-list (funcall expected-thunk)))
592 (result (assert-result type test expected actual)))
593 (if result
594 (incf *pass*)
595 (push
596 (record-failure
597 type form actual expected
598 (when extras (funcall extras)) test)
599 *fail*))
600 ;; Return the result
601 result))
603 ;;; Unit test results
605 (defclass test-result ()
606 ((name
607 :type symbol
608 :initarg :name
609 :reader name)
610 (pass
611 :type fixnum
612 :initarg :pass
613 :reader pass)
614 (fail
615 :type list
616 :initarg :fail
617 :reader fail)
618 (exerr
619 :initarg :exerr
620 :reader exerr)
621 (run-time
622 :initarg :run-time
623 :reader run-time
624 :documentation
625 "Test run time measured in internal time units"))
626 (:default-initargs :exerr nil)
627 (:documentation
628 "Store the results of the unit test."))
630 (defun print-summary (test-result &optional
631 (stream *standard-output*))
632 "Print a summary of the test result."
633 (format stream "~&~A: ~S assertions passed, ~S failed"
634 (name test-result)
635 (pass test-result)
636 (length (fail test-result)))
637 (if (exerr test-result)
638 (format stream ", and an execution error.")
639 (write-char #\. stream))
640 (terpri stream)
641 (terpri stream))
643 (defun run-code (code)
644 "Run the code to test the assertions."
645 (funcall (coerce `(lambda () ,@code) 'function)))
647 (defun run-test-thunk (name code)
648 (let ((*pass* 0)
649 (*fail* ())
650 (start (get-internal-run-time)))
651 (handler-bind
652 ((error
653 (lambda (condition)
654 (if (use-debugger-p condition)
655 condition
656 (return-from run-test-thunk
657 (make-instance
658 'test-result
659 :name name
660 :pass *pass*
661 :fail *fail*
662 :run-time (- (get-internal-run-time) start)
663 :exerr condition))))))
664 (run-code code))
665 ;; Return the result count
666 (make-instance
667 'test-result
668 :name name
669 :pass *pass*
670 :fail *fail*
671 :run-time (- (get-internal-run-time) start))))
673 ;;; Test results database
675 (defclass test-results-db ()
676 ((database
677 :type hash-table
678 :initform (make-hash-table :test #'eq)
679 :reader database)
680 (pass
681 :type fixnum
682 :initform 0
683 :accessor pass)
684 (fail
685 :type fixnum
686 :initform 0
687 :accessor fail)
688 (exerr
689 :type fixnum
690 :initform 0
691 :accessor exerr)
692 (failed-tests
693 :type list
694 :initform ()
695 :accessor failed-tests)
696 (error-tests
697 :type list
698 :initform ()
699 :accessor error-tests)
700 (missing-tests
701 :type list
702 :initform ()
703 :accessor missing-tests))
704 (:documentation
705 "Store the results of the tests for further evaluation."))
707 (defmethod print-object ((object test-results-db) stream)
708 "Print the summary counts with the object."
709 (let ((pass (pass object))
710 (fail (fail object))
711 (exerr (exerr object)))
712 (format
713 stream "#<~A Total(~D) Passed(~D) Failed(~D) Errors(~D)>~%"
714 (class-name (class-of object))
715 (+ pass fail) pass fail exerr)))
717 (defun test-names (test-results-db)
718 "Return a list of the test names in the database."
719 (loop for name being each hash-key in (database test-results-db)
720 collect name))
722 (defun record-result (test-name code results)
723 "Run the test code and record the result."
724 (let ((result (run-test-thunk test-name code)))
725 ;; Store the result
726 (setf (gethash test-name (database results)) result)
727 ;; Count passed tests
728 (when (plusp (pass result))
729 (incf (pass results) (pass result)))
730 ;; Count failed tests and record the name
731 (when (fail result)
732 (incf (fail results) (length (fail result)))
733 (push test-name (failed-tests results)))
734 ;; Count errors and record the name
735 (when (exerr result)
736 (incf (exerr results))
737 (push test-name (error-tests results)))
738 ;; Running output
739 (when *print-failures* (print-failures result))
740 (when *print-errors* (print-errors result))
741 (when (or *print-summary* *print-failures* *print-errors*)
742 (print-summary result))))
744 (defun summarize-results (results &optional
745 (stream *standard-output*))
746 "Print a summary of all results to the stream."
747 (let ((pass (pass results))
748 (fail (fail results)))
749 (format stream "~&Unit Test Summary~%")
750 (format stream " | ~D assertions total~%" (+ pass fail))
751 (format stream " | ~D passed~%" pass)
752 (format stream " | ~D failed~%" fail)
753 (format stream " | ~D execution errors~%" (exerr results))
754 (format stream " | ~D missing tests~2%"
755 (length (missing-tests results)))))
757 ;;; Run the tests
759 (define-condition test-run-complete ()
760 ((results
761 :type 'test-results-db
762 :initarg :results
763 :reader results))
764 (:documentation
765 "Signaled when a test run is finished."))
767 (defun %run-all-thunks (&optional (package *package*))
768 "Run all of the test thunks in the package."
769 (with-package-table (table package)
770 (loop
771 with results = (make-instance 'test-results-db)
772 for test-name being each hash-key in table
773 using (hash-value unit-test)
774 if unit-test do
775 (record-result test-name (code unit-test) results)
776 else do
777 (push test-name (missing-tests results))
778 ;; Summarize and return the test results
779 finally
780 (when *signal-results*
781 (signal 'test-run-complete :results results))
782 (summarize-results results)
783 (return results))))
785 (defun %run-thunks (test-names &optional (package *package*))
786 "Run the list of test thunks in the package."
787 (with-package-table (table package)
788 (loop
789 with results = (make-instance 'test-results-db)
790 for test-name in test-names
791 as unit-test = (gethash test-name table)
792 if unit-test do
793 (record-result test-name (code unit-test) results)
794 else do
795 (push test-name (missing-tests results))
796 finally
797 (when *signal-results*
798 (signal 'test-run-complete :results results))
799 (summarize-results results)
800 (return results))))
802 (defun run-tests (&optional (test-names :all) (package *package*))
803 "Run the specified tests in package."
804 (reset-counters)
805 (if (eq :all test-names)
806 (%run-all-thunks package)
807 (%run-thunks test-names package)))
809 (defun run-tags (&optional (tags :all) (package *package*))
810 "Run the tests associated with the specified tags in package."
811 (reset-counters)
812 (%run-thunks (tagged-tests tags package) package))
814 ;;; Print failures
816 (defgeneric print-failures (result &optional stream)
817 (:documentation
818 "Report the results of the failed assertion."))
820 (defmethod print-failures :around ((result failure-result) &optional
821 (stream *standard-output*))
822 "Failure header and footer output."
823 (format stream "~& | Failed Form: ~S" (form result))
824 (call-next-method)
825 (when (extras result)
826 (format stream "~{~& | ~S => ~S~}~%" (extras result)))
827 (format stream "~& |~%"))
829 (defmethod print-failures ((result failure-result) &optional
830 (stream *standard-output*))
831 (format stream "~& | Expected ~{~S~^; ~} " (expected result))
832 (format stream "~<~% | ~:;but saw ~{~S~^; ~}~>" (actual result)))
834 (defmethod print-failures ((result error-result) &optional
835 (stream *standard-output*))
836 (format stream "~& | ~@[Should have signalled ~{~S~^; ~} but saw~]"
837 (expected result))
838 (format stream " ~{~S~^; ~}" (actual result)))
840 (defmethod print-failures ((result macro-result) &optional
841 (stream *standard-output*))
842 (format stream "~& | Should have expanded to ~{~S~^; ~} "
843 (expected result))
844 (format stream "~<~%~:;but saw ~{~S~^; ~}~>" (actual result)))
846 (defmethod print-failures ((result output-result) &optional
847 (stream *standard-output*))
848 (format stream "~& | Should have printed ~{~S~^; ~} "
849 (expected result))
850 (format stream "~<~%~:;but saw ~{~S~^; ~}~>"
851 (actual result)))
853 (defmethod print-failures ((result test-result) &optional
854 (stream *standard-output*))
855 "Print the failed assertions in the unit test."
856 (loop for fail in (fail result) do
857 (print-failures fail stream)))
859 (defmethod print-failures ((results test-results-db) &optional
860 (stream *standard-output*))
861 "Print all of the failure tests."
862 (loop with db = (database results)
863 for test in (failed-tests results)
864 as result = (gethash test db)
866 (print-failures result stream)
867 (print-summary result stream)))
869 ;;; Print errors
871 (defgeneric print-errors (result &optional stream)
872 (:documentation
873 "Print the error condition."))
875 (defmethod print-errors ((result test-result) &optional
876 (stream *standard-output*))
877 "Print the error condition."
878 (let ((exerr (exerr result))
879 (*print-escape* nil))
880 (when exerr
881 (format stream "~& | Execution error:~% | ~W" exerr)
882 (format stream "~& |~%"))))
884 (defmethod print-errors ((results test-results-db) &optional
885 (stream *standard-output*))
886 "Print all of the error tests."
887 (loop with db = (database results)
888 for test in (error-tests results)
889 as result = (gethash test db)
891 (print-errors result stream)
892 (print-summary result stream)))
894 ;;; Useful equality predicates for tests
896 (defun logically-equal (x y)
897 "Return true if x and y are both false or both true."
898 (eql (not x) (not y)))
900 (defun set-equal (list1 list2 &rest initargs &key key (test #'equal))
901 "Return true if every element of list1 is an element of list2 and
902 vice versa."
903 (declare (ignore key test))
904 (and
905 (listp list1)
906 (listp list2)
907 (apply #'subsetp list1 list2 initargs)
908 (apply #'subsetp list2 list1 initargs)))
910 (pushnew :lisp-unit common-lisp:*features*)