ee4b2f622207d98ce8c84779e9610f261de53713
[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 default-test-args)
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
27 DEFAULT-TEST-ARGS, if provided, will ba passed to the TEST forms
28 defined in this suite."
29   `(eval-when (:compile-toplevel :load-toplevel :execute)
30      (make-suite ',name
31                  ,@(when description `(:description ,description))
32                  ,@(when in `(:in ',in))
33                  ,@(when default-test-args `(:default-test-args ,default-test-args)))
34      ',name))
35
36 (defun make-suite (name &key description in default-test-args)
37   "Create a new test suite object.
38
39 Overides any existing suite named NAME."
40   (let ((suite (make-instance 'test-suite :name name :default-test-args default-test-args)))
41     (when description
42       (setf (description suite) description))
43     (loop for i in (ensure-list in)
44           for in-suite = (get-test i)
45           do (progn
46                (when (null in-suite)
47                  (cerror "Create a new suite named ~A." "Unknown suite ~A." i)
48                  (setf (get-test in-suite) (make-suite i)
49                        in-suite (get-test in-suite)))
50                (setf (gethash name (tests in-suite)) suite)))
51     (setf (get-test name) suite)
52     suite))
53
54 ;;;; ** Managing the Current Suite
55
56 (defvar *suite* (setf (get-test 'NIL)
57                       (make-suite 'NIL :description "Global Suite"))
58   "The current test suite object")
59
60 (defmacro in-suite (suite-name)
61   "Set the *suite* special variable so that all tests defined
62 after the execution of this form are, unless specified otherwise,
63 in the test-suite named SUITE-NAME.
64
65 See also: DEF-SUITE *SUITE*"
66   `(eval-when (:compile-toplevel :load-toplevel :execute)
67      (%in-suite ,suite-name)))
68
69 (defmacro in-suite* (suite-name &key in)
70   "Just like in-suite, but silently creates missing suites."
71   `(%in-suite ,suite-name :in ,in :fail-on-error nil))
72
73 (defmacro %in-suite (suite-name &key (fail-on-error t) in)
74   (with-unique-names (suite)
75     `(progn
76        (if-bind ,suite (get-test ',suite-name)
77            (setf *suite* ,suite)
78            (progn
79              (when ,fail-on-error
80                (cerror "Create a new suite named ~A."
81                        "Unkown suite ~A." ',suite-name))
82              (setf (get-test ',suite-name) (make-suite ',suite-name :in ',in)
83                    *suite* (get-test ',suite-name))))
84        ',suite-name)))
85
86 ;; Copyright (c) 2002-2003, Edward Marco Baringer
87 ;; All rights reserved. 
88 ;; 
89 ;; Redistribution and use in source and binary forms, with or without
90 ;; modification, are permitted provided that the following conditions are
91 ;; met:
92 ;; 
93 ;;  - Redistributions of source code must retain the above copyright
94 ;;    notice, this list of conditions and the following disclaimer.
95 ;; 
96 ;;  - Redistributions in binary form must reproduce the above copyright
97 ;;    notice, this list of conditions and the following disclaimer in the
98 ;;    documentation and/or other materials provided with the distribution.
99 ;;
100 ;;  - Neither the name of Edward Marco Baringer, nor BESE, nor the names
101 ;;    of its contributors may be used to endorse or promote products
102 ;;    derived from this software without specific prior written permission.
103 ;; 
104 ;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
105 ;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
106 ;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
107 ;; A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
108 ;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
109 ;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
110 ;; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
111 ;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
112 ;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
113 ;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
114 ;; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE