forked from mighty-gerbils/gerbil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.ss
323 lines (274 loc) · 9.41 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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
;;; -*- Gerbil -*-
;;; (C) vyzo at hackzen.org
;;; Unit testing support
(import :gerbil/gambit
:std/error
:std/misc/list
: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!
run-test-suite!
set-test-verbose!
test-begin!
test-result
test-report-summary!)
(defstruct !check-exception ())
(defstruct (!check-fail !check-exception) (e value expected loc))
(defstruct (!check-error !check-exception) (exn check loc))
(defstruct !test-suite (desc thunk tests error))
(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 expected loc) self)
(fprintf port "check ~a at ~a FAILED: ~s [expected: ~s]~n"
check loc value expected))))
(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 body-rest ...)
(stx-string? #'desc)
(make-test-suite desc (lambda () body body-rest ...))))
(defrule (test-case desc body body-rest ...)
(run-test-case! desc (lambda () body body-rest ...)))
(defrules check (=> ?)
((_ expr => value)
(check-equal? expr value))
((_ expr => value :: eqf)
(checkf eqf expr value))
((_ expr ? pred)
(check-predicate expr (? pred)))
((_ eqf expr value)
(checkf eqf expr value)))
(defrule (print-check-e expr eqv value)
(verbose "... check ~a is ~a to ~s~n" 'expr 'eqv value))
(defrule (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))))
(defrule (check-eq? expr value)
(checkf eq? expr value))
(defrule (check-not-eq? expr value)
(checkf not-eq? expr value))
(def (not-eq? x y)
(not (eq? x y)))
(defrule (check-eqv? expr value)
(checkf eqv? expr value))
(defrule (check-not-eqv? expr value)
(checkf not-eqv? expr value))
(def (not-eqv? x y)
(not (eqv? x y)))
(defrule (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))))
(defrule (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)))
(defrule (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))))
(defrule (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))))
(defrule (check-exception expr exn-pred)
(begin
(verbose "... check ~a raises ~a~n" 'expr 'exn-pred)
(test-check-exception '(check-exception expr exn-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
(with-stack-trace 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 [] #f))
(def (run-tests! suite . more)
(test-begin!)
(foldl (lambda (s r) (and (run-test-suite! s) r)) #t [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 (or (!test-suite-error suite)
(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))
(cond
((!test-suite-error suite)
=> (lambda (exn)
(eprintf "~a: FAILED~n" (!test-suite-desc suite))
(eprintf "~a: ERROR " (!test-suite-desc suite))
(display-exception exn (current-error-port))))
((ormap (? (or !test-case-fail !test-case-error))
tests)
(eprintf "~a: FAILED~n" (!test-suite-desc suite))
(for-each print-failed tests))
(else
(eprintf "~a: OK~n" (!test-suite-desc suite))))))
(def (test-begin!)
(set! *tests* []))
(def (test-add-test! suite)
(push! suite *tests*))
(def (with-stack-trace thunk)
(with-exception-handler
(let (E (current-exception-handler))
(lambda (exn)
(##continuation-capture
(lambda (cont)
(unless (!check-exception? exn)
(dump-stack-trace! cont exn (current-error-port)))
(E exn)))))
thunk))
(def (run-test-suite! suite)
(test-suite-begin! suite)
(try
(parameterize ((current-test-suite suite))
(with-stack-trace (!test-suite-thunk suite)))
(catch (e)
(set! (!test-suite-error suite) e)))
(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 (or (!test-suite-error suite)
(find (? (or !test-case-fail !test-case-error))
(!test-suite-tests suite)))
(begin (eprintf "*** Test FAILED~n") #f)
(begin (eprintf "... All tests OK~n") #t)))
(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))
(with-stack-trace 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; expected: ~s~n"
(!check-fail-e fail)
(!check-fail-loc fail)
(!check-fail-value fail)
(!check-fail-expected 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 value 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 value 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 "(predicate check)" loc)))))
(def (test-check-exception what thunk pred loc)
(test-case-add-check! (current-test-case))
(let/cc success
(let/cc fail-to-throw
(let ((val (with-catch values (lambda () (thunk) (fail-to-throw)))))
(if (pred val)
(success)
(raise (make-!check-fail what val "(exception check)" loc)))))
(raise (make-!check-fail what "(failed to throw an exception)" "(exception check)" loc))))