2f559ec35736ad306de5d720ddde4a67d7661cca
[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 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 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 (largs &rest lbody) (get-fixture fixture-name)
55     `(macrolet ((&body () '(progn ,@body)))
56        (funcall (lambda ,largs ,@lbody) ,@args))))
57
58 ;; Copyright (c) 2002-2003, Edward Marco Baringer
59 ;; All rights reserved.
60 ;;
61 ;; Redistribution and use in source and binary forms, with or without
62 ;; modification, are permitted provided that the following conditions are
63 ;; met:
64 ;;
65 ;;  - Redistributions of source code must retain the above copyright
66 ;;    notice, this list of conditions and the following disclaimer.
67 ;;
68 ;;  - Redistributions in binary form must reproduce the above copyright
69 ;;    notice, this list of conditions and the following disclaimer in the
70 ;;    documentation and/or other materials provided with the distribution.
71 ;;
72 ;;  - Neither the name of Edward Marco Baringer, nor BESE, nor the names
73 ;;    of its contributors may be used to endorse or promote products
74 ;;    derived from this software without specific prior written permission.
75 ;;
76 ;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
77 ;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
78 ;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
79 ;; A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
80 ;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
81 ;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
82 ;; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
83 ;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
84 ;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
85 ;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
86 ;; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.