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