Organize the files to differentiate extensions from internal tests.
[lisp-unit.git] / lisp-unit.lisp
blob866b616ed19901162026b615adad1f6c97782c46
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.
26 ;;; A test suite package, modelled after JUnit.
27 ;;; Author: Chris Riesbeck
28 ;;;
29 ;;; Update history:
30 ;;;
31 ;;; 04/07/06 added ~<...~> to remaining error output forms [CKR]
32 ;;; 04/06/06 added ~<...~> to compact error output better [CKR]
33 ;;; 04/06/06 fixed RUN-TESTS to get tests dynamically (bug reported
34 ;;; by Daniel Edward Burke) [CKR]
35 ;;; 02/08/06 added newlines to error output [CKR]
36 ;;; 12/30/05 renamed ASSERT-PREDICATE to ASSERT-EQUALITY [CKR]
37 ;;; 12/29/05 added ASSERT-EQ, ASSERT-EQL, ASSERT-EQUALP [CKR]
38 ;;; 12/22/05 recoded use-debugger to use handler-bind, added option to prompt for debugger,
39 ;;; 11/07/05 added *use-debugger* and assert-predicate [DFB]
40 ;;; 09/18/05 replaced Academic Free License with MIT Licence [CKR]
41 ;;; 08/30/05 added license notice [CKR]
42 ;;; 06/28/05 changed RUN-TESTS to compile code at run time, not expand time [CKR]
43 ;;; 02/21/05 removed length check from SET-EQUAL [CKR]
44 ;;; 02/17/05 added RUN-ALL-TESTS [CKR]
45 ;;; 01/18/05 added ASSERT-EQUAL back in [CKR]
46 ;;; 01/17/05 much clean up, added WITH-TEST-LISTENER [CKR]
47 ;;; 01/15/05 replaced ASSERT-EQUAL etc. with ASSERT-TRUE and ASSERT-FALSE [CKR]
48 ;;; 01/04/05 changed COLLECT-RESULTS to echo output on *STANDARD-OUTPuT* [CKR]
49 ;;; 01/04/05 added optional package argument to REMOVE-ALL-TESTS [CKR]
50 ;;; 01/04/05 changed OUTPUT-OK-P to trim spaces and returns [CKR]
51 ;;; 01/04/05 changed OUTPUT-OK-P to not check output except when asked to [CKR]
52 ;;; 12/03/04 merged REMOVE-TEST into REMOVE-TESTS [CKR]
53 ;;; 12/03/04 removed ability to pass forms to RUN-TESTS [CKR]
54 ;;; 12/03/04 refactored RUN-TESTS expansion into RUN-TEST-THUNKS [CKR]
55 ;;; 12/02/04 changed to group tests under packages [CKR]
56 ;;; 11/30/04 changed assertions to put expected value first, like JUnit [CKR]
57 ;;; 11/30/04 improved error handling and summarization [CKR]
58 ;;; 11/30/04 generalized RUN-TESTS, removed RUN-TEST [CKR]
59 ;;; 02/27/04 fixed ASSERT-PRINTS not ignoring value [CKR]
60 ;;; 02/07/04 fixed ASSERT-EXPANDS failure message [CKR]
61 ;;; 02/07/04 added ASSERT-NULL, ASSERT-NOT-NULL [CKR]
62 ;;; 01/31/04 added error handling and totalling to RUN-TESTS [CKR]
63 ;;; 01/31/04 made RUN-TEST/RUN-TESTS macros [CKR]
64 ;;; 01/29/04 fixed ASSERT-EXPANDS quote bug [CKR]
65 ;;; 01/28/04 major changes from BUG-FINDER to be more like JUnit [CKR]
69 How to use
70 ----------
72 1. Read the documentation in lisp-unit.html.
74 2. Make a file of DEFINE-TEST's. See exercise-tests.lisp for many
75 examples. If you want, start your test file with (REMOVE-TESTS) to
76 clear any previously defined tests.
78 2. Load this file.
80 2. (use-package :lisp-unit)
82 3. Load your code file and your file of tests.
84 4. Test your code with (RUN-TESTS test-name1 test-name2 ...) -- no quotes! --
85 or simply (RUN-TESTS) to run all defined tests.
87 A summary of how many tests passed and failed will be printed,
88 with details on the failures.
90 Note: Nothing is compiled until RUN-TESTS is expanded. Redefining
91 functions or even macros does not require reloading any tests.
93 For more information, see lisp-unit.html.
97 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
98 ;;; Packages
99 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
101 (in-package :cl-user)
103 (defpackage :lisp-unit
104 (:use :common-lisp)
105 (:export
106 :define-test :run-all-tests :run-tests
107 :assert-eq :assert-eql :assert-equal :assert-equalp
108 :assert-error :assert-expands :assert-false
109 :assert-equality :assert-prints :assert-true
110 :get-test-code :get-tests
111 :remove-all-tests :remove-tests
112 :logically-equal :set-equal
113 :use-debugger
114 :with-test-listener))
116 (in-package :lisp-unit)
118 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
119 ;;; Globals
120 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
122 (defparameter *test-listener* nil)
124 (defparameter *tests* (make-hash-table))
126 ;;; Used by RUN-TESTS to collect summary statistics
127 (defvar *test-count* 0)
128 (defvar *pass-count* 0)
130 ;;; Set by RUN-TESTS for use by SHOW-FAILURE
131 (defvar *test-name* nil)
133 ;;; If nil, errors in tests are caught and counted.
134 ;;; If :ask, user is given option of entering debugger or not.
135 ;;; If true and not :ask, debugger is entered.
136 (defparameter *use-debugger* nil)
138 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
139 ;;; Macros
140 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
142 ;;; DEFINE-TEST
144 (defmacro define-test (name &body body)
145 `(progn
146 (store-test-code ',name ',body)
147 ',name))
149 ;;; ASSERT macros
151 (defmacro assert-eq (expected form &rest extras)
152 `(expand-assert :equal ,form ,form ,expected ,extras :test #'eq))
154 (defmacro assert-eql (expected form &rest extras)
155 `(expand-assert :equal ,form ,form ,expected ,extras :test #'eql))
157 (defmacro assert-equal (expected form &rest extras)
158 `(expand-assert :equal ,form ,form ,expected ,extras :test #'equal))
160 (defmacro assert-equalp (expected form &rest extras)
161 `(expand-assert :equal ,form ,form ,expected ,extras :test #'equalp))
163 (defmacro assert-error (condition form &rest extras)
164 `(expand-assert :error ,form ,(expand-error-form form)
165 ,condition ,extras))
167 (defmacro assert-expands (&environment env expansion form &rest extras)
168 `(expand-assert :macro ,form
169 ,(expand-macro-form form #+lispworks nil #-lispworks env)
170 ,expansion ,extras))
172 (defmacro assert-false (form &rest extras)
173 `(expand-assert :result ,form ,form nil ,extras))
175 (defmacro assert-equality (test expected form &rest extras)
176 `(expand-assert :equal ,form ,form ,expected ,extras :test ,test))
178 (defmacro assert-prints (output form &rest extras)
179 `(expand-assert :output ,form ,(expand-output-form form)
180 ,output ,extras))
182 (defmacro assert-true (form &rest extras)
183 `(expand-assert :result ,form ,form t ,extras))
185 (defmacro expand-assert (type form body expected extras &key (test '#'eql))
186 `(internal-assert ,type ',form
187 (lambda () ,body)
188 (lambda () ,expected)
189 ,(expand-extras extras)
190 ,test))
192 (defun expand-error-form (form)
193 `(handler-case ,form
194 (condition (error) error)))
196 (defun expand-output-form (form)
197 (let ((out (gensym)))
198 `(let* ((,out (make-string-output-stream))
199 (*standard-output* (make-broadcast-stream *standard-output* ,out)))
200 ,form
201 (get-output-stream-string ,out))))
203 (defun expand-macro-form (form env)
204 `(macroexpand-1 ',form ,env))
206 (defun expand-extras (extras)
207 `(lambda () (list ,@(mapcan (lambda (form) (list `',form form)) extras))))
209 ;;; RUN-TESTS
211 (defmacro run-all-tests (package &rest tests)
212 `(let ((*package* (find-package ',package)))
213 (run-tests
214 ,@(mapcar (lambda (test) (find-symbol (symbol-name test) package))
215 tests))))
217 (defmacro run-tests (&rest names)
218 `(run-test-thunks
219 (get-test-thunks ,(if (null names) '(get-tests *package*) `',names))))
221 (defun get-test-thunks (names &optional (package *package*))
222 (mapcar (lambda (name) (get-test-thunk name package))
223 names))
225 (defun get-test-thunk (name package)
226 (assert (get-test-code name package) (name package)
227 "No test defined for ~S in package ~S" name package)
228 (list name (coerce `(lambda () ,@(get-test-code name)) 'function)))
230 (defun use-debugger (&optional (flag t))
231 (setq *use-debugger* flag))
233 ;;; WITH-TEST-LISTENER
234 (defmacro with-test-listener (listener &body body)
235 `(let ((*test-listener* #',listener)) ,@body))
237 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
238 ;;; Public functions
239 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
241 (defun get-test-code (name &optional (package *package*))
242 (let ((table (get-package-table package)))
243 (unless (null table)
244 (gethash name table))))
246 (defun get-tests (&optional (package *package*))
247 (let ((l nil)
248 (table (get-package-table package)))
249 (cond ((null table) nil)
251 (maphash (lambda (key val)
252 (declare (ignore val))
253 (push key l))
254 table)
255 (sort l #'string< :key #'string)))))
257 (defun remove-tests (names &optional (package *package*))
258 (let ((table (get-package-table package)))
259 (unless (null table)
260 (if (null names)
261 (clrhash table)
262 (dolist (name names)
263 (remhash name table))))))
265 (defun remove-all-tests (&optional (package *package*))
266 (if (null package)
267 (clrhash *tests*)
268 (remhash (find-package package) *tests*)))
270 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
271 ;;; Private functions
272 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
274 ;;; DEFINE-TEST support
276 (defun get-package-table (package &key create)
277 (let ((table (gethash (find-package package) *tests*)))
278 (or table
279 (and create
280 (setf (gethash package *tests*)
281 (make-hash-table))))))
283 (defun get-test-name (form)
284 (if (atom form) form (cadr form)))
286 (defun store-test-code (name code &optional (package *package*))
287 (setf (gethash name (get-package-table package :create t))
288 code))
290 ;;; ASSERTION support
292 (defun internal-assert (type form code-thunk expected-thunk extras test)
293 (let* ((expected (multiple-value-list (funcall expected-thunk)))
294 (actual (multiple-value-list (funcall code-thunk)))
295 (passed (test-passed-p type expected actual test)))
296 (incf *test-count*)
297 (when passed
298 (incf *pass-count*))
299 (record-result passed type form expected actual extras)
300 passed))
302 (defun record-result (passed type form expected actual extras)
303 (funcall (or *test-listener* 'default-listener)
304 passed type *test-name* form expected actual
305 (and extras (funcall extras))
306 *test-count* *pass-count*))
308 (defun default-listener
309 (passed type name form expected actual extras test-count pass-count)
310 (declare (ignore test-count pass-count))
311 (unless passed
312 (show-failure type (get-failure-message type)
313 name form expected actual extras)))
315 (defun test-passed-p (type expected actual test)
316 (ecase type
317 (:error
318 (or (eql (car actual) (car expected))
319 (typep (car actual) (car expected))))
320 (:equal
321 (and (<= (length expected) (length actual))
322 (every test expected actual)))
323 (:macro
324 (equal (car actual) (car expected)))
325 (:output
326 (string= (string-trim '(#\newline #\return #\space)
327 (car actual))
328 (car expected)))
329 (:result
330 (logically-equal (car actual) (car expected)))))
332 ;;; RUN-TESTS support
334 (defun run-test-thunks (test-thunks)
335 (unless (null test-thunks)
336 (let ((total-test-count 0)
337 (total-pass-count 0)
338 (total-error-count 0))
339 (dolist (test-thunk test-thunks)
340 (multiple-value-bind (test-count pass-count error-count)
341 (run-test-thunk (car test-thunk) (cadr test-thunk))
342 (incf total-test-count test-count)
343 (incf total-pass-count pass-count)
344 (incf total-error-count error-count)))
345 (unless (null (cdr test-thunks))
346 (show-summary 'total total-test-count total-pass-count total-error-count))
347 (values))))
349 (defun run-test-thunk (*test-name* thunk)
350 (if (null thunk)
351 (format t "~& Test ~S not found" *test-name*)
352 (prog ((*test-count* 0)
353 (*pass-count* 0)
354 (error-count 0))
355 (handler-bind
356 ((error (lambda (e)
357 (let ((*print-escape* nil))
358 (setq error-count 1)
359 (format t "~& ~S: ~W" *test-name* e))
360 (if (use-debugger-p e) e (go exit)))))
361 (funcall thunk)
362 (show-summary *test-name* *test-count* *pass-count*))
363 exit
364 (return (values *test-count* *pass-count* error-count)))))
366 (defun use-debugger-p (e)
367 (and *use-debugger*
368 (or (not (eql *use-debugger* :ask))
369 (y-or-n-p "~A -- debug?" e))))
371 ;;; OUTPUT support
373 (defun get-failure-message (type)
374 (case type
375 (:error "~&~@[Should have signalled ~{~S~^; ~} but saw~] ~{~S~^; ~}")
376 (:macro "~&Should have expanded to ~{~S~^; ~} ~<~%~:;but saw ~{~S~^; ~}~>")
377 (:output "~&Should have printed ~{~S~^; ~} ~<~%~:;but saw ~{~S~^; ~}~>")
378 (t "~&Expected ~{~S~^; ~} ~<~%~:;but saw ~{~S~^; ~}~>")))
380 (defun show-failure (type msg name form expected actual extras)
381 (format t "~&~@[~S: ~]~S failed: " name form)
382 (format t msg expected actual)
383 (format t "~{~& ~S => ~S~}~%" extras)
384 type)
386 (defun show-summary (name test-count pass-count &optional error-count)
387 (format t "~&~A: ~S assertions passed, ~S failed~@[, ~S execution errors~]."
388 name pass-count (- test-count pass-count) error-count))
390 (defun collect-form-values (form values)
391 (mapcan (lambda (form-arg value)
392 (if (constantp form-arg)
394 (list form-arg value)))
395 (cdr form)
396 values))
398 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
399 ;;; Useful equality predicates for tests
400 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
402 ;;; (LOGICALLY-EQUAL x y) => true or false
403 ;;; Return true if x and y both false or both true
404 (defun logically-equal (x y)
405 (eql (not x) (not y)))
407 ;;; (SET-EQUAL l1 l2 :test) => true or false
408 ;;; Return true if every element of l1 is an element of l2
409 ;;; and vice versa.
410 (defun set-equal (l1 l2 &key (test #'equal))
411 (and (listp l1)
412 (listp l2)
413 (subsetp l1 l2 :test test)
414 (subsetp l2 l1 :test test)))
416 (pushnew :lisp-unit common-lisp:*features*)