File: test-util.lisp

package info (click to toggle)
sbcl 1%3A0.9.16.0-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 19,960 kB
  • ctags: 16,537
  • sloc: lisp: 231,164; ansic: 19,558; asm: 2,539; sh: 1,925; makefile: 308
file content (57 lines) | stat: -rw-r--r-- 1,966 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
(defpackage :test-util
  (:use :cl :sb-ext)
  (:export #:with-test #:report-test-status #:*failures*
           #:really-invoke-debugger
           #:*break-on-failure* #:*break-on-expected-failure*))

(in-package :test-util)

(defvar *test-count* 0)
(defvar *test-file* nil)
(defvar *failures* nil)
(defvar *break-on-failure* nil)
(defvar *break-on-expected-failure* nil)

(defmacro with-test ((&key fails-on name) &body body)
  (let ((block-name (gensym)))
    `(block ,block-name
       (handler-bind ((error (lambda (error)
                               (if (expected-failure-p ,fails-on)
                                   (fail-test :expected-failure ',name error)
                                   (fail-test :unexpected-failure ',name error))
                               (return-from ,block-name))))
         (progn
           (start-test)
           ,@body
           (when (expected-failure-p ,fails-on)
             (fail-test :unexpected-success ',name nil)))))))

(defun report-test-status ()
  (with-standard-io-syntax
      (with-open-file (stream "test-status.lisp-expr"
                              :direction :output
                              :if-exists :supersede)
        (format stream "~s~%" *failures*))))

(defun start-test ()
  (unless (eq *test-file* *load-pathname*)
    (setf *test-file* *load-pathname*)
    (setf *test-count* 0))
  (incf *test-count*))

(defun fail-test (type test-name condition)
  (push (list type *test-file* (or test-name *test-count*))
        *failures*)
  (when (or (and *break-on-failure*
                 (not (eq type :expected-failure)))
            *break-on-expected-failure*)
    (really-invoke-debugger condition)))

(defun expected-failure-p (fails-on)
  (sb-impl::featurep fails-on))

(defun really-invoke-debugger (condition)
  (with-simple-restart (continue "Continue")
    (let ((*invoke-debugger-hook* *invoke-debugger-hook*))
      (enable-debugger)
      (invoke-debugger condition))))