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