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