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 (suite nil suite-p) fixture
51 (compile-at :run-time) profile)
53 "Create a test named NAME.
55 NAME is the symbol which names the test.
57 DEPENDS-ON is a list of the form:
59 \(AND . test-names) - This test is run only if all of the tests
60 in TEST-NAMES have passed, otherwise a single test-skipped
63 \(OR . test-names) - If any of TEST-NAMES has passed this test is
64 run, otherwise a test-skipped result is generated.
66 \(NOT test-name) - This is test is run only if TEST-NAME failed.
68 AND, OR and NOT can be combined to produce complex dependencies.
70 If DEPENDS-ON is a symbol it is interpreted as `(AND
71 ,depends-on), this is accomadate the common case of one test
74 SUITE is the suite to put the test under. It defaults to
75 *SUITE* (which itself defaults to the default global suite).
77 FIXTURE specifies a fixture to wrap the body in.
79 If PROFILE is T profiling information will be collected as well.
81 COMPILE-AT can be either :RUN-TIME, in which case compilation of the
82 test code will be delayed until the test is run, or :DEFINITION-TIME,
83 in which case the code will be compiled when the DEF-TEST form itself
85 (check-type compile-at (member :run-time :definition-time))
86 (multiple-value-bind (forms decls docstring)
87 (parse-body body :documentation t :whole name)
88 (let* ((description (or docstring ""))
89 (body-forms (append decls forms))
90 (suite-form (if suite-p
93 (effective-body (let* ((test-fixture fixture)
94 (suite-fixture (if suite-p
95 (fixture (get-test suite))
97 (effective-fixture (or test-fixture suite-fixture)))
99 (destructuring-bind (name &rest args)
100 (ensure-list effective-fixture)
101 `((with-fixture ,name ,args ,@body-forms)))
104 (register-test :name ',name
105 :description ,description
106 :body ',effective-body
108 :depends-on ',depends-on
109 :compile-at ,compile-at
111 (when *run-test-when-defined*
115 (defun register-test (&key name description body suite depends-on compile-at profile)
117 (format-symbol t "%~A-~A" '#:test name))
119 (format-symbol t "%~A-~A" '#:inner-test name)))
120 (setf (get-test name)
121 (make-instance 'test-case
123 :runtime-package (find-package (package-name *package*))
126 `(named-lambda ,lambda-name ()
128 (:run-time `((funcall
129 (let ((*package* (find-package ',(package-name *package*))))
130 (compile ',inner-lambda-name
131 '(lambda () ,@body))))))
132 (:definition-time body))))
133 :description description
134 :depends-on depends-on
135 :collect-profiling-info profile))
136 (setf (gethash name (tests suite)) name)))
138 (defvar *run-test-when-defined* nil
139 "When non-NIL tests are run as soon as they are defined.")
141 ;; Copyright (c) 2002-2003, Edward Marco Baringer
142 ;; All rights reserved.
144 ;; Redistribution and use in source and binary forms, with or without
145 ;; modification, are permitted provided that the following conditions are
148 ;; - Redistributions of source code must retain the above copyright
149 ;; notice, this list of conditions and the following disclaimer.
151 ;; - Redistributions in binary form must reproduce the above copyright
152 ;; notice, this list of conditions and the following disclaimer in the
153 ;; documentation and/or other materials provided with the distribution.
155 ;; - Neither the name of Edward Marco Baringer, nor BESE, nor the names
156 ;; of its contributors may be used to endorse or promote products
157 ;; derived from this software without specific prior written permission.
159 ;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
160 ;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
161 ;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
162 ;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
163 ;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
164 ;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
165 ;; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
166 ;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
167 ;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
168 ;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
169 ;; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.