Commit | Line | Data |
---|---|---|
000ee07f | 1 | ;;;; test-suite/lib.scm --- generic support for testing |
0ba0b384 | 2 | ;;;; Copyright (C) 1999, 2000, 2001, 2004, 2006, 2007, 2009 Free Software Foundation, Inc. |
bba2d190 | 3 | ;;;; |
53befeb7 NJ |
4 | ;;;; This program is free software; you can redistribute it and/or |
5 | ;;;; modify it under the terms of the GNU Lesser General Public | |
6 | ;;;; License as published by the Free Software Foundation; either | |
7 | ;;;; version 3, or (at your option) any later version. | |
bba2d190 | 8 | ;;;; |
000ee07f JB |
9 | ;;;; This program is distributed in the hope that it will be useful, |
10 | ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
53befeb7 | 12 | ;;;; GNU Lesser General Public License for more details. |
bba2d190 | 13 | ;;;; |
53befeb7 NJ |
14 | ;;;; You should have received a copy of the GNU Lesser General Public |
15 | ;;;; License along with this software; see the file COPYING.LESSER. | |
16 | ;;;; If not, write to the Free Software Foundation, Inc., 51 Franklin | |
17 | ;;;; Street, Fifth Floor, Boston, MA 02110-1301 USA | |
000ee07f | 18 | |
6b4113af | 19 | (define-module (test-suite lib) |
8bc4547c | 20 | :use-module (ice-9 stack-catch) |
1a179b03 MD |
21 | :use-module (ice-9 regex) |
22 | :export ( | |
000ee07f | 23 | |
6b4113af | 24 | ;; Exceptions which are commonly being tested for. |
d6754c23 | 25 | exception:bad-variable |
d6e04e7c | 26 | exception:missing-expression |
08c608e1 | 27 | exception:out-of-range exception:unbound-var |
0ac46745 | 28 | exception:used-before-defined |
08c608e1 | 29 | exception:wrong-num-args exception:wrong-type-arg |
0825ae0b | 30 | exception:numerical-overflow |
d15ad007 | 31 | exception:struct-set!-denied |
a1ef7406 | 32 | exception:system-error |
d15ad007 | 33 | exception:miscellaneous-error |
970aac16 | 34 | exception:string-contains-nul |
0ba0b384 | 35 | exception:read-error |
6b4113af | 36 | |
000ee07f | 37 | ;; Reporting passes and failures. |
6b4113af DH |
38 | run-test |
39 | pass-if expect-fail | |
40 | pass-if-exception expect-fail-exception | |
000ee07f JB |
41 | |
42 | ;; Naming groups of tests in a regular fashion. | |
43 | with-test-prefix with-test-prefix* current-test-prefix | |
d6e04e7c | 44 | format-test-name |
000ee07f | 45 | |
2d04022c NJ |
46 | ;; Using the debugging evaluator. |
47 | with-debugging-evaluator with-debugging-evaluator* | |
48 | ||
000ee07f JB |
49 | ;; Reporting results in various ways. |
50 | register-reporter unregister-reporter reporter-registered? | |
51 | make-count-reporter print-counts | |
bba2d190 | 52 | make-log-reporter |
087dab1c | 53 | full-reporter |
d6e04e7c | 54 | user-reporter)) |
000ee07f JB |
55 | |
56 | ||
57 | ;;;; If you're using Emacs's Scheme mode: | |
000ee07f JB |
58 | ;;;; (put 'with-test-prefix 'scheme-indent-function 1) |
59 | ||
60 | \f | |
57e7f270 DH |
61 | ;;;; CORE FUNCTIONS |
62 | ;;;; | |
63 | ;;;; The function (run-test name expected-result thunk) is the heart of the | |
64 | ;;;; testing environment. The first parameter NAME is a unique name for the | |
65 | ;;;; test to be executed (for an explanation of this parameter see below under | |
66 | ;;;; TEST NAMES). The second parameter EXPECTED-RESULT is a boolean value | |
67 | ;;;; that indicates whether the corresponding test is expected to pass. If | |
68 | ;;;; EXPECTED-RESULT is #t the test is expected to pass, if EXPECTED-RESULT is | |
69 | ;;;; #f the test is expected to fail. Finally, THUNK is the function that | |
70 | ;;;; actually performs the test. For example: | |
71 | ;;;; | |
72 | ;;;; (run-test "integer addition" #t (lambda () (= 2 (+ 1 1)))) | |
73 | ;;;; | |
74 | ;;;; To report success, THUNK should either return #t or throw 'pass. To | |
75 | ;;;; report failure, THUNK should either return #f or throw 'fail. If THUNK | |
76 | ;;;; returns a non boolean value or throws 'unresolved, this indicates that | |
77 | ;;;; the test did not perform as expected. For example the property that was | |
78 | ;;;; to be tested could not be tested because something else went wrong. | |
79 | ;;;; THUNK may also throw 'untested to indicate that the test was deliberately | |
80 | ;;;; not performed, for example because the test case is not complete yet. | |
81 | ;;;; Finally, if THUNK throws 'unsupported, this indicates that this test | |
82 | ;;;; requires some feature that is not available in the configured testing | |
83 | ;;;; environment. All other exceptions thrown by THUNK are considered as | |
84 | ;;;; errors. | |
85 | ;;;; | |
6b4113af DH |
86 | ;;;; |
87 | ;;;; Convenience macros for tests expected to pass or fail | |
88 | ;;;; | |
bba2d190 | 89 | ;;;; * (pass-if name body) is a short form for |
57e7f270 | 90 | ;;;; (run-test name #t (lambda () body)) |
bba2d190 | 91 | ;;;; * (expect-fail name body) is a short form for |
57e7f270 DH |
92 | ;;;; (run-test name #f (lambda () body)) |
93 | ;;;; | |
bba2d190 | 94 | ;;;; For example: |
57e7f270 DH |
95 | ;;;; |
96 | ;;;; (pass-if "integer addition" (= 2 (+ 1 1))) | |
6b4113af DH |
97 | ;;;; |
98 | ;;;; | |
99 | ;;;; Convenience macros to test for exceptions | |
100 | ;;;; | |
101 | ;;;; The following macros take exception parameters which are pairs | |
102 | ;;;; (type . message), where type is a symbol that denotes an exception type | |
103 | ;;;; like 'wrong-type-arg or 'out-of-range, and message is a string holding a | |
104 | ;;;; regular expression that describes the error message for the exception | |
105 | ;;;; like "Argument .* out of range". | |
106 | ;;;; | |
107 | ;;;; * (pass-if-exception name exception body) will pass if the execution of | |
108 | ;;;; body causes the given exception to be thrown. If no exception is | |
109 | ;;;; thrown, the test fails. If some other exception is thrown, is is an | |
110 | ;;;; error. | |
111 | ;;;; * (expect-fail-exception name exception body) will pass unexpectedly if | |
112 | ;;;; the execution of body causes the given exception to be thrown. If no | |
113 | ;;;; exception is thrown, the test fails expectedly. If some other | |
114 | ;;;; exception is thrown, it is an error. | |
57e7f270 DH |
115 | |
116 | \f | |
000ee07f JB |
117 | ;;;; TEST NAMES |
118 | ;;;; | |
119 | ;;;; Every test in the test suite has a unique name, to help | |
120 | ;;;; developers find tests that are failing (or unexpectedly passing), | |
121 | ;;;; and to help gather statistics. | |
122 | ;;;; | |
123 | ;;;; A test name is a list of printable objects. For example: | |
124 | ;;;; ("ports.scm" "file" "read and write back list of strings") | |
125 | ;;;; ("ports.scm" "pipe" "read") | |
126 | ;;;; | |
127 | ;;;; Test names may contain arbitrary objects, but they always have | |
128 | ;;;; the following properties: | |
129 | ;;;; - Test names can be compared with EQUAL?. | |
130 | ;;;; - Test names can be reliably stored and retrieved with the standard WRITE | |
131 | ;;;; and READ procedures; doing so preserves their identity. | |
bba2d190 | 132 | ;;;; |
000ee07f | 133 | ;;;; For example: |
bba2d190 | 134 | ;;;; |
57e7f270 | 135 | ;;;; (pass-if "simple addition" (= 4 (+ 2 2))) |
bba2d190 | 136 | ;;;; |
000ee07f JB |
137 | ;;;; In that case, the test name is the list ("simple addition"). |
138 | ;;;; | |
e46083d5 DH |
139 | ;;;; In the case of simple tests the expression that is tested would often |
140 | ;;;; suffice as a test name by itself. Therefore, the convenience macros | |
141 | ;;;; pass-if and expect-fail provide a shorthand notation that allows to omit | |
142 | ;;;; a test name in such cases. | |
143 | ;;;; | |
144 | ;;;; * (pass-if expression) is a short form for | |
145 | ;;;; (run-test 'expression #t (lambda () expression)) | |
146 | ;;;; * (expect-fail expression) is a short form for | |
147 | ;;;; (run-test 'expression #f (lambda () expression)) | |
148 | ;;;; | |
149 | ;;;; For example: | |
150 | ;;;; | |
151 | ;;;; (pass-if (= 2 (+ 1 1))) | |
152 | ;;;; | |
000ee07f JB |
153 | ;;;; The WITH-TEST-PREFIX syntax and WITH-TEST-PREFIX* procedure establish |
154 | ;;;; a prefix for the names of all tests whose results are reported | |
155 | ;;;; within their dynamic scope. For example: | |
bba2d190 | 156 | ;;;; |
000ee07f JB |
157 | ;;;; (begin |
158 | ;;;; (with-test-prefix "basic arithmetic" | |
159 | ;;;; (pass-if "addition" (= (+ 2 2) 4)) | |
05c4ba00 | 160 | ;;;; (pass-if "subtraction" (= (- 4 2) 2))) |
000ee07f | 161 | ;;;; (pass-if "multiplication" (= (* 2 2) 4))) |
bba2d190 | 162 | ;;;; |
000ee07f JB |
163 | ;;;; In that example, the three test names are: |
164 | ;;;; ("basic arithmetic" "addition"), | |
05c4ba00 | 165 | ;;;; ("basic arithmetic" "subtraction"), and |
000ee07f JB |
166 | ;;;; ("multiplication"). |
167 | ;;;; | |
168 | ;;;; WITH-TEST-PREFIX can be nested. Each WITH-TEST-PREFIX postpends | |
169 | ;;;; a new element to the current prefix: | |
bba2d190 | 170 | ;;;; |
000ee07f JB |
171 | ;;;; (with-test-prefix "arithmetic" |
172 | ;;;; (with-test-prefix "addition" | |
173 | ;;;; (pass-if "integer" (= (+ 2 2) 4)) | |
174 | ;;;; (pass-if "complex" (= (+ 2+3i 4+5i) 6+8i))) | |
175 | ;;;; (with-test-prefix "subtraction" | |
176 | ;;;; (pass-if "integer" (= (- 2 2) 0)) | |
177 | ;;;; (pass-if "complex" (= (- 2+3i 1+2i) 1+1i)))) | |
bba2d190 | 178 | ;;;; |
000ee07f JB |
179 | ;;;; The four test names here are: |
180 | ;;;; ("arithmetic" "addition" "integer") | |
181 | ;;;; ("arithmetic" "addition" "complex") | |
182 | ;;;; ("arithmetic" "subtraction" "integer") | |
183 | ;;;; ("arithmetic" "subtraction" "complex") | |
184 | ;;;; | |
185 | ;;;; To print a name for a human reader, we DISPLAY its elements, | |
186 | ;;;; separated by ": ". So, the last set of test names would be | |
187 | ;;;; reported as: | |
bba2d190 | 188 | ;;;; |
000ee07f JB |
189 | ;;;; arithmetic: addition: integer |
190 | ;;;; arithmetic: addition: complex | |
191 | ;;;; arithmetic: subtraction: integer | |
192 | ;;;; arithmetic: subtraction: complex | |
193 | ;;;; | |
194 | ;;;; The Guile benchmarks use with-test-prefix to include the name of | |
195 | ;;;; the source file containing the test in the test name, to help | |
196 | ;;;; developers to find failing tests, and to provide each file with its | |
197 | ;;;; own namespace. | |
198 | ||
199 | \f | |
200 | ;;;; REPORTERS | |
bba2d190 | 201 | ;;;; |
000ee07f JB |
202 | ;;;; A reporter is a function which we apply to each test outcome. |
203 | ;;;; Reporters can log results, print interesting results to the | |
204 | ;;;; standard output, collect statistics, etc. | |
bba2d190 | 205 | ;;;; |
57e7f270 DH |
206 | ;;;; A reporter function takes two mandatory arguments, RESULT and TEST, and |
207 | ;;;; possibly additional arguments depending on RESULT; its return value | |
000ee07f JB |
208 | ;;;; is ignored. RESULT has one of the following forms: |
209 | ;;;; | |
bba2d190 | 210 | ;;;; pass - The test named TEST passed. |
57e7f270 DH |
211 | ;;;; Additional arguments are ignored. |
212 | ;;;; upass - The test named TEST passed unexpectedly. | |
213 | ;;;; Additional arguments are ignored. | |
214 | ;;;; fail - The test named TEST failed. | |
215 | ;;;; Additional arguments are ignored. | |
216 | ;;;; xfail - The test named TEST failed, as expected. | |
217 | ;;;; Additional arguments are ignored. | |
218 | ;;;; unresolved - The test named TEST did not perform as expected, for | |
219 | ;;;; example the property that was to be tested could not be | |
220 | ;;;; tested because something else went wrong. | |
221 | ;;;; Additional arguments are ignored. | |
222 | ;;;; untested - The test named TEST was not actually performed, for | |
bba2d190 | 223 | ;;;; example because the test case is not complete yet. |
57e7f270 DH |
224 | ;;;; Additional arguments are ignored. |
225 | ;;;; unsupported - The test named TEST requires some feature that is not | |
226 | ;;;; available in the configured testing environment. | |
227 | ;;;; Additional arguments are ignored. | |
228 | ;;;; error - An error occurred while the test named TEST was | |
229 | ;;;; performed. Since this result means that the system caught | |
230 | ;;;; an exception it could not handle, the exception arguments | |
231 | ;;;; are passed as additional arguments. | |
000ee07f JB |
232 | ;;;; |
233 | ;;;; This library provides some standard reporters for logging results | |
234 | ;;;; to a file, reporting interesting results to the user, and | |
235 | ;;;; collecting totals. | |
087dab1c JB |
236 | ;;;; |
237 | ;;;; You can use the REGISTER-REPORTER function and friends to add | |
238 | ;;;; whatever reporting functions you like. If you don't register any | |
239 | ;;;; reporters, the library uses FULL-REPORTER, which simply writes | |
240 | ;;;; all results to the standard output. | |
000ee07f JB |
241 | |
242 | \f | |
57e7f270 DH |
243 | ;;;; MISCELLANEOUS |
244 | ;;;; | |
245 | ||
6b4113af | 246 | ;;; Define some exceptions which are commonly being tested for. |
d6754c23 DH |
247 | (define exception:bad-variable |
248 | (cons 'syntax-error "Bad variable")) | |
d6e04e7c DH |
249 | (define exception:missing-expression |
250 | (cons 'misc-error "^missing or extra expression")) | |
6b4113af | 251 | (define exception:out-of-range |
a47d845f | 252 | (cons 'out-of-range "^.*out of range")) |
08c608e1 DH |
253 | (define exception:unbound-var |
254 | (cons 'unbound-variable "^Unbound variable")) | |
0ac46745 MV |
255 | (define exception:used-before-defined |
256 | (cons 'unbound-variable "^Variable used before given a value")) | |
08c608e1 DH |
257 | (define exception:wrong-num-args |
258 | (cons 'wrong-number-of-args "^Wrong number of arguments")) | |
6b4113af | 259 | (define exception:wrong-type-arg |
a47d845f | 260 | (cons 'wrong-type-arg "^Wrong type")) |
0825ae0b KR |
261 | (define exception:numerical-overflow |
262 | (cons 'numerical-overflow "^Numerical overflow")) | |
d15ad007 LC |
263 | (define exception:struct-set!-denied |
264 | (cons 'misc-error "^set! denied for field")) | |
a1ef7406 LC |
265 | (define exception:system-error |
266 | (cons 'system-error ".*")) | |
d15ad007 LC |
267 | (define exception:miscellaneous-error |
268 | (cons 'misc-error "^.*")) | |
0ba0b384 LC |
269 | (define exception:read-error |
270 | (cons 'read-error "^.*$")) | |
6b4113af | 271 | |
970aac16 KR |
272 | ;; as per throw in scm_to_locale_stringn() |
273 | (define exception:string-contains-nul | |
274 | (cons 'misc-error "^string contains #\\\\nul character")) | |
275 | ||
276 | ||
57e7f270 DH |
277 | ;;; Display all parameters to the default output port, followed by a newline. |
278 | (define (display-line . objs) | |
279 | (for-each display objs) | |
280 | (newline)) | |
281 | ||
282 | ;;; Display all parameters to the given output port, followed by a newline. | |
283 | (define (display-line-port port . objs) | |
284 | (for-each (lambda (obj) (display obj port)) objs) | |
285 | (newline port)) | |
286 | ||
287 | \f | |
288 | ;;;; CORE FUNCTIONS | |
289 | ;;;; | |
290 | ||
291 | ;;; The central testing routine. | |
292 | ;;; The idea is taken from Greg, the GNUstep regression test environment. | |
293 | (define run-test #f) | |
294 | (let ((test-running #f)) | |
295 | (define (local-run-test name expect-pass thunk) | |
296 | (if test-running | |
297 | (error "Nested calls to run-test are not permitted.") | |
298 | (let ((test-name (full-name name))) | |
299 | (set! test-running #t) | |
300 | (catch #t | |
301 | (lambda () | |
302 | (let ((result (thunk))) | |
303 | (if (eq? result #t) (throw 'pass)) | |
304 | (if (eq? result #f) (throw 'fail)) | |
305 | (throw 'unresolved))) | |
306 | (lambda (key . args) | |
307 | (case key | |
bba2d190 | 308 | ((pass) |
57e7f270 | 309 | (report (if expect-pass 'pass 'upass) test-name)) |
bba2d190 | 310 | ((fail) |
57e7f270 | 311 | (report (if expect-pass 'fail 'xfail) test-name)) |
bba2d190 | 312 | ((unresolved untested unsupported) |
57e7f270 | 313 | (report key test-name)) |
bba2d190 | 314 | ((quit) |
57e7f270 DH |
315 | (report 'unresolved test-name) |
316 | (quit)) | |
bba2d190 | 317 | (else |
57e7f270 DH |
318 | (report 'error test-name (cons key args)))))) |
319 | (set! test-running #f)))) | |
320 | (set! run-test local-run-test)) | |
321 | ||
322 | ;;; A short form for tests that are expected to pass, taken from Greg. | |
ce09ee19 AW |
323 | (define-syntax pass-if |
324 | (syntax-rules () | |
325 | ((_ name) | |
326 | ;; presume this is a simple test, i.e. (pass-if (even? 2)) | |
327 | ;; where the body should also be the name. | |
328 | (run-test 'name #t (lambda () name))) | |
329 | ((_ name rest ...) | |
330 | (run-test name #t (lambda () rest ...))))) | |
57e7f270 DH |
331 | |
332 | ;;; A short form for tests that are expected to fail, taken from Greg. | |
ce09ee19 AW |
333 | (define-syntax expect-fail |
334 | (syntax-rules () | |
335 | ((_ name) | |
336 | ;; presume this is a simple test, i.e. (expect-fail (even? 2)) | |
337 | ;; where the body should also be the name. | |
338 | (run-test 'name #f (lambda () name))) | |
339 | ((_ name rest ...) | |
340 | (run-test name #f (lambda () rest ...))))) | |
57e7f270 | 341 | |
6b4113af DH |
342 | ;;; A helper function to implement the macros that test for exceptions. |
343 | (define (run-test-exception name exception expect-pass thunk) | |
344 | (run-test name expect-pass | |
345 | (lambda () | |
8bc4547c | 346 | (stack-catch (car exception) |
6b4113af | 347 | (lambda () (thunk) #f) |
bba2d190 TTN |
348 | (lambda (key proc message . rest) |
349 | (cond | |
350 | ;; handle explicit key | |
351 | ((string-match (cdr exception) message) | |
352 | #t) | |
353 | ;; handle `(error ...)' which uses `misc-error' for key and doesn't | |
354 | ;; yet format the message and args (we have to do it here). | |
355 | ((and (eq? 'misc-error (car exception)) | |
356 | (list? rest) | |
357 | (string-match (cdr exception) | |
358 | (apply simple-format #f message (car rest)))) | |
359 | #t) | |
e6729603 DH |
360 | ;; handle syntax errors which use `syntax-error' for key and don't |
361 | ;; yet format the message and args (we have to do it here). | |
362 | ((and (eq? 'syntax-error (car exception)) | |
363 | (list? rest) | |
364 | (string-match (cdr exception) | |
365 | (apply simple-format #f message (car rest)))) | |
366 | #t) | |
bba2d190 TTN |
367 | ;; unhandled; throw again |
368 | (else | |
369 | (apply throw key proc message rest)))))))) | |
6b4113af DH |
370 | |
371 | ;;; A short form for tests that expect a certain exception to be thrown. | |
ce09ee19 AW |
372 | (define-syntax pass-if-exception |
373 | (syntax-rules () | |
374 | ((_ name exception body rest ...) | |
375 | (run-test-exception name exception #t (lambda () body rest ...))))) | |
6b4113af DH |
376 | |
377 | ;;; A short form for tests expected to fail to throw a certain exception. | |
ce09ee19 AW |
378 | (define-syntax expect-fail-exception |
379 | (syntax-rules () | |
380 | ((_ name exception body rest ...) | |
381 | (run-test-exception name exception #f (lambda () body rest ...))))) | |
6b4113af | 382 | |
57e7f270 DH |
383 | \f |
384 | ;;;; TEST NAMES | |
385 | ;;;; | |
386 | ||
387 | ;;;; Turn a test name into a nice human-readable string. | |
388 | (define (format-test-name name) | |
389 | (call-with-output-string | |
390 | (lambda (port) | |
391 | (let loop ((name name) | |
392 | (separator "")) | |
393 | (if (pair? name) | |
394 | (begin | |
395 | (display separator port) | |
396 | (display (car name) port) | |
397 | (loop (cdr name) ": "))))))) | |
398 | ||
399 | ;;;; For a given test-name, deliver the full name including all prefixes. | |
400 | (define (full-name name) | |
401 | (append (current-test-prefix) (list name))) | |
000ee07f JB |
402 | |
403 | ;;; A fluid containing the current test prefix, as a list. | |
404 | (define prefix-fluid (make-fluid)) | |
405 | (fluid-set! prefix-fluid '()) | |
57e7f270 DH |
406 | (define (current-test-prefix) |
407 | (fluid-ref prefix-fluid)) | |
000ee07f JB |
408 | |
409 | ;;; Postpend PREFIX to the current name prefix while evaluting THUNK. | |
410 | ;;; The name prefix is only changed within the dynamic scope of the | |
411 | ;;; call to with-test-prefix*. Return the value returned by THUNK. | |
412 | (define (with-test-prefix* prefix thunk) | |
413 | (with-fluids ((prefix-fluid | |
414 | (append (fluid-ref prefix-fluid) (list prefix)))) | |
415 | (thunk))) | |
416 | ||
417 | ;;; (with-test-prefix PREFIX BODY ...) | |
418 | ;;; Postpend PREFIX to the current name prefix while evaluating BODY ... | |
419 | ;;; The name prefix is only changed within the dynamic scope of the | |
420 | ;;; with-test-prefix expression. Return the value returned by the last | |
421 | ;;; BODY expression. | |
422 | (defmacro with-test-prefix (prefix . body) | |
423 | `(with-test-prefix* ,prefix (lambda () ,@body))) | |
424 | ||
2d04022c NJ |
425 | ;;; Call THUNK using the debugging evaluator. |
426 | (define (with-debugging-evaluator* thunk) | |
427 | (let ((dopts #f)) | |
428 | (dynamic-wind | |
429 | (lambda () | |
430 | (set! dopts (debug-options)) | |
431 | (debug-enable 'debug)) | |
432 | thunk | |
433 | (lambda () | |
434 | (debug-options dopts))))) | |
435 | ||
436 | ;;; Evaluate BODY... using the debugging evaluator. | |
437 | (define-macro (with-debugging-evaluator . body) | |
438 | `(with-debugging-evaluator* (lambda () ,@body))) | |
439 | ||
440 | ||
000ee07f | 441 | \f |
57e7f270 | 442 | ;;;; REPORTERS |
bba2d190 | 443 | ;;;; |
000ee07f JB |
444 | |
445 | ;;; The global list of reporters. | |
446 | (define reporters '()) | |
447 | ||
087dab1c JB |
448 | ;;; The default reporter, to be used only if no others exist. |
449 | (define default-reporter #f) | |
450 | ||
000ee07f JB |
451 | ;;; Add the procedure REPORTER to the current set of reporter functions. |
452 | ;;; Signal an error if that reporter procedure object is already registered. | |
453 | (define (register-reporter reporter) | |
454 | (if (memq reporter reporters) | |
455 | (error "register-reporter: reporter already registered: " reporter)) | |
456 | (set! reporters (cons reporter reporters))) | |
457 | ||
458 | ;;; Remove the procedure REPORTER from the current set of reporter | |
459 | ;;; functions. Signal an error if REPORTER is not currently registered. | |
460 | (define (unregister-reporter reporter) | |
461 | (if (memq reporter reporters) | |
462 | (set! reporters (delq! reporter reporters)) | |
463 | (error "unregister-reporter: reporter not registered: " reporter))) | |
464 | ||
465 | ;;; Return true iff REPORTER is in the current set of reporter functions. | |
466 | (define (reporter-registered? reporter) | |
467 | (if (memq reporter reporters) #t #f)) | |
468 | ||
000ee07f | 469 | ;;; Send RESULT to all currently registered reporter functions. |
57e7f270 | 470 | (define (report . args) |
087dab1c | 471 | (if (pair? reporters) |
57e7f270 | 472 | (for-each (lambda (reporter) (apply reporter args)) |
087dab1c | 473 | reporters) |
57e7f270 | 474 | (apply default-reporter args))) |
000ee07f JB |
475 | |
476 | \f | |
57e7f270 DH |
477 | ;;;; Some useful standard reporters: |
478 | ;;;; Count reporters count the occurrence of each test result type. | |
479 | ;;;; Log reporters write all test results to a given log file. | |
480 | ;;;; Full reporters write all test results to the standard output. | |
481 | ;;;; User reporters write interesting test results to the standard output. | |
482 | ||
483 | ;;; The complete list of possible test results. | |
bba2d190 | 484 | (define result-tags |
57e7f270 DH |
485 | '((pass "PASS" "passes: ") |
486 | (fail "FAIL" "failures: ") | |
487 | (upass "UPASS" "unexpected passes: ") | |
488 | (xfail "XFAIL" "expected failures: ") | |
489 | (unresolved "UNRESOLVED" "unresolved test cases: ") | |
490 | (untested "UNTESTED" "untested test cases: ") | |
491 | (unsupported "UNSUPPORTED" "unsupported test cases: ") | |
492 | (error "ERROR" "errors: "))) | |
493 | ||
494 | ;;; The list of important test results. | |
bba2d190 | 495 | (define important-result-tags |
57e7f270 DH |
496 | '(fail upass unresolved error)) |
497 | ||
498 | ;;; Display a single test result in formatted form to the given port | |
499 | (define (print-result port result name . args) | |
500 | (let* ((tag (assq result result-tags)) | |
501 | (label (if tag (cadr tag) #f))) | |
502 | (if label | |
503 | (begin | |
504 | (display label port) | |
505 | (display ": " port) | |
506 | (display (format-test-name name) port) | |
507 | (if (pair? args) | |
508 | (begin | |
509 | (display " - arguments: " port) | |
510 | (write args port))) | |
511 | (newline port)) | |
512 | (error "(test-suite lib) FULL-REPORTER: unrecognized result: " | |
513 | result)))) | |
000ee07f JB |
514 | |
515 | ;;; Return a list of the form (COUNTER RESULTS), where: | |
516 | ;;; - COUNTER is a reporter procedure, and | |
517 | ;;; - RESULTS is a procedure taking no arguments which returns the | |
518 | ;;; results seen so far by COUNTER. The return value is an alist | |
519 | ;;; mapping outcome symbols (`pass', `fail', etc.) onto counts. | |
520 | (define (make-count-reporter) | |
57e7f270 | 521 | (let ((counts (map (lambda (tag) (cons (car tag) 0)) result-tags))) |
000ee07f | 522 | (list |
57e7f270 DH |
523 | (lambda (result name . args) |
524 | (let ((pair (assq result counts))) | |
bba2d190 | 525 | (if pair |
57e7f270 | 526 | (set-cdr! pair (+ 1 (cdr pair))) |
bba2d190 | 527 | (error "count-reporter: unexpected test result: " |
57e7f270 | 528 | (cons result (cons name args)))))) |
000ee07f JB |
529 | (lambda () |
530 | (append counts '()))))) | |
531 | ||
532 | ;;; Print a count reporter's results nicely. Pass this function the value | |
533 | ;;; returned by a count reporter's RESULTS procedure. | |
57e7f270 | 534 | (define (print-counts results . port?) |
bba2d190 | 535 | (let ((port (if (pair? port?) |
57e7f270 DH |
536 | (car port?) |
537 | (current-output-port)))) | |
538 | (newline port) | |
539 | (display-line-port port "Totals for this test run:") | |
540 | (for-each | |
541 | (lambda (tag) | |
542 | (let ((result (assq (car tag) results))) | |
543 | (if result | |
544 | (display-line-port port (caddr tag) (cdr result)) | |
545 | (display-line-port port | |
546 | "Test suite bug: " | |
547 | "no total available for `" (car tag) "'")))) | |
548 | result-tags) | |
549 | (newline port))) | |
000ee07f JB |
550 | |
551 | ;;; Return a reporter procedure which prints all results to the file | |
552 | ;;; FILE, in human-readable form. FILE may be a filename, or a port. | |
553 | (define (make-log-reporter file) | |
554 | (let ((port (if (output-port? file) file | |
555 | (open-output-file file)))) | |
57e7f270 DH |
556 | (lambda args |
557 | (apply print-result port args) | |
000ee07f JB |
558 | (force-output port)))) |
559 | ||
087dab1c | 560 | ;;; A reporter that reports all results to the user. |
57e7f270 DH |
561 | (define (full-reporter . args) |
562 | (apply print-result (current-output-port) args)) | |
087dab1c JB |
563 | |
564 | ;;; A reporter procedure which shows interesting results (failures, | |
57e7f270 DH |
565 | ;;; unexpected passes etc.) to the user. |
566 | (define (user-reporter result name . args) | |
567 | (if (memq result important-result-tags) | |
568 | (apply full-reporter result name args))) | |
087dab1c JB |
569 | |
570 | (set! default-reporter full-reporter) |