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