1.0.25.20: test util: print names, status
[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" type test-name)
52   (push (list type *test-file* (or test-name *test-count*))
53         *failures*)
54   (when (or (and *break-on-failure*
55                  (not (eq type :expected-failure)))
56             *break-on-expected-failure*)
57     (really-invoke-debugger condition)))
58
59 (defun expected-failure-p (fails-on)
60   (sb-impl::featurep fails-on))
61
62 (defun really-invoke-debugger (condition)
63   (with-simple-restart (continue "Continue")
64     (let ((*invoke-debugger-hook* *invoke-debugger-hook*))
65       (enable-debugger)
66       (invoke-debugger condition))))