a77abf3de45a308e69df5b46438e9100ca21d916
[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 :description "Suite for tests which should fail.")
8
9 (defmacro with-test-results ((results test-name) &body body)
10   `(let ((,results (with-*test-dribble* nil (run ',test-name))))
11      ,@body))
12
13 (def-fixture null-fixture ()
14   `(progn ,@(&body)))
15
16 ;;;; Test the checks
17
18 (test (is1 :suite test-suite)
19   (is (plusp 1))
20   (is (< 0 1))
21   (is (not (plusp -1)))
22   (is (not (< 1 0)))
23   (is-true t)
24   (is-false nil))
25
26 (test (is2 :suite test-suite :fixture null-fixture)
27   (is (plusp 0))
28   (is (< 0 -1))
29   (is (not (plusp 1)))
30   (is (not (< 0 1)))
31   (is-true nil)
32   (is-false t))
33
34 (test (is :profile t)
35   (with-test-results (results is1)
36     (is (= 6 (length results)))
37     (is (every #'test-passed-p results)))
38   (with-test-results (results is2)
39     (is (= 6 (length results)))
40     (is (every #'test-failure-p results))))
41
42 (test signals/finishes
43   (signals error
44     (error "an error"))
45   (finishes
46    (signals error
47     (error "an error"))))
48
49 (test pass
50   (pass))
51
52 (test (fail1 :suite test-suite)
53   (fail "This is supposed to fail"))
54
55 (test fail
56   (with-test-results (results fail1)
57     (is (= 1 (length results)))
58     (is (test-failure-p (first results)))))
59
60 ;;;; non top level checks
61
62 (test foo-bar
63   (let ((state 0))
64     (is (= 0 state))
65     (is (= 1 (incf state)))))
66
67 ;;;; Test dependencies
68
69 (test (ok :suite test-suite)
70   (pass))
71
72 (test (not-ok :suite test-suite)
73   (fail "This is supposed to fail."))
74
75 (test (and1 :depends-on (and ok not-ok) :suite test-suite)
76   (fail))
77
78 (test (and2 :depends-on (and ok) :suite test-suite)
79   (pass))
80
81 (test dep-and 
82   (with-test-results (results and1)
83     (is (= 3 (length results)))
84     ;; we should have one skippedw one failed and one passed
85     (is (some #'test-passed-p results))
86     (is (some #'test-skipped-p results))
87     (is (some #'test-failure-p results)))
88   (with-test-results (results and2)
89     (is (= 2 (length results)))
90     (is (every #'test-passed-p results))))
91
92 (test (or1 :depends-on (or ok not-ok) :suite test-suite)
93   (pass))
94
95 (test (or2 :depends-on (or not-ok ok) :suite test-suite)
96   (pass))
97
98 (test dep-or
99   (with-test-results (results or1)
100     (is (= 2 (length results)))
101     (is (every #'test-passed-p results)))
102   (with-test-results (results or2)
103     (is (= 3 (length results)))
104     (is (= 2 (length (remove-if-not #'test-passed-p results))))))
105
106 (test (not1 :depends-on (not not-ok) :suite test-suite)
107   (pass))
108
109 (test (not2 :depends-on (not ok) :suite test-suite)
110   (fail))
111
112 (test not
113   (with-test-results (results not1)
114     (is (= 2 (length results)))
115     (is (some #'test-passed-p results))
116     (is (some #'test-failure-p results)))
117   (with-test-results (results not2)
118     (is (= 2 (length results)))
119     (is (some #'test-passed-p results))
120     (is (some #'test-skipped-p results))))
121
122 (test (nested-logic :depends-on (and ok (not not-ok) (not not-ok))
123                     :suite test-suite)
124   (pass))
125
126 (test dep-nested
127   (with-test-results (results nested-logic)
128     (is (= 3 (length results)))
129     (is (= 2 (length (remove-if-not #'test-passed-p results))))
130     (is (= 1 (length (remove-if-not #'test-failure-p results))))))
131
132 (test (circular-0 :depends-on (and circular-1 circular-2 or1) 
133                   :suite test-suite)
134   (fail "we depend on a circular dependency, we should not be tested."))
135
136 (test (circular-1 :depends-on (and circular-2)
137                   :suite test-suite)
138   (fail "we have a circular depednency, we should not be tested."))
139
140 (test (circular-2 :depends-on (and circular-1)
141                   :suite test-suite)
142   (fail "we have a circular depednency, we should not be tested."))
143
144 (test circular
145   (signals circular-dependency
146     (run 'circular-0))
147   (signals circular-dependency
148     (run 'circular-1))
149   (signals circular-dependency
150     (run 'circular-2)))
151
152
153 (def-suite before-test-suite :description "Suite for before test")
154
155 (test (before-0 :suite before-test-suite)
156   (pass))
157
158 (test (before-1 :depends-on (:before before-0)
159                 :suite before-test-suite)
160   (fail))
161
162 (def-suite before-test-suite-2 :description "Suite for before test")
163
164 (test (before-2 :depends-on (:before before-3)
165                 :suite before-test-suite-2)
166   (pass))
167
168 (test (before-3 :suite before-test-suite-2)
169   (pass))
170
171 (test before
172   (with-test-results (results before-test-suite)
173     (is (some #'test-skipped-p results)))
174   
175   (with-test-results (results before-test-suite-2)
176     (is (every #'test-passed-p results))))
177
178
179 ;;;; dependencies with symbol
180 (test (dep-with-symbol-first :suite test-suite)
181   (pass))
182
183 (test (dep-with-symbol-dependencies-not-met :depends-on (not dep-with-symbol-first)
184                                             :suite test-suite)
185   (fail "Error in the test of the test, this should not ever happen"))
186
187 (test (dep-with-symbol-depends-on-ok :depends-on dep-with-symbol-first :suite test-suite)
188   (pass))
189
190 (test (dep-with-symbol-depends-on-failed-dependency :depends-on dep-with-symbol-dependencies-not-met
191                                                     :suite test-suite)
192   (fail "No, I should not be tested becuase I depend on a test that in its turn has a failed dependecy."))
193
194 (test dependencies-with-symbol
195   (with-test-results (results dep-with-symbol-first)
196     (is (some #'test-passed-p results)))
197
198   (with-test-results (results dep-with-symbol-depends-on-ok)
199     (is (some #'test-passed-p results)))
200
201   (with-test-results (results dep-with-symbol-dependencies-not-met)
202     (is (some #'test-skipped-p results)))
203
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)))))
207
208
209 ;;;; test for-all
210
211 (test gen-integer
212   (for-all ((a (gen-integer)))
213     (is (integerp a))))
214
215 (test for-all-guarded
216   (for-all ((less (gen-integer))
217             (more (gen-integer) (< less more)))
218     (is (< less more))))
219
220 (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)))
231
232 (test gen-character
233   (for-all ((c (gen-character)))
234     (is (characterp c)))
235   (for-all ((c (gen-character :code (gen-integer :min 32 :max 40))))
236     (is (characterp c))
237     (member c (list #\Space #\! #\" #\# #\$ #\% #\& #\' #\())))
238
239 (test gen-string
240   (for-all ((s (gen-string)))
241     (is (stringp s)))
242   (for-all ((s (gen-string :length (gen-integer :min 0 :max 2))))
243     (is (<= (length s) 2)))
244   (for-all ((s (gen-string :elements (gen-character :code (gen-integer :min 0 :max 0))
245                            :length (constantly 2))))
246     (is (= 2 (length s)))
247     (is (every (curry #'char= #\Null) s))))
248
249 (defun dummy-mv-generator ()
250   (lambda ()
251     (list 1 1)))
252
253 (test for-all-destructuring-bind
254   (for-all (((a b) (dummy-mv-generator)))
255     (is (= 1 a))
256     (is (= 1 b))))