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