Added :default-test-args parameter to def-suite.
[fiveam.git] / t / tests.lisp
1 ;;;; -*- lisp -*-
2
3 (in-package :it.bese.FiveAM)
4
5 (in-suite :it.bese.FiveAM)
6
7 (def-suite test-suite
8     :description "Suite for tests which should fail."
9     :default-test-args '(:fixture null-fixture :compile-at :run-time))
10
11 (defmacro with-test-results ((results test-name) &body body)
12   `(let ((,results (with-*test-dribble* nil (run ',test-name))))
13      ,@body))
14
15 (def-fixture null-fixture ()
16   `(progn ,@(&body)))
17
18 ;;;; Test the checks
19
20 (test (is1 :suite test-suite)
21   (is (plusp 1))
22   (is (< 0 1))
23   (is (not (plusp -1)))
24   (is (not (< 1 0)))
25   (is-true t)
26   (is-false nil))
27
28 (test (is2 :suite test-suite :fixture foo)
29   (is (plusp 0))
30   (is (< 0 -1))
31   (is (not (plusp 1)))
32   (is (not (< 0 1)))
33   (is-true nil)
34   (is-false t))
35
36 (test is
37   (with-test-results (results is1)
38     (is (= 6 (length results)))
39     (is (every #'test-passed-p results)))
40   (with-test-results (results is2)
41     (is (= 6 (length results)))
42     (is (every #'test-failure-p results))))
43
44 (test signals/finishes
45   (signals error
46     (error "an error"))
47   (finishes
48    (signals error
49     (error "an error"))))
50
51 (test pass
52   (pass))
53
54 (test (fail1 :suite test-suite)
55   (fail "This is supposed to fail"))
56
57 (test fail
58   (with-test-results (results fail1)
59     (is (= 1 (length results)))
60     (is (test-failure-p (first results)))))
61
62 ;;;; non top level checks
63
64 (test foo-bar
65   (let ((state 0))
66     (is (= 0 state))
67     (is (= 1 (incf state)))))
68
69 ;;;; Test dependencies
70
71 (test (ok :suite test-suite)
72   (pass))
73
74 (test (not-ok :suite test-suite)
75   (fail "This is supposed to fail."))
76
77 (test (and1 :depends-on (and ok not-ok) :suite test-suite)
78   (fail))
79
80 (test (and2 :depends-on (and ok) :suite test-suite)
81   (pass))
82
83 (test dep-and 
84   (with-test-results (results and1)
85     (is (= 3 (length results)))
86     ;; we should have one skippedw one failed and one passed
87     (is (some #'test-passed-p results))
88     (is (some #'test-skipped-p results))
89     (is (some #'test-failure-p results)))
90   (with-test-results (results and2)
91     (is (= 2 (length results)))
92     (is (every #'test-passed-p results))))
93
94 (test (or1 :depends-on (or ok not-ok) :suite test-suite)
95   (pass))
96
97 (test (or2 :depends-on (or not-ok ok) :suite test-suite)
98   (pass))
99
100 (test dep-or
101   (with-test-results (results or1)
102     (is (= 2 (length results)))
103     (is (every #'test-passed-p results)))
104   (with-test-results (results or2)
105     (is (= 3 (length results)))
106     (is (= 2 (length (remove-if-not #'test-passed-p results))))))
107
108 (test (not1 :depends-on (not not-ok) :suite test-suite)
109   (pass))
110
111 (test (not2 :depends-on (not ok) :suite test-suite)
112   (fail))
113
114 (test not
115   (with-test-results (results not1)
116     (is (= 2 (length results)))
117     (is (some #'test-passed-p results))
118     (is (some #'test-failure-p results)))
119   (with-test-results (results not2)
120     (is (= 2 (length results)))
121     (is (some #'test-passed-p results))
122     (is (some #'test-skipped-p results))))
123
124 (test (nested-logic :depends-on (and ok (not not-ok) (not not-ok))
125                     :suite test-suite)
126   (pass))
127
128 (test dep-nested
129   (with-test-results (results nested-logic)
130     (is (= 3 (length results)))
131     (is (= 2 (length (remove-if-not #'test-passed-p results))))
132     (is (= 1 (length (remove-if-not #'test-failure-p results))))))
133
134 (test (circular-0 :depends-on (and circular-1 circular-2 or1) 
135                   :suite test-suite)
136   (fail "we depend on a circular dependency, we should not be tested."))
137
138 (test (circular-1 :depends-on (and circular-2)
139                   :suite test-suite)
140   (fail "we have a circular depednency, we should not be tested."))
141
142 (test (circular-2 :depends-on (and circular-1)
143                   :suite test-suite)
144   (fail "we have a circular depednency, we should not be tested."))
145
146 (test circular
147   (signals circular-dependency
148     (run 'circular-0))
149   (signals circular-dependency
150     (run 'circular-1))
151   (signals circular-dependency
152     (run 'circular-2)))
153
154 (test gen-integer
155   (for-all ((a (gen-integer)))
156     (is (integerp a))))
157
158 (test for-all-guarded
159   (for-all ((less (gen-integer))
160             (more (gen-integer) (< less more)))
161     (is (< less more))))
162
163 (test gen-float
164   (macrolet ((test-gen-float (type)
165                `(for-all ((unbounded (gen-float :type ',type))
166                           (bounded   (gen-float :type ',type :bound 42)))
167                   (is (typep unbounded ',type))
168                   (is (typep bounded ',type))
169                   (is (<= (abs bounded) 42)))))
170     (test-gen-float single-float)
171     (test-gen-float short-float)
172     (test-gen-float double-float)
173     (test-gen-float long-float)))
174
175 (test gen-character
176   (for-all ((c (gen-character)))
177     (is (characterp c)))
178   (for-all ((c (gen-character :code (gen-integer :min 32 :max 40))))
179     (is (characterp c))
180     (member c (list #\Space #\! #\" #\# #\$ #\% #\& #\' #\())))
181
182 (test gen-string
183   (for-all ((s (gen-string)))
184     (is (stringp s)))
185   (for-all ((s (gen-string :length (gen-integer :min 0 :max 2))))
186     (is (<= (length s) 2)))
187   (for-all ((s (gen-string :elements (gen-character :code (gen-integer :min 0 :max 0))
188                            :length (constantly 2))))
189     (is (= 2 (length s)))
190     (is (every (curry #'char= #\Null) s))))
191
192 (defun dummy-mv-generator ()
193   (lambda ()
194     (list 1 1)))
195
196 (test for-all-destructuring-bind
197   (for-all (((a b) (dummy-mv-generator)))
198     (is (= 1 a))
199     (is (= 1 b))))