Shut up warnings about unknown *SUITE* variable.
[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) names to objects.")
17
18 (defun get-test (key &key default error)
19   "Finds the test named KEY. If KEY is a testable-object (a test case
20 or a test suite) then we just return KEY, otherwise we look for a test
21 named KEY in the *TEST* hash table."
22   (if (testable-object-p key)
23       key
24       (multiple-value-bind (value foundp)
25           (gethash key *test*)
26         (if foundp
27             value
28             (if error
29                 (error "Unable to find test named ~S." key)
30                 default)))))
31
32 (defun (setf get-test) (value key)
33   (setf (gethash key *test*) value))
34
35 (defun rem-test (key)
36   (remhash key *test*))
37
38 (defun test-names ()
39   (loop for test being the hash-keys of *test*
40         collect test))
41
42 (defmacro test (name &body body)
43   "Alias for DEF-TEST."
44   (destructuring-bind (name &rest args)
45       (ensure-list name)
46     `(def-test ,name (,@args) ,@body)))
47
48 (declaim (special *suite*))
49
50 (defmacro def-test (name (&key (suite nil suite-p)
51                                fixture
52                                (compile-at :run-time)
53                                depends-on
54                                profile)
55                     &body body)
56   "Create a test named NAME.
57
58 NAME (a symbol)::
59   The name of the test.
60
61 SUITE (a test name)::
62   The suite to put the test under. It defaults to *SUITE* (which
63   itself defaults to the default global suite).
64
65 FIXTURE::
66   The name of the fixture to use for this test. See `WITH-FIXTURE` for
67   details on fixtures.
68
69 COMPILE-AT (a keyword)::
70   When the body of this test should be compiled. By default, or when
71   `:compile-at` is `:run-time`, test bodies are only compiled before
72   they are run. Set this to to `:definition-time` to force
73   compilation, and errors/warnings, to be done at compile time.
74
75 DEPENDS-ON::
76   A list, or a symbol, which specifies the relationship between this
77   test and other tests. These conditions, `AND`, `OR` and `NOT` can be
78   combined to produce complex dependencies (whethere this is something
79   you should actually be doing is a question for another day).
80
81   `(and &rest TEST-NAMES)`:::
82     This test is run only if all of the tests in TEST-NAMES have
83     passed, otherwise a single test-skipped result is generated.
84
85   `(or &rest TEST-NAMES)`:::
86     If any of TEST-NAMES has passed this test is run, otherwise a
87     test-skipped result is generated.
88
89   `(NOT TEST-NAME`:::
90     This is test is run only if TEST-NAME failed.
91
92   __a-symbol__:::
93     Shorthand for `(AND a-symbol)`
94
95 PROFILE::
96   When non-`NIL` profiling information will be collected as well."
97   (check-type compile-at (member :run-time :definition-time))
98   (multiple-value-bind (forms decls docstring)
99       (parse-body body :documentation t :whole name)
100     (let* ((description (or docstring ""))
101            (body-forms (append decls forms))
102            (suite-form (if suite-p
103                            (if suite
104                                `(get-test ',suite)
105                                nil)
106                            '*suite*))
107            (effective-body (let* ((test-fixture fixture)
108                                   (suite-fixture (if suite-p
109                                                      (if suite
110                                                          (fixture (get-test suite :error t))
111                                                          nil)
112                                                      (if *suite*
113                                                          (fixture *suite*)
114                                                          nil)))
115                                   (effective-fixture (or test-fixture suite-fixture)))
116                              (if effective-fixture
117                                  (destructuring-bind (name &rest args)
118                                      (ensure-list effective-fixture)
119                                    `((with-fixture ,name ,args ,@body-forms)))
120                                  body-forms))))
121       `(progn
122          (register-test :name ',name
123                         :description ,description
124                         :body ',effective-body
125                         :suite ,suite-form
126                         :depends-on ',depends-on
127                         :compile-at ,compile-at
128                         :profile ,profile)
129          (when *run-test-when-defined*
130            (run! ',name))
131          ',name))))
132
133 (defun register-test (&key name description body suite depends-on compile-at profile)
134   (remove-from-suites name)
135   (let ((lambda-name
136           (format-symbol t "%~A-~A" '#:test name))
137         (inner-lambda-name
138           (format-symbol t "%~A-~A" '#:inner-test name)))
139     (setf (get-test name)
140           (make-instance 'test-case
141                          :name name
142                          :runtime-package (find-package (package-name *package*))
143                          :test-lambda
144                          (eval
145                           `(named-lambda ,lambda-name ()
146                              ,@(ecase compile-at
147                                  (:run-time `((funcall
148                                                (let ((*package* (find-package ',(package-name *package*))))
149                                                  (compile ',inner-lambda-name
150                                                           '(lambda () ,@body))))))
151                                  (:definition-time body))))
152                          :description description
153                          :depends-on depends-on
154                          :collect-profiling-info profile))
155     (when suite
156       (setf (gethash name (tests (get-test suite :error t))) name))))
157
158 (defvar *run-test-when-defined* nil
159   "When non-NIL tests are run as soon as they are defined.")
160
161 ;; Copyright (c) 2002-2003, Edward Marco Baringer
162 ;; All rights reserved.
163 ;;
164 ;; Redistribution and use in source and binary forms, with or without
165 ;; modification, are permitted provided that the following conditions are
166 ;; met:
167 ;;
168 ;;  - Redistributions of source code must retain the above copyright
169 ;;    notice, this list of conditions and the following disclaimer.
170 ;;
171 ;;  - Redistributions in binary form must reproduce the above copyright
172 ;;    notice, this list of conditions and the following disclaimer in the
173 ;;    documentation and/or other materials provided with the distribution.
174 ;;
175 ;;  - Neither the name of Edward Marco Baringer, nor BESE, nor the names
176 ;;    of its contributors may be used to endorse or promote products
177 ;;    derived from this software without specific prior written permission.
178 ;;
179 ;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
180 ;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
181 ;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
182 ;; A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
183 ;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
184 ;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
185 ;; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
186 ;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
187 ;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
188 ;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
189 ;; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.