-
Notifications
You must be signed in to change notification settings - Fork 4
/
test.sld
305 lines (276 loc) · 10.6 KB
/
test.sld
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
(define-library (schemepunk test)
(export test-suite test end-test-runner
assert-true assert-false
assert-eq assert-eqv assert-equal assert-approximate
fail failure?
test-begin test-end test-group
test-assert test-error
test-eq test-eqv test-equal test-approximate
current-test-comparator chibi-test-shim)
(import (scheme base)
(scheme write)
(scheme process-context)
(scheme cxr)
(schemepunk syntax)
(only (schemepunk list) snoc last drop-right)
(schemepunk show)
(schemepunk show report))
(cond-expand
(chicken
(import (only (chicken condition) condition? print-error-message))
(begin
(define (test-error? e) (or (error-object? e) (condition? e)))
(define write-test-error print-error-message)))
(gauche
(import (only (gauche base) report-error))
(begin
(define test-error? error-object?)
(define write-test-error report-error)))
(chibi
(import (only (chibi) print-exception))
(begin
(define test-error? error-object?)
(define (write-test-error err port)
(parameterize ((current-error-port port))
(print-exception err)))))
(gerbil
(import (only (gerbil core) exception? error-message error-irritants error-trace))
(begin
(define (test-error? e) (or (error-object? e) (exception? e)))
(define (write-test-error err port)
(format port "Test raised error:~%~%~a~%message: ~a~%irritants: ~a~%~a"
err
(error-message err)
(error-irritants err)
(error-trace err)))))
(kawa
(import (only (kawa reflect) instance?)
(class java.lang Exception))
(begin
(define (test-error? e) (instance? e Exception))
(define (write-test-error err port)
(err:printStackTrace port))))
((library (rnrs conditions))
(import (rnrs conditions))
(begin
(define (test-error? e) (or (error-object? e) (condition? e)))
(define (write-test-error err port)
(format port "Test raised error:~%~%~a" err)
(cond
((condition? err)
(when (who-condition? err)
(format port "~%who: ~a" (condition-who err)))
(when (message-condition? err)
(format port "~%message: ~a" (condition-message err)))
(when (irritants-condition? err)
(format port "~%irritants: ~a" (condition-irritants err))))
((error-object? err)
(format port "~%message: ~a~%irritants: ~a"
(error-object-message err)
(error-object-irritants err)))))))
(else
(begin
(define test-error? error-object?)
(define (write-test-error err port)
(format port "Test raised error:~%~%~a~%message: ~a~%irritants: ~a"
err
(error-object-message err)
(error-object-irritants err))))))
(begin
(define passed-count (list 0))
(define failed (list '()))
(define test-result-formats '())
(define current-test-group '())
(define (indent)
(make-string (* (length current-test-group) 2) #\space))
(define (test-begin name)
(set! test-result-formats (cons '() test-result-formats))
(set! passed-count (cons 0 passed-count))
(set! failed (cons '() failed))
(set! current-test-group (snoc current-test-group name)))
(define (test-end name)
(cond
((null? current-test-group)
(error "test-end without test-begin" (list name)))
((equal? (last current-test-group) name)
(set! current-test-group (drop-right current-test-group 1))
(let* ((group-passed (car passed-count))
(group-failed (car failed))
(counts (each " ("
group-passed
"/"
(+ group-passed (length group-failed))
")"))
(fmt (if (null? group-failed)
(each (indent)
(as-bold (as-green "✓ " name counts))
nl)
(each (indent)
(as-bold (as-red "✗ " name counts ":"))
nl
(each-in-list (car test-result-formats))))))
(set! test-result-formats (cdr test-result-formats))
(set! passed-count (cdr passed-count))
(set-car! passed-count (+ group-passed (car passed-count)))
(set! failed (cdr failed))
(set-car! failed (append (car failed) group-failed))
(if (pair? test-result-formats)
(set-car! test-result-formats (cons fmt (car test-result-formats)))
(begin (show #t fmt)
(flush-output-port)))))
(else
(error "test-end name does not match test-begin"
(list name (last current-test-group))))))
(define-syntax test-group
(syntax-rules ()
((_ name . body)
(let1 group-name name
(test-begin group-name)
(inline-defines . body)
(test-end group-name)))))
(define-syntax test-suite
(syntax-rules ()
((_ . args) (test-group . args))))
(define (pass-test name)
(set-car! passed-count (+ (car passed-count) 1))
(set-car! test-result-formats
`(,@(car test-result-formats) ,(indent) ,(as-green "✓ " name) ,nl)))
(define (fail-test name err)
(set-car! failed (snoc (car failed) `(,current-test-group ,name ,err)))
(set-car! test-result-formats
`(,@(car test-result-formats) ,(indent) ,(as-red "✗ " name) ,nl)))
(define-syntax test
(syntax-rules ()
((test name body ...)
(guard (err ((test-error? err)
(fail-test name
(let ((str (open-output-string)))
(write-test-error err str)
(as-red (get-output-string str)))))
(#t (fail-test name err)))
(begin (inline-defines body ...)
(pass-test name))))))
(define (fail . xs)
(raise `(test-failure ,@xs)))
(define-syntax assert-true
(syntax-rules ()
((assert-true condition)
(unless condition (fail '(assert-true condition))))
((assert-true msg condition)
(unless condition (fail msg)))))
(define-syntax assert-false
(syntax-rules ()
((assert-true condition)
(when condition (fail '(assert-false condition))))
((assert-true msg condition)
(when condition (fail msg)))))
(define current-test-comparator (make-parameter equal?))
(define (assert-eq actual expected)
(parameterize ((current-test-comparator eq?))
(assert-equal actual expected)))
(define (assert-eqv actual expected)
(parameterize ((current-test-comparator eqv?))
(assert-equal actual expected)))
(define (assert-equal actual expected)
(unless ((current-test-comparator) actual expected)
(fail (pretty-color actual) (pretty-color expected))))
(define (assert-approximate actual expected error)
(unless (and (number? actual)
(>= actual (- expected error))
(<= actual (+ expected error)))
(fail (pretty-color actual) (pretty-color expected))))
(define-syntax test-assert
(syntax-rules ()
((_ name expr) (test name (assert-true expr)))
((_ expr) (test (format #f "~s" 'expr) (assert-true expr)))))
(define-syntax test-eq
(syntax-rules ()
((_ name expected actual)
(test name
(assert-eq actual expected)))
((_ expected actual)
(test (format #f "~s" 'actual)
(assert-eq actual expected)))))
(define-syntax test-eqv
(syntax-rules ()
((_ name expected actual)
(test name
(assert-eqv actual expected)))
((_ expected actual)
(test (format #f "~s" 'actual)
(assert-eqv actual expected)))))
(define-syntax test-equal
(syntax-rules ()
((_ name expected actual)
(test name
(assert-equal actual expected)))
((_ expected actual)
(test (format #f "~s" 'actual)
(assert-equal actual expected)))))
(define-syntax test-approximate
(syntax-rules ()
((_ name expected actual error)
(test name
(assert-approximate actual expected error)))
((_ expected actual error)
(test (format #f "~s" 'actual)
(assert-approximate actual expected error)))))
(define-syntax test-error
(syntax-rules ()
((_ name #t . body)
(test name
(guard (e ((not (failure? e)) #t))
(begin . body)
(fail "did not raise error"))))
((_ name expr)
(test-error name #t expr))
((_ expr)
(test-error (format #f "~s" 'expr) #t expr))))
(define (failure? x)
(and (pair? x) (eq? (car x) 'test-failure)))
(define (reported-failure suite test err)
(reported test
(match err
(('test-failure actual expected)
(wrapped/blocks
"Expected " expected " but got " actual))
(('test-failure (? string? msg))
(wrapped msg))
(('test-failure (? procedure? fmt))
fmt)
(('test-failure value)
(pretty-color value))
((? procedure? fmt)
err)
(else
(wrapped/blocks
(as-red "Test raised error: ")
(pretty-color err))))))
(define-syntax chibi-test-shim
(syntax-rules ()
((_ test-name . chibi-test-body)
(let-syntax
((test-name
(syntax-rules ()
((_ . args) (test-equal . args)))))
. chibi-test-body))))
(define (end-test-runner)
(define failed-count (length (car failed)))
(show #t
nl
(as-bold (as-green
(car passed-count)
(if (= 1 (car passed-count)) " test" " tests")
" passed"))
nl)
(unless (zero? failed-count)
(show #t
(as-bold (as-red
failed-count
(if (= 1 failed-count) " test" " tests")
" failed"))
nl
(each-in-list (map (cut apply reported-failure <>) (car failed)))
report-line))
(flush-output-port)
(exit failed-count))))