bugfixed test of depends on symbol
[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 ;;;; dependencies with symbol
153 (test (dep-with-symbol-first :suite test-suite)
154   (pass))
155
156 (test (dep-with-symbol-dependencies-not-met :depends-on (not dep-with-symbol-first)
157                                             :suite test-suite)
158   (fail "Error in the test of the test, this should not ever happen"))
159
160 (test (dep-with-symbol-depends-on-ok :depends-on dep-with-symbol-first :suite test-suite)
161   (pass))
162
163 (test (dep-with-symbol-depends-on-failed-dependency :depends-on dep-with-symbol-dependencies-not-met
164                                                     :suite test-suite)
165   (fail "No, I should not be tested becuase I depend on a test that in its turn has a failed dependecy."))
166
167 (test dependencies-with-symbol
168   (with-test-results (results dep-with-symbol-first)
169     (is (some #'test-passed-p results)))
170
171   (with-test-results (results dep-with-symbol-depends-on-ok)
172     (is (some #'test-passed-p results)))
173
174   (with-test-results (results dep-with-symbol-dependencies-not-met)
175     (is (some #'test-skipped-p results)))
176
177   ;; No failure here, because it means the test was run.
178   (with-test-results (results dep-with-symbol-depends-on-failed-dependency)
179     (is (not (some #'test-failure-p results)))))
180
181
182 ;;;; test for-all
183
184 (test gen-integer
185   (for-all ((a (gen-integer)))
186     (is (integerp a))))
187
188 (test for-all-guarded
189   (for-all ((less (gen-integer))
190             (more (gen-integer) (< less more)))
191     (is (< less more))))
192
193 (test gen-float
194   (macrolet ((test-gen-float (type)
195                `(for-all ((unbounded (gen-float :type ',type))
196                           (bounded   (gen-float :type ',type :bound 42)))
197                   (is (typep unbounded ',type))
198                   (is (typep bounded ',type))
199                   (is (<= (abs bounded) 42)))))
200     (test-gen-float single-float)
201     (test-gen-float short-float)
202     (test-gen-float double-float)
203     (test-gen-float long-float)))
204
205 (test gen-character
206   (for-all ((c (gen-character)))
207     (is (characterp c)))
208   (for-all ((c (gen-character :code (gen-integer :min 32 :max 40))))
209     (is (characterp c))
210     (member c (list #\Space #\! #\" #\# #\$ #\% #\& #\' #\())))
211
212 (test gen-string
213   (for-all ((s (gen-string)))
214     (is (stringp s)))
215   (for-all ((s (gen-string :length (gen-integer :min 0 :max 2))))
216     (is (<= (length s) 2)))
217   (for-all ((s (gen-string :elements (gen-character :code (gen-integer :min 0 :max 0))
218                            :length (constantly 2))))
219     (is (= 2 (length s)))
220     (is (every (curry #'char= #\Null) s))))
221
222 (defun dummy-mv-generator ()
223   (lambda ()
224     (list 1 1)))
225
226 (test for-all-destructuring-bind
227   (for-all (((a b) (dummy-mv-generator)))
228     (is (= 1 a))
229     (is (= 1 b))))