1.0.37.40: More informative UNEXPECTED-FAILURE reporting...
[sbcl.git] / tests / test-util.lisp
1 (defpackage :test-util
2   (:use :cl :sb-ext)
3   (:export #:with-test #:report-test-status #:*failures*
4            #:really-invoke-debugger
5            #:*break-on-failure* #:*break-on-expected-failure*))
6
7 (in-package :test-util)
8
9 (defvar *test-count* 0)
10 (defvar *test-file* nil)
11 (defvar *failures* nil)
12 (defvar *break-on-failure* nil)
13 (defvar *break-on-expected-failure* nil)
14
15 (defun log-msg (&rest args)
16   (format *trace-output* "~&::: ")
17   (apply #'format *trace-output* args)
18   (terpri *trace-output*)
19   (force-output *trace-output*))
20
21 (defmacro with-test ((&key fails-on name) &body body)
22   (let ((block-name (gensym)))
23     `(block ,block-name
24        (handler-bind ((error (lambda (error)
25                                (if (expected-failure-p ,fails-on)
26                                    (fail-test :expected-failure ',name error)
27                                    (fail-test :unexpected-failure ',name error))
28                                (return-from ,block-name))))
29          (progn
30            (log-msg "Running ~S" ',name)
31            (start-test)
32            ,@body
33            (if (expected-failure-p ,fails-on)
34                (fail-test :unexpected-success ',name nil)
35                (log-msg "Success ~S" ',name)))))))
36
37 (defun report-test-status ()
38   (with-standard-io-syntax
39       (with-open-file (stream "test-status.lisp-expr"
40                               :direction :output
41                               :if-exists :supersede)
42         (format stream "~s~%" *failures*))))
43
44 (defun start-test ()
45   (unless (eq *test-file* *load-pathname*)
46     (setf *test-file* *load-pathname*)
47     (setf *test-count* 0))
48   (incf *test-count*))
49
50 (defun fail-test (type test-name condition)
51   (log-msg "~@<~A ~S ~:_due to ~S: ~4I~:_\"~A\"~:>"
52            type test-name condition condition)
53   (push (list type *test-file* (or test-name *test-count*))
54         *failures*)
55   (when (or (and *break-on-failure*
56                  (not (eq type :expected-failure)))
57             *break-on-expected-failure*)
58     (really-invoke-debugger condition)))
59
60 (defun expected-failure-p (fails-on)
61   (sb-impl::featurep fails-on))
62
63 (defun really-invoke-debugger (condition)
64   (with-simple-restart (continue "Continue")
65     (let ((*invoke-debugger-hook* *invoke-debugger-hook*))
66       (enable-debugger)
67       (invoke-debugger condition))))