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