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 (defclass test-failure (test-result)
51 (:documentation "Class for unsuccessful checks."))
53 (defgeneric test-failure-p (object)
55 (:method ((o test-failure)) t))
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
63 Note: This is very different than a SIGNALS check which instead
64 creates a TEST-PASSED or TEST-FAILURE object."))
66 (defclass test-skipped (test-result)
68 (:documentation "A test which was not run. Usually this is due
69 to unsatisfied dependencies, but users can decide to skip test
72 (defgeneric test-skipped-p (object)
74 (:method ((o test-skipped)) t))
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)))))
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))))
90 ;;;; ** The check operators
94 (defmacro is (test &rest reason-args)
95 "The DWIM checking operator.
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
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.
106 (predicate value) - Means that we want to ensure that VALUE
109 Wrapping the TEST form in a NOT simply preducse a negated reason string."
112 "Argument to IS must be a list, not ~S" test)
114 (add-result 'test-passed :test-expr ',test)
115 (add-result 'test-failure
116 :reason ,(if (null reason-args)
117 (list-match-case test
118 ((not (?predicate ?expected ?actual))
119 `(format nil "~S was ~S to ~S" ,?actual ',?predicate ,?expected))
120 ((not (?satisfies ?value))
121 `(format nil "~S satisfied ~S" ,?value ',?satisfies))
122 ((?predicate ?expected ?actual)
123 `(format nil "~S was not ~S to ~S" ,?actual ',?predicate ,?expected))
125 `(format nil "~S did not satisfy ~S" ,?value ',?satisfies))
127 `(is-true ,test ,@reason-args)))
128 `(format nil ,@reason-args))
131 ;;;; *** Other checks
133 (defmacro skip (&rest reason)
134 "Generates a TEST-SKIPPED result."
136 (format *test-dribble* "s")
137 (add-result 'test-skipped :reason (format nil ,@reason))))
139 (defmacro is-true (condition &rest reason-args)
140 "Like IS this check generates a pass if CONDITION returns true
141 and a failure if CONDITION returns false. Unlike IS this check
142 does not inspect CONDITION to determine how to report the
145 (add-result 'test-passed :test-expr ',condition)
146 (add-result 'test-failure :reason ,(if reason-args
147 `(format nil ,@reason-args)
148 `(format nil "~S did not return a true value" ',condition))
149 :test-expr ',condition)))
151 (defmacro is-false (condition &rest reason-args)
152 "Generates a pass if CONDITION returns false, generates a
153 failure otherwise. Like IS-TRUE, and unlike IS, IS-FALSE does
154 not inspect CONDITION to determine what reason to give it case
157 (add-result 'test-failure :reason ,(if reason-args
158 `(format nil ,@reason-args)
159 `(format nil "~S returned a true value" ',condition))
160 :test-expr ',condition)
161 (add-result 'test-passed :test-expr ',condition)))
163 (defmacro signals (condition &body body)
164 "Generates a pass if BODY signals a condition of type
165 CONDITION. BODY is evaluated in a block named NIL, CONDITION is
167 (let ((block-name (gensym)))
169 (handler-bind ((,condition (lambda (c)
171 ;; ok, body threw condition
172 (add-result 'test-passed
173 :test-expr ',condition)
174 (return-from ,block-name t))))
177 (add-result 'test-failure
178 :reason (format nil "Failed to signal a ~S" ',condition)
179 :test-expr ',condition)
180 (return-from ,block-name nil))))))
182 (defmacro finishes (&body body)
183 "Generates a pass if BODY executes to normal completion. In
184 other words if body does signal, return-from or throw this test
192 (add-result 'test-passed :test-expr ',body)
193 (add-result 'test-failure
194 :reason (format nil "Test didn't finish")
195 :test-expr ',body)))))
197 (defmacro pass (&rest message-args)
198 "Simply generate a PASS."
199 `(add-result 'test-passed
200 :test-expr ',message-args
202 `(:reason (format nil ,@message-args)))))
204 (defmacro fail (&rest message-args)
205 "Simply generate a FAIL."
206 `(add-result 'test-failure
207 :test-expr ',message-args
209 `(:reason (format nil ,@message-args)))))
211 ;; Copyright (c) 2002-2003, Edward Marco Baringer
212 ;; All rights reserved.
214 ;; Redistribution and use in source and binary forms, with or without
215 ;; modification, are permitted provided that the following conditions are
218 ;; - Redistributions of source code must retain the above copyright
219 ;; notice, this list of conditions and the following disclaimer.
221 ;; - Redistributions in binary form must reproduce the above copyright
222 ;; notice, this list of conditions and the following disclaimer in the
223 ;; documentation and/or other materials provided with the distribution.
225 ;; - Neither the name of Edward Marco Baringer, nor BESE, nor the names
226 ;; of its contributors may be used to endorse or promote products
227 ;; derived from this software without specific prior written permission.
229 ;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
230 ;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
231 ;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
232 ;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
233 ;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
234 ;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
235 ;; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
236 ;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
237 ;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
238 ;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
239 ;; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE