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
70 ;; Forms for assertions
81 ;; Functions for managing tests
88 (:export
:logically-equal
:set-equal
))
90 (in-package :lisp-unit
)
94 (defparameter *pass
* 0
95 "The number of passed assertions.")
97 (defparameter *fail
* 0
98 "The number of failed assertions.")
102 (defparameter *show-results
* nil
103 "If not NIL, show the results of the assertions.")
105 (defparameter *use-debugger
* nil
106 "If not NIL, enter the debugger when an error is encountered in an
109 (defun show-results (&optional
(flag t
))
110 "Set flag to report results."
111 (setq *show-results
* flag
))
113 (defun use-debugger (&optional
(flag t
))
114 "Set flag to use the debugger."
115 (setq *use-debugger
* flag
))
117 (defun use-debugger-p (condition)
118 "Debug or ignore errors."
120 ((eq :ask
*use-debugger
*)
121 (y-or-n-p "~A -- debug?" condition
))
124 ;;; Global unit test database
126 (defparameter *test-db
* (make-hash-table :test
#'eq
)
127 "The unit test database is simply a hash table.")
129 (defun package-table (package &optional create
)
131 ((gethash (find-package package
) *test-db
*))
133 (setf (gethash package
*test-db
*) (make-hash-table)))))
135 (defmacro define-test
(name &body body
)
136 "Store the test in the test database."
139 (gethash ',name
(package-table *package
* t
))
141 ;; Return the name of the test
144 ;;; Remove tests from the test DB
146 (defun remove-tests (names &optional
(package *package
*))
147 "Remove individual tests or entire sets."
151 (remhash (find-package package
) *test-db
*))
152 (let ((table (package-table package
)))
154 (loop for name in names always
155 (remhash name table
))))))
159 (defmacro assert-eq
(expected form
&rest extras
)
160 "Assert whether expected and form are EQ."
161 `(expand-assert :equal
,form
,form
,expected
,extras
:test
#'eq
))
163 (defmacro assert-eql
(expected form
&rest extras
)
164 "Assert whether expected and form are EQL."
165 `(expand-assert :equal
,form
,form
,expected
,extras
:test
#'eql
))
167 (defmacro assert-equal
(expected form
&rest extras
)
168 "Assert whether expected and form are EQUAL."
169 `(expand-assert :equal
,form
,form
,expected
,extras
:test
#'equal
))
171 (defmacro assert-equalp
(expected form
&rest extras
)
172 "Assert whether expected and form are EQUALP."
173 `(expand-assert :equal
,form
,form
,expected
,extras
:test
#'equalp
))
175 (defmacro assert-error
(condition form
&rest extras
)
176 "Assert whether form signals condition."
177 `(expand-assert :error
,form
(expand-error-form ,form
)
180 (defmacro assert-expands
(expansion form
&rest extras
)
181 "Assert whether form expands to expansion."
182 `(expand-assert :macro
,form
183 (expand-macro-form ,form nil
)
186 (defmacro assert-false
(form &rest extras
)
187 "Assert whether the form is false."
188 `(expand-assert :result
,form
,form nil
,extras
))
190 (defmacro assert-equality
(test expected form
&rest extras
)
191 "Assert whether expected and form are equal according to test."
192 `(expand-assert :equal
,form
,form
,expected
,extras
:test
,test
))
194 (defmacro assert-prints
(output form
&rest extras
)
195 "Assert whether printing the form generates the output."
196 `(expand-assert :output
,form
(expand-output-form ,form
)
199 (defmacro assert-true
(form &rest extras
)
200 "Assert whether the form is true."
201 `(expand-assert :result
,form
,form t
,extras
))
203 (defmacro expand-assert
(type form body expected extras
&key
(test '#'eql
))
204 "Expand the assertion to the internal format."
205 `(internal-assert ,type
',form
207 (lambda () ,expected
)
208 (expand-extras ,extras
)
211 (defmacro expand-error-form
(form)
212 "Wrap the error assertion in HANDLER-CASE."
214 (condition (error) error
)))
216 (defmacro expand-output-form
(form)
217 "Capture the output of the form in a string."
218 (let ((out (gensym)))
219 `(let* ((,out
(make-string-output-stream))
221 (make-broadcast-stream *standard-output
* ,out
)))
223 (get-output-stream-string ,out
))))
225 (defmacro expand-macro-form
(form env
)
226 "Expand the macro form once."
227 `(macroexpand-1 ',form
,env
))
229 (defmacro expand-extras
(extras)
230 "Expand extra forms."
232 (list ,@(mapcan (lambda (form) (list `',form form
)) extras
))))
234 (defun internal-assert
235 (type form code-thunk expected-thunk extras test
)
236 (let ((expected (multiple-value-list (funcall expected-thunk
)))
237 (actual (multiple-value-list (funcall code-thunk
)))
239 ;; Count the assertion
240 (if (setq passed
(test-passed-p type expected actual test
))
243 ;; Report the assertion
244 (when (and (not passed
) *show-results
*)
245 (report-failure type form expected actual extras
))
249 ;;; Test passed predicate.
251 (defgeneric test-passed-p
(type expected actual test
)
253 "Return the result of the test."))
255 (defmethod test-passed-p ((type (eql :error
)) expected actual test
)
256 "Return the result of the error assertion."
258 (eql (car actual
) (car expected
))
259 (typep (car actual
) (car expected
))))
261 (defmethod test-passed-p ((type (eql :equal
)) expected actual test
)
262 "Return the result of the equality assertion."
264 (<= (length expected
) (length actual
))
265 (every test expected actual
)))
267 (defmethod test-passed-p ((type (eql :macro
)) expected actual test
)
268 "Return the result of the macro expansion."
269 (equal (car actual
) (car expected
)))
271 (defmethod test-passed-p ((type (eql :output
)) expected actual test
)
272 "Return the result of the printed output."
274 (string-trim '(#\newline
#\return
#\space
) (car actual
))
277 (defmethod test-passed-p ((type (eql :result
)) expected actual test
)
278 "Return the result of the assertion."
279 (logically-equal (car actual
) (car expected
)))
281 ;;; Failure control strings for reports.
283 (defgeneric failure-control-string
(type)
285 "~&Expected ~{~S~^; ~} ~<~%~:;but saw ~{~S~^; ~}~>")
287 "Return the FORMAT control string for the failure type."))
289 (defmethod failure-control-string ((type (eql :error
)))
290 "~&~@[Should have signalled ~{~S~^; ~} but saw~] ~{~S~^; ~}")
292 (defmethod failure-control-string ((type (eql :macro
)))
293 "~&Should have expanded to ~{~S~^; ~} ~<~%~:;but saw ~{~S~^; ~}~>")
295 (defmethod failure-control-string ((type (eql :output
)))
296 "~&Should have printed ~{~S~^; ~} ~<~%~:;but saw ~{~S~^; ~}~>")
300 (defun report-failure (type form expected actual extras
)
301 "Report the details of the failure assertion."
302 (format t
"Failed Form: ~S" form
)
303 (format t
(failure-control-string type
) expected actual
)
305 (format t
"~{~& ~S => ~S~}~%" (funcall extras
)))
310 (defclass test-results
()
314 :accessor test-names
)
330 :accessor failed-tests
)
334 :accessor error-tests
)
338 :accessor missing-tests
))
339 (:default-initargs
:test-names
())
341 "Store the results of the tests for further evaluation."))
343 (defmethod print-object ((object test-results
) stream
)
344 "Print the summary counts with the object."
345 (format stream
"#<~A Total(~D) Passed(~D) Failed(~D) Errors(~D)>~%"
346 (class-name (class-of object
))
347 (+ (pass object
) (fail object
))
348 (pass object
) (fail object
) (exerr object
)))
350 (defun report-summary (results)
351 "Print a summary of the results."
352 (let ((pass (pass results
))
353 (fail (fail results
)))
354 (format t
" ~D assertions total~%" (+ pass fail
))
355 (format t
" ~D passed~%" pass
)
356 (format t
" ~D failed~%" fail
)
357 (format t
" ~D execution errors~%" (exerr results
))))
361 (defun run-code (code)
362 "Run the code to test the assertions."
363 (funcall (coerce `(lambda () ,@code
) 'function
)))
365 (defun run-test-thunk (code)
368 (handler-case (run-code code
)
370 (if (use-debugger-p condition
)
372 (return-from run-test-thunk
373 (values *pass
* *fail
* :error
)))))
374 ;; Return the result count
375 (values *pass
* *fail
* nil
)))
377 (defun record-result (test-name code results
)
378 "Run the test code and record the result."
379 (multiple-value-bind (pass fail exerr
)
380 (run-test-thunk code
)
381 (push test-name
(test-names results
))
382 ;; Count passed tests
384 (incf (pass results
) pass
))
385 ;; Count failed tests and record name
387 (incf (fail results
) fail
)
388 (push test-name
(failed-tests results
)))
389 ;; Count errors and record name
390 (when (eq :error exerr
)
391 (incf (exerr results
))
392 (push test-name
(error-tests results
)))))
394 (defun %run-all-thunks
(&optional
(package *package
*))
395 "Run all of the test thunks in the package."
397 with results
= (make-instance 'test-results
)
398 for test-name being each hash-key in
(package-table package
)
399 using
(hash-value code
)
401 (record-result test-name code results
)
403 (push test-name
(missing-tests results
))
404 ;; Summarize and return the test results
408 (defun %run-thunks
(test-names &optional
(package *package
*))
409 "Run the list of test thunks in the package."
411 with table
= (package-table package
)
412 and results
= (make-instance 'test-results
)
413 for test-name in test-names
414 as code
= (gethash test-name table
)
416 (record-result test-name code results
)
418 (push test-name
(missing-tests results
))
422 (defun run-tests (test-names &optional
(package *package
*))
423 "Run the specified tests in package."
424 (if (eq :all test-names
)
425 (%run-all-thunks package
)
426 (%run-thunks test-names package
)))
428 ;;; Useful equality predicates for tests
430 ;;; (LOGICALLY-EQUAL x y) => true or false
431 ;;; Return true if x and y both false or both true
432 (defun logically-equal (x y
)
433 (eql (not x
) (not y
)))
435 ;;; (SET-EQUAL l1 l2 :test) => true or false
436 ;;; Return true if every element of l1 is an element of l2
438 (defun set-equal (l1 l2
&key
(test #'equal
))
441 (subsetp l1 l2
:test test
)
442 (subsetp l2 l1
:test test
)))
444 (pushnew :lisp-unit common-lisp
:*features
*)