Dropped is-equal and is-string=, added is-every
[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 (define-condition check-failure (error)
50   ((reason :accessor reason :initarg :reason :initform "no reason given")
51    (test-case :accessor test-case :initarg :test-case)
52    (test-expr :accessor test-expr :initarg :test-expr))
53   (:documentation "Signaled when a check fails.")
54   (:report  (lambda (c stream)
55               (format stream "The following check failed: ~S~%~A."
56                       (test-expr c)
57                       (reason c)))))
58
59 (defmacro process-failure (&rest args)
60   `(progn
61     (with-simple-restart (ignore-failure "Continue the test run.")
62       (error 'check-failure ,@args))
63     (add-result 'test-failure ,@args)))
64
65 (defclass test-failure (test-result)
66   ()
67   (:documentation "Class for unsuccessful checks."))
68
69 (defgeneric test-failure-p (object)
70   (:method ((o t)) nil)
71   (:method ((o test-failure)) t))
72
73 (defclass unexpected-test-failure (test-failure)
74   ((actual-condition :accessor actual-condition :initarg :condition))
75   (:documentation "Represents the result of a test which neither
76 passed nor failed, but signaled an error we couldn't deal
77 with.
78
79 Note: This is very different than a SIGNALS check which instead
80 creates a TEST-PASSED or TEST-FAILURE object."))
81
82 (defclass test-skipped (test-result)
83   ()
84   (:documentation "A test which was not run. Usually this is due
85 to unsatisfied dependencies, but users can decide to skip test
86 when appropiate."))
87
88 (defgeneric test-skipped-p (object)
89   (:method ((o t)) nil)
90   (:method ((o test-skipped)) t))
91
92 (defun add-result (result-type &rest make-instance-args)
93   "Create a TEST-RESULT object of type RESULT-TYPE passing it the
94   initialize args MAKE-INSTANCE-ARGS and adds the resulting
95   object to the list of test results."
96   (with-run-state (result-list current-test)
97     (let ((result (apply #'make-instance result-type
98                          (append make-instance-args (list :test-case current-test)))))
99       (etypecase result
100         (test-passed  (format *test-dribble* "."))
101         (unexpected-test-failure (format *test-dribble* "X"))
102         (test-failure (format *test-dribble* "f"))
103         (test-skipped (format *test-dribble* "s")))
104       (push result result-list))))
105
106 ;;;; ** The check operators
107
108 ;;;; *** The IS check
109
110 (defmacro is (test &rest reason-args)
111   "The DWIM checking operator.
112
113 If TEST returns a true value a test-passed result is generated,
114 otherwise a test-failure result is generated and the reason,
115 unless REASON-ARGS is provided, is generated based on the form of
116 TEST:
117
118  (predicate expected actual) - Means that we want to check
119  whether, according to PREDICATE, the ACTUAL value is
120  in fact what we EXPECTED.
121
122  (predicate value) - Means that we want to ensure that VALUE
123  satisfies PREDICATE.
124
125 Wrapping the TEST form in a NOT simply preducse a negated reason string."
126   (assert (listp test)
127           (test)
128           "Argument to IS must be a list, not ~S" test)
129   (let (bindings effective-test default-reason-args)
130     (with-unique-names (e a v)
131       (list-match-case test
132         ((not (?predicate ?expected ?actual))
133          (setf bindings (list (list e ?expected)
134                               (list a ?actual))
135                effective-test `(not (,?predicate ,e ,a))
136                default-reason-args (list "~S was ~S to ~S" a `',?predicate e)))
137         ((not (?satisfies ?value))
138          (setf bindings (list (list v ?value))
139                effective-test `(not (,?satisfies ,v))
140                default-reason-args (list  "~S satisfied ~S" v `',?satisfies)))
141         ((?predicate ?expected ?actual)
142          (setf bindings (list (list e ?expected)
143                               (list a ?actual))
144                effective-test `(,?predicate ,e ,a)
145                default-reason-args (list "~S was not ~S to ~S" a `',?predicate e)))
146         ((?satisfies ?value)
147          (setf bindings (list (list v ?value))
148                effective-test `(,?satisfies ,v)
149                default-reason-args (list "~S did not satisfy ~S" v `',?satisfies)))
150         (?_
151          (setf bindings '()
152                effective-test test
153                default-reason-args (list "No reason supplied"))))
154       `(let ,bindings
155          (if ,effective-test
156              (add-result 'test-passed :test-expr ',test)
157              (process-failure :reason (format nil ,@(or reason-args default-reason-args))
158                               :test-expr ',test))))))
159
160 ;;;; *** Other checks
161
162 (defmacro skip (&rest reason)
163   "Generates a TEST-SKIPPED result."
164   `(progn
165      (format *test-dribble* "s")
166      (add-result 'test-skipped :reason (format nil ,@reason))))
167
168 (defmacro is-every (predicate &body clauses)
169   "The input is either a list of lists, or a list of pairs. Generates (is (,predicate ,expr ,value))
170    for each pair of elements or (is (,predicate ,expr ,value) ,@reason) for each list."
171   `(progn
172     ,@(if (every #'consp clauses)
173           (loop for (expected actual &rest reason) in clauses
174                 collect `(is (,predicate ,expected ,actual) ,@reason))
175           (progn
176             (assert (evenp (list-length clauses)))
177             (loop for (expr value) on clauses by #'cddr
178                   collect `(is (,predicate ,expr ,value)))))))
179
180 (defmacro is-true (condition &rest reason-args)
181   "Like IS this check generates a pass if CONDITION returns true
182   and a failure if CONDITION returns false. Unlike IS this check
183   does not inspect CONDITION to determine how to report the
184   failure."
185   `(if ,condition
186     (add-result 'test-passed :test-expr ',condition)
187     (process-failure
188      :reason ,(if reason-args
189                   `(format nil ,@reason-args)
190                   `(format nil "~S did not return a true value" ',condition))
191      :test-expr ',condition)))
192
193 (defmacro is-false (condition &rest reason-args)
194   "Generates a pass if CONDITION returns false, generates a
195   failure otherwise. Like IS-TRUE, and unlike IS, IS-FALSE does
196   not inspect CONDITION to determine what reason to give it case
197   of test failure"
198   `(if ,condition
199     (process-failure
200      :reason ,(if reason-args
201                   `(format nil ,@reason-args)
202                   `(format nil "~S returned a true value" ',condition))
203      :test-expr ',condition)
204     (add-result 'test-passed :test-expr ',condition)))
205
206 (defmacro signals (condition-spec
207                    &body body)
208   "Generates a pass if BODY signals a condition of type
209 CONDITION. BODY is evaluated in a block named NIL, CONDITION is
210 not evaluated."
211   (let ((block-name (gensym)))
212     (destructuring-bind (condition &optional reason-control reason-args)
213         (ensure-list condition-spec)
214       `(block ,block-name
215          (handler-bind ((,condition (lambda (c)
216                                       (declare (ignore c))
217                                       ;; ok, body threw condition
218                                       (add-result 'test-passed 
219                                                   :test-expr ',condition)
220                                       (return-from ,block-name t))))
221            (block nil
222              ,@body))
223          (process-failure
224           :reason ,(if reason-control
225                        `(format nil ,reason-control ,@reason-args)
226                        `(format nil "Failed to signal a ~S" ',condition))
227           :test-expr ',condition)
228          (return-from ,block-name nil)))))
229
230 (defmacro finishes (&body body)
231   "Generates a pass if BODY executes to normal completion. In
232 other words if body does signal, return-from or throw this test
233 fails."
234   `(let ((ok nil))
235      (unwind-protect
236          (progn 
237            ,@body
238            (setf ok t))
239        (if ok
240            (add-result 'test-passed :test-expr ',body)
241            (process-failure
242             :reason (format nil "Test didn't finish")
243             :test-expr ',body)))))
244
245 (defmacro pass (&rest message-args)
246   "Simply generate a PASS."
247   `(add-result 'test-passed 
248                :test-expr ',message-args
249                ,@(when message-args
250                        `(:reason (format nil ,@message-args)))))
251
252 (defmacro fail (&rest message-args)
253   "Simply generate a FAIL."
254   `(process-failure
255     :test-expr ',message-args
256     ,@(when message-args
257             `(:reason (format nil ,@message-args)))))
258
259 ;; Copyright (c) 2002-2003, Edward Marco Baringer
260 ;; All rights reserved. 
261 ;; 
262 ;; Redistribution and use in source and binary forms, with or without
263 ;; modification, are permitted provided that the following conditions are
264 ;; met:
265 ;; 
266 ;;  - Redistributions of source code must retain the above copyright
267 ;;    notice, this list of conditions and the following disclaimer.
268 ;; 
269 ;;  - Redistributions in binary form must reproduce the above copyright
270 ;;    notice, this list of conditions and the following disclaimer in the
271 ;;    documentation and/or other materials provided with the distribution.
272 ;;
273 ;;  - Neither the name of Edward Marco Baringer, nor BESE, nor the names
274 ;;    of its contributors may be used to endorse or promote products
275 ;;    derived from this software without specific prior written permission.
276 ;; 
277 ;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
278 ;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
279 ;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
280 ;; A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
281 ;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
282 ;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
283 ;; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
284 ;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
285 ;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
286 ;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
287 ;; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE