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 "Create a test named NAME. If NAME is a list it must be of the
36 (name &key depends-on suite fixture compile-at profile)
38 NAME is the symbol which names the test.
40 DEPENDS-ON is a list of the form:
42 (AND . test-names) - This test is run only if all of the tests
43 in TEST-NAMES have passed, otherwise a single test-skipped
46 (OR . test-names) - If any of TEST-NAMES has passed this test is
47 run, otherwise a test-skipped result is generated.
49 (NOT test-name) - This is test is run only if TEST-NAME failed.
51 AND, OR and NOT can be combined to produce complex dependencies.
53 If DEPENDS-ON is a symbol it is interpreted as `(AND
54 ,depends-on), this is accomadate the common case of one test
57 FIXTURE specifies a fixture to wrap the body in.
59 If PROFILE is T profiling information will be collected as well."
60 (simple-style-warning "~A is OBSOLETE! Use ~A instead."
62 (destructuring-bind (name &rest args)
64 `(def-test ,name (,@args) ,@body)))
66 (defmacro def-test (name (&key depends-on (suite '*suite* suite-p) fixture
67 (compile-at :run-time) profile)
69 "Create a test named NAME.
71 NAME is the symbol which names the test.
73 DEPENDS-ON is a list of the form:
75 (AND . test-names) - This test is run only if all of the tests
76 in TEST-NAMES have passed, otherwise a single test-skipped
79 (OR . test-names) - If any of TEST-NAMES has passed this test is
80 run, otherwise a test-skipped result is generated.
82 (NOT test-name) - This is test is run only if TEST-NAME failed.
84 AND, OR and NOT can be combined to produce complex dependencies.
86 If DEPENDS-ON is a symbol it is interpreted as `(AND
87 ,depends-on), this is accomadate the common case of one test
90 FIXTURE specifies a fixture to wrap the body in.
92 If PROFILE is T profiling information will be collected as well."
96 (or suite '*suite*))))
97 (check-type compile-at (member :run-time :definition-time))
98 (let ((description (if (stringp (car body))
101 (effective-body (if fixture
102 (destructuring-bind (name &rest args)
103 (ensure-list fixture)
104 `((with-fixture ,name ,args ,@body)))
107 (format-symbol t "%~A-~A" '#:test name))
109 (format-symbol t "%~A-~A" '#:inner-test name)))
111 (setf (get-test ',name)
112 (make-instance 'test-case
114 :runtime-package (find-package ,(package-name *package*))
116 (named-lambda ,lambda-name ()
118 (:run-time `((funcall
119 (let ((*package* (find-package ',(package-name *package*))))
120 (compile ',inner-lambda-name
121 '(lambda () ,@effective-body))))))
122 (:definition-time effective-body)))
123 :description ,description
124 :depends-on ',depends-on
125 :collect-profiling-info ,profile))
126 (setf (gethash ',name (tests ,suite-form)) ',name)
127 (when *run-test-when-defined*
131 (defvar *run-test-when-defined* nil
132 "When non-NIL tests are run as soon as they are defined.")
134 ;; Copyright (c) 2002-2003, Edward Marco Baringer
135 ;; All rights reserved.
137 ;; Redistribution and use in source and binary forms, with or without
138 ;; modification, are permitted provided that the following conditions are
141 ;; - Redistributions of source code must retain the above copyright
142 ;; notice, this list of conditions and the following disclaimer.
144 ;; - Redistributions in binary form must reproduce the above copyright
145 ;; notice, this list of conditions and the following disclaimer in the
146 ;; documentation and/or other materials provided with the distribution.
148 ;; - Neither the name of Edward Marco Baringer, nor BESE, nor the names
149 ;; of its contributors may be used to endorse or promote products
150 ;; derived from this software without specific prior written permission.
152 ;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
153 ;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
154 ;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
155 ;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
156 ;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
157 ;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
158 ;; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
159 ;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
160 ;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
161 ;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
162 ;; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.