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)
19 (defun get-test (key &key default error)
20 "Finds the test named KEY. If KEY is a testable-object (a test case
21 or a test suite) then we just return KEY, otherwise we look for a test
22 named KEY in the *TEST* hash table."
23 (if (testable-object-p key)
25 (multiple-value-bind (value foundp)
30 (error "Unable to find test named ~S." key)
33 (defun (setf get-test) (value key)
34 (setf (gethash key *test*) value))
40 (loop for test being the hash-keys of *test*
43 (defmacro test (name &body body)
44 "Deprecated. See DEF-TEST."
45 (simple-style-warning "~A is OBSOLETE! Use ~A instead." 'test 'def-test)
46 (destructuring-bind (name &rest args)
48 `(def-test ,name (,@args) ,@body)))
50 (defmacro def-test (name (&key depends-on
53 (compile-at :run-time)
56 "Create a test named NAME.
58 NAME is the symbol which names the test.
60 DEPENDS-ON is a list of the form:
62 \(AND . test-names) - This test is run only if all of the tests
63 in TEST-NAMES have passed, otherwise a single test-skipped
66 \(OR . test-names) - If any of TEST-NAMES has passed this test is
67 run, otherwise a test-skipped result is generated.
69 \(NOT test-name) - This is test is run only if TEST-NAME failed.
71 AND, OR and NOT can be combined to produce complex dependencies.
73 If DEPENDS-ON is a symbol it is interpreted as `(AND
74 ,depends-on), this is accomadate the common case of one test
77 SUITE is the suite to put the test under. It defaults to
78 *SUITE* (which itself defaults to the default global suite).
80 FIXTURE specifies a fixture to wrap the body in.
82 If PROFILE is T profiling information will be collected as well.
84 COMPILE-AT can be either :RUN-TIME, in which case compilation of the
85 test code will be delayed until the test is run, or :DEFINITION-TIME,
86 in which case the code will be compiled when the DEF-TEST form itself
88 (check-type compile-at (member :run-time :definition-time))
89 (multiple-value-bind (forms decls docstring)
90 (parse-body body :documentation t :whole name)
91 (let* ((description (or docstring ""))
92 (body-forms (append decls forms))
93 (suite-form (if suite-p
98 (effective-body (let* ((test-fixture fixture)
99 (suite-fixture (if suite-p
101 (fixture (get-test suite :error t))
106 (effective-fixture (or test-fixture suite-fixture)))
107 (if effective-fixture
108 (destructuring-bind (name &rest args)
109 (ensure-list effective-fixture)
110 `((with-fixture ,name ,args ,@body-forms)))
113 (register-test :name ',name
114 :description ,description
115 :body ',effective-body
117 :depends-on ',depends-on
118 :compile-at ,compile-at
120 (when *run-test-when-defined*
124 (defun register-test (&key name description body suite depends-on compile-at profile)
125 (remove-from-suites name)
127 (format-symbol t "%~A-~A" '#:test name))
129 (format-symbol t "%~A-~A" '#:inner-test name)))
130 (setf (get-test name)
131 (make-instance 'test-case
133 :runtime-package (find-package (package-name *package*))
136 `(named-lambda ,lambda-name ()
138 (:run-time `((funcall
139 (let ((*package* (find-package ',(package-name *package*))))
140 (compile ',inner-lambda-name
141 '(lambda () ,@body))))))
142 (:definition-time body))))
143 :description description
144 :depends-on depends-on
145 :collect-profiling-info profile))
147 (setf (gethash name (tests (get-test suite :error t))) name))))
149 (defvar *run-test-when-defined* nil
150 "When non-NIL tests are run as soon as they are defined.")
152 ;; Copyright (c) 2002-2003, Edward Marco Baringer
153 ;; All rights reserved.
155 ;; Redistribution and use in source and binary forms, with or without
156 ;; modification, are permitted provided that the following conditions are
159 ;; - Redistributions of source code must retain the above copyright
160 ;; notice, this list of conditions and the following disclaimer.
162 ;; - Redistributions in binary form must reproduce the above copyright
163 ;; notice, this list of conditions and the following disclaimer in the
164 ;; documentation and/or other materials provided with the distribution.
166 ;; - Neither the name of Edward Marco Baringer, nor BESE, nor the names
167 ;; of its contributors may be used to endorse or promote products
168 ;; derived from this software without specific prior written permission.
170 ;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
171 ;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
172 ;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
173 ;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
174 ;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
175 ;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
176 ;; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
177 ;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
178 ;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
179 ;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
180 ;; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.