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