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