More documentation (suites and fixtures)
[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   (setf (gethash key *fixture*) value))
26
27 (defun rem-fixture (key)
28   (remhash key *fixture*))
29
30 (defmacro def-fixture (name (&rest args) &body body)
31   "Defines a fixture named NAME. At \"evaluation time\" (not macro
32 expansion time) `BODY` will be run, however `BODY` can call the local
33 macro `&body` which will expand to the body passed to the
34 `with-fixture` call.
35
36 See Also: `WITH-FIXTURE`"
37   `(eval-when (:compile-toplevel :load-toplevel :execute)
38      (setf (get-fixture ',name) (cons ',args ',body))
39      ',name))
40
41 (defmacro with-fixture (fixture-name (&rest args) &body body)
42   "Lookup a fixture named `NAME` (at macro expansion time),
43 replace the fixture's `&body` with `BODY` and compile the resulting
44 form.
45
46 See Also: `DEF-FIXTURE`"
47   (assert (get-fixture fixture-name)
48           (fixture-name)
49           "Unknown fixture ~S." fixture-name)
50   (destructuring-bind ((&rest largs) &rest lbody)
51       (get-fixture fixture-name)
52     `(macrolet ((&body () '(progn ,@body)))
53        (funcall (lambda (,@largs) ,@lbody) ,@args))))
54
55 ;; Copyright (c) 2002-2003, Edward Marco Baringer
56 ;; All rights reserved.
57 ;;
58 ;; Redistribution and use in source and binary forms, with or without
59 ;; modification, are permitted provided that the following conditions are
60 ;; met:
61 ;;
62 ;;  - Redistributions of source code must retain the above copyright
63 ;;    notice, this list of conditions and the following disclaimer.
64 ;;
65 ;;  - Redistributions in binary form must reproduce the above copyright
66 ;;    notice, this list of conditions and the following disclaimer in the
67 ;;    documentation and/or other materials provided with the distribution.
68 ;;
69 ;;  - Neither the name of Edward Marco Baringer, nor BESE, nor the names
70 ;;    of its contributors may be used to endorse or promote products
71 ;;    derived from this software without specific prior written permission.
72 ;;
73 ;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
74 ;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
75 ;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
76 ;; A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
77 ;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
78 ;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
79 ;; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
80 ;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
81 ;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
82 ;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
83 ;; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.