2 ;; A test harness for Allegro CL.
4 ;; copyright (c) 1985-1986 Franz Inc, Alameda, CA
5 ;; copyright (c) 1986-2002 Franz Inc, Berkeley, CA - All rights reserved.
7 ;; This code is free software; you can redistribute it and/or
8 ;; modify it under the terms of the version 2.1 of
9 ;; the GNU Lesser General Public License as published by
10 ;; the Free Software Foundation, as clarified by the Franz
11 ;; preamble to the LGPL found in
12 ;; http://opensource.franz.com/preamble.html.
14 ;; This code is distributed in the hope that it will be useful,
15 ;; but without any warranty; without even the implied warranty of
16 ;; merchantability or fitness for a particular purpose. See the GNU
17 ;; Lesser General Public License for more details.
19 ;; Version 2.1 of the GNU Lesser General Public License can be
20 ;; found at http://opensource.franz.com/license.html.
21 ;; If it is not present, you can access it from
22 ;; http://www.gnu.org/copyleft/lesser.txt (until superseded by a newer
23 ;; version) or write to the Free Software Foundation, Inc., 59 Temple
24 ;; Place, Suite 330, Boston, MA 02111-1307 USA
26 ;;;; from the original ACL 6.1 sources:
27 ;; Id: tester.cl,v 2.2.12.1 2001/06/05 18:45:10 layer Exp
29 ;; $Id: tester.cl,v 1.1 2002/09/20 07:34:06 kevin Exp $
31 (defpackage :util.test
32 (:use :common-lisp :excl)
35 ;;;; Control variables:
36 #:*break-on-test-failures*
37 #:*error-protect-tests*
40 #:*test-unexpected-failures*
52 (in-package :util.test)
54 ;; Added by Kevin Rosenberg
56 (define-condition simple-break (error simple-condition) ())
59 (unless (find-class 'break nil)
60 (define-condition break (simple-condition) ()))
62 ;; the if* macro used in Allegro:
64 ;; This is in the public domain... please feel free to put this definition
65 ;; in your code or distribute it with your version of lisp.
67 (defvar if*-keyword-list '("then" "thenret" "else" "elseif"))
69 (defmacro if* (&rest args)
70 (do ((xx (reverse args) (cdr xx))
77 (cond ((eq state :compl)
79 (t (error "if*: illegal form ~s" args))))
80 (cond ((and (symbolp (car xx))
81 (member (symbol-name (car xx))
83 :test #'string-equal))
84 (setq lookat (symbol-name (car xx)))))
86 (cond ((eq state :init)
87 (cond (lookat (cond ((string-equal lookat "thenret")
91 "if*: bad keyword ~a" lookat))))
94 (push (car xx) col))))
97 (cond ((string-equal lookat "else")
100 "if*: multiples elses")))
103 (push `(t ,@col) totalcol))
104 ((string-equal lookat "then")
106 (t (error "if*: bad keyword ~s"
108 (t (push (car xx) col))))
112 "if*: keyword ~s at the wrong place " (car xx)))
113 (t (setq state :compl)
114 (push `(,(car xx) ,@col) totalcol))))
116 (cond ((not (string-equal lookat "elseif"))
117 (error "if*: missing elseif clause ")))
118 (setq state :init)))))
121 (defvar *break-on-test-failures* nil
122 "When a test failure occurs, common-lisp:break is called, allowing
123 interactive debugging of the failure.")
125 (defvar *test-errors* 0
126 "The value is the number of test errors which have occurred.")
127 (defvar *test-successes* 0
128 "The value is the number of test successes which have occurred.")
129 (defvar *test-unexpected-failures* 0
130 "The value is the number of unexpected test failures which have occurred.")
132 (defvar *error-protect-tests* nil
133 "Protect each test from errors. If an error occurs, then that will be
134 taken as a test failure unless test-error is being used.")
136 (defmacro test-values-errorset (form &optional announce catch-breaks)
138 (let ((g-announce (gensym))
139 (g-catch-breaks (gensym)))
140 `(let* ((,g-announce ,announce)
141 (,g-catch-breaks ,catch-breaks))
142 (handler-case (cons t (multiple-value-list ,form))
143 (condition (condition)
144 (if* (and (null ,g-catch-breaks)
145 (typep condition 'simple-break))
146 then (break condition)
148 then (format *error-output* "~&Condition type: ~a~%"
149 (class-of condition))
150 (format *error-output* "~&Message: ~a~%" condition))
153 (defmacro test-values (form &optional announce catch-breaks)
155 (if* *error-protect-tests*
156 then `(test-values-errorset ,form ,announce ,catch-breaks)
157 else `(cons t (multiple-value-list ,form))))
159 (defmacro test (expected-value test-form
160 &key (test #'eql test-given)
161 (multiple-values nil multiple-values-given)
162 (fail-info nil fail-info-given)
163 (known-failure nil known-failure-given)
165 ;;;;;;;;;; internal, undocumented keywords:
166 ;;;; Note about these keywords: if they were documented, we'd have a
167 ;;;; problem, since they break the left-to-right order of evaluation.
168 ;;;; Specifically, errorset breaks it, and I don't see any way around
169 ;;;; that. `errorset' is used by the old test.cl module (eg,
170 ;;;; test-equal-errorset).
173 (wanted-message nil wanted-message-given)
174 (got-message nil got-message-given))
175 "Perform a single test. `expected-value' is the reference value for the
176 test. `test-form' is a form that will produce the value to be compared to
177 the expected-value. If the values are not the same, then an error is
178 logged, otherwise a success is logged.
180 Normally the comparison of values is done with `eql'. The `test' keyword
181 argument can be used to specify other comparison functions, such as eq,
182 equal,equalp, string=, string-equal, etc.
184 Normally, only the first return value from the test-form is considered,
185 however if `multiple-values' is t, then all values returned from test-form
188 `fail-info' allows more information to be printed with a test failure.
190 `known-failure' marks the test as a known failure. This allows for
191 programs that do regression analysis on the output from a test run to
192 discriminate on new versus known failures."
194 :expected-result ,expected-value
196 (,(if errorset 'test-values-errorset 'test-values) ,test-form t)
197 ,@(when test-given `(:predicate ,test))
198 ,@(when multiple-values-given `(:multiple-values ,multiple-values))
199 ,@(when fail-info-given `(:fail-info ,fail-info))
200 ,@(when known-failure-given `(:known-failure ,known-failure))
201 :test-form ',(if reported-form reported-form test-form)
202 ,@(when wanted-message-given `(:wanted-message ,wanted-message))
203 ,@(when got-message-given `(:got-message ,got-message))))
205 (defmethod conditionp ((thing condition)) t)
206 (defmethod conditionp ((thing t)) nil)
208 (defmacro test-error (form &key announce
210 (fail-info nil fail-info-given)
211 (known-failure nil known-failure-given)
212 (condition-type ''simple-error)
213 (include-subtypes nil include-subtypes-given)
214 (format-control nil format-control-given)
215 (format-arguments nil format-arguments-given))
216 "Test that `form' signals an error. The order of evaluation of the
217 arguments is keywords first, then test form.
219 If `announce' is non-nil, then cause the error message to be printed.
221 The `catch-breaks' is non-nil then consider a call to common-lisp:break an
224 `fail-info' allows more information to be printed with a test failure.
226 `known-failure' marks the test as a known failure. This allows for
227 programs that do regression analysis on the output from a test run to
228 discriminate on new versus known failures.
230 If `condition-type' is non-nil, it should be a symbol naming a condition
231 type, which is used to check against the signalled condition type. The
232 test will fail if they do not match.
234 `include-subtypes', used with `condition-type', can be used to match a
235 condition to an entire subclass of the condition type hierarchy.
237 `format-control' and `format-arguments' can be used to check the error
239 (let ((g-announce (gensym))
240 (g-catch-breaks (gensym))
241 (g-fail-info (gensym))
242 (g-known-failure (gensym))
243 (g-condition-type (gensym))
244 (g-include-subtypes (gensym))
245 (g-format-control (gensym))
246 (g-format-arguments (gensym))
248 `(let* ((,g-announce ,announce)
249 (,g-catch-breaks ,catch-breaks)
250 ,@(when fail-info-given `((,g-fail-info ,fail-info)))
251 ,@(when known-failure-given `((,g-known-failure ,known-failure)))
252 (,g-condition-type ,condition-type)
253 ,@(when include-subtypes-given
254 `((,g-include-subtypes ,include-subtypes)))
255 ,@(when format-control-given
256 `((,g-format-control ,format-control)))
257 ,@(when format-arguments-given
258 `((,g-format-arguments ,format-arguments)))
259 (,g-c (test-values-errorset ,form ,g-announce ,g-catch-breaks)))
264 (test-values (and (conditionp ,g-c)
265 ,@(if* include-subtypes-given
266 then `((if* ,g-include-subtypes
267 then (typep ,g-c ,g-condition-type)
268 else (eq (class-of ,g-c)
270 ,g-condition-type))))
271 else `((eq (class-of ,g-c)
272 (find-class ,g-condition-type))))
273 ,@(when format-control-given
275 (null ,g-format-control)
277 (concatenate 'simple-string
278 "~1@<" ,g-format-control "~:@>")
279 (simple-condition-format-control ,g-c)))))
280 ,@(when format-arguments-given
282 (null ,g-format-arguments)
285 (simple-condition-format-arguments ,g-c))))))
288 ,@(when fail-info-given `(:fail-info ,g-fail-info))
289 ,@(when known-failure-given `(:known-failure ,g-known-failure))
290 :condition-type ,g-condition-type
292 ,@(when include-subtypes-given
293 `(:include-subtypes ,g-include-subtypes))
294 ,@(when format-control-given
295 `(:format-control ,g-format-control))
296 ,@(when format-arguments-given
297 `(:format-arguments ,g-format-arguments))))))
299 (defmacro test-no-error (form &key announce
301 (fail-info nil fail-info-given)
302 (known-failure nil known-failure-given))
303 "Test that `form' does not signal an error. The order of evaluation of
304 the arguments is keywords first, then test form.
306 If `announce' is non-nil, then cause the error message to be printed.
308 The `catch-breaks' is non-nil then consider a call to common-lisp:break an
311 `fail-info' allows more information to be printed with a test failure.
313 `known-failure' marks the test as a known failure. This allows for
314 programs that do regression analysis on the output from a test run to
315 discriminate on new versus known failures."
316 (let ((g-announce (gensym))
317 (g-catch-breaks (gensym))
318 (g-fail-info (gensym))
319 (g-known-failure (gensym))
321 `(let* ((,g-announce ,announce)
322 (,g-catch-breaks ,catch-breaks)
323 ,@(when fail-info-given `((,g-fail-info ,fail-info)))
324 ,@(when known-failure-given `((,g-known-failure ,known-failure)))
325 (,g-c (test-values-errorset ,form ,g-announce ,g-catch-breaks)))
329 :test-results (test-values (not (conditionp ,g-c)))
332 ,@(when fail-info-given `(:fail-info ,g-fail-info))
333 ,@(when known-failure-given `(:known-failure ,g-known-failure))))))
335 (defvar *warn-cookie* (cons nil nil))
337 (defmacro test-warning (form &key fail-info known-failure)
338 "Test that `form' signals a warning. The order of evaluation of
339 the arguments is keywords first, then test form.
341 `fail-info' allows more information to be printed with a test failure.
343 `known-failure' marks the test as a known failure. This allows for
344 programs that do regression analysis on the output from a test run to
345 discriminate on new versus known failures."
346 (let ((g-fail-info (gensym))
347 (g-known-failure (gensym))
349 `(let* ((,g-fail-info ,fail-info)
350 (,g-known-failure ,known-failure)
351 (,g-value (test-values-errorset ,form nil t)))
354 (if* (or (typep ,g-value 'simple-warning) (typep ,g-value 'warning))
356 else ;; test produced no warning
359 :reported-form ,form ;; quoted by test macro
360 :wanted-message "a warning"
361 :got-message "no warning"
362 :fail-info ,g-fail-info
363 :known-failure ,g-known-failure))))
365 (defmacro test-no-warning (form &key fail-info known-failure)
366 "Test that `form' does not signal a warning. The order of evaluation of
367 the arguments is keywords first, then test form.
369 `fail-info' allows more information to be printed with a test failure.
371 `known-failure' marks the test as a known failure. This allows for
372 programs that do regression analysis on the output from a test run to
373 discriminate on new versus known failures."
374 (let ((g-fail-info (gensym))
375 (g-known-failure (gensym))
377 `(let* ((,g-fail-info ,fail-info)
378 (,g-known-failure ,known-failure)
379 (,g-value (test-values-errorset ,form nil t)))
382 (if* (or (typep ,g-value 'simple-warning) (typep ,g-value 'warning))
383 then nil ;; test produced warning
386 :reported-form ',form
387 :wanted-message "no warning"
388 :got-message "a warning"
389 :fail-info ,g-fail-info
390 :known-failure ,g-known-failure))))
392 (defvar *announce-test* nil) ;; if true announce each test that was done
394 (defun test-check (&key (predicate #'eql)
395 expected-result test-results test-form
396 multiple-values fail-info known-failure
397 wanted-message got-message condition-type condition
398 include-subtypes format-control format-arguments
399 &aux fail predicate-failed got wanted)
400 ;; for debugging large/complex test sets:
401 (when *announce-test*
402 (format t "Just did test ~s~%" test-form)
405 ;; this is an internal function
406 (flet ((check (expected-result result)
409 (errorset (funcall predicate expected-result result) t)))
410 (failed (null (car results))))
412 then (setq predicate-failed t)
414 else (cadr results)))))
415 (when (conditionp test-results)
416 (setq condition test-results)
417 (setq test-results nil))
418 (when (null (car test-results))
420 (if* (and (not fail) (not multiple-values))
421 then ;; should be a single result
422 ;; expected-result is the single result wanted
423 (when (not (and (cdr test-results)
424 (check expected-result (cadr test-results))))
426 (when (and (not fail) (cddr test-results))
427 (setq fail 'single-got-multiple))
428 else ;; multiple results wanted
429 ;; expected-result is a list of results, each of which
430 ;; should be checked against the corresponding test-results
431 ;; using the predicate
432 (do ((got (cdr test-results) (cdr got))
433 (want expected-result (cdr want)))
434 ((or (null got) (null want))
435 (when (not (and (null want) (null got)))
437 (when (not (check (car got) (car want)))
438 (return (setq fail t)))))
440 then (when (not known-failure)
441 (format *error-output*
442 "~& * * * UNEXPECTED TEST FAILURE * * *~%")
443 (incf *test-unexpected-failures*))
444 (format *error-output* "~&Test failed: ~@[known failure: ~*~]~s~%"
445 known-failure test-form)
446 (if* (eq 'single-got-multiple fail)
450 Reason: additional value were returned from test form.~%")
451 elseif predicate-failed
452 then (format *error-output* "Reason: predicate error.~%")
453 elseif (null (car test-results))
454 then (format *error-output* "~
455 Reason: an error~@[ (of type `~s')~] was detected.~%"
456 (when condition (class-of condition)))
458 then (if* (not (conditionp condition))
459 then (format *error-output* "~
460 Reason: expected but did not detect an error of type `~s'.~%"
462 elseif (null condition-type)
463 then (format *error-output* "~
464 Reason: detected an unexpected error of type `~s':
468 elseif (not (if* include-subtypes
469 then (typep condition condition-type)
470 else (eq (class-of condition)
471 (find-class condition-type))))
472 then (format *error-output* "~
473 Reason: detected an incorrect condition type.~%")
474 (format *error-output*
475 " wanted: ~s~%" condition-type)
476 (format *error-output*
477 " got: ~s~%" (class-of condition))
478 elseif (and format-control
481 (concatenate 'simple-string
482 "~1@<" format-control "~:@>"))
484 (simple-condition-format-control
486 then ;; format control doesn't match
487 (format *error-output* "~
488 Reason: the format-control was incorrect.~%")
489 (format *error-output* " wanted: ~s~%" wanted)
490 (format *error-output* " got: ~s~%" got)
491 elseif (and format-arguments
493 (setq got format-arguments)
495 (simple-condition-format-arguments
497 then (format *error-output* "~
498 Reason: the format-arguments were incorrect.~%")
499 (format *error-output* " wanted: ~s~%" wanted)
500 (format *error-output* " got: ~s~%" got)
501 else ;; what else????
502 (error "internal-error"))
503 else (let ((*print-length* 50)
506 then (format *error-output*
507 " wanted: ~a~%" wanted-message)
508 else (if* (not multiple-values)
509 then (format *error-output*
514 " wanted values: ~{~s~^, ~}~%"
517 then (format *error-output*
518 " got: ~a~%" got-message)
519 else (if* (not multiple-values)
520 then (format *error-output* " got: ~s~%"
521 (second test-results))
524 " got values: ~{~s~^, ~}~%"
525 (cdr test-results))))))
527 (format *error-output* "Additional info: ~a~%" fail-info))
529 (when *break-on-test-failures*
530 (break "~a is non-nil." '*break-on-test-failures*))
531 else (when known-failure
532 (format *error-output*
533 "~&Expected test failure for ~s did not occur.~%"
536 (format *error-output* "Additional info: ~a~%" fail-info))
538 (incf *test-successes*))
541 (defmacro with-tests ((&key (name "unnamed")) &body body)
542 (let ((g-name (gensym)))
543 `(flet ((doit () ,@body))
544 (let ((,g-name ,name)
547 (*test-unexpected-failures* 0))
548 (format *error-output* "Begin ~a test~%" ,g-name)
549 (if* *break-on-test-failures*
551 else (handler-case (doit)
556 ~&Test ~a aborted by signalling an uncaught error:~%~a~%"
558 (let ((state (sys:gsgc-switch :print)))
559 (setf (sys:gsgc-switch :print) nil)
560 (format t "~&**********************************~%" ,g-name)
561 (format t "End ~a test~%" ,g-name)
562 (format t "Errors detected in this test: ~s " *test-errors*)
563 (unless (zerop *test-unexpected-failures*)
564 (format t "UNEXPECTED: ~s" *test-unexpected-failures*))
565 (format t "~%Successes this test:~s~%" *test-successes*)
566 (setf (sys:gsgc-switch :print) state))))))
568 (provide :tester #+module-versions 1.1)