1 ;;;; -*- Mode: Lisp; indent-tabs-mode: nil -*-
3 (in-package :it.bese.fiveam)
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.
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.
18 (defvar *test-dribble* t)
20 (defmacro with-*test-dribble* (stream &body body)
21 `(let ((*test-dribble* ,stream))
22 (declare (special *test-dribble*))
25 (eval-when (:compile-toplevel :load-toplevel :execute)
26 (def-special-environment run-state ()
30 ;;;; ** Types of test results
32 ;;;; Every check produces a result object.
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
41 (defclass test-passed (test-result)
43 (:documentation "Class for successful checks."))
45 (defgeneric test-passed-p (object)
47 (:method ((o test-passed)) t))
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."
59 (defmacro process-failure (&rest args)
61 (with-simple-restart (ignore-failure "Continue the test run.")
62 (error 'check-failure ,@args))
63 (add-result 'test-failure ,@args)))
65 (defclass test-failure (test-result)
67 (:documentation "Class for unsuccessful checks."))
69 (defgeneric test-failure-p (object)
71 (:method ((o test-failure)) t))
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
79 Note: This is very different than a SIGNALS check which instead
80 creates a TEST-PASSED or TEST-FAILURE object."))
82 (defclass test-skipped (test-result)
84 (:documentation "A test which was not run. Usually this is due
85 to unsatisfied dependencies, but users can decide to skip test
88 (defgeneric test-skipped-p (object)
90 (:method ((o test-skipped)) t))
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 object to
95 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)))))
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))))
106 ;;;; ** The check operators
108 ;;;; *** The IS check
110 (defmacro is (test &rest reason-args)
111 "The DWIM checking operator.
113 If TEST returns a true value a test-passed result is generated,
114 otherwise a test-failure result is generated. The reason, unless
115 REASON-ARGS is provided, is generated based on the form of TEST:
117 \(predicate expected actual) - Means that we want to check whether,
118 according to PREDICATE, the ACTUAL value is in fact what we EXPECTED.
120 \(predicate value) - Means that we want to ensure that VALUE satisfies
123 Wrapping the TEST form in a NOT simply produces a negated reason
127 "Argument to IS must be a list, not ~S" test)
128 (let (bindings effective-test default-reason-args)
129 (with-gensyms (e a v)
130 (flet ((process-entry (predicate expected actual &optional negatedp)
131 ;; make sure EXPECTED is holding the entry that starts with 'values
132 (when (and (consp actual)
133 (eq (car actual) 'values))
134 (assert (not (and (consp expected)
135 (eq (car expected) 'values))) ()
136 "Both the expected and actual part is a values expression.")
137 (rotatef expected actual))
139 (if (and (consp expected)
140 (eq (car expected) 'values))
142 (setf expected (copy-list expected))
143 (setf setf-forms (loop for cell = (rest expected) then (cdr cell)
146 when (eq (car cell) '*)
147 collect `(setf (elt ,a ,i) nil)
148 and do (setf (car cell) nil)))
149 (setf bindings (list (list e `(list ,@(rest expected)))
150 (list a `(multiple-value-list ,actual)))))
151 (setf bindings (list (list e expected)
153 (setf effective-test `(progn
156 `(not (,predicate ,e ,a))
157 `(,predicate ,e ,a)))))))
158 (list-match-case test
159 ((not (?predicate ?expected ?actual))
160 (process-entry ?predicate ?expected ?actual t)
161 (setf default-reason-args
162 (list "~2&~S~2% evaluated to ~2&~S~2% which is ~2&~S~2%to ~2&~S~2% (it should not be)"
163 `',?actual a `',?predicate e)))
164 ((not (?satisfies ?value))
165 (setf bindings (list (list v ?value))
166 effective-test `(not (,?satisfies ,v))
168 (list "~2&~S~2% evaluated to ~2&~S~2% which satisfies ~2&~S~2% (it should not)"
169 `',?value v `',?satisfies)))
170 ((?predicate ?expected ?actual)
171 (process-entry ?predicate ?expected ?actual)
172 (setf default-reason-args
173 (list "~2&~S~2% evaluated to ~2&~S~2% which is not ~2&~S~2% to ~2&~S~2%."
174 `',?actual a `',?predicate e)))
176 (setf bindings (list (list v ?value))
177 effective-test `(,?satisfies ,v)
179 (list "~2&~S~2% evaluated to ~2&~S~2% which does not satisfy ~2&~S~2%"
180 `',?value v `',?satisfies)))
184 default-reason-args (list "~2&~S~2% was NIL." `',test)))))
187 (add-result 'test-passed :test-expr ',test)
188 (process-failure :reason (format nil ,@(or reason-args default-reason-args))
189 :test-expr ',test))))))
191 ;;;; *** Other checks
193 (defmacro skip (&rest reason)
194 "Generates a TEST-SKIPPED result."
196 (format *test-dribble* "s")
197 (add-result 'test-skipped :reason (format nil ,@reason))))
199 (defmacro is-every (predicate &body clauses)
200 "The input is either a list of lists, or a list of pairs. Generates (is (,predicate ,expr ,value))
201 for each pair of elements or (is (,predicate ,expr ,value) ,@reason) for each list."
203 ,@(if (every #'consp clauses)
204 (loop for (expected actual . reason) in clauses
205 collect `(is (,predicate ,expected ,actual) ,@reason))
207 (assert (evenp (list-length clauses)))
208 (loop for (expr value) on clauses by #'cddr
209 collect `(is (,predicate ,expr ,value)))))))
211 (defmacro is-true (condition &rest reason-args)
212 "Like IS this check generates a pass if CONDITION returns true
213 and a failure if CONDITION returns false. Unlike IS this check
214 does not inspect CONDITION to determine how to report the
217 (add-result 'test-passed :test-expr ',condition)
219 :reason ,(if reason-args
220 `(format nil ,@reason-args)
221 `(format nil "~S did not return a true value" ',condition))
222 :test-expr ',condition)))
224 (defmacro is-false (condition &rest reason-args)
225 "Generates a pass if CONDITION returns false, generates a
226 failure otherwise. Like IS-TRUE, and unlike IS, IS-FALSE does
227 not inspect CONDITION to determine what reason to give it case
230 (with-gensyms (value)
231 `(let ((,value ,condition))
234 :reason ,(if reason-args
235 `(format nil ,@reason-args)
236 `(format nil "~S returned the value ~S, which is true" ',condition ,value ))
237 :test-expr ',condition)
238 (add-result 'test-passed :test-expr ',condition)))))
240 (defmacro signals (condition-spec
242 "Generates a pass if BODY signals a condition of type
243 CONDITION. BODY is evaluated in a block named NIL, CONDITION is
245 (let ((block-name (gensym)))
246 (destructuring-bind (condition &optional reason-control reason-args)
247 (ensure-list condition-spec)
249 (handler-bind ((,condition (lambda (c)
251 ;; ok, body threw condition
252 (add-result 'test-passed
253 :test-expr ',condition)
254 (return-from ,block-name t))))
258 :reason ,(if reason-control
259 `(format nil ,reason-control ,@reason-args)
260 `(format nil "Failed to signal a ~S" ',condition))
261 :test-expr ',condition)
262 (return-from ,block-name nil)))))
264 (defmacro finishes (&body body)
265 "Generates a pass if BODY executes to normal completion. In
266 other words if body does signal, return-from or throw this test
274 (add-result 'test-passed :test-expr ',body)
276 :reason (format nil "Test didn't finish")
277 :test-expr ',body)))))
279 (defmacro pass (&rest message-args)
280 "Simply generate a PASS."
281 `(add-result 'test-passed
282 :test-expr ',message-args
284 `(:reason (format nil ,@message-args)))))
286 (defmacro fail (&rest message-args)
287 "Simply generate a FAIL."
289 :test-expr ',message-args
291 `(:reason (format nil ,@message-args)))))
293 ;; Copyright (c) 2002-2003, Edward Marco Baringer
294 ;; All rights reserved.
296 ;; Redistribution and use in source and binary forms, with or without
297 ;; modification, are permitted provided that the following conditions are
300 ;; - Redistributions of source code must retain the above copyright
301 ;; notice, this list of conditions and the following disclaimer.
303 ;; - Redistributions in binary form must reproduce the above copyright
304 ;; notice, this list of conditions and the following disclaimer in the
305 ;; documentation and/or other materials provided with the distribution.
307 ;; - Neither the name of Edward Marco Baringer, nor BESE, nor the names
308 ;; of its contributors may be used to endorse or promote products
309 ;; derived from this software without specific prior written permission.
311 ;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
312 ;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
313 ;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
314 ;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
315 ;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
316 ;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
317 ;; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
318 ;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
319 ;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
320 ;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
321 ;; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE