Use lowercase-only characters for package names to fix compilation on Allegro
[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 (defparameter *debug-on-failure* nil
40   "T if we should drop into a debugger on a failing check, NIL otherwise.")
41
42 (defun import-testing-symbols (package-designator)
43   (import '(5am::is 5am::is-true 5am::is-false 5am::signals 5am::finishes)
44           package-designator))
45
46 (defparameter *run-queue* '()
47   "List of test waiting to be run.")
48
49 (define-condition circular-dependency (error)
50   ((test-case :initarg :test-case))
51   (:report (lambda (cd stream)
52              (format stream "A circular dependency wes detected in ~S." (slot-value cd 'test-case))))
53   (:documentation "Condition signaled when a circular dependency
54 between test-cases has been detected."))
55
56 (defgeneric run-resolving-dependencies (test)
57   (:documentation "Given a dependency spec determine if the spec
58 is satisfied or not, this will generally involve running other
59 tests. If the dependency spec can be satisfied the test is alos
60 run."))
61
62 (defmethod run-resolving-dependencies ((test test-case))
63   "Return true if this test, and its dependencies, are satisfied,
64   NIL otherwise."
65   (case (status test)
66     (:unknown
67      (setf (status test) :resolving)
68      (if (or (not (depends-on test))
69              (eql t (resolve-dependencies (depends-on test))))
70          (progn
71            (run-test-lambda test)
72            (status test))
73          (with-run-state (result-list)
74            (unless (eql :circular (status test))
75              (push (make-instance 'test-skipped
76                                   :test-case test
77                                   :reason "Dependencies not satisfied")
78                    result-list)
79              (setf (status test) :depends-not-satisfied)))))
80     (:resolving
81      (restart-case
82          (error 'circular-dependency :test-case test)
83        (skip ()
84          :report (lambda (s)
85                    (format s "Skip the test ~S and all its dependencies." (name test)))
86          (with-run-state (result-list)
87            (push (make-instance 'test-skipped :reason "Circular dependencies" :test-case test)
88                  result-list))
89          (setf (status test) :circular))))
90     (t (status test))))
91
92 (defmethod resolve-dependencies ((depends-on symbol))
93   "A test which depends on a symbol is interpreted as `(AND
94   ,DEPENDS-ON)."
95   (run-resolving-dependencies (get-test depends-on)))
96
97 (defmethod resolve-dependencies ((depends-on list))
98   "Return true if the dependency spec DEPENDS-ON is satisfied,
99   nil otherwise."
100   (if (null depends-on)
101       t
102       (flet ((satisfies-depends-p (test)
103                (funcall test (lambda (dep)
104                                (eql t (resolve-dependencies dep)))
105                         (cdr depends-on))))
106         (ecase (car depends-on)
107           (and (satisfies-depends-p #'every))
108           (or  (satisfies-depends-p #'some))
109           (not (satisfies-depends-p #'notany))
110           (:before (every #'(lambda (dep)
111                               (let ((status (status (get-test dep))))
112                                 (eql :unknown status)))
113                           (cdr depends-on)))))))
114
115 (defun results-status (result-list)
116   "Given a list of test results (generated while running a test)
117   return true if all of the results are of type TEST-PASSED,
118   faile otherwise."
119   (every (lambda (res)
120            (typep res 'test-passed))
121          result-list))
122
123 (defun return-result-list (test-lambda)
124   "Run the test function TEST-LAMBDA and return a list of all
125   test results generated, does not modify the special environment
126   variable RESULT-LIST."
127   (bind-run-state ((result-list '()))
128     (funcall test-lambda)
129     result-list))
130
131 (defmethod run-test-lambda ((test test-case))
132   (with-run-state (result-list)
133     (bind-run-state ((current-test test))
134       (labels ((abort-test (e)
135                  (add-result 'unexpected-test-failure
136                              :test-expr nil
137                              :test-case test
138                              :reason (format nil "Unexpected Error: ~S~%~A." e e)
139                              :condition e))
140                (run-it ()
141                  (let ((result-list '()))
142                    (declare (special result-list))
143                    (handler-bind ((check-failure (lambda (e)
144                                                    (declare (ignore e))
145                                                    (unless *debug-on-failure*
146                                                      (invoke-restart
147                                                       (find-restart 'ignore-failure)))))
148                                   (error (lambda (e)
149                                            (unless (or *debug-on-error*
150                                                        (typep e 'check-failure))
151                                              (abort-test e)
152                                              (return-from run-it result-list)))))
153                      (restart-case
154                          (let ((*readtable* (copy-readtable))
155                                (*package* (runtime-package test)))
156                            (if (collect-profiling-info test)
157                                (setf (profiling-info test)
158                                      (arnesi:collect-timing (test-lambda test)))
159                                (funcall (test-lambda test))))
160                        (retest ()
161                          :report (lambda (stream)
162                                    (format stream "~@<Rerun the test ~S~@:>" test))
163                          (return-from run-it (run-it)))
164                        (ignore ()
165                          :report (lambda (stream)
166                                    (format stream "~@<Signal an exceptional test failure and abort the test ~S.~@:>" test))
167                          (abort-test (make-instance 'test-failure :test-case test
168                                                     :reason "Failure restart."))))
169                      result-list))))
170         (let ((results (run-it)))
171           (setf (status test) (results-status results)
172                 result-list (nconc result-list results)))))))
173
174 (defgeneric %run (test-spec)
175   (:documentation "Internal method for running a test. Does not
176   update the status of the tests nor the special variables !,
177   !!, !!!"))
178
179 (defmethod %run ((test test-case))
180   (run-resolving-dependencies test))
181
182 (defmethod %run ((tests list))
183   (mapc #'%run tests))
184
185 (defmethod %run ((suite test-suite))
186   (let ((suite-results '()))
187     (flet ((run-tests ()
188              (loop
189                 for test being the hash-values of (tests suite)
190                 do (%run test))))
191       (unwind-protect
192            (bind-run-state ((result-list '()))
193              (unwind-protect
194                   (if (collect-profiling-info suite)
195                       (setf (profiling-info suite) (collect-timing #'run-tests))
196                       (run-tests)))
197              (setf suite-results result-list
198                    (status suite) (every (lambda (res)
199                                            (typep res 'test-passed))
200                                          suite-results)))
201         (with-run-state (result-list)
202           (setf result-list (nconc result-list suite-results)))))))
203
204 (defmethod %run ((test-name symbol))
205   (when-bind test (get-test test-name)
206     (%run test)))
207
208 (defvar *initial-!* (lambda () (format t "Haven't run that many tests yet.~%")))
209
210 (defvar *!* *initial-!*)
211 (defvar *!!* *initial-!*)
212 (defvar *!!!* *initial-!*)
213
214 ;;;; ** Public entry points
215
216 (defun run! (&optional (test-spec *suite*))
217   "Equivalent to (explain (run TEST-SPEC))."
218   (explain! (run test-spec)))
219
220 (defun explain! (result-list)
221   "Explain the results of RESULT-LIST using a
222 detailed-text-explainer with output going to *test-dribble*"
223   (explain (make-instance 'detailed-text-explainer) result-list *test-dribble*))
224
225 (defun debug! (&optional (test-spec *suite*))
226   "Calls (run! test-spec) but enters the debugger if any kind of error happens."
227   (let ((*debug-on-error* t)
228         (*debug-on-failure* t))
229     (run! test-spec)))
230
231 (defun run (test-spec)
232   "Run the test specified by TEST-SPEC.
233
234 TEST-SPEC can be either a symbol naming a test or test suite, or
235 a testable-object object. This function changes the operations
236 performed by the !, !! and !!! functions."
237   (psetf *!* (lambda ()
238                (loop for test being the hash-keys of *test*
239                      do (setf (status (get-test test)) :unknown))
240                (bind-run-state ((result-list '()))
241                  (with-simple-restart (explain "Ignore the rest of the tests and explain current results")
242                    (%run test-spec))
243                  result-list))
244          *!!* *!*
245          *!!!* *!!*)
246   (funcall *!*))
247
248 (defun ! ()
249   "Rerun the most recently run test and explain the results."
250   (explain! (funcall *!*)))
251
252 (defun !! ()
253   "Rerun the second most recently run test and explain the results."
254   (explain! (funcall *!!*)))
255
256 (defun !!! ()
257   "Rerun the third most recently run test and explain the results."
258   (explain! (funcall *!!!*)))
259
260 ;; Copyright (c) 2002-2003, Edward Marco Baringer
261 ;; All rights reserved.
262 ;;
263 ;; Redistribution and use in source and binary forms, with or without
264 ;; modification, are permitted provided that the following conditions are
265 ;; met:
266 ;;
267 ;;  - Redistributions of source code must retain the above copyright
268 ;;    notice, this list of conditions and the following disclaimer.
269 ;;
270 ;;  - Redistributions in binary form must reproduce the above copyright
271 ;;    notice, this list of conditions and the following disclaimer in the
272 ;;    documentation and/or other materials provided with the distribution.
273 ;;
274 ;;  - Neither the name of Edward Marco Baringer, nor BESE, nor the names
275 ;;    of its contributors may be used to endorse or promote products
276 ;;    derived from this software without specific prior written permission.
277 ;;
278 ;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
279 ;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
280 ;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
281 ;; A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
282 ;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
283 ;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
284 ;; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
285 ;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
286 ;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
287 ;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
288 ;; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.