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