Removed the default-test-args slot from test-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 :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 (test gen-integer
153   (for-all ((a (gen-integer)))
154     (is (integerp a))))
155
156 (test for-all-guarded
157   (for-all ((less (gen-integer))
158             (more (gen-integer) (< less more)))
159     (is (< less more))))
160
161 (test gen-float
162   (macrolet ((test-gen-float (type)
163                `(for-all ((unbounded (gen-float :type ',type))
164                           (bounded   (gen-float :type ',type :bound 42)))
165                   (is (typep unbounded ',type))
166                   (is (typep bounded ',type))
167                   (is (<= (abs bounded) 42)))))
168     (test-gen-float single-float)
169     (test-gen-float short-float)
170     (test-gen-float double-float)
171     (test-gen-float long-float)))
172
173 (test gen-character
174   (for-all ((c (gen-character)))
175     (is (characterp c)))
176   (for-all ((c (gen-character :code (gen-integer :min 32 :max 40))))
177     (is (characterp c))
178     (member c (list #\Space #\! #\" #\# #\$ #\% #\& #\' #\())))
179
180 (test gen-string
181   (for-all ((s (gen-string)))
182     (is (stringp s)))
183   (for-all ((s (gen-string :length (gen-integer :min 0 :max 2))))
184     (is (<= (length s) 2)))
185   (for-all ((s (gen-string :elements (gen-character :code (gen-integer :min 0 :max 0))
186                            :length (constantly 2))))
187     (is (= 2 (length s)))
188     (is (every (curry #'char= #\Null) s))))
189
190 (defun dummy-mv-generator ()
191   (lambda ()
192     (list 1 1)))
193
194 (test for-all-destructuring-bind
195   (for-all (((a b) (dummy-mv-generator)))
196     (is (= 1 a))
197     (is (= 1 b))))