updating LIFT and keeping doc and darcs dirs.
[CommonLispStat.git] / external / lift.darcs / _darcs / pristine / examples / basic-examples.lisp
blob53f6c1540957881c1e69b25d0a3d63a86f6afd65
1 ;;;-*- Mode: Lisp; Package: LIFT -*-
3 #| simple-header
5 Copyright (c) 2001-2006 Gary Warren King (gwking@cs.umass.edu)
7 Permission is hereby granted, free of charge, to any person obtaining a
8 copy of this software and associated documentation files (the "Software"),
9 to deal in the Software without restriction, including without limitation
10 the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 and/or sell copies of the Software, and to permit persons to whom the
12 Software is furnished to do so, subject to the following conditions:
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 DEALINGS IN THE SOFTWARE.
27 (in-package #:lift)
29 ;;; ---------------------------------------------------------------------------
30 ;;; a simple example
31 ;;; ---------------------------------------------------------------------------
33 ;;; define an empty testsuite
34 (deftestsuite lift-examples-1 () ())
35 ;; => #<LIFT-EXAMPLES-1: no tests defined>
37 ;;; and add a test to it
38 (addtest (lift-examples-1)
39 (ensure-same (+ 1 1) 2))
40 ;; => #<Test passed>
42 ;;; add another test using ensure-error
43 (addtest (lift-examples-1)
44 (ensure-error (let ((x 0)) (/ x))))
45 ;; => #<Test passed>
47 ;;; add another, slightly more specific test
48 (addtest (lift-examples-1)
49 (ensure-condition division-by-zero (let ((x 0)) (/ x))))
50 ;; => #<Test passed>
52 ;;; run all the defined tests
53 (run-tests)
54 ;; => #<Results for LIFT-EXAMPLES-1 [3 Successful tests]>
57 ;;; ---------------------------------------------------------------------------
58 ;;; a simple example using deftestsuites :tests clause
59 ;;; ---------------------------------------------------------------------------
61 (deftestsuite lift-examples-2 ()
63 (:tests
64 ((ensure-same (+ 1 1) 2))
65 ((ensure-error (let ((x 0)) (/ x))))
66 ((ensure-condition division-by-zero (let ((x 0)) (/ x))))))
69 ;;; ---------------------------------------------------------------------------
70 ;;; testing a simple function
71 ;;; ---------------------------------------------------------------------------
73 ;; !!! Incorrect definition
74 (defun dotted-pair-p (putative-pair)
75 (and (consp putative-pair)
76 (cdr putative-pair)))
78 ;;; ---------------------------------------------------------------------------
80 (deftestsuite test-dotted-pair-p ()
82 (:tests
83 ((ensure (dotted-pair-p '(a . b))))
84 ((ensure (not (dotted-pair-p '(a b)))))
85 ((ensure (not (dotted-pair-p :a))))
86 ((ensure (not (dotted-pair-p '(a b . c)))))
87 ((ensure (not (dotted-pair-p nil))))))
88 ;; ==> #<Results for TEST-DOTTED-PAIR-P [5 Tests, 2 Failures]>
90 (describe (run-tests))
91 ;; ==> (prints)
92 Test Report for TEST-DOTTED-PAIR-P: 5 tests run, 2 Failures.
94 Failure: TEST-2
95 Condition: Ensure failed: (NOT (DOTTED-PAIR-P '(A B)))
97 Code : ((ENSURE (NOT (DOTTED-PAIR-P '(A B)))))
99 Failure: TEST-4
100 Condition: Ensure failed: (NOT (DOTTED-PAIR-P '(A B . C)))
102 Code : ((ENSURE (NOT (DOTTED-PAIR-P '(A B . C)))))
104 ;;; ---------------------------------------------------------------------------
106 ;; !!! Correct the defintion and run tests again
107 (defun dotted-pair-p (putative-pair)
108 (and (consp putative-pair)
109 (cdr putative-pair)
110 (not (consp (cdr putative-pair)))))
112 ;;; ---------------------------------------------------------------------------
114 (describe (run-tests))
115 ;; ==> Prints
116 Test Report for TEST-DOTTED-PAIR-P: 5 tests run, all passed!
119 ;;; ---------------------------------------------------------------------------
120 ;;; a test suite using slots
121 ;;; ---------------------------------------------------------------------------
123 (defun nearly-zero-p (number &optional (tolerance 0.0001))
124 (< (abs number) tolerance))
126 (progn
127 (deftestsuite test-nearly-zero-p ()
128 ((the-number-zero 0.0)
129 (not-nearly-zero 10000.0)
130 (close-to-zero 0.000000001)
131 (close-but-no-cigar 0.01)))
133 (addtest (test-nearly-zero-p)
134 (ensure (nearly-zero-p the-number-zero)))
136 (addtest (test-nearly-zero-p)
137 (ensure (not (nearly-zero-p not-nearly-zero))))
139 (addtest (test-nearly-zero-p)
140 (ensure (nearly-zero-p close-to-zero)))
142 (addtest (test-nearly-zero-p)
143 (ensure (not (nearly-zero-p close-but-no-cigar))))
145 (addtest (test-nearly-zero-p)
146 (ensure (nearly-zero-p close-but-no-cigar 0.1))))
149 (deftestsuite lift-examples () ())
151 (addtest (lift-examples)
152 (:documentation "This is the best test of all")
153 (let ((foo 1)
154 (faa 2)
155 (bar 3))
156 (setf foo (+ foo faa bar))
157 (setf foo 2)
158 (ensure (= (+ foo faa bar) (* foo faa bar)))))
160 (addtest (lift-examples)
161 (:documentation "This is the best test of all")
162 (let ((foo 1)
163 (faa 2)
164 (bar 3))
165 (setf foo (+ foo faa bar))
166 (setf foo 2)
167 (ensure (= (+ foo faa bar) (* foo far bar)))))
169 (addtest (lift-examples)
170 (ensure (= 2 3)))
172 (addtest (lift-examples)
173 (ensure (= 2 2)))
175 (addtest (lift-examples)
176 test-warning-2
177 (ensure-warning (+ 2 3)))
179 (addtest (lift-examples)
180 test-warning
181 (ensure-warning (warn "Help!")))
183 (addtest (lift-examples)
184 (:documentation "Testing ensure-same, should pass.")
185 (ensure-same (values "1" "2" "3") (values "1" "2" "3") :test #'string-equal))
187 (addtest (lift-examples)
188 (:documentation "Testing ensure-equal, should fail")
189 (ensure-same (values "1" "2" "3") (values "1" "2" "3") :test #'eql))
191 (addtest (lift-examples)
192 (ensure-error (warn "This test fails because a warning
193 is not an error.")))
195 (addTest (lift-examples)
196 (:documentation "This test will be logged as a
197 failure because no error will be generated.")
198 (ensure-warning (= 2 2)))
200 (addTest (lift-examples)
201 (:documentation "This test succeeds!")
202 (ensure-error (let ((x 0)) (print (/ 4 x)))))
204 (addTest (lift-examples)
205 (:documentation "This test should fail. Tests a bug where a warning would abort the test with no message.")
206 (warn "A test warning")
207 (ensure-same 1 2))
209 (run-tests :suite 'lift-examples)
211 ;;; ---------------------------------------------------------------------------
212 ;;;
213 ;;; ---------------------------------------------------------------------------
215 (deftestsuite more-lift-examples (lift-examples)
216 ((var-1 1))
217 (:documentation "More Examples")
218 (:test (test-initial-slot-value (ensure (= var-1 1))))
219 (:test ((ensure (= (1+ var-1) 2))))
220 (:test ((setf var-1 0) (ensure (= (1+ var-1) 1))))
221 (:test ((setf var-1 0) (ensure-warning (/ var-1))))
222 (:test ((setf var-1 0) (/ var-1) :documentation "Wow")))
224 (deftestsuite more-lift-examples (lift-examples)
225 ((var-1 1)
226 (var-2 2)))
228 (addtest (more-lift-examples)
229 test-initial-slot-value
230 (ensure (= var-1 1)))
231 (remove-test)
232 (addtest (more-lift-examples)
233 (ensure (= (1+ var-1) 2)))
234 (addtest (more-lift-examples)
235 (setf var-1 0)
236 (ensure (= (1+ var-1) 1)))
237 (addtest (more-lift-examples)
238 (setf var-1 0)
239 (ensure-warning (/ var-1)))
242 (addtest (more-lift-examples)
243 test-initial-slot-value
244 (ensure-same var-1 1))
246 (addtest (more-lift-examples)
247 test-initial-slot-value
248 (ensure-same "Hello" (concatenate 'string "he" "ll" "o")))
250 (addtest (more-lift-examples)
251 test-initial-slot-value
252 (ensure-same 1.23 1.23))
254 (addtest (more-lift-examples)
255 test-initial-slot-value
256 (ensure-same (floor 5/3) (values 1 2/3) :test #'=))
259 (addtest (more-lift-examples)
260 test-initial-slot-value
261 (ensure-same var-1 2))
263 (addtest (more-lift-examples)
264 test-initial-slot-value
265 (ensure-same var-1 1 :report "Var-1 is ~A, not 1." :args (list var-1)))
267 (addtest (more-lift-examples)
268 test-initial-slot-value
269 (ensure-same var-1 1 :report (lambda ()
270 (format nil "Var-1 is ~A, not 1." var-1))))
272 (addtest (more-lift-examples)
273 test-initial-slot-value
274 (ensure-same var-1 1 :report ("Var-1 is ~A, not 1." var-1)))
277 ;;; ---------------------------------------------------------------------------
278 ;;; compare with fiveam
279 ;;; ---------------------------------------------------------------------------
281 (deftestsuite my-suite ()
283 (:documentation "My example suite")
284 (:tests
285 ((ensure-same 4 (+ 2 2)))
286 ((ensure-same 0 (+ -1 1)))
287 ((ensure-error (+ 'foo 4)))
288 ((ensure-same 0 (+ 1 1) :report "This should fail."))))