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