506d7ba5c54993b401b73dbab430a69fd2603164
[fiveam.git] / src / check.lisp
1 ;; -*- lisp -*-
2
3 (in-package :it.bese.FiveAM)
4
5 ;;;; * Checks
6
7 ;;;; At the lowest level testing the system requires that certain
8 ;;;; forms be evaluated and that certain post conditions are met: the
9 ;;;; value returned must satisfy a certain predicate, the form must
10 ;;;; (or must not) signal a certain condition, etc. In FiveAM these
11 ;;;; low level operations are called 'checks' and are defined using
12 ;;;; the various checking macros.
13
14 ;;;; Checks are the basic operators for collecting results. Tests and
15 ;;;; test suites on the other hand allow grouping multiple checks into
16 ;;;; logic collections.
17
18 (defvar *test-dribble* t)
19
20 (defmacro with-*test-dribble* (stream &body body)
21   `(let ((*test-dribble* ,stream))
22      (declare (special *test-dribble*))
23      ,@body))
24
25 (eval-when (:compile-toplevel :load-toplevel :execute)
26   (def-special-environment run-state ()
27     result-list
28     current-test))
29
30 ;;;; ** Types of test results
31
32 ;;;; Every check produces a result object. 
33
34 (defclass test-result ()
35   ((reason :accessor reason :initarg :reason :initform "no reason given")
36    (test-case :accessor test-case :initarg :test-case)
37    (test-expr :accessor test-expr :initarg :test-expr))
38   (:documentation "All checking macros will generate an object of
39  type TEST-RESULT."))
40
41 (defclass test-passed (test-result)
42   ()
43   (:documentation "Class for successful checks."))
44
45 (defgeneric test-passed-p (object)
46   (:method ((o t)) nil)
47   (:method ((o test-passed)) t))
48
49 (defclass test-failure (test-result)
50   ()
51   (:documentation "Class for unsuccessful checks."))
52
53 (defgeneric test-failure-p (object)
54   (:method ((o t)) nil)
55   (:method ((o test-failure)) t))
56
57 (defclass unexpected-test-failure (test-failure)
58   ((actual-condition :accessor actual-condition :initarg :condition))
59   (:documentation "Represents the result of a test which neither
60 passed nor failed, but signaled an error we couldn't deal
61 with.
62
63 Note: This is very different than a SIGNALS check which instead
64 creates a TEST-PASSED or TEST-FAILURE object."))
65
66 (defclass test-skipped (test-result)
67   ()
68   (:documentation "A test which was not run. Usually this is due
69 to unsatisfied dependencies, but users can decide to skip test
70 when appropiate."))
71
72 (defgeneric test-skipped-p (object)
73   (:method ((o t)) nil)
74   (:method ((o test-skipped)) t))
75
76 (defun add-result (result-type &rest make-instance-args)
77   "Create a TEST-RESULT object of type RESULT-TYPE passing it the
78   initialize args MAKE-INSTANCE-ARGS and adds the resulting
79   object to the list of test results."
80   (with-run-state (result-list current-test)
81     (let ((result (apply #'make-instance result-type
82                          (append make-instance-args (list :test-case current-test)))))
83       (etypecase result
84         (test-passed  (format *test-dribble* "."))
85         (unexpected-test-failure (format *test-dribble* "X"))
86         (test-failure (format *test-dribble* "f"))
87         (test-skipped (format *test-dribble* "s")))
88       (push result result-list))))
89
90 ;;;; ** The check operators
91
92 ;;;; *** The IS check
93
94 (defmacro is (test &rest reason-args)
95   "The DWIM checking operator.
96
97 If TEST returns a true value a test-passed result is generated,
98 otherwise a test-failure result is generated and the reason,
99 unless REASON-ARGS is provided, is generated based on the form of
100 TEST:
101
102  (predicate expected actual) - Means that we want to check
103  whether, according to PREDICATE, the ACTUAL value is
104  in fact what we EXPECTED.
105
106  (predicate value) - Means that we want to ensure that VALUE
107  satisfies PREDICATE.
108
109 Wrapping the TEST form in a NOT simply preducse a negated reason string."
110   (assert (listp test)
111           (test)
112           "Argument to IS must be a list, not ~S" test)
113   (let (bindings effective-test default-reason-args)
114     (with-unique-names (e a v)
115       (list-match-case test
116         ((not (?predicate ?expected ?actual))
117          (setf bindings (list (list e ?expected)
118                               (list a ?actual))
119                effective-test `(not (,?predicate ,e ,a))
120                default-reason-args (list "~S was ~S to ~S" a `',?predicate e)))
121         ((not (?satisfies ?value))
122          (setf bindings (list (list v ?value))
123                effective-test `(not (,?satisfies ,v))
124                default-reason-args (list  "~S satisfied ~S" v `',?satisfies)))
125         ((?predicate ?expected ?actual)
126          (setf bindings (list (list e ?expected)
127                               (list a ?actual))
128                effective-test `(,?predicate ,e ,a)
129                default-reason-args (list "~S was not ~S to ~S" a `',?predicate e)))
130         ((?satisfies ?value)
131          (setf bindings (list (list v ?value))
132                effective-test `(,?satisfies ,v)
133                default-reason-args (list "~S did not satisfy ~S" v `',?satisfies)))
134         (t
135          (setf bindings '()
136                effective-test test)))
137       `(let ,bindings
138          (if ,effective-test
139              (add-result 'test-passed :test-expr ',test)
140              (add-result 'test-failure
141                          :reason ,(if (null reason-args)
142                                       `(format nil ,@default-reason-args)
143                                       `(format nil ,@reason-args))
144                          :test-expr ',test))))))
145
146 ;;;; *** Other checks
147
148 (defmacro skip (&rest reason)
149   "Generates a TEST-SKIPPED result."
150   `(progn
151      (format *test-dribble* "s")
152      (add-result 'test-skipped :reason (format nil ,@reason))))
153
154 (defmacro is-true (condition &rest reason-args)
155   "Like IS this check generates a pass if CONDITION returns true
156   and a failure if CONDITION returns false. Unlike IS this check
157   does not inspect CONDITION to determine how to report the
158   failure."
159   `(if ,condition
160        (add-result 'test-passed :test-expr ',condition)
161        (add-result 'test-failure :reason ,(if reason-args
162                                               `(format nil ,@reason-args)
163                                               `(format nil "~S did not return a true value" ',condition))
164                    :test-expr ',condition)))
165
166 (defmacro is-false (condition &rest reason-args)
167   "Generates a pass if CONDITION returns false, generates a
168   failure otherwise. Like IS-TRUE, and unlike IS, IS-FALSE does
169   not inspect CONDITION to determine what reason to give it case
170   of test failure"
171   `(if ,condition
172        (add-result 'test-failure :reason ,(if reason-args
173                                               `(format nil ,@reason-args)
174                                               `(format nil "~S returned a true value" ',condition))
175                    :test-expr ',condition)
176        (add-result 'test-passed :test-expr ',condition)))
177
178 (defmacro signals (condition &body body)
179   "Generates a pass if BODY signals a condition of type
180 CONDITION. BODY is evaluated in a block named NIL, CONDITION is
181 not evaluated."
182   (let ((block-name (gensym)))
183     `(block ,block-name
184        (handler-bind ((,condition (lambda (c)
185                                     (declare (ignore c))
186                                     ;; ok, body threw condition
187                                     (add-result 'test-passed 
188                                                 :test-expr ',condition)
189                                     (return-from ,block-name t))))
190          (block nil
191            ,@body
192            (add-result 'test-failure 
193                        :reason (format nil "Failed to signal a ~S" ',condition)
194                        :test-expr ',condition)
195            (return-from ,block-name nil))))))
196
197 (defmacro finishes (&body body)
198   "Generates a pass if BODY executes to normal completion. In
199 other words if body does signal, return-from or throw this test
200 fails."
201   `(let ((ok nil))
202      (unwind-protect
203          (progn 
204            ,@body
205            (setf ok t))
206        (if ok
207            (add-result 'test-passed :test-expr ',body)
208            (add-result 'test-failure
209                        :reason (format nil "Test didn't finish")
210                        :test-expr ',body)))))
211
212 (defmacro pass (&rest message-args)
213   "Simply generate a PASS."
214   `(add-result 'test-passed 
215                :test-expr ',message-args
216                ,@(when message-args
217                        `(:reason (format nil ,@message-args)))))
218
219 (defmacro fail (&rest message-args)
220   "Simply generate a FAIL."
221   `(add-result 'test-failure
222                :test-expr ',message-args
223                ,@(when message-args
224                        `(:reason (format nil ,@message-args)))))
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