Renamed the default suite to T; allow a suite parameter of NIL to mean a test/suite...
[fiveam.git] / src / suite.lisp
1 ;;;; -*- Mode: Lisp; indent-tabs-mode: nil -*-
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 (defvar *suites* (make-hash-table :test 'eql))
20
21 (defmacro def-suite (name &key description (in nil in-p) (fixture nil fixture-p))
22   "Define a new test-suite named NAME.
23
24 IN (a symbol), if provided, causes this suite te be nested in the
25 suite named by IN. NB: This macro is built on top of make-suite,
26 as such it, like make-suite, will overrwrite any existing suite
27 named NAME.
28
29 DESCRIPTION is just a string.
30
31 FIXTURE is the fixture argument (exactly like the :fixture argument to
32 def-test) to pass to tests in this suite."
33   `(eval-when (:compile-toplevel :load-toplevel :execute)
34      (make-suite ',name
35                  ,@(when description `(:description ,description))
36                  ,@(when in-p      `(:in ',in))
37                  ,@(when fixture-p `(:fixture ',fixture)))
38      ',name))
39
40 (defmacro def-suite* (name &rest def-suite-args)
41   `(progn
42      (def-suite ,name ,@def-suite-args)
43      (in-suite ,name)))
44
45 (defun remove-from-suites (test-name)
46   (when (get-test test-name)
47     ;; if this suite alruady exists, and its :IN some other suite, remove it.
48     (dolist (s (list-all-suites))
49       (when (gethash test-name (tests s))
50         (remhash test-name (tests s))))))
51
52 (defun make-suite (name &key description ((:in parent-suite) *suite*) fixture)
53   "Create a new test suite object.
54
55 Overrides any existing suite named NAME."
56   (remove-from-suites name)
57   (let ((suite (make-instance 'test-suite :name name :fixture fixture)))
58     (when description
59       (setf (description suite) description))
60     (setf (gethash name *suites*) suite)
61     (loop for i in (ensure-list parent-suite)
62           for in-suite = (get-test i)
63           do (progn
64                (when (null in-suite)
65                  (cerror "Create a new suite named ~A." "Unknown suite ~A." i)
66                  (setf (get-test in-suite) (make-suite i)
67                        in-suite (get-test in-suite)))
68                (setf (gethash name (tests in-suite)) suite)))
69     (setf (get-test name) suite)
70     suite))
71
72 (defun list-all-suites ()
73   (loop for suite being the hash-value in *suites*
74        collect suite))
75
76 ;;;; ** Managing the Current Suite
77
78 (defvar *suite* (setf (get-test 'T) (make-suite 'T :description "Default global suite" :in nil))
79   "The current test suite object")
80
81 (defmacro in-suite (suite-name)
82   "Set the *suite* special variable so that all tests defined
83 after the execution of this form are, unless specified otherwise,
84 in the test-suite named SUITE-NAME.
85
86 See also: DEF-SUITE *SUITE*"
87   `(eval-when (:compile-toplevel :load-toplevel :execute)
88      (%in-suite ,suite-name)))
89
90 (defmacro in-suite* (suite-name &key (in nil in-p))
91   "Just like in-suite, but silently creates missing suites."
92   `(%in-suite ,suite-name
93               ,@(when in-p `(:in ,in))
94               :fail-on-error nil))
95
96 (defmacro %in-suite (suite-name &key (fail-on-error t) (in nil in-p))
97   (with-gensyms (suite)
98     `(progn
99        (if-let (,suite (get-test ',suite-name))
100          (setf *suite* ,suite)
101          (progn
102            (when ,fail-on-error
103              (cerror "Create a new suite named ~A."
104                      "Unknown suite ~A." ',suite-name))
105            (setf (get-test ',suite-name) (make-suite ',suite-name ,@(when in-p `(:in ',in)))
106                  *suite* (get-test ',suite-name))))
107        ',suite-name)))
108
109 ;; Copyright (c) 2002-2003, Edward Marco Baringer
110 ;; All rights reserved.
111 ;;
112 ;; Redistribution and use in source and binary forms, with or without
113 ;; modification, are permitted provided that the following conditions are
114 ;; met:
115 ;;
116 ;;  - Redistributions of source code must retain the above copyright
117 ;;    notice, this list of conditions and the following disclaimer.
118 ;;
119 ;;  - Redistributions in binary form must reproduce the above copyright
120 ;;    notice, this list of conditions and the following disclaimer in the
121 ;;    documentation and/or other materials provided with the distribution.
122 ;;
123 ;;  - Neither the name of Edward Marco Baringer, nor BESE, nor the names
124 ;;    of its contributors may be used to endorse or promote products
125 ;;    derived from this software without specific prior written permission.
126 ;;
127 ;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
128 ;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
129 ;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
130 ;; A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
131 ;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
132 ;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
133 ;; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
134 ;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
135 ;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
136 ;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
137 ;; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE