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