1 ;;;; -*- Mode: Lisp; indent-tabs-mode: nil -*-
3 (in-package :it.bese.fiveam)
7 ;;;; While executing checks and collecting the results is the core job
8 ;;;; of a testing framework it is also important to be able to
9 ;;;; organize checks into groups, fiveam provides two mechanisms for
10 ;;;; organizing checks: tests and test suites. A test is a named
11 ;;;; collection of checks which can be run and a test suite is a named
12 ;;;; collection of tests and test suites.
15 (make-hash-table :test 'eql)
16 "Lookup table mapping test (and test suite) names to objects.")
18 (defun get-test (key &key default error)
19 "Finds the test named KEY. If KEY is a testable-object (a test case
20 or a test suite) then we just return KEY, otherwise we look for a test
21 named KEY in the *TEST* hash table."
22 (if (testable-object-p key)
24 (multiple-value-bind (value foundp)
29 (error "Unable to find test named ~S." key)
32 (defun (setf get-test) (value key)
33 (setf (gethash key *test*) value))
39 (loop for test being the hash-keys of *test*
42 (defmacro test (name &body body)
43 "Deprecated. See DEF-TEST."
44 (simple-style-warning "~A is OBSOLETE! Use ~A instead." 'test 'def-test)
45 (destructuring-bind (name &rest args)
47 `(def-test ,name (,@args) ,@body)))
49 (declaim (special *suite*))
51 (defmacro def-test (name (&key (suite nil suite-p)
53 (compile-at :run-time)
57 "Create a test named NAME.
63 The suite to put the test under. It defaults to *SUITE* (which
64 itself defaults to the default global suite).
67 The name of the fixture to use for this test. See `WITH-FIXTURE` for
70 COMPILE-AT (a keyword)::
71 When the body of this test should be compiled. By default, or when
72 `:compile-at` is `:run-time`, test bodies are only compiled before
73 they are run. Set this to to `:definition-time` to force
74 compilation, and errors/warnings, to be done at compile time.
77 A list, or a symbol, which specifies the relationship between this
78 test and other tests. These conditions, `AND`, `OR` and `NOT` can be
79 combined to produce complex dependencies (whethere this is something
80 you should actually be doing is a question for another day).
82 `(and &rest TEST-NAMES)`:::
83 This test is run only if all of the tests in TEST-NAMES have
84 passed, otherwise a single test-skipped result is generated.
86 `(or &rest TEST-NAMES)`:::
87 If any of TEST-NAMES has passed this test is run, otherwise a
88 test-skipped result is generated.
91 This is test is run only if TEST-NAME failed.
94 Shorthand for `(AND a-symbol)`
97 When non-`NIL` profiling information will be collected as well."
98 (check-type compile-at (member :run-time :definition-time))
99 (multiple-value-bind (forms decls docstring)
100 (parse-body body :documentation t :whole name)
101 (let* ((description (or docstring ""))
102 (body-forms (append decls forms))
103 (suite-form (if suite-p
108 (effective-body (let* ((test-fixture fixture)
109 (suite-fixture (if suite-p
111 (fixture (get-test suite :error t))
116 (effective-fixture (or test-fixture suite-fixture)))
117 (if effective-fixture
118 (destructuring-bind (name &rest args)
119 (ensure-list effective-fixture)
120 `((with-fixture ,name ,args ,@body-forms)))
123 (register-test :name ',name
124 :description ,description
125 :body ',effective-body
127 :depends-on ',depends-on
128 :compile-at ,compile-at
130 (when *run-test-when-defined*
134 (defun register-test (&key name description body suite depends-on compile-at profile)
135 (remove-from-suites name)
137 (format-symbol t "%~A-~A" '#:test name))
139 (format-symbol t "%~A-~A" '#:inner-test name)))
140 (setf (get-test name)
141 (make-instance 'test-case
143 :runtime-package (find-package (package-name *package*))
146 `(named-lambda ,lambda-name ()
148 (:run-time `((funcall
149 (let ((*package* (find-package ',(package-name *package*))))
150 (compile ',inner-lambda-name
151 '(lambda () ,@body))))))
152 (:definition-time body))))
153 :description description
154 :depends-on depends-on
155 :collect-profiling-info profile))
157 (setf (gethash name (tests (get-test suite :error t))) name))))
159 (defvar *run-test-when-defined* nil
160 "When non-NIL tests are run as soon as they are defined.")
162 ;; Copyright (c) 2002-2003, Edward Marco Baringer
163 ;; All rights reserved.
165 ;; Redistribution and use in source and binary forms, with or without
166 ;; modification, are permitted provided that the following conditions are
169 ;; - Redistributions of source code must retain the above copyright
170 ;; notice, this list of conditions and the following disclaimer.
172 ;; - Redistributions in binary form must reproduce the above copyright
173 ;; notice, this list of conditions and the following disclaimer in the
174 ;; documentation and/or other materials provided with the distribution.
176 ;; - Neither the name of Edward Marco Baringer, nor BESE, nor the names
177 ;; of its contributors may be used to endorse or promote products
178 ;; derived from this software without specific prior written permission.
180 ;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
181 ;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182 ;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
183 ;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
184 ;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
185 ;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
186 ;; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
187 ;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
188 ;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
189 ;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
190 ;; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.