a8269c699dff7a639a6dbdd62bece8f2f9ff557c
[fiveam.git] / src / test.lisp
1 ;;;; -*- Mode: Lisp; indent-tabs-mode: nil -*-
2
3 (in-package :it.bese.fiveam)
4
5 ;;;; * Tests
6
7 ;;;; While executing checks and collecting the results is the core job
8 ;;;; of a testing framework it is also important to be able to
9 ;;;; organize checks into groups, fiveam provides two mechanisms for
10 ;;;; organizing checks: tests and test suites. A test is a named
11 ;;;; collection of checks which can be run and a test suite is a named
12 ;;;; collection of tests and test suites.
13
14 (defvar *test*
15   (make-hash-table :test 'eql)
16   "Lookup table mapping test (and test suite)
17   names to objects.")
18
19 (defun get-test (key &optional default)
20   (gethash key *test* default))
21
22 (defun (setf get-test) (value key)
23   (setf (gethash key *test*) value))
24
25 (defun rem-test (key)
26   (remhash key *test*))
27
28 (defun test-names ()
29   (loop for test being the hash-keys of *test*
30         collect test))
31
32 (defmacro test (name &body body)
33   "Create a test named NAME. If NAME is a list it must be of the
34 form:
35
36   (name &key depends-on suite fixture compile-at profile)
37
38 NAME is the symbol which names the test.
39
40 DEPENDS-ON is a list of the form:
41
42  (AND . test-names) - This test is run only if all of the tests
43  in TEST-NAMES have passed, otherwise a single test-skipped
44  result is generated.
45
46  (OR . test-names) - If any of TEST-NAMES has passed this test is
47  run, otherwise a test-skipped result is generated.
48
49  (NOT test-name) - This is test is run only if TEST-NAME failed.
50
51 AND, OR and NOT can be combined to produce complex dependencies.
52
53 If DEPENDS-ON is a symbol it is interpreted as `(AND
54 ,depends-on), this is accomadate the common case of one test
55 depending on another.
56
57 FIXTURE specifies a fixtrue to wrap the body in.
58
59 If PROFILE is T profiling information will be collected as well."
60   (let* ((tmp (gensym))
61          (suite-arg (getf (cdr (ensure-list name)) :suite tmp))
62          (suite-form (cond
63                        ((eq tmp suite-arg) '*suite*)
64                        (t                  `(get-test ',suite-arg)))))
65     (when (consp name)
66       (remf (cdr name) :suite))
67     (destructuring-bind (name &key depends-on (compile-at :run-time) fixture profile)
68         (ensure-list name)
69       (declare (type (member :run-time :definition-time) compile-at))
70       (let ((description (if (stringp (car body))
71                              (pop body)
72                              ""))
73             (effective-body (if fixture
74                                 (destructuring-bind (name &rest args)
75                                     (ensure-list fixture)
76                                   `((with-fixture ,name ,args ,@body)))
77                                 body)))
78         `(progn
79            (setf (get-test ',name)
80                  (make-instance 'test-case
81                                 :name ',name
82                                 :runtime-package (find-package ,(package-name *package*))
83                                 :test-lambda
84                                 (lambda ()
85                                   ,@ (ecase compile-at
86                                        (:run-time `((funcall
87                                                      (let ((*package* (find-package ',(package-name *package*))))
88                                                        (compile nil '(lambda ()
89                                                                       ,@effective-body))))))
90                                        (:definition-time effective-body)))
91                                 :description ,description
92                                 :depends-on ',depends-on
93                                 :collect-profiling-info ,profile))
94            (setf (gethash ',name (tests ,suite-form)) ',name)
95            (when *run-test-when-defined*
96              (run! ',name))
97            ',name)))))
98
99 (defvar *run-test-when-defined* nil
100   "When non-NIL tests are run as soon as they are defined.")
101
102 ;; Copyright (c) 2002-2003, Edward Marco Baringer
103 ;; All rights reserved.
104 ;;
105 ;; Redistribution and use in source and binary forms, with or without
106 ;; modification, are permitted provided that the following conditions are
107 ;; met:
108 ;;
109 ;;  - Redistributions of source code must retain the above copyright
110 ;;    notice, this list of conditions and the following disclaimer.
111 ;;
112 ;;  - Redistributions in binary form must reproduce the above copyright
113 ;;    notice, this list of conditions and the following disclaimer in the
114 ;;    documentation and/or other materials provided with the distribution.
115 ;;
116 ;;  - Neither the name of Edward Marco Baringer, nor BESE, nor the names
117 ;;    of its contributors may be used to endorse or promote products
118 ;;    derived from this software without specific prior written permission.
119 ;;
120 ;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
121 ;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
122 ;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
123 ;; A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
124 ;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
125 ;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
126 ;; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
127 ;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
128 ;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
129 ;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
130 ;; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.