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