1 ;;;; test-suite/lib.scm --- generic support for testing
2 ;;;; Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
4 ;;;; This program is free software; you can redistribute it and/or modify
5 ;;;; it under the terms of the GNU General Public License as published by
6 ;;;; the Free Software Foundation; either version 2, or (at your option)
7 ;;;; any later version.
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
12 ;;;; GNU General Public License for more details.
14 ;;;; You should have received a copy of the GNU General Public License
15 ;;;; along with this software; see the file COPYING. If not, write to
16 ;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17 ;;;; Boston, MA 02111-1307 USA
19 (define-module (test-suite lib)
20 :use-module (ice-9 stack-catch)
21 :use-module (ice-9 regex)
24 ;; Exceptions which are commonly being tested for.
25 exception:bad-variable
26 exception:missing-expression
27 exception:out-of-range exception:unbound-var
28 exception:wrong-num-args exception:wrong-type-arg
30 ;; Reporting passes and failures.
33 pass-if-exception expect-fail-exception
35 ;; Naming groups of tests in a regular fashion.
36 with-test-prefix with-test-prefix* current-test-prefix
39 ;; Reporting results in various ways.
40 register-reporter unregister-reporter reporter-registered?
41 make-count-reporter print-counts
47 ;;;; If you're using Emacs's Scheme mode:
48 ;;;; (put 'with-test-prefix 'scheme-indent-function 1)
53 ;;;; The function (run-test name expected-result thunk) is the heart of the
54 ;;;; testing environment. The first parameter NAME is a unique name for the
55 ;;;; test to be executed (for an explanation of this parameter see below under
56 ;;;; TEST NAMES). The second parameter EXPECTED-RESULT is a boolean value
57 ;;;; that indicates whether the corresponding test is expected to pass. If
58 ;;;; EXPECTED-RESULT is #t the test is expected to pass, if EXPECTED-RESULT is
59 ;;;; #f the test is expected to fail. Finally, THUNK is the function that
60 ;;;; actually performs the test. For example:
62 ;;;; (run-test "integer addition" #t (lambda () (= 2 (+ 1 1))))
64 ;;;; To report success, THUNK should either return #t or throw 'pass. To
65 ;;;; report failure, THUNK should either return #f or throw 'fail. If THUNK
66 ;;;; returns a non boolean value or throws 'unresolved, this indicates that
67 ;;;; the test did not perform as expected. For example the property that was
68 ;;;; to be tested could not be tested because something else went wrong.
69 ;;;; THUNK may also throw 'untested to indicate that the test was deliberately
70 ;;;; not performed, for example because the test case is not complete yet.
71 ;;;; Finally, if THUNK throws 'unsupported, this indicates that this test
72 ;;;; requires some feature that is not available in the configured testing
73 ;;;; environment. All other exceptions thrown by THUNK are considered as
77 ;;;; Convenience macros for tests expected to pass or fail
79 ;;;; * (pass-if name body) is a short form for
80 ;;;; (run-test name #t (lambda () body))
81 ;;;; * (expect-fail name body) is a short form for
82 ;;;; (run-test name #f (lambda () body))
86 ;;;; (pass-if "integer addition" (= 2 (+ 1 1)))
89 ;;;; Convenience macros to test for exceptions
91 ;;;; The following macros take exception parameters which are pairs
92 ;;;; (type . message), where type is a symbol that denotes an exception type
93 ;;;; like 'wrong-type-arg or 'out-of-range, and message is a string holding a
94 ;;;; regular expression that describes the error message for the exception
95 ;;;; like "Argument .* out of range".
97 ;;;; * (pass-if-exception name exception body) will pass if the execution of
98 ;;;; body causes the given exception to be thrown. If no exception is
99 ;;;; thrown, the test fails. If some other exception is thrown, is is an
101 ;;;; * (expect-fail-exception name exception body) will pass unexpectedly if
102 ;;;; the execution of body causes the given exception to be thrown. If no
103 ;;;; exception is thrown, the test fails expectedly. If some other
104 ;;;; exception is thrown, it is an error.
109 ;;;; Every test in the test suite has a unique name, to help
110 ;;;; developers find tests that are failing (or unexpectedly passing),
111 ;;;; and to help gather statistics.
113 ;;;; A test name is a list of printable objects. For example:
114 ;;;; ("ports.scm" "file" "read and write back list of strings")
115 ;;;; ("ports.scm" "pipe" "read")
117 ;;;; Test names may contain arbitrary objects, but they always have
118 ;;;; the following properties:
119 ;;;; - Test names can be compared with EQUAL?.
120 ;;;; - Test names can be reliably stored and retrieved with the standard WRITE
121 ;;;; and READ procedures; doing so preserves their identity.
125 ;;;; (pass-if "simple addition" (= 4 (+ 2 2)))
127 ;;;; In that case, the test name is the list ("simple addition").
129 ;;;; In the case of simple tests the expression that is tested would often
130 ;;;; suffice as a test name by itself. Therefore, the convenience macros
131 ;;;; pass-if and expect-fail provide a shorthand notation that allows to omit
132 ;;;; a test name in such cases.
134 ;;;; * (pass-if expression) is a short form for
135 ;;;; (run-test 'expression #t (lambda () expression))
136 ;;;; * (expect-fail expression) is a short form for
137 ;;;; (run-test 'expression #f (lambda () expression))
141 ;;;; (pass-if (= 2 (+ 1 1)))
143 ;;;; The WITH-TEST-PREFIX syntax and WITH-TEST-PREFIX* procedure establish
144 ;;;; a prefix for the names of all tests whose results are reported
145 ;;;; within their dynamic scope. For example:
148 ;;;; (with-test-prefix "basic arithmetic"
149 ;;;; (pass-if "addition" (= (+ 2 2) 4))
150 ;;;; (pass-if "subtraction" (= (- 4 2) 2)))
151 ;;;; (pass-if "multiplication" (= (* 2 2) 4)))
153 ;;;; In that example, the three test names are:
154 ;;;; ("basic arithmetic" "addition"),
155 ;;;; ("basic arithmetic" "subtraction"), and
156 ;;;; ("multiplication").
158 ;;;; WITH-TEST-PREFIX can be nested. Each WITH-TEST-PREFIX postpends
159 ;;;; a new element to the current prefix:
161 ;;;; (with-test-prefix "arithmetic"
162 ;;;; (with-test-prefix "addition"
163 ;;;; (pass-if "integer" (= (+ 2 2) 4))
164 ;;;; (pass-if "complex" (= (+ 2+3i 4+5i) 6+8i)))
165 ;;;; (with-test-prefix "subtraction"
166 ;;;; (pass-if "integer" (= (- 2 2) 0))
167 ;;;; (pass-if "complex" (= (- 2+3i 1+2i) 1+1i))))
169 ;;;; The four test names here are:
170 ;;;; ("arithmetic" "addition" "integer")
171 ;;;; ("arithmetic" "addition" "complex")
172 ;;;; ("arithmetic" "subtraction" "integer")
173 ;;;; ("arithmetic" "subtraction" "complex")
175 ;;;; To print a name for a human reader, we DISPLAY its elements,
176 ;;;; separated by ": ". So, the last set of test names would be
179 ;;;; arithmetic: addition: integer
180 ;;;; arithmetic: addition: complex
181 ;;;; arithmetic: subtraction: integer
182 ;;;; arithmetic: subtraction: complex
184 ;;;; The Guile benchmarks use with-test-prefix to include the name of
185 ;;;; the source file containing the test in the test name, to help
186 ;;;; developers to find failing tests, and to provide each file with its
192 ;;;; A reporter is a function which we apply to each test outcome.
193 ;;;; Reporters can log results, print interesting results to the
194 ;;;; standard output, collect statistics, etc.
196 ;;;; A reporter function takes two mandatory arguments, RESULT and TEST, and
197 ;;;; possibly additional arguments depending on RESULT; its return value
198 ;;;; is ignored. RESULT has one of the following forms:
200 ;;;; pass - The test named TEST passed.
201 ;;;; Additional arguments are ignored.
202 ;;;; upass - The test named TEST passed unexpectedly.
203 ;;;; Additional arguments are ignored.
204 ;;;; fail - The test named TEST failed.
205 ;;;; Additional arguments are ignored.
206 ;;;; xfail - The test named TEST failed, as expected.
207 ;;;; Additional arguments are ignored.
208 ;;;; unresolved - The test named TEST did not perform as expected, for
209 ;;;; example the property that was to be tested could not be
210 ;;;; tested because something else went wrong.
211 ;;;; Additional arguments are ignored.
212 ;;;; untested - The test named TEST was not actually performed, for
213 ;;;; example because the test case is not complete yet.
214 ;;;; Additional arguments are ignored.
215 ;;;; unsupported - The test named TEST requires some feature that is not
216 ;;;; available in the configured testing environment.
217 ;;;; Additional arguments are ignored.
218 ;;;; error - An error occurred while the test named TEST was
219 ;;;; performed. Since this result means that the system caught
220 ;;;; an exception it could not handle, the exception arguments
221 ;;;; are passed as additional arguments.
223 ;;;; This library provides some standard reporters for logging results
224 ;;;; to a file, reporting interesting results to the user, and
225 ;;;; collecting totals.
227 ;;;; You can use the REGISTER-REPORTER function and friends to add
228 ;;;; whatever reporting functions you like. If you don't register any
229 ;;;; reporters, the library uses FULL-REPORTER, which simply writes
230 ;;;; all results to the standard output.
236 ;;; Define some exceptions which are commonly being tested for.
237 (define exception:bad-variable
238 (cons 'syntax-error "Bad variable"))
239 (define exception:missing-expression
240 (cons 'misc-error "^missing or extra expression"))
241 (define exception:out-of-range
242 (cons 'out-of-range "^Argument .*out of range"))
243 (define exception:unbound-var
244 (cons 'unbound-variable "^Unbound variable"))
245 (define exception:wrong-num-args
246 (cons 'wrong-number-of-args "^Wrong number of arguments"))
247 (define exception:wrong-type-arg
248 (cons 'wrong-type-arg "^Wrong type argument"))
250 ;;; Display all parameters to the default output port, followed by a newline.
251 (define (display-line . objs)
252 (for-each display objs)
255 ;;; Display all parameters to the given output port, followed by a newline.
256 (define (display-line-port port . objs)
257 (for-each (lambda (obj) (display obj port)) objs)
264 ;;; The central testing routine.
265 ;;; The idea is taken from Greg, the GNUstep regression test environment.
267 (let ((test-running #f))
268 (define (local-run-test name expect-pass thunk)
270 (error "Nested calls to run-test are not permitted.")
271 (let ((test-name (full-name name)))
272 (set! test-running #t)
275 (let ((result (thunk)))
276 (if (eq? result #t) (throw 'pass))
277 (if (eq? result #f) (throw 'fail))
278 (throw 'unresolved)))
282 (report (if expect-pass 'pass 'upass) test-name))
284 (report (if expect-pass 'fail 'xfail) test-name))
285 ((unresolved untested unsupported)
286 (report key test-name))
288 (report 'unresolved test-name)
291 (report 'error test-name (cons key args))))))
292 (set! test-running #f))))
293 (set! run-test local-run-test))
295 ;;; A short form for tests that are expected to pass, taken from Greg.
296 (defmacro pass-if (name . rest)
297 (if (and (null? rest) (pair? name))
298 ;; presume this is a simple test, i.e. (pass-if (even? 2))
299 ;; where the body should also be the name.
300 `(run-test ',name #t (lambda () ,name))
301 `(run-test ,name #t (lambda () ,@rest))))
303 ;;; A short form for tests that are expected to fail, taken from Greg.
304 (defmacro expect-fail (name . rest)
305 (if (and (null? rest) (pair? name))
306 ;; presume this is a simple test, i.e. (expect-fail (even? 2))
307 ;; where the body should also be the name.
308 `(run-test ',name #f (lambda () ,name))
309 `(run-test ,name #f (lambda () ,@rest))))
311 ;;; A helper function to implement the macros that test for exceptions.
312 (define (run-test-exception name exception expect-pass thunk)
313 (run-test name expect-pass
315 (stack-catch (car exception)
316 (lambda () (thunk) #f)
317 (lambda (key proc message . rest)
319 ;; handle explicit key
320 ((string-match (cdr exception) message)
322 ;; handle `(error ...)' which uses `misc-error' for key and doesn't
323 ;; yet format the message and args (we have to do it here).
324 ((and (eq? 'misc-error (car exception))
326 (string-match (cdr exception)
327 (apply simple-format #f message (car rest))))
329 ;; handle syntax errors which use `syntax-error' for key and don't
330 ;; yet format the message and args (we have to do it here).
331 ((and (eq? 'syntax-error (car exception))
333 (string-match (cdr exception)
334 (apply simple-format #f message (car rest))))
336 ;; unhandled; throw again
338 (apply throw key proc message rest))))))))
340 ;;; A short form for tests that expect a certain exception to be thrown.
341 (defmacro pass-if-exception (name exception body . rest)
342 `(,run-test-exception ,name ,exception #t (lambda () ,body ,@rest)))
344 ;;; A short form for tests expected to fail to throw a certain exception.
345 (defmacro expect-fail-exception (name exception body . rest)
346 `(,run-test-exception ,name ,exception #f (lambda () ,body ,@rest)))
352 ;;;; Turn a test name into a nice human-readable string.
353 (define (format-test-name name)
354 (call-with-output-string
356 (let loop ((name name)
360 (display separator port)
361 (display (car name) port)
362 (loop (cdr name) ": ")))))))
364 ;;;; For a given test-name, deliver the full name including all prefixes.
365 (define (full-name name)
366 (append (current-test-prefix) (list name)))
368 ;;; A fluid containing the current test prefix, as a list.
369 (define prefix-fluid (make-fluid))
370 (fluid-set! prefix-fluid '())
371 (define (current-test-prefix)
372 (fluid-ref prefix-fluid))
374 ;;; Postpend PREFIX to the current name prefix while evaluting THUNK.
375 ;;; The name prefix is only changed within the dynamic scope of the
376 ;;; call to with-test-prefix*. Return the value returned by THUNK.
377 (define (with-test-prefix* prefix thunk)
378 (with-fluids ((prefix-fluid
379 (append (fluid-ref prefix-fluid) (list prefix))))
382 ;;; (with-test-prefix PREFIX BODY ...)
383 ;;; Postpend PREFIX to the current name prefix while evaluating BODY ...
384 ;;; The name prefix is only changed within the dynamic scope of the
385 ;;; with-test-prefix expression. Return the value returned by the last
387 (defmacro with-test-prefix (prefix . body)
388 `(with-test-prefix* ,prefix (lambda () ,@body)))
394 ;;; The global list of reporters.
395 (define reporters '())
397 ;;; The default reporter, to be used only if no others exist.
398 (define default-reporter #f)
400 ;;; Add the procedure REPORTER to the current set of reporter functions.
401 ;;; Signal an error if that reporter procedure object is already registered.
402 (define (register-reporter reporter)
403 (if (memq reporter reporters)
404 (error "register-reporter: reporter already registered: " reporter))
405 (set! reporters (cons reporter reporters)))
407 ;;; Remove the procedure REPORTER from the current set of reporter
408 ;;; functions. Signal an error if REPORTER is not currently registered.
409 (define (unregister-reporter reporter)
410 (if (memq reporter reporters)
411 (set! reporters (delq! reporter reporters))
412 (error "unregister-reporter: reporter not registered: " reporter)))
414 ;;; Return true iff REPORTER is in the current set of reporter functions.
415 (define (reporter-registered? reporter)
416 (if (memq reporter reporters) #t #f))
418 ;;; Send RESULT to all currently registered reporter functions.
419 (define (report . args)
420 (if (pair? reporters)
421 (for-each (lambda (reporter) (apply reporter args))
423 (apply default-reporter args)))
426 ;;;; Some useful standard reporters:
427 ;;;; Count reporters count the occurrence of each test result type.
428 ;;;; Log reporters write all test results to a given log file.
429 ;;;; Full reporters write all test results to the standard output.
430 ;;;; User reporters write interesting test results to the standard output.
432 ;;; The complete list of possible test results.
434 '((pass "PASS" "passes: ")
435 (fail "FAIL" "failures: ")
436 (upass "UPASS" "unexpected passes: ")
437 (xfail "XFAIL" "expected failures: ")
438 (unresolved "UNRESOLVED" "unresolved test cases: ")
439 (untested "UNTESTED" "untested test cases: ")
440 (unsupported "UNSUPPORTED" "unsupported test cases: ")
441 (error "ERROR" "errors: ")))
443 ;;; The list of important test results.
444 (define important-result-tags
445 '(fail upass unresolved error))
447 ;;; Display a single test result in formatted form to the given port
448 (define (print-result port result name . args)
449 (let* ((tag (assq result result-tags))
450 (label (if tag (cadr tag) #f)))
455 (display (format-test-name name) port)
458 (display " - arguments: " port)
461 (error "(test-suite lib) FULL-REPORTER: unrecognized result: "
464 ;;; Return a list of the form (COUNTER RESULTS), where:
465 ;;; - COUNTER is a reporter procedure, and
466 ;;; - RESULTS is a procedure taking no arguments which returns the
467 ;;; results seen so far by COUNTER. The return value is an alist
468 ;;; mapping outcome symbols (`pass', `fail', etc.) onto counts.
469 (define (make-count-reporter)
470 (let ((counts (map (lambda (tag) (cons (car tag) 0)) result-tags)))
472 (lambda (result name . args)
473 (let ((pair (assq result counts)))
475 (set-cdr! pair (+ 1 (cdr pair)))
476 (error "count-reporter: unexpected test result: "
477 (cons result (cons name args))))))
479 (append counts '())))))
481 ;;; Print a count reporter's results nicely. Pass this function the value
482 ;;; returned by a count reporter's RESULTS procedure.
483 (define (print-counts results . port?)
484 (let ((port (if (pair? port?)
486 (current-output-port))))
488 (display-line-port port "Totals for this test run:")
491 (let ((result (assq (car tag) results)))
493 (display-line-port port (caddr tag) (cdr result))
494 (display-line-port port
496 "no total available for `" (car tag) "'"))))
500 ;;; Return a reporter procedure which prints all results to the file
501 ;;; FILE, in human-readable form. FILE may be a filename, or a port.
502 (define (make-log-reporter file)
503 (let ((port (if (output-port? file) file
504 (open-output-file file))))
506 (apply print-result port args)
507 (force-output port))))
509 ;;; A reporter that reports all results to the user.
510 (define (full-reporter . args)
511 (apply print-result (current-output-port) args))
513 ;;; A reporter procedure which shows interesting results (failures,
514 ;;; unexpected passes etc.) to the user.
515 (define (user-reporter result name . args)
516 (if (memq result important-result-tags)
517 (apply full-reporter result name args)))
519 (set! default-reporter full-reporter)