Don't signal a warning when the old TEST macro is used
[fiveam.git] / src / test.lisp
1 ;;;; -*- Mode: Lisp; indent-tabs-mode: nil -*-
2
3 (in-package :it.bese.fiveam)
4
5 ;;;; * Tests
6
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.
13
14 (defvar *test*
15   (make-hash-table :test 'eql)
16   "Lookup table mapping test (and test suite)
17   names to objects.")
18
19 (defun get-test (key &optional default)
20   (gethash key *test* default))
21
22 (defun (setf get-test) (value key)
23   (setf (gethash key *test*) value))
24
25 (defun rem-test (key)
26   (remhash key *test*))
27
28 (defun test-names ()
29   (loop for test being the hash-keys of *test*
30         collect test))
31
32 (defmacro test (name &body body)
33   "Create a test named NAME. If NAME is a list it must be of the
34 form:
35
36   (name &key depends-on suite fixture compile-at profile)
37
38 NAME is the symbol which names the test.
39
40 DEPENDS-ON is a list of the form:
41
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
44  result is generated.
45
46  (OR . test-names) - If any of TEST-NAMES has passed this test is
47  run, otherwise a test-skipped result is generated.
48
49  (NOT test-name) - This is test is run only if TEST-NAME failed.
50
51 AND, OR and NOT can be combined to produce complex dependencies.
52
53 If DEPENDS-ON is a symbol it is interpreted as `(AND
54 ,depends-on), this is accomadate the common case of one test
55 depending on another.
56
57 FIXTURE specifies a fixture to wrap the body in.
58
59 If PROFILE is T profiling information will be collected as well."
60   (destructuring-bind (name &rest args)
61       (ensure-list name)
62     `(def-test ,name (,@args) ,@body)))
63
64 (defmacro def-test (name (&key depends-on (suite '*suite* suite-p) fixture
65                             (compile-at :run-time) profile)
66                     &body body)
67   "Create a test named NAME.
68
69 NAME is the symbol which names the test.
70
71 DEPENDS-ON is a list of the form:
72
73  (AND . test-names) - This test is run only if all of the tests
74  in TEST-NAMES have passed, otherwise a single test-skipped
75  result is generated.
76
77  (OR . test-names) - If any of TEST-NAMES has passed this test is
78  run, otherwise a test-skipped result is generated.
79
80  (NOT test-name) - This is test is run only if TEST-NAME failed.
81
82 AND, OR and NOT can be combined to produce complex dependencies.
83
84 If DEPENDS-ON is a symbol it is interpreted as `(AND
85 ,depends-on), this is accomadate the common case of one test
86 depending on another.
87
88 FIXTURE specifies a fixture to wrap the body in.
89
90 If PROFILE is T profiling information will be collected as well."
91   (check-type compile-at (member :run-time :definition-time))
92   (multiple-value-bind (forms decls docstring)
93       (parse-body body :documentation t :whole name)
94     (let* ((description (or docstring ""))
95            (body-forms (append decls forms))
96            (suite-form (if suite-p
97                            `(get-test ',suite)
98                            (or suite '*suite*)))
99            (effective-body (if fixture
100                                (destructuring-bind (name &rest args)
101                                    (ensure-list fixture)
102                                  `((with-fixture ,name ,args ,@body-forms)))
103                                body-forms)))
104       `(progn
105          (register-test ',name ,description ',effective-body ,suite-form ',depends-on ,compile-at ,profile)
106          (when *run-test-when-defined*
107            (run! ',name))
108          ',name))))
109
110 (defun register-test (name description body suite depends-on compile-at profile)
111   (let ((lambda-name
112           (format-symbol t "%~A-~A" '#:test name))
113         (inner-lambda-name
114           (format-symbol t "%~A-~A" '#:inner-test name)))
115     (setf (get-test name)
116           (make-instance 'test-case
117                          :name name
118                          :runtime-package (find-package (package-name *package*))
119                          :test-lambda
120                          (eval
121                           `(named-lambda ,lambda-name ()
122                              ,@(ecase compile-at
123                                  (:run-time `((funcall
124                                                (let ((*package* (find-package ',(package-name *package*))))
125                                                  (compile ',inner-lambda-name
126                                                           '(lambda () ,@body))))))
127                                  (:definition-time body))))
128                          :description description
129                          :depends-on depends-on
130                          :collect-profiling-info profile))
131     (setf (gethash name (tests suite)) name)))
132
133 (defvar *run-test-when-defined* nil
134   "When non-NIL tests are run as soon as they are defined.")
135
136 ;; Copyright (c) 2002-2003, Edward Marco Baringer
137 ;; All rights reserved.
138 ;;
139 ;; Redistribution and use in source and binary forms, with or without
140 ;; modification, are permitted provided that the following conditions are
141 ;; met:
142 ;;
143 ;;  - Redistributions of source code must retain the above copyright
144 ;;    notice, this list of conditions and the following disclaimer.
145 ;;
146 ;;  - Redistributions in binary form must reproduce the above copyright
147 ;;    notice, this list of conditions and the following disclaimer in the
148 ;;    documentation and/or other materials provided with the distribution.
149 ;;
150 ;;  - Neither the name of Edward Marco Baringer, nor BESE, nor the names
151 ;;    of its contributors may be used to endorse or promote products
152 ;;    derived from this software without specific prior written permission.
153 ;;
154 ;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
155 ;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
156 ;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
157 ;; A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
158 ;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
159 ;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
160 ;; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
161 ;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
162 ;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
163 ;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
164 ;; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.