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