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