e7aab24ff3cc8bf085f62ee16a40bec3481ab0b5
[fiveam.git] / src / suite.lisp
1 ;; -*- lisp -*-
2
3 (in-package :it.bese.FiveAM)
4
5 ;;;; * Test Suites
6
7 ;;;; Test suites allow us to collect multiple tests into a single
8 ;;;; object and run them all using asingle name. Test suites do not
9 ;;;; affect the way test are run nor the way the results are handled,
10 ;;;; they are simply a test organizing group.
11
12 ;;;; Test suites can contain both tests and other test suites. Running
13 ;;;; a test suite causes all of its tests and test suites to be
14 ;;;; run. Suites do not affect test dependencies, running a test suite
15 ;;;; can cause tests which are not in the suite to be run.
16
17 ;;;; ** Creating Suits
18
19 (defmacro def-suite (name &key description in)
20   "Define a new test-suite named NAME.
21
22 IN (a symbol), if provided, causes this suite te be nested in the
23 suite named by IN. NB: This macro is built on top of make-suite,
24 as such it, like make-suite, will overrwrite any existing suite
25 named NAME."
26   `(progn
27      (make-suite ',name
28                  ,@(when description `(:description ,description))
29                  ,@(when in `(:in ',in)))
30      ',name))
31
32 (defun make-suite (name &key description in)
33   "Create a new test suite object.
34
35 Overides any existing suite named NAME."
36   (let ((suite (make-instance 'test-suite :name name)))
37     (when description
38       (setf (description suite) description))
39     (loop for i in (ensure-list in)
40           for in-suite = (get-test i)
41           do (progn
42                (when (null in-suite)
43                  (cerror "Create a new suite named ~A." "Unknown suite ~A." i)
44                  (setf (get-test in-suite) (make-suite i)
45                        in-suite (get-test in-suite)))
46                (setf (gethash name (tests in-suite)) suite)))
47     (setf (get-test name) suite)
48     suite))
49
50 ;;;; ** Managing the Current Suite
51
52 (defvar *suite* (setf (get-test 'NIL)
53                       (make-suite 'NIL :description "Global Suite"))
54   "The current test suite object")
55
56 (defmacro in-suite (suite-name)
57   "Set the *suite* special variable so that all tests defined
58 after the execution of this form are, unless specified otherwise,
59 in the test-suite named SUITE-NAME.
60
61 See also: DEF-SUITE *SUITE*"
62   `(%in-suite ,suite-name))
63
64 (defmacro in-suite* (suite-name &key in)
65   "Just like in-suite, but silently creates missing suites."
66   `(%in-suite ,suite-name :in ,in :fail-on-error nil))
67
68 (defmacro %in-suite (suite-name &key (fail-on-error t) in)
69   (with-unique-names (suite)
70     `(progn
71        (if-bind ,suite (get-test ',suite-name)
72            (setf *suite* ,suite)
73            (progn
74              (when ,fail-on-error
75                (cerror "Create a new suite named ~A."
76                        "Unkown suite ~A." ',suite-name))
77              (setf (get-test ',suite-name) (make-suite ',suite-name :in ',in)
78                    *suite* (get-test ',suite-name))))
79        ',suite-name)))
80
81 ;; Copyright (c) 2002-2003, Edward Marco Baringer
82 ;; All rights reserved. 
83 ;; 
84 ;; Redistribution and use in source and binary forms, with or without
85 ;; modification, are permitted provided that the following conditions are
86 ;; met:
87 ;; 
88 ;;  - Redistributions of source code must retain the above copyright
89 ;;    notice, this list of conditions and the following disclaimer.
90 ;; 
91 ;;  - Redistributions in binary form must reproduce the above copyright
92 ;;    notice, this list of conditions and the following disclaimer in the
93 ;;    documentation and/or other materials provided with the distribution.
94 ;;
95 ;;  - Neither the name of Edward Marco Baringer, nor BESE, nor the names
96 ;;    of its contributors may be used to endorse or promote products
97 ;;    derived from this software without specific prior written permission.
98 ;; 
99 ;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
100 ;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
101 ;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
102 ;; A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
103 ;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
104 ;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
105 ;; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
106 ;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
107 ;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
108 ;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
109 ;; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE