TEST macro now recompiles the body on every run.
[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   :documentation "Lookup table mapping test (and test suite)
16   names to objects.")
17
18 (defmacro test (name &body body)
19   "Create a test named NAME. If NAME is a list it must be of the
20 form:
21
22   (name &key depends-on suite)
23
24 NAME is the symbol which names the test.
25
26 DEPENDS-ON is a list of the form:
27
28  (AND . test-names) - This test is run only if all of the tests
29  in TEST-NAMES have passed, otherwise a single test-skipped
30  result is generated.
31
32  (OR . test-names) - If any of TEST-NAMES has passed this test is
33  run, otherwise a test-skipped result is generated.
34
35  (NOT test-name) - This is test is run only if TEST-NAME failed.
36
37 AND, OR and NOT can be combined to produce complex dependencies.
38
39 If DEPENDS-ON is a symbol it is interpreted as `(AND
40 ,depends-on), this is accomadate the common case of one test
41 depending on another.
42
43 SUITE defaults to the current value of *SUITE*."
44   (destructuring-bind (name &key depends-on (suite nil suite-supplied-p))
45       (ensure-list name)
46     (let (description)
47       (setf description (if (stringp (car body))
48                             (pop body)
49                             ""))
50       `(progn
51          (setf (get-test ',name) (make-instance 'test-case
52                                                 :name ',name
53                                                 :test-lambda
54                                                 (lambda ()
55                                                   (funcall (compile nil '(lambda () ,@body))))
56                                                 :description ,description
57                                                 :depends-on ',depends-on))
58          ,(if suite-supplied-p
59               `(setf (gethash ',name (tests (get-test ',suite)))
60                      ',name)
61               `(setf (gethash ',name (tests (or *suite* (get-test 'NIL))))
62                      ',name))
63          ',name))))
64
65 ;; Copyright (c) 2002-2003, Edward Marco Baringer
66 ;; All rights reserved. 
67 ;; 
68 ;; Redistribution and use in source and binary forms, with or without
69 ;; modification, are permitted provided that the following conditions are
70 ;; met:
71 ;; 
72 ;;  - Redistributions of source code must retain the above copyright
73 ;;    notice, this list of conditions and the following disclaimer.
74 ;; 
75 ;;  - Redistributions in binary form must reproduce the above copyright
76 ;;    notice, this list of conditions and the following disclaimer in the
77 ;;    documentation and/or other materials provided with the distribution.
78 ;;
79 ;;  - Neither the name of Edward Marco Baringer, nor BESE, nor the names
80 ;;    of its contributors may be used to endorse or promote products
81 ;;    derived from this software without specific prior written permission.
82 ;; 
83 ;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
84 ;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
85 ;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
86 ;; A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
87 ;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
88 ;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
89 ;; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
90 ;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
91 ;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
92 ;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
93 ;; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.