1 ;;;; -*- Mode: Lisp; indent-tabs-mode: nil -*-
3 (in-package :it.bese.fiveam)
5 (in-suite :it.bese.fiveam)
8 :description "Suite for tests which should fail."
11 (defmacro with-test-results ((results test-name) &body body)
12 `(let ((,results (with-*test-dribble* nil (run ',test-name))))
15 (def-fixture null-fixture ()
20 (def-test is1 (:suite test-suite)
28 (def-test is2 (:suite test-suite :fixture null-fixture)
36 (def-test is (:profile t)
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))))
44 (def-test signals/finishes ()
54 (def-test fail1 (:suite test-suite)
55 (fail "This is supposed to fail"))
58 (with-test-results (results fail1)
59 (is (= 1 (length results)))
60 (is (test-failure-p (first results)))))
62 ;;;; non top level checks
67 (is (= 1 (incf state)))))
69 ;;;; Test dependencies
71 (def-test ok (:suite test-suite)
74 (def-test not-ok (:suite test-suite)
75 (fail "This is supposed to fail."))
77 (def-test and1 (:depends-on (and ok not-ok) :suite test-suite)
80 (def-test and2 (:depends-on (and ok) :suite test-suite)
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))))
94 (def-test or1 (:depends-on (or ok not-ok) :suite test-suite)
97 (def-test or2 (:depends-on (or not-ok ok) :suite test-suite)
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))))))
108 (def-test not1 (:depends-on (not not-ok) :suite test-suite)
111 (def-test not2 (:depends-on (not ok) :suite test-suite)
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))))
124 (def-test nested-logic (:depends-on (and ok (not not-ok) (not not-ok))
128 (def-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))))))
134 (def-test circular-0 (:depends-on (and circular-1 circular-2 or1)
136 (fail "we depend on a circular dependency, we should not be tested."))
138 (def-test circular-1 (:depends-on (and circular-2)
140 (fail "we have a circular depednency, we should not be tested."))
142 (def-test circular-2 (:depends-on (and circular-1)
144 (fail "we have a circular depednency, we should not be tested."))
146 (def-test circular ()
147 (signals circular-dependency
149 (signals circular-dependency
151 (signals circular-dependency
154 (def-suite before-test-suite :description "Suite for before test" :in nil)
156 (def-test before-0 (:suite before-test-suite)
159 (def-test before-1 (:depends-on (:before before-0)
160 :suite before-test-suite)
163 (def-suite before-test-suite-2 :description "Suite for before test" :in nil)
165 (def-test before-2 (:depends-on (:before before-3)
166 :suite before-test-suite-2)
169 (def-test before-3 (:suite before-test-suite-2)
173 (with-test-results (results before-test-suite)
174 (is (some #'test-skipped-p results)))
176 (with-test-results (results before-test-suite-2)
177 (is (every #'test-passed-p results))))
179 ;;;; dependencies with symbol
180 (def-test dep-with-symbol-first (:suite test-suite)
183 (def-test dep-with-symbol-dependencies-not-met (:depends-on (not dep-with-symbol-first)
185 (fail "Error in the test of the test, this should not ever happen"))
187 (def-test dep-with-symbol-depends-on-ok (:depends-on dep-with-symbol-first :suite test-suite)
190 (def-test dep-with-symbol-depends-on-failed-dependency (:depends-on dep-with-symbol-dependencies-not-met
192 (fail "No, I should not be tested becuase I depend on a test that in its turn has a failed dependecy."))
194 (def-test dependencies-with-symbol ()
195 (with-test-results (results dep-with-symbol-first)
196 (is (some #'test-passed-p results)))
198 (with-test-results (results dep-with-symbol-depends-on-ok)
199 (is (some #'test-passed-p results)))
201 (with-test-results (results dep-with-symbol-dependencies-not-met)
202 (is (some #'test-skipped-p results)))
204 ;; No failure here, because it means the test was run.
205 (with-test-results (results dep-with-symbol-depends-on-failed-dependency)
206 (is (not (some #'test-failure-p results)))))
211 (def-test gen-integer ()
212 (for-all ((a (gen-integer)))
215 (def-test for-all-guarded ()
216 (for-all ((less (gen-integer))
217 (more (gen-integer) (< less more)))
220 (def-test gen-float ()
221 (macrolet ((test-gen-float (type)
222 `(for-all ((unbounded (gen-float :type ',type))
223 (bounded (gen-float :type ',type :bound 42)))
224 (is (typep unbounded ',type))
225 (is (typep bounded ',type))
226 (is (<= (abs bounded) 42)))))
227 (test-gen-float single-float)
228 (test-gen-float short-float)
229 (test-gen-float double-float)
230 (test-gen-float long-float)
232 (for-all ((value (gen-float :type 'single-float :min 1 :max 2)))
233 (is (typep value 'single-float))
234 (is (<= (coerce 1 'single-float) value (coerce 2 'single-float))))))
236 (def-test gen-character ()
237 (for-all ((c (gen-character)))
239 (for-all ((c (gen-character :code (gen-integer :min 32 :max 40))))
241 (member c (list #\Space #\! #\" #\# #\$ #\% #\& #\' #\())))
243 (def-test gen-string ()
244 (for-all ((s (gen-string)))
246 (for-all ((s (gen-string :length (gen-integer :min 0 :max 2))))
247 (is (<= (length s) 2)))
248 (for-all ((s (gen-string :elements (gen-character :code (gen-integer :min 0 :max 0))
249 :length (constantly 2))))
250 (is (= 2 (length s)))
251 (is (every (curry #'char= #\Null) s))))
253 (defun dummy-mv-generator ()
257 (def-test for-all-destructuring-bind ()
258 (for-all (((a b) (dummy-mv-generator)))
262 (def-test introspection ()
263 (is (= (length (list-all-suites))
264 (hash-table-count *suites*))))
266 (defvar *special-variable* nil)
268 (def-fixture fixture-for-suite (value)
270 (setf *special-variable* value)
273 (def-suite suite-with-fixture :fixture (fixture-for-suite 42) :in :it.bese.fiveam)
275 (def-test test-with-suite-fixture (:suite suite-with-fixture)
276 (is (= 42 *special-variable*)))
278 (def-test add-remove-test-from-suite ()
279 (let ((*test* (make-hash-table :test 'eql))
280 (*suites* (make-hash-table :test 'eql)))
281 (def-suite empty :in nil)
283 (is (null (get-test 'foo)))
285 (def-test foo (:suite nil) t)
286 (is-true (get-test 'foo))
287 (is-false (gethash 'foo (tests *suite*)))
290 (is-true (gethash 'foo (tests *suite*)))
292 (def-test foo (:suite nil) t)
293 (is-false (gethash 'foo (tests *suite*)))))