forked from mighty-gerbils/gerbil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.ss
304 lines (256 loc) · 8.25 KB
/
test.ss
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
;;; -*- Gerbil -*-
;;; (C) vyzo at hackzen.org
;;; Unit testing support
package: std
(import :gerbil/gambit
:std/error
:std/sugar
:std/format)
(export
test-suite test-case
check checkf
check-eq? check-not-eq?
check-eqv? check-not-eqv?
check-equal? check-not-equal?
check-output check-predicate check-exception
!check-fail? !check-fail-e
run-tests! test-report-summary!
run-test-suite!
test-result)
(defstruct !check-fail (e value loc))
(defstruct !check-error (exn check loc))
(defstruct !test-suite (desc thunk tests))
(defstruct !test-case (desc checks fail error))
(defmethod {display-exception !check-error}
(lambda (self port)
(with ((!check-error exn check loc) self)
(fprintf port "~a at ~a: " check loc)
(display-exception exn port))))
;; this is only necessary for stray checks outside a test-case
(defmethod {display-exception !check-fail}
(lambda (self port)
(with ((!check-fail check value loc) self)
(fprintf port "check ~a at ~a FAILED: ~a~n"
check loc value))))
(def *test-verbose* #t)
(def (set-test-verbose! val)
(set! *test-verbose* val))
(def (verbose fmt . args)
(when *test-verbose*
(apply printf fmt args)))
(defrules test-suite ()
((_ desc body ...)
(stx-string? #'desc)
(make-test-suite desc (lambda () body ...))))
(defrules test-case ()
((_ desc body ...)
(stx-string? #'desc)
(run-test-case! desc (lambda () body ...))))
(defrules check (=> ?)
((_ expr => value)
(check-equal? expr value))
((_ expr ? pred)
(check-predicate expr (? pred)))
((eqf expr value)
(checkf eqf expr value)))
(defrules print-check-e ()
((_ expr eqv value)
(verbose "... check ~a is ~a to ~s~n" 'expr 'eqv value)))
(defrules checkf ()
((_ eqf expr value)
(let (val value)
(print-check-e expr eqf val)
(test-check-e '(check eqf expr value) eqf (lambda () expr) val
(location expr)))))
(defrules check-eq? ()
((_ expr value)
(checkf eq? expr value)))
(defrules check-not-eq? ()
((_ expr value)
(checkf not-eq? expr value)))
(def (not-eq? x y)
(not (eq? x y)))
(defrules check-eqv? ()
((_ expr value)
(checkf eqv? expr value)))
(defrules check-not-eqv? ()
((_ expr value)
(checkf not-eqv? expr value)))
(def (not-eqv? x y)
(not (eqv? x y)))
(defrules check-equal? ()
((_ expr value)
(let (val value)
(print-check-e expr equal? val)
(test-check-e '(check equal? expr value) equal-values? (lambda () expr) val
(location expr)))))
(defrules check-not-equal? ()
((_ expr value)
(let (val value)
(print-check-e expr equal? value)
(test-check-e '(check not-equal? expr value) not-equal-values? (lambda () expr) val
(location expr)))))
(def (equal-values? obj-a obj-b)
(if (##values? obj-a)
(and (##values? obj-b)
(equal? (##vector->list obj-a)
(##vector->list obj-b)))
(equal? obj-a obj-b)))
(def (not-equal-values? x y)
(not (equal-values? x y)))
(defrules check-output ()
((_ expr value)
(let (val value)
(verbose "... check ~a outputs ~s~n" 'expr val)
(test-check-output '(check-output expr value) (lambda () expr) value
(location expr)))))
(defrules check-predicate ()
((_ expr pred)
(begin
(verbose "... check ~a is ~a~n" 'expr 'pred)
(test-check-predicate '(check-predicate expr pred) (lambda () expr) pred
(location expr)))))
(defrules check-exception ()
((_ expr exn-pred)
(begin
(verbose "... check ~a raises ~a~n" 'expr 'exn-pred)
(test-check-exception '(check-exception expr pred) (lambda () expr) exn-pred
(location expr)))))
(defsyntax (location stx)
(syntax-case stx ()
((_ expr)
(with-syntax ((loc
(cond
((stx-source #'expr)
=> (lambda (loc)
(call-with-output-string "" (cut ##display-locat loc #t <>))))
(else '?))))
#'(quote loc)))))
(def (with-check-error thunk what loc)
(try
(thunk)
(catch (e)
(raise (make-!check-error e what loc)))))
(def current-test-case
(make-parameter #f))
(def current-test-suite
(make-parameter #f))
(def *tests* [])
(def (make-test-suite desc thunk)
(make-!test-suite desc thunk []))
(def (run-tests! suite . more)
(test-begin!)
(run-test-suite! suite)
(for-each run-test-suite! more))
(def (test-report-summary!)
(let (tests (reverse *tests*))
(unless (null? tests)
(eprintf "--- Test Summary\n")
(for-each test-suite-summary! tests)
(eprintf "~a~n" (test-result)))))
(def (test-result)
(let lp ((rest *tests*))
(match rest
([suite . rest]
(if (ormap (? (or !test-case-fail !test-case-error))
(!test-suite-tests suite))
'FAILURE
(lp rest)))
(else 'OK))))
(def (test-suite-summary! suite)
(def (print-failed tc)
(cond
((!test-case-fail tc)
=> (lambda (fail)
(eprintf "~a: Check FAILED ~a at ~a~n"
(!test-case-desc tc)
(!check-fail-e fail)
(!check-fail-loc fail))))
((!test-case-error tc)
=> (lambda (exn)
(eprintf "~a: ERROR " (!test-case-desc tc))
(display-exception exn (current-error-port))))))
(let (tests (!test-suite-tests suite))
(if (ormap (? (or !test-case-fail !test-case-error))
tests)
(begin
(eprintf "~a: FAILED~n" (!test-suite-desc suite))
(for-each print-failed tests))
(eprintf "~a: OK~n" (!test-suite-desc suite)))))
(def (test-begin!)
(set! *tests* []))
(def (test-add-test! suite)
(set! *tests* (cons suite *tests*)))
(def (run-test-suite! suite)
(test-suite-begin! suite)
(parameterize ((current-test-suite suite))
((!test-suite-thunk suite)))
(test-suite-end! suite))
(def (test-suite-begin! suite)
(set! (!test-suite-tests suite) [])
(test-add-test! suite)
(eprintf "Test suite: ~a~n" (!test-suite-desc suite)))
(def (test-suite-end! suite)
(if (find (? (or !test-case-fail !test-case-error))
(!test-suite-tests suite))
(eprintf "*** Test FAILED~n")
(eprintf "... All tests OK~n")))
(def (test-suite-add-test! suite tc)
(set! (!test-suite-tests suite)
(cons tc (!test-suite-tests suite))))
(def (run-test-case! desc thunk)
(let (tc (make-!test-case desc 0 #f #f))
(test-case-begin! tc)
(try
(parameterize ((current-test-case tc))
(thunk))
(catch (!check-fail? e)
(set! (!test-case-fail tc) e))
(catch (e)
(set! (!test-case-error tc) e)))
(when *test-verbose*
(force-output))
(test-case-end! tc)))
(def (test-case-begin! tc)
(eprintf "Test case: ~a~n" (!test-case-desc tc))
(cond
((current-test-suite)
=> (cut test-suite-add-test! <> tc))))
(def (test-case-end! tc)
(cond
((!test-case-fail tc)
=> (lambda (fail)
(eprintf "*** FAILED: ~a at ~a; value: ~s~n"
(!check-fail-e fail)
(!check-fail-loc fail)
(!check-fail-value fail))))
((!test-case-error tc)
=> (lambda (e)
(eprintf "*** ERROR: ")
(display-exception e (current-error-port))))
(else
(eprintf "... ~a checks OK~n" (!test-case-checks tc)))))
(def (test-case-add-check! tc)
(when tc
(set! (!test-case-checks tc)
(fx1+ (!test-case-checks tc)))))
(def (test-check-e what eqf thunk value loc)
(test-case-add-check! (current-test-case))
(let (val (with-check-error thunk what loc))
(unless (eqf val value)
(raise (make-!check-fail what val loc)))))
(def (test-check-output what thunk value loc)
(test-case-add-check! (current-test-case))
(let (val (with-output-to-string [] (cut with-check-error thunk what loc)))
(unless (equal? val value)
(raise (make-!check-fail what val loc)))))
(def (test-check-predicate what thunk pred loc)
(test-case-add-check! (current-test-case))
(let (val (with-check-error thunk what loc))
(unless (pred val)
(raise (make-!check-fail what val loc)))))
(def (test-check-exception what thunk pred loc)
(test-case-add-check! (current-test-case))
(let (val (with-catch values thunk))
(unless (pred val)
(raise (make-!check-fail what val loc)))))