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