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