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