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