e4ca2d853e0666de7cd5ed0b4ac2144f404567f0
[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 (if fixture
83                                (destructuring-bind (name &rest args)
84                                    (ensure-list fixture)
85                                  `((with-fixture ,name ,args ,@body-forms)))
86                                body-forms)))
87       `(progn
88          (register-test ',name ,description ',effective-body ,suite-form ',depends-on ,compile-at ,profile)
89          (when *run-test-when-defined*
90            (run! ',name))
91          ',name))))
92
93 (defun register-test (name description body suite depends-on compile-at profile)
94   (let ((lambda-name
95           (format-symbol t "%~A-~A" '#:test name))
96         (inner-lambda-name
97           (format-symbol t "%~A-~A" '#:inner-test name)))
98     (setf (get-test name)
99           (make-instance 'test-case
100                          :name name
101                          :runtime-package (find-package (package-name *package*))
102                          :test-lambda
103                          (eval
104                           `(named-lambda ,lambda-name ()
105                              ,@(ecase compile-at
106                                  (:run-time `((funcall
107                                                (let ((*package* (find-package ',(package-name *package*))))
108                                                  (compile ',inner-lambda-name
109                                                           '(lambda () ,@body))))))
110                                  (:definition-time body))))
111                          :description description
112                          :depends-on depends-on
113                          :collect-profiling-info profile))
114     (setf (gethash name (tests suite)) name)))
115
116 (defvar *run-test-when-defined* nil
117   "When non-NIL tests are run as soon as they are defined.")
118
119 ;; Copyright (c) 2002-2003, Edward Marco Baringer
120 ;; All rights reserved.
121 ;;
122 ;; Redistribution and use in source and binary forms, with or without
123 ;; modification, are permitted provided that the following conditions are
124 ;; met:
125 ;;
126 ;;  - Redistributions of source code must retain the above copyright
127 ;;    notice, this list of conditions and the following disclaimer.
128 ;;
129 ;;  - Redistributions in binary form must reproduce the above copyright
130 ;;    notice, this list of conditions and the following disclaimer in the
131 ;;    documentation and/or other materials provided with the distribution.
132 ;;
133 ;;  - Neither the name of Edward Marco Baringer, nor BESE, nor the names
134 ;;    of its contributors may be used to endorse or promote products
135 ;;    derived from this software without specific prior written permission.
136 ;;
137 ;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
138 ;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
139 ;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
140 ;; A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
141 ;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
142 ;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
143 ;; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
144 ;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
145 ;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
146 ;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
147 ;; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.