e046859f304c05782da044ad3518fc048971ef95
[fiveam.git] / src / test.lisp
1 ;; -*- lisp -*-
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 (deflookup-table test
15   :at-redefinition nil
16   :documentation "Lookup table mapping test (and test suite)
17   names to objects.")
18
19 (defun test-names ()
20   (loop for test being the hash-keys of *test*
21         collect test))
22
23 (defmacro test (name &body body)
24   "Create a test named NAME. If NAME is a list it must be of the
25 form:
26
27   (name &key depends-on suite fixture compile-at profile)
28
29 NAME is the symbol which names the test.
30
31 DEPENDS-ON is a list of the form:
32
33  (AND . test-names) - This test is run only if all of the tests
34  in TEST-NAMES have passed, otherwise a single test-skipped
35  result is generated.
36
37  (OR . test-names) - If any of TEST-NAMES has passed this test is
38  run, otherwise a test-skipped result is generated.
39
40  (NOT test-name) - This is test is run only if TEST-NAME failed.
41
42 AND, OR and NOT can be combined to produce complex dependencies.
43
44 If DEPENDS-ON is a symbol it is interpreted as `(AND
45 ,depends-on), this is accomadate the common case of one test
46 depending on another.
47
48 FIXTURE specifies a fixtrue to wrap the body in.
49
50 If PROFILE is T profiling information will be collected as well."
51   (let* ((tmp (gensym))
52          (suite-arg (getf (cdr (ensure-list name)) :suite tmp))
53          (suite-form (cond
54                        ((eq tmp suite-arg) '*suite*)
55                        (t                  `(get-test ',suite-arg)))))
56     (when (consp name)
57       (remf (cdr name) :suite))
58     (destructuring-bind (name &key depends-on (compile-at :run-time) fixture profile)
59         (ensure-list name)
60       (declare (type (member :run-time :definition-time) compile-at))
61       (let ((description (if (stringp (car body))
62                              (pop body)
63                              ""))
64             (effective-body (if fixture
65                                 (destructuring-bind (name &rest args)
66                                     (ensure-list fixture)
67                                   `((with-fixture ,name ,args ,@body)))
68                                 body)))
69         `(progn
70            (setf (get-test ',name)
71                  (make-instance 'test-case
72                                 :name ',name
73                                 :runtime-package (find-package ,(package-name *package*))
74                                 :test-lambda
75                                 (lambda ()
76                                   ,@ (ecase compile-at
77                                        (:run-time `((funcall
78                                                      (let ((*package* (find-package ',(package-name *package*))))
79                                                        (compile nil '(lambda ()
80                                                                       ,@effective-body))))))
81                                        (:definition-time effective-body)))
82                                 :description ,description
83                                 :depends-on ',depends-on
84                                 :collect-profiling-info ,profile))
85            (setf (gethash ',name (tests ,suite-form)) ',name)
86            (when *run-test-when-defined*
87              (run! ',name))
88            ',name)))))
89
90 (defvar *run-test-when-defined* nil
91   "When non-NIL tests are run as soon as they are defined.")
92
93 ;; Copyright (c) 2002-2003, Edward Marco Baringer
94 ;; All rights reserved.
95 ;;
96 ;; Redistribution and use in source and binary forms, with or without
97 ;; modification, are permitted provided that the following conditions are
98 ;; met:
99 ;;
100 ;;  - Redistributions of source code must retain the above copyright
101 ;;    notice, this list of conditions and the following disclaimer.
102 ;;
103 ;;  - Redistributions in binary form must reproduce the above copyright
104 ;;    notice, this list of conditions and the following disclaimer in the
105 ;;    documentation and/or other materials provided with the distribution.
106 ;;
107 ;;  - Neither the name of Edward Marco Baringer, nor BESE, nor the names
108 ;;    of its contributors may be used to endorse or promote products
109 ;;    derived from this software without specific prior written permission.
110 ;;
111 ;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
112 ;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
113 ;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
114 ;; A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
115 ;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
116 ;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
117 ;; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
118 ;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
119 ;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
120 ;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
121 ;; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.