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 (:documentation "All checking macros will generate an object of
40 (defclass test-passed (test-result)
42 (:documentation "Class for successful checks."))
44 (defgeneric test-passed-p (object)
46 (:method ((o test-passed)) t))
48 (defclass test-failure (test-result)
50 (:documentation "Class for unsuccessful checks."))
52 (defgeneric test-failure-p (object)
54 (:method ((o test-failure)) t))
56 (defclass unexpected-test-failure (test-failure)
57 ((actual-condition :accessor actual-condition :initarg :condition))
58 (:documentation "Represents the result of a test which neither
59 passed nor failed, but signaled an error we couldn't deal
62 Note: This is very different than a SIGNALS check which instead
63 creates a TEST-PASSED or TEST-FAILURE object."))
65 (defclass test-skipped (test-result)
67 (:documentation "A test which was not run. Usually this is due
68 to unsatisfied dependencies, but users can decide to skip test
71 (defgeneric test-skipped-p (object)
73 (:method ((o test-skipped)) t))
75 (defun add-result (result-type &rest make-instance-args)
76 "Create a TEST-RESULT object of type RESULT-TYPE passing it the
77 initialize args MAKE-INSTANCE-ARGS and adds the resulting
78 object to the list of test results."
79 (with-run-state (result-list current-test)
80 (let ((result (apply #'make-instance result-type
81 (append make-instance-args (list :test-case current-test)))))
83 (test-passed (format *test-dribble* "."))
84 (unexpected-test-failure (format *test-dribble* "X"))
85 (test-failure (format *test-dribble* "f"))
86 (test-skipped (format *test-dribble* "s")))
87 (push result result-list))))
89 ;;;; ** The check operators
93 (defmacro is (test &rest reason-args)
94 "The DWIM checking operator.
96 If TEST returns a true value a test-passed result is generated,
97 otherwise a test-failure result is generated and the reason,
98 unless REASON-ARGS is provided, is generated based on the form of
101 (predicate expected actual) - Means that we want to check
102 whether, according to PREDICATE, the ACTUAL value is
103 in fact what we EXPECTED.
105 (predicate value) - Means that we want to ensure that VALUE
108 Wrapping the TEST form in a NOT simply preducse a negated reason string."
111 "Argument to IS must be a list, not ~S" test)
113 (add-result 'test-passed)
114 (add-result 'test-failure
115 :reason ,(if (null reason-args)
116 (list-match-case test
117 ((not (?predicate ?expected ?actual))
118 `(format nil "~S was ~S to ~S" ,?actual ',?predicate ,?expected))
119 ((not (?satisfies ?value))
120 `(format nil "~S satisfied ~S" ,?value ',?satisfies))
121 ((?predicate ?expected ?actual)
122 `(format nil "~S was not ~S to ~S" ,?actual ',?predicate ,?expected))
124 `(format nil "~S did not satisfy ~S" ,?value ',?satisfies))
126 `(is-true ,test ,@reason-args)))
127 `(format nil ,@reason-args)))))
129 ;;;; *** Other checks
131 (defmacro skip (&rest reason)
132 "Generates a TEST-SKIPPED result."
134 (format *test-dribble* "s")
135 (add-result 'test-skipped :reason (format nil ,@reason))))
137 (defmacro is-true (condition &rest reason-args)
138 "Like IS this check generates a pass if CONDITION returns true
139 and a failure if CONDITION returns false. Unlike IS this check
140 does not inspect CONDITION to determine how to report the
143 (add-result 'test-passed)
144 (add-result 'test-failure :reason ,(if reason-args
145 `(format nil ,@reason-args)
146 `(format nil "~S did not return a true value" ',condition)))))
148 (defmacro is-false (condition &rest reason-args)
149 "Generates a pass if CONDITION returns false, generates a
150 failure otherwise. Like IS-TRUE, and unlike IS, IS-FALSE does
151 not inspect CONDITION to determine what reason to give it case
154 (add-result 'test-failure :reason ,(if reason-args
155 `(format nil ,@reason-args)
156 `(format nil "~S returned a true value" ',condition)))
157 (add-result 'test-passed)))
159 (defmacro signals (condition &body body)
160 "Generates a pass if BODY signals a condition of type
161 CONDITION. BODY is evaluated in a block named NIL, CONDITION is
163 (let ((block-name (gensym)))
165 (handler-bind ((,condition (lambda (c)
167 ;; ok, body threw condition
168 (add-result 'test-passed)
169 (return-from ,block-name t))))
172 (add-result 'test-failure :reason (format nil "Failed to signal a ~S" ',condition))
173 (return-from ,block-name nil))))))
175 (defmacro finishes (&body body)
176 "Generates a pass if BODY executes to normal completion. In
177 other words if body does signal, return-from or throw this test
185 (add-result 'test-passed)
186 (add-result 'test-failure
187 :reason (format nil "Test didn't finish"))))))
189 (defmacro pass (&rest message-args)
190 "Simply generate a PASS."
191 `(add-result 'test-passed ,@(when message-args
192 `(:reason (format nil ,@message-args)))))
194 (defmacro fail (&rest message-args)
195 "Simply generate a FAIL."
196 `(add-result 'test-failure ,@(when message-args
197 `(:reason (format nil ,@message-args)))))
199 ;; Copyright (c) 2002-2003, Edward Marco Baringer
200 ;; All rights reserved.
202 ;; Redistribution and use in source and binary forms, with or without
203 ;; modification, are permitted provided that the following conditions are
206 ;; - Redistributions of source code must retain the above copyright
207 ;; notice, this list of conditions and the following disclaimer.
209 ;; - Redistributions in binary form must reproduce the above copyright
210 ;; notice, this list of conditions and the following disclaimer in the
211 ;; documentation and/or other materials provided with the distribution.
213 ;; - Neither the name of Edward Marco Baringer, nor BESE, nor the names
214 ;; of its contributors may be used to endorse or promote products
215 ;; derived from this software without specific prior written permission.
217 ;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
218 ;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
219 ;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
220 ;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
221 ;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
222 ;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
223 ;; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
224 ;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
225 ;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
226 ;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
227 ;; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE