Better arglists for DEF-FIXTURE and WITH-FIXTURE
[fiveam.git] / src / fixture.lisp
1 ;; -*- lisp -*-
2
3 (in-package :it.bese.fiveam)
4
5 ;;;; ** Fixtures
6
7 ;;;; When running tests we often need to setup some kind of context
8 ;;;; (create dummy db connections, simulate an http request,
9 ;;;; etc.). Fixtures provide a way to conviently hide this context
10 ;;;; into a macro and allow the test to focus on testing.
11
12 ;;;; NB: A FiveAM fixture is nothing more than a macro. Since the term
13 ;;;; 'fixture' is so common in testing frameworks we've provided a
14 ;;;; wrapper around defmacro for this purpose.
15
16 (defvar *fixture*
17   (make-hash-table :test 'eql)
18   "Lookup table mapping fixture names to fixture
19   objects.")
20
21 (defun get-fixture (key &optional default)
22   (gethash key *fixture* default))
23
24 (defun (setf get-fixture) (value key)
25   (when (gethash key *fixture*)
26     (warn "Redefining ~A in deflookup-table named ~S"
27           (let ((*package* (find-package :keyword)))
28             (format nil "~S" key))
29           'fixture))
30   (setf (gethash key *fixture*) value))
31
32 (defun rem-fixture (key)
33   (remhash key *fixture*))
34
35 (defmacro def-fixture (name (&rest args) &body body)
36   "Defines a fixture named NAME. A fixture is very much like a
37 macro but is used only for simple templating. A fixture created
38 with DEF-FIXTURE is a macro which can use the special macrolet
39 &BODY to specify where the body should go.
40
41 See Also: WITH-FIXTURE
42 "
43   `(eval-when (:compile-toplevel :load-toplevel :execute)
44      (setf (get-fixture ',name) (cons ',args ',body))
45      ',name))
46
47 (defmacro with-fixture (fixture-name (&rest args) &body body)
48   "Insert BODY into the fixture named FIXTURE-NAME.
49
50 See Also: DEF-FIXTURE"
51   (assert (get-fixture fixture-name)
52           (fixture-name)
53           "Unknown fixture ~S." fixture-name)
54   (destructuring-bind ((&rest largs) &rest lbody)
55       (get-fixture fixture-name)
56     `(macrolet ((&body () '(progn ,@body)))
57        (funcall (lambda (,@largs) ,@lbody) ,@args))))
58
59 ;; Copyright (c) 2002-2003, Edward Marco Baringer
60 ;; All rights reserved.
61 ;;
62 ;; Redistribution and use in source and binary forms, with or without
63 ;; modification, are permitted provided that the following conditions are
64 ;; met:
65 ;;
66 ;;  - Redistributions of source code must retain the above copyright
67 ;;    notice, this list of conditions and the following disclaimer.
68 ;;
69 ;;  - Redistributions in binary form must reproduce the above copyright
70 ;;    notice, this list of conditions and the following disclaimer in the
71 ;;    documentation and/or other materials provided with the distribution.
72 ;;
73 ;;  - Neither the name of Edward Marco Baringer, nor BESE, nor the names
74 ;;    of its contributors may be used to endorse or promote products
75 ;;    derived from this software without specific prior written permission.
76 ;;
77 ;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
78 ;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
79 ;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
80 ;; A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
81 ;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
82 ;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
83 ;; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
84 ;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
85 ;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
86 ;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
87 ;; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.