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
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)))))
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
118 whether, according to PREDICATE, the ACTUAL value is
119 in fact what we EXPECTED.
121 (predicate value) - Means that we want to ensure that VALUE
124 Wrapping the TEST form in a NOT simply produces a negated reason
128 "Argument to IS must be a list, not ~S" test)
129 (let (bindings effective-test default-reason-args)
130 (with-gensyms (e a v)
131 (flet ((process-entry (predicate expected actual &optional negatedp)
132 ;; make sure EXPECTED is holding the entry that starts with 'values
133 (when (and (consp actual)
134 (eq (car actual) 'values))
135 (assert (not (and (consp expected)
136 (eq (car expected) 'values))) ()
137 "Both the expected and actual part is a values expression.")
138 (rotatef expected actual))
140 (if (and (consp expected)
141 (eq (car expected) 'values))
143 (setf expected (copy-list expected))
144 (setf setf-forms (loop for cell = (rest expected) then (cdr cell)
147 when (eq (car cell) '*)
148 collect `(setf (elt ,a ,i) nil)
149 and do (setf (car cell) nil)))
150 (setf bindings (list (list e `(list ,@(rest expected)))
151 (list a `(multiple-value-list ,actual)))))
152 (setf bindings (list (list e expected)
154 (setf effective-test `(progn
157 `(not (,predicate ,e ,a))
158 `(,predicate ,e ,a)))))))
159 (list-match-case test
160 ((not (?predicate ?expected ?actual))
161 (process-entry ?predicate ?expected ?actual t)
162 (setf default-reason-args
163 (list "~2&~S~2% evaluated to ~2&~S~2% which is ~2&~S~2%to ~2&~S~2% (it should not be)"
164 `',?actual a `',?predicate e)))
165 ((not (?satisfies ?value))
166 (setf bindings (list (list v ?value))
167 effective-test `(not (,?satisfies ,v))
169 (list "~2&~S~2% evaluated to ~2&~S~2% which satisfies ~2&~S~2% (it should not)"
170 `',?value v `',?satisfies)))
171 ((?predicate ?expected ?actual)
172 (process-entry ?predicate ?expected ?actual)
173 (setf default-reason-args
174 (list "~2&~S~2% evaluated to ~2&~S~2% which is not ~2&~S~2% to ~2&~S~2%."
175 `',?actual a `',?predicate e)))
177 (setf bindings (list (list v ?value))
178 effective-test `(,?satisfies ,v)
180 (list "~2&~S~2% evaluated to ~2&~S~2% which does not satisfy ~2&~S~2%"
181 `',?value v `',?satisfies)))
185 default-reason-args (list "~2&~S~2% was NIL." `',test)))))
188 (add-result 'test-passed :test-expr ',test)
189 (process-failure :reason (format nil ,@(or reason-args default-reason-args))
190 :test-expr ',test))))))
192 ;;;; *** Other checks
194 (defmacro skip (&rest reason)
195 "Generates a TEST-SKIPPED result."
197 (format *test-dribble* "s")
198 (add-result 'test-skipped :reason (format nil ,@reason))))
200 (defmacro is-every (predicate &body clauses)
201 "The input is either a list of lists, or a list of pairs. Generates (is (,predicate ,expr ,value))
202 for each pair of elements or (is (,predicate ,expr ,value) ,@reason) for each list."
204 ,@(if (every #'consp clauses)
205 (loop for (expected actual . reason) in clauses
206 collect `(is (,predicate ,expected ,actual) ,@reason))
208 (assert (evenp (list-length clauses)))
209 (loop for (expr value) on clauses by #'cddr
210 collect `(is (,predicate ,expr ,value)))))))
212 (defmacro is-all (&body clauses)
213 "The input is a list of tests. Generates (is ,clause) for each one."
214 `(progn ,@(mapcar (lambda (clause) `(is ,clause)) clauses)))
216 (defmacro is-true (condition &rest reason-args)
217 "Like IS this check generates a pass if CONDITION returns true
218 and a failure if CONDITION returns false. Unlike IS this check
219 does not inspect CONDITION to determine how to report the
222 (add-result 'test-passed :test-expr ',condition)
224 :reason ,(if reason-args
225 `(format nil ,@reason-args)
226 `(format nil "~S did not return a true value" ',condition))
227 :test-expr ',condition)))
229 (defmacro is-false (condition &rest reason-args)
230 "Generates a pass if CONDITION returns false, generates a
231 failure otherwise. Like IS-TRUE, and unlike IS, IS-FALSE does
232 not inspect CONDITION to determine what reason to give it case
235 (with-gensyms (value)
236 `(let ((,value ,condition))
239 :reason ,(if reason-args
240 `(format nil ,@reason-args)
241 `(format nil "~S returned the value ~S, which is true" ',condition ,value ))
242 :test-expr ',condition)
243 (add-result 'test-passed :test-expr ',condition)))))
245 (defmacro signals (condition-spec
247 "Generates a pass if BODY signals a condition of type
248 CONDITION. BODY is evaluated in a block named NIL, CONDITION is
250 (let ((block-name (gensym)))
251 (destructuring-bind (condition &optional reason-control reason-args)
252 (ensure-list condition-spec)
254 (handler-bind ((,condition (lambda (c)
256 ;; ok, body threw condition
257 (add-result 'test-passed
258 :test-expr ',condition)
259 (return-from ,block-name t))))
263 :reason ,(if reason-control
264 `(format nil ,reason-control ,@reason-args)
265 `(format nil "Failed to signal a ~S" ',condition))
266 :test-expr ',condition)
267 (return-from ,block-name nil)))))
269 (defmacro finishes (&body body)
270 "Generates a pass if BODY executes to normal completion. In
271 other words if body does signal, return-from or throw this test
279 (add-result 'test-passed :test-expr ',body)
281 :reason (format nil "Test didn't finish")
282 :test-expr ',body)))))
284 (defmacro pass (&rest message-args)
285 "Simply generate a PASS."
286 `(add-result 'test-passed
287 :test-expr ',message-args
289 `(:reason (format nil ,@message-args)))))
291 (defmacro fail (&rest message-args)
292 "Simply generate a FAIL."
294 :test-expr ',message-args
296 `(:reason (format nil ,@message-args)))))
298 ;; Copyright (c) 2002-2003, Edward Marco Baringer
299 ;; All rights reserved.
301 ;; Redistribution and use in source and binary forms, with or without
302 ;; modification, are permitted provided that the following conditions are
305 ;; - Redistributions of source code must retain the above copyright
306 ;; notice, this list of conditions and the following disclaimer.
308 ;; - Redistributions in binary form must reproduce the above copyright
309 ;; notice, this list of conditions and the following disclaimer in the
310 ;; documentation and/or other materials provided with the distribution.
312 ;; - Neither the name of Edward Marco Baringer, nor BESE, nor the names
313 ;; of its contributors may be used to endorse or promote products
314 ;; derived from this software without specific prior written permission.
316 ;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
317 ;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
318 ;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
319 ;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
320 ;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
321 ;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
322 ;; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
323 ;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
324 ;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
325 ;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
326 ;; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE