1 ;;;-*- Mode: Lisp; Syntax: ANSI-Common-Lisp -*-
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.
26 ;;; A test suite package, modelled after JUnit.
27 ;;; Author: Chris Riesbeck
37 1. Read the documentation in lisp-unit.html.
39 2. Make a file of DEFINE-TEST
's. See exercise-tests.lisp for many
40 examples. If you want
, start your test file with
(REMOVE-TESTS) to
41 clear any previously defined tests.
45 2.
(use-package :lisp-unit
)
47 3. Load your code file and your file of tests.
49 4. Test your code with
(RUN-TESTS test-name1 test-name2 ...
) -- no quotes
! --
50 or simply
(RUN-TESTS) to run all defined tests.
52 A summary of how many tests passed and failed will be printed
,
53 with details on the failures.
55 Note
: Nothing is compiled until RUN-TESTS is expanded. Redefining
56 functions or even macros does not require reloading any tests.
58 For more information
, see lisp-unit.html.
62 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
64 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
68 (defpackage :lisp-unit
71 (:export
:*print-summary
*
74 ;; Forms for assertions
85 ;; Functions for managing tests
90 ;; Functions for reporting test results
97 (:export
:logically-equal
:set-equal
))
99 (in-package :lisp-unit
)
103 (defparameter *pass
* 0
104 "The number of passed assertions.")
106 (defparameter *fail
* 0
107 "The number of failed assertions.")
111 (defparameter *print-summary
* nil
112 "Print a summary of the pass, fail, and error count if non-nil.")
114 (defparameter *print-failures
* nil
115 "Print failure messages if non-NIL.")
117 (defparameter *print-errors
* nil
118 "Print error messages if non-NIL.")
120 (defparameter *use-debugger
* nil
121 "If not NIL, enter the debugger when an error is encountered in an
124 (defun use-debugger-p (condition)
125 "Debug or ignore errors."
127 ((eq :ask
*use-debugger
*)
128 (y-or-n-p "~A -- debug?" condition
))
131 ;;; Failure control strings
133 (defgeneric failure-control-string
(type)
135 "~& | Expected ~{~S~^; ~} ~<~% | ~:;but saw ~{~S~^; ~}~>")
137 "Return the FORMAT control string for the failure type."))
139 (defmethod failure-control-string ((type (eql :error
)))
140 "~& | ~@[Should have signalled ~{~S~^; ~} but saw~] ~{~S~^; ~}")
142 (defmethod failure-control-string ((type (eql :macro
)))
143 "~& | Should have expanded to ~{~S~^; ~} ~<~%~:;but saw ~{~S~^; ~}~>")
145 (defmethod failure-control-string ((type (eql :output
)))
146 "~& | Should have printed ~{~S~^; ~} ~<~%~:;but saw ~{~S~^; ~}~>")
148 (defun print-failure (type form expected actual extras
)
149 "Report the details of the failure assertion."
150 (format t
" | Failed Form: ~S" form
)
151 (format t
(failure-control-string type
) expected actual
)
153 (format t
"~{~& | ~S => ~S~}~%" (funcall extras
)))
157 (defun print-error (condition)
158 "Print the error condition."
159 (let ((*print-escape
* nil
))
160 (format t
"~& | Execution error:~% | ~W" condition
)
161 (format t
"~& |~%")))
163 (defun print-summary (name pass fail
&optional exerr
)
164 "Print a summary of the test results."
165 (format t
"~&~A: ~S assertions passed, ~S failed"
167 (format t
"~@[, ~S execution errors~].~2%" exerr
))
169 ;;; Global unit test database
171 (defparameter *test-db
* (make-hash-table :test
#'eq
)
172 "The unit test database is simply a hash table.")
174 (defun package-table (package &optional create
)
176 ((gethash (find-package package
) *test-db
*))
178 (setf (gethash package
*test-db
*) (make-hash-table)))))
180 (defmacro define-test
(name &body body
)
181 "Store the test in the test database."
184 (gethash ',name
(package-table *package
* t
))
186 ;; Return the name of the test
189 ;;; Remove tests from the test DB
191 (defun remove-tests (names &optional
(package *package
*))
192 "Remove individual tests or entire sets."
196 (remhash (find-package package
) *test-db
*))
197 (let ((table (package-table package
)))
199 (loop for name in names
200 always
(remhash name table
)
201 collect name into removed
202 finally
(return removed
))))))
206 (defmacro assert-eq
(expected form
&rest extras
)
207 "Assert whether expected and form are EQ."
208 `(expand-assert :equal
,form
,form
,expected
,extras
:test
#'eq
))
210 (defmacro assert-eql
(expected form
&rest extras
)
211 "Assert whether expected and form are EQL."
212 `(expand-assert :equal
,form
,form
,expected
,extras
:test
#'eql
))
214 (defmacro assert-equal
(expected form
&rest extras
)
215 "Assert whether expected and form are EQUAL."
216 `(expand-assert :equal
,form
,form
,expected
,extras
:test
#'equal
))
218 (defmacro assert-equalp
(expected form
&rest extras
)
219 "Assert whether expected and form are EQUALP."
220 `(expand-assert :equal
,form
,form
,expected
,extras
:test
#'equalp
))
222 (defmacro assert-error
(condition form
&rest extras
)
223 "Assert whether form signals condition."
224 `(expand-assert :error
,form
(expand-error-form ,form
)
227 (defmacro assert-expands
(expansion form
&rest extras
)
228 "Assert whether form expands to expansion."
229 `(expand-assert :macro
,form
230 (expand-macro-form ,form nil
)
233 (defmacro assert-false
(form &rest extras
)
234 "Assert whether the form is false."
235 `(expand-assert :result
,form
,form nil
,extras
))
237 (defmacro assert-equality
(test expected form
&rest extras
)
238 "Assert whether expected and form are equal according to test."
239 `(expand-assert :equal
,form
,form
,expected
,extras
:test
,test
))
241 (defmacro assert-prints
(output form
&rest extras
)
242 "Assert whether printing the form generates the output."
243 `(expand-assert :output
,form
(expand-output-form ,form
)
246 (defmacro assert-true
(form &rest extras
)
247 "Assert whether the form is true."
248 `(expand-assert :result
,form
,form t
,extras
))
250 (defmacro expand-assert
(type form body expected extras
&key
(test '#'eql
))
251 "Expand the assertion to the internal format."
252 `(internal-assert ,type
',form
254 (lambda () ,expected
)
255 (expand-extras ,extras
)
258 (defmacro expand-error-form
(form)
259 "Wrap the error assertion in HANDLER-CASE."
261 (condition (error) error
)))
263 (defmacro expand-output-form
(form)
264 "Capture the output of the form in a string."
265 (let ((out (gensym)))
266 `(let* ((,out
(make-string-output-stream))
268 (make-broadcast-stream *standard-output
* ,out
)))
270 (get-output-stream-string ,out
))))
272 (defmacro expand-macro-form
(form env
)
273 "Expand the macro form once."
274 `(macroexpand-1 ',form
,env
))
276 (defmacro expand-extras
(extras)
277 "Expand extra forms."
279 (list ,@(mapcan (lambda (form) (list `',form form
)) extras
))))
281 (defun internal-assert
282 (type form code-thunk expected-thunk extras test
)
283 "Perform the assertion and record the results."
284 (let ((expected (multiple-value-list (funcall expected-thunk
)))
285 (actual (multiple-value-list (funcall code-thunk
)))
287 ;; Count the assertion
288 (if (setq passed
(test-passed-p type expected actual test
))
291 ;; Report the assertion
292 (when (and (not passed
) *print-failures
*)
293 (print-failure type form expected actual extras
))
297 ;;; Test passed predicate.
299 (defgeneric test-passed-p
(type expected actual test
)
301 "Return the result of the test."))
303 (defmethod test-passed-p ((type (eql :error
)) expected actual test
)
304 "Return the result of the error assertion."
306 (eql (car actual
) (car expected
))
307 (typep (car actual
) (car expected
))))
309 (defmethod test-passed-p ((type (eql :equal
)) expected actual test
)
310 "Return the result of the equality assertion."
312 (<= (length expected
) (length actual
))
313 (every test expected actual
)))
315 (defmethod test-passed-p ((type (eql :macro
)) expected actual test
)
316 "Return the result of the macro expansion."
317 (equal (car actual
) (car expected
)))
319 (defmethod test-passed-p ((type (eql :output
)) expected actual test
)
320 "Return the result of the printed output."
322 (string-trim '(#\newline
#\return
#\space
) (car actual
))
325 (defmethod test-passed-p ((type (eql :result
)) expected actual test
)
326 "Return the result of the assertion."
327 (logically-equal (car actual
) (car expected
)))
331 (defclass test-results
()
335 :accessor test-names
)
351 :accessor failed-tests
)
355 :accessor error-tests
)
359 :accessor missing-tests
))
360 (:default-initargs
:test-names
())
362 "Store the results of the tests for further evaluation."))
364 (defmethod print-object ((object test-results
) stream
)
365 "Print the summary counts with the object."
366 (format stream
"#<~A Total(~D) Passed(~D) Failed(~D) Errors(~D)>~%"
367 (class-name (class-of object
))
368 (+ (pass object
) (fail object
))
369 (pass object
) (fail object
) (exerr object
)))
371 (defun record-result (test-name code results
)
372 "Run the test code and record the result."
373 (multiple-value-bind (pass fail exerr
)
374 (run-test-thunk code
)
375 (push test-name
(test-names results
))
376 ;; Count passed tests
378 (incf (pass results
) pass
))
379 ;; Count failed tests and record name
381 (incf (fail results
) fail
)
382 (push test-name
(failed-tests results
)))
383 ;; Count errors and record name
384 (when (eq :error exerr
)
385 (incf (exerr results
))
386 (push test-name
(error-tests results
)))
387 ;; Print a summary of the results
388 (when (or *print-summary
* *print-failures
* *print-errors
*)
391 (when (eq :error exerr
) 1)))))
393 (defun summarize-results (results)
394 "Print a summary of all results."
395 (let ((pass (pass results
))
396 (fail (fail results
)))
397 (format t
"Unit Test Summary~%")
398 (format t
" | ~D assertions total~%" (+ pass fail
))
399 (format t
" | ~D passed~%" pass
)
400 (format t
" | ~D failed~%" fail
)
401 (format t
" | ~D execution errors~%" (exerr results
))
402 (format t
" | ~D missing tests~2%"
403 (length (missing-tests results
)))))
407 (defun run-code (code)
408 "Run the code to test the assertions."
409 (funcall (coerce `(lambda () ,@code
) 'function
)))
411 (defun run-test-thunk (code)
414 (handler-case (run-code code
)
417 (print-error condition
))
418 (if (use-debugger-p condition
)
420 (return-from run-test-thunk
421 (values *pass
* *fail
* :error
)))))
422 ;; Return the result count
423 (values *pass
* *fail
* nil
)))
425 (defun %run-all-thunks
(&optional
(package *package
*))
426 "Run all of the test thunks in the package."
428 with results
= (make-instance 'test-results
)
429 for test-name being each hash-key in
(package-table package
)
430 using
(hash-value code
)
432 (record-result test-name code results
)
434 (push test-name
(missing-tests results
))
435 ;; Summarize and return the test results
437 (summarize-results results
)
440 (defun %run-thunks
(test-names &optional
(package *package
*))
441 "Run the list of test thunks in the package."
443 with table
= (package-table package
)
444 and results
= (make-instance 'test-results
)
445 for test-name in test-names
446 as code
= (gethash test-name table
)
448 (record-result test-name code results
)
450 (push test-name
(missing-tests results
))
452 (summarize-results results
)
455 (defun run-tests (test-names &optional
(package *package
*))
456 "Run the specified tests in package."
457 (if (eq :all test-names
)
458 (%run-all-thunks package
)
459 (%run-thunks test-names package
)))
461 ;;; Useful equality predicates for tests
463 ;;; (LOGICALLY-EQUAL x y) => true or false
464 ;;; Return true if x and y both false or both true
465 (defun logically-equal (x y
)
466 (eql (not x
) (not y
)))
468 ;;; (SET-EQUAL l1 l2 :test) => true or false
469 ;;; Return true if every element of l1 is an element of l2
471 (defun set-equal (l1 l2
&key
(test #'equal
))
474 (subsetp l1 l2
:test test
)
475 (subsetp l2 l1
:test test
)))
477 (pushnew :lisp-unit common-lisp
:*features
*)