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 "The current test suite object")
18 (make-hash-table :test 'eql)
19 "Lookup table mapping test (and test suite)
22 (defun get-test (key &optional default)
23 (gethash key *test* default))
25 (defun (setf get-test) (value key)
26 (setf (gethash key *test*) value))
32 (loop for test being the hash-keys of *test*
35 (defmacro test (name &body body)
36 "Create a test named NAME. If NAME is a list it must be of the
39 (name &key depends-on suite fixture compile-at profile)
41 NAME is the symbol which names the test.
43 DEPENDS-ON is a list of the form:
45 (AND . test-names) - This test is run only if all of the tests
46 in TEST-NAMES have passed, otherwise a single test-skipped
49 (OR . test-names) - If any of TEST-NAMES has passed this test is
50 run, otherwise a test-skipped result is generated.
52 (NOT test-name) - This is test is run only if TEST-NAME failed.
54 AND, OR and NOT can be combined to produce complex dependencies.
56 If DEPENDS-ON is a symbol it is interpreted as `(AND
57 ,depends-on), this is accomadate the common case of one test
60 FIXTURE specifies a fixture to wrap the body in.
62 If PROFILE is T profiling information will be collected as well."
63 (destructuring-bind (name &rest args)
65 `(def-test ,name (,@args) ,@body)))
67 (defmacro def-test (name (&key depends-on (suite '*suite* suite-p) fixture
68 (compile-at :run-time) profile)
70 "Create a test named NAME.
72 NAME is the symbol which names the test.
74 DEPENDS-ON is a list of the form:
76 (AND . test-names) - This test is run only if all of the tests
77 in TEST-NAMES have passed, otherwise a single test-skipped
80 (OR . test-names) - If any of TEST-NAMES has passed this test is
81 run, otherwise a test-skipped result is generated.
83 (NOT test-name) - This is test is run only if TEST-NAME failed.
85 AND, OR and NOT can be combined to produce complex dependencies.
87 If DEPENDS-ON is a symbol it is interpreted as `(AND
88 ,depends-on), this is accomadate the common case of one test
91 FIXTURE specifies a fixture to wrap the body in.
93 If PROFILE is T profiling information will be collected as well."
94 (check-type compile-at (member :run-time :definition-time))
95 (multiple-value-bind (forms decls docstring)
96 (parse-body body :documentation t :whole name)
97 (let* ((description (or docstring ""))
98 (body-forms (append decls forms))
99 (suite-form (if suite-p
101 (or suite '*suite*)))
102 (effective-body (if fixture
103 (destructuring-bind (name &rest args)
104 (ensure-list fixture)
105 `((with-fixture ,name ,args ,@body-forms)))
108 (register-test ',name ,description ',effective-body ,suite-form ',depends-on ,compile-at ,profile)
109 (when *run-test-when-defined*
113 (defun register-test (name description body suite depends-on compile-at profile)
115 (format-symbol t "%~A-~A" '#:test name))
117 (format-symbol t "%~A-~A" '#:inner-test name)))
118 (setf (get-test name)
119 (make-instance 'test-case
121 :runtime-package (find-package (package-name *package*))
124 `(named-lambda ,lambda-name ()
126 (:run-time `((funcall
127 (let ((*package* (find-package ',(package-name *package*))))
128 (compile ',inner-lambda-name
129 '(lambda () ,@body))))))
130 (:definition-time body))))
131 :description description
132 :depends-on depends-on
133 :collect-profiling-info profile))
134 (setf (gethash name (tests suite)) name)))
136 (defvar *run-test-when-defined* nil
137 "When non-NIL tests are run as soon as they are defined.")
139 ;; Copyright (c) 2002-2003, Edward Marco Baringer
140 ;; All rights reserved.
142 ;; Redistribution and use in source and binary forms, with or without
143 ;; modification, are permitted provided that the following conditions are
146 ;; - Redistributions of source code must retain the above copyright
147 ;; notice, this list of conditions and the following disclaimer.
149 ;; - Redistributions in binary form must reproduce the above copyright
150 ;; notice, this list of conditions and the following disclaimer in the
151 ;; documentation and/or other materials provided with the distribution.
153 ;; - Neither the name of Edward Marco Baringer, nor BESE, nor the names
154 ;; of its contributors may be used to endorse or promote products
155 ;; derived from this software without specific prior written permission.
157 ;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
158 ;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
159 ;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
160 ;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
161 ;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
162 ;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
163 ;; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
164 ;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
165 ;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
166 ;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
167 ;; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.