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)
44 (destructuring-bind (name &rest args)
46 `(def-test ,name (,@args) ,@body)))
48 (defmacro def-test (name (&key (suite nil suite-p)
50 (compile-at :run-time)
54 "Create a test named NAME.
60 The suite to put the test under. It defaults to *SUITE* (which
61 itself defaults to the default global suite).
64 The name of the fixture to use for this test. See `WITH-FIXTURE` for
67 COMPILE-AT (a keyword)::
68 When the body of this test should be compiled. By default, or when
69 `:compile-at` is `:run-time`, test bodies are only compiled before
70 they are run. Set this to to `:definition-time` to force
71 compilation, and errors/warnings, to be done at compile time.
74 A list, or a symbol, which specifies the relationship between this
75 test and other tests. These conditions, `AND`, `OR` and `NOT` can be
76 combined to produce complex dependencies (whethere this is something
77 you should actually be doing is a question for another day).
79 `(and &rest TEST-NAMES)`:::
80 This test is run only if all of the tests in TEST-NAMES have
81 passed, otherwise a single test-skipped result is generated.
83 `(or &rest TEST-NAMES)`:::
84 If any of TEST-NAMES has passed this test is run, otherwise a
85 test-skipped result is generated.
88 This is test is run only if TEST-NAME failed.
91 Shorthand for `(AND a-symbol)`
94 When non-`NIL` profiling information will be collected as well."
95 (check-type compile-at (member :run-time :definition-time))
96 (multiple-value-bind (forms decls docstring)
97 (parse-body body :documentation t :whole name)
98 (let* ((description (or docstring ""))
99 (body-forms (append decls forms))
100 (suite-form (if suite-p
105 (effective-body (let* ((test-fixture fixture)
106 (suite-fixture (if suite-p
108 (fixture (get-test suite :error t))
113 (effective-fixture (or test-fixture suite-fixture)))
114 (if effective-fixture
115 (destructuring-bind (name &rest args)
116 (ensure-list effective-fixture)
117 `((with-fixture ,name ,args ,@body-forms)))
120 (register-test :name ',name
121 :description ,description
122 :body ',effective-body
124 :depends-on ',depends-on
125 :compile-at ,compile-at
127 (when *run-test-when-defined*
131 (defun register-test (&key name description body suite depends-on compile-at profile)
132 (remove-from-suites name)
134 (format-symbol t "%~A-~A" '#:test name))
136 (format-symbol t "%~A-~A" '#:inner-test name)))
137 (setf (get-test name)
138 (make-instance 'test-case
140 :runtime-package (find-package (package-name *package*))
143 `(named-lambda ,lambda-name ()
145 (:run-time `((funcall
146 (let ((*package* (find-package ',(package-name *package*))))
147 (compile ',inner-lambda-name
148 '(lambda () ,@body))))))
149 (:definition-time body))))
150 :description description
151 :depends-on depends-on
152 :collect-profiling-info profile))
154 (setf (gethash name (tests (get-test suite :error t))) name))))
156 (defvar *run-test-when-defined* nil
157 "When non-NIL tests are run as soon as they are defined.")
159 ;; Copyright (c) 2002-2003, Edward Marco Baringer
160 ;; All rights reserved.
162 ;; Redistribution and use in source and binary forms, with or without
163 ;; modification, are permitted provided that the following conditions are
166 ;; - Redistributions of source code must retain the above copyright
167 ;; notice, this list of conditions and the following disclaimer.
169 ;; - Redistributions in binary form must reproduce the above copyright
170 ;; notice, this list of conditions and the following disclaimer in the
171 ;; documentation and/or other materials provided with the distribution.
173 ;; - Neither the name of Edward Marco Baringer, nor BESE, nor the names
174 ;; of its contributors may be used to endorse or promote products
175 ;; derived from this software without specific prior written permission.
177 ;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
178 ;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
179 ;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
180 ;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
181 ;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
182 ;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
183 ;; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
184 ;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
185 ;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
186 ;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
187 ;; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.