a3de037d246b5d95a2bb286ca056181e1ef1021e
[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 (deflookup-table fixture
17   :documentation "Lookup table mapping fixture names to fixture
18   objects.")
19
20 (defmacro def-fixture (name args &body body)
21   "Defines a fixture named NAME. A fixture is very much like a
22 macro but is used only for simple templating. A fixture created
23 with DEF-FIXTURE is a macro which can use the special macrolet
24 &BODY to specify where the body should go.
25
26 See Also: WITH-FIXTURE
27 "
28   `(eval-when (:compile-toplevel :load-toplevel :execute)
29      (setf (get-fixture ',name) (cons ',args ',body))
30      ',name))
31
32 (defmacro with-fixture (fixture-name args &body body)
33   "Insert BODY into the fixture named FIXTURE-NAME.
34
35 See Also: DEF-FIXTURE"
36   (assert (get-fixture fixture-name)
37           (fixture-name)
38           "Unknown fixture ~S." fixture-name)
39   (destructuring-bind (largs &rest lbody) (get-fixture fixture-name)
40     `(macrolet ((&body () '(progn ,@body)))
41        (funcall (lambda ,largs ,@lbody) ,@args))))
42
43 ;; Copyright (c) 2002-2003, Edward Marco Baringer
44 ;; All rights reserved.
45 ;;
46 ;; Redistribution and use in source and binary forms, with or without
47 ;; modification, are permitted provided that the following conditions are
48 ;; met:
49 ;;
50 ;;  - Redistributions of source code must retain the above copyright
51 ;;    notice, this list of conditions and the following disclaimer.
52 ;;
53 ;;  - Redistributions in binary form must reproduce the above copyright
54 ;;    notice, this list of conditions and the following disclaimer in the
55 ;;    documentation and/or other materials provided with the distribution.
56 ;;
57 ;;  - Neither the name of Edward Marco Baringer, nor BESE, nor the names
58 ;;    of its contributors may be used to endorse or promote products
59 ;;    derived from this software without specific prior written permission.
60 ;;
61 ;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
62 ;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
63 ;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
64 ;; A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
65 ;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
66 ;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
67 ;; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
68 ;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
69 ;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
70 ;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
71 ;; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.