The default value of *debug-on-error* should be NIL, not T
[fiveam.git] / src / run.lisp
1 ;; -*- lisp -*-
2
3 (in-package :it.bese.FiveAM)
4
5 ;;;; * Running Tests
6
7 ;;;; Once the programmer has defined what the tests are these need to
8 ;;;; be run and the expected effects should be compared with the
9 ;;;; actual effects. FiveAM provides the function RUN for this
10 ;;;; purpose, RUN executes a number of tests and collects the results
11 ;;;; of each individual check into a list which is then
12 ;;;; returned. There are three types of test results: passed, failed
13 ;;;; and skipped, these are represented by TEST-RESULT objects.
14
15 ;;;; Generally running a test will return normally, but there are two
16 ;;;; exceptional situations which can occur:
17
18 ;;;; - An exception is signaled while running the test. If the
19 ;;;;   variable *debug-on-error* is T than FiveAM will enter the
20 ;;;;   debugger, otherwise a test failure (of type
21 ;;;;   unexpected-test-failure) is returned. When entering the
22 ;;;;   debugger two restarts are made available, one simply reruns the
23 ;;;;   current test and another signals a test-failure and continues
24 ;;;;   with the remaining tests.
25
26 ;;;; - A circular dependency is detected. An error is signaled and a
27 ;;;;   restart is made available which signals a test-skipped and
28 ;;;;   continues with the remaining tests. This restart also sets the
29 ;;;;   dependency status of the test to nil, so any tests which depend
30 ;;;;   on this one (even if the dependency is not circular) will be
31 ;;;;   skipped.
32
33 ;;;; The functions RUN!, !, !! and !!! are convenient wrappers around
34 ;;;; RUN and EXPLAIN.
35
36 (defparameter *debug-on-error* nil
37   "T if we should drop into a debugger on error, NIL otherwise.")
38
39 (defun import-testing-symbols (package-designator)
40   (import '(5am::is 5am::is-true 5am::is-false 5am::signals 5am::finishes)
41           package-designator))
42
43 (defparameter *run-queue* '()
44   "List of test waiting to be run.")
45
46 (define-condition circular-dependency (error)
47   ((test-case :initarg :test-case))
48   (:report (lambda (cd stream)
49              (format stream "A circular dependency wes detected in ~S." (slot-value cd 'test-case))))
50   (:documentation "Condition signaled when a circular dependency
51 between test-cases has been detected."))
52
53 (defgeneric run-resolving-dependencies (test)
54   (:documentation "Given a dependency spec determine if the spec
55 is satisfied or not, this will generally involve running other
56 tests. If the dependency spec can be satisfied the test is alos
57 run."))
58
59 (defmethod run-resolving-dependencies ((test test-case))
60   "Return true if this test, and its dependencies, are satisfied,
61   NIL otherwise."
62   (case (status test)
63     (:unknown
64      (setf (status test) :resolving)
65      (if (or (not (depends-on test))
66              (resolve-dependencies (depends-on test)))
67          (progn
68            (run-test-lambda test)
69            (status test))
70          (with-run-state (result-list)
71            (unless (eql :circular (status test))
72              (push (make-instance 'test-skipped
73                                   :test-case test
74                                   :reason "Dependencies not satisfied")
75                    result-list)
76              (setf (status test) :depends-not-satisfied)))))         
77     (:resolving
78      (restart-case
79          (error 'circular-dependency :test-case test)
80        (skip ()
81          :report (lambda (s)
82                    (format s "Skip the test ~S and all its dependencies." (name test)))
83          (with-run-state (result-list)
84            (push (make-instance 'test-skipped :reason "Circular dependencies" :test-case test)
85                  result-list))
86          (setf (status test) :circular))))
87     (t (status test))))
88
89 (defmethod resolve-dependencies ((depends-on symbol))
90   "A test which depends on a symbol is interpreted as `(AND
91   ,DEPENDS-ON)."
92   (run-resolving-dependencies (get-test depends-on)))
93
94 (defmethod resolve-dependencies ((depends-on list))
95   "Return true if the dependency spec DEPENDS-ON is satisfied,
96   nil otherwise."
97   (if (null depends-on)
98       t
99       (flet ((satisfies-depends-p (test)
100                (funcall test (lambda (dep)
101                                (eql t (resolve-dependencies dep)))
102                              (cdr depends-on))))
103         (ecase (car depends-on)
104           (and (satisfies-depends-p #'every))
105           (or  (satisfies-depends-p #'some))
106           (not (satisfies-depends-p #'notany))))))
107     
108 (defun handle-unexpected-error (test error)
109   "Handler for unexpected conditions raised during test
110   execution."
111   (when (not *debug-on-error*)
112     (format *test-dribble* "F")
113     (with-run-state (result-list)
114       (push (make-instance 'unexpected-test-failure
115                            :test-case test
116                            :reason (format nil "Unexpected Error: ~S." error)
117                            :condition test)
118             result-list)
119       (throw 'run-block result-list))))
120
121 (defun results-status (result-list)
122   "Given a list of test results (generated while running a test)
123   return true if all of the results are of type TEST-PASSED,
124   faile otherwise."
125   (every (lambda (res)
126            (typep res 'test-passed))
127          result-list))
128
129 (defun return-result-list (test-lambda)
130   "Run the test function TEST-LAMBDA and return a list of all
131   test results generated, does not modify the special environment
132   variable RESULT-LIST."
133   (bind-run-state ((result-list '())) 
134     (funcall test-lambda)
135     result-list))
136
137 (defmethod run-test-lambda ((test test-case))
138   (with-run-state (result-list)
139     (catch 'run-block
140       (bind-run-state ((current-test test))
141         (handler-bind ((error (lambda (e) (handle-unexpected-error test e))))
142           (restart-case
143               (let ((results (return-result-list (test-lambda test))))
144                 (setf (status test) (results-status results)
145                       result-list (nconc result-list results)))
146             (retest ()
147               :report (lambda (stream)
148                         (format stream "~@<Rerun the test ~S~@:>" test))
149               (%run test))
150             (ignore ()
151               :report (lambda (stream)
152                         (format stream "~@<Signal a test failure and abort the test ~S.~@:>" test))
153               (push (make-instance 'test-failure :test-case test
154                                    :reason "Failure restart.")
155                     result-list))))))))
156
157 (defgeneric %run (test-spec)
158   (:documentation "Internal method for running a test. Does not
159   update the status of the tests nor the special vairables !,
160   !!, !!!"))
161
162 (defmethod %run ((test test-case))
163   (run-resolving-dependencies test))
164
165 (defmethod %run ((suite test-suite))
166   (let ((suite-results '()))
167     (bind-run-state ((result-list '()))
168       (loop for test being the hash-values of (tests suite)
169             do (%run test)
170             finally (setf suite-results result-list)))
171     (setf (status suite) (every (lambda (res)
172                                   (typep res 'test-passed))
173                                   suite-results))
174     (with-run-state (result-list)
175       (setf result-list (nconc result-list suite-results)))))
176
177 (defmethod %run ((test-name symbol))
178   (when-bind test (get-test test-name)
179     (%run test)))
180
181 (defvar *initial-!* (lambda () (format t "Haven't run that many tests yet.~%")))
182
183 (defvar *!* *initial-!*)
184 (defvar *!!* *initial-!*)
185 (defvar *!!!* *initial-!*)
186
187 ;;;; ** Public entry points
188
189 (defun run! (test-spec)
190   "Equivalent to (explain (run TEST-SPEC))."
191   (explain! (run test-spec)))
192
193 (defun explain! (result-list)
194   "Explain the results of RESULT-LIST using a
195 detailed-text-explainer with output going to *test-dribble*"
196   (explain (make-instance 'detailed-text-explainer) result-list *test-dribble*))
197
198 (defun run (test-spec)
199     "Run the test specified by TEST-SPEC.
200
201 TEST-SPEC can be either a symbol naming a test or test suite, or
202 a testable-object object. This function changes the operations
203 performed by the !, !! and !!! functions."
204     (psetf *!* (lambda ()
205                  (loop for test being the hash-keys of *test*
206                     do (setf (status (get-test test)) :unknown))
207                  (bind-run-state ((result-list '()))
208                    (%run test-spec)
209                    result-list))
210            *!!* *!*
211            *!!!* *!!*)
212     (funcall *!*))
213
214 (defun ! () 
215   "Rerun the most recently run test and explain the results."
216   (explain! (funcall *!*)))
217
218 (defun !! () 
219   "Rerun the second most recently run test and explain the results."
220   (explain! (funcall *!!*)))
221   
222 (defun !!! ()
223   "Rerun the third most recently run test and explain the results."
224   (explain! (funcall *!!!*)))
225
226 ;; Copyright (c) 2002-2003, Edward Marco Baringer
227 ;; All rights reserved. 
228 ;; 
229 ;; Redistribution and use in source and binary forms, with or without
230 ;; modification, are permitted provided that the following conditions are
231 ;; met:
232 ;; 
233 ;;  - Redistributions of source code must retain the above copyright
234 ;;    notice, this list of conditions and the following disclaimer.
235 ;; 
236 ;;  - Redistributions in binary form must reproduce the above copyright
237 ;;    notice, this list of conditions and the following disclaimer in the
238 ;;    documentation and/or other materials provided with the distribution.
239 ;;
240 ;;  - Neither the name of Edward Marco Baringer, nor BESE, nor the names
241 ;;    of its contributors may be used to endorse or promote products
242 ;;    derived from this software without specific prior written permission.
243 ;; 
244 ;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
245 ;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
246 ;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
247 ;; A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
248 ;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
249 ;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
250 ;; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
251 ;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
252 ;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
253 ;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
254 ;; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.