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