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