1 ;;;; test-suite/lib.scm --- generic support for testing
2 ;;;; Copyright (C) 1999, 2000, 2001, 2004, 2006, 2007, 2009, 2010 Free Software Foundation, Inc.
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.
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 Lesser General Public License for more details.
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
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:syntax-pattern-unmatched
26 exception:bad-variable
27 exception:missing-expression
28 exception:out-of-range exception:unbound-var
29 exception:used-before-defined
30 exception:wrong-num-args exception:wrong-type-arg
31 exception:numerical-overflow
32 exception:struct-set!-denied
33 exception:system-error
34 exception:encoding-error
35 exception:miscellaneous-error
36 exception:string-contains-nul
39 ;; Reporting passes and failures.
42 pass-if-exception expect-fail-exception
44 ;; Naming groups of tests in a regular fashion.
45 with-test-prefix with-test-prefix* current-test-prefix
48 ;; Using the debugging evaluator.
49 with-debugging-evaluator with-debugging-evaluator*
51 ;; Using a given locale
52 with-locale with-locale*
54 ;; Reporting results in various ways.
55 register-reporter unregister-reporter reporter-registered?
56 make-count-reporter print-counts
62 ;;;; If you're using Emacs's Scheme mode:
63 ;;;; (put 'with-test-prefix 'scheme-indent-function 1)
68 ;;;; The function (run-test name expected-result thunk) is the heart of the
69 ;;;; testing environment. The first parameter NAME is a unique name for the
70 ;;;; test to be executed (for an explanation of this parameter see below under
71 ;;;; TEST NAMES). The second parameter EXPECTED-RESULT is a boolean value
72 ;;;; that indicates whether the corresponding test is expected to pass. If
73 ;;;; EXPECTED-RESULT is #t the test is expected to pass, if EXPECTED-RESULT is
74 ;;;; #f the test is expected to fail. Finally, THUNK is the function that
75 ;;;; actually performs the test. For example:
77 ;;;; (run-test "integer addition" #t (lambda () (= 2 (+ 1 1))))
79 ;;;; To report success, THUNK should either return #t or throw 'pass. To
80 ;;;; report failure, THUNK should either return #f or throw 'fail. If THUNK
81 ;;;; returns a non boolean value or throws 'unresolved, this indicates that
82 ;;;; the test did not perform as expected. For example the property that was
83 ;;;; to be tested could not be tested because something else went wrong.
84 ;;;; THUNK may also throw 'untested to indicate that the test was deliberately
85 ;;;; not performed, for example because the test case is not complete yet.
86 ;;;; Finally, if THUNK throws 'unsupported, this indicates that this test
87 ;;;; requires some feature that is not available in the configured testing
88 ;;;; environment. All other exceptions thrown by THUNK are considered as
92 ;;;; Convenience macros for tests expected to pass or fail
94 ;;;; * (pass-if name body) is a short form for
95 ;;;; (run-test name #t (lambda () body))
96 ;;;; * (expect-fail name body) is a short form for
97 ;;;; (run-test name #f (lambda () body))
101 ;;;; (pass-if "integer addition" (= 2 (+ 1 1)))
104 ;;;; Convenience macros to test for exceptions
106 ;;;; The following macros take exception parameters which are pairs
107 ;;;; (type . message), where type is a symbol that denotes an exception type
108 ;;;; like 'wrong-type-arg or 'out-of-range, and message is a string holding a
109 ;;;; regular expression that describes the error message for the exception
110 ;;;; like "Argument .* out of range".
112 ;;;; * (pass-if-exception name exception body) will pass if the execution of
113 ;;;; body causes the given exception to be thrown. If no exception is
114 ;;;; thrown, the test fails. If some other exception is thrown, is is an
116 ;;;; * (expect-fail-exception name exception body) will pass unexpectedly if
117 ;;;; the execution of body causes the given exception to be thrown. If no
118 ;;;; exception is thrown, the test fails expectedly. If some other
119 ;;;; exception is thrown, it is an error.
124 ;;;; Every test in the test suite has a unique name, to help
125 ;;;; developers find tests that are failing (or unexpectedly passing),
126 ;;;; and to help gather statistics.
128 ;;;; A test name is a list of printable objects. For example:
129 ;;;; ("ports.scm" "file" "read and write back list of strings")
130 ;;;; ("ports.scm" "pipe" "read")
132 ;;;; Test names may contain arbitrary objects, but they always have
133 ;;;; the following properties:
134 ;;;; - Test names can be compared with EQUAL?.
135 ;;;; - Test names can be reliably stored and retrieved with the standard WRITE
136 ;;;; and READ procedures; doing so preserves their identity.
140 ;;;; (pass-if "simple addition" (= 4 (+ 2 2)))
142 ;;;; In that case, the test name is the list ("simple addition").
144 ;;;; In the case of simple tests the expression that is tested would often
145 ;;;; suffice as a test name by itself. Therefore, the convenience macros
146 ;;;; pass-if and expect-fail provide a shorthand notation that allows to omit
147 ;;;; a test name in such cases.
149 ;;;; * (pass-if expression) is a short form for
150 ;;;; (run-test 'expression #t (lambda () expression))
151 ;;;; * (expect-fail expression) is a short form for
152 ;;;; (run-test 'expression #f (lambda () expression))
156 ;;;; (pass-if (= 2 (+ 1 1)))
158 ;;;; The WITH-TEST-PREFIX syntax and WITH-TEST-PREFIX* procedure establish
159 ;;;; a prefix for the names of all tests whose results are reported
160 ;;;; within their dynamic scope. For example:
163 ;;;; (with-test-prefix "basic arithmetic"
164 ;;;; (pass-if "addition" (= (+ 2 2) 4))
165 ;;;; (pass-if "subtraction" (= (- 4 2) 2)))
166 ;;;; (pass-if "multiplication" (= (* 2 2) 4)))
168 ;;;; In that example, the three test names are:
169 ;;;; ("basic arithmetic" "addition"),
170 ;;;; ("basic arithmetic" "subtraction"), and
171 ;;;; ("multiplication").
173 ;;;; WITH-TEST-PREFIX can be nested. Each WITH-TEST-PREFIX postpends
174 ;;;; a new element to the current prefix:
176 ;;;; (with-test-prefix "arithmetic"
177 ;;;; (with-test-prefix "addition"
178 ;;;; (pass-if "integer" (= (+ 2 2) 4))
179 ;;;; (pass-if "complex" (= (+ 2+3i 4+5i) 6+8i)))
180 ;;;; (with-test-prefix "subtraction"
181 ;;;; (pass-if "integer" (= (- 2 2) 0))
182 ;;;; (pass-if "complex" (= (- 2+3i 1+2i) 1+1i))))
184 ;;;; The four test names here are:
185 ;;;; ("arithmetic" "addition" "integer")
186 ;;;; ("arithmetic" "addition" "complex")
187 ;;;; ("arithmetic" "subtraction" "integer")
188 ;;;; ("arithmetic" "subtraction" "complex")
190 ;;;; To print a name for a human reader, we DISPLAY its elements,
191 ;;;; separated by ": ". So, the last set of test names would be
194 ;;;; arithmetic: addition: integer
195 ;;;; arithmetic: addition: complex
196 ;;;; arithmetic: subtraction: integer
197 ;;;; arithmetic: subtraction: complex
199 ;;;; The Guile benchmarks use with-test-prefix to include the name of
200 ;;;; the source file containing the test in the test name, to help
201 ;;;; developers to find failing tests, and to provide each file with its
207 ;;;; A reporter is a function which we apply to each test outcome.
208 ;;;; Reporters can log results, print interesting results to the
209 ;;;; standard output, collect statistics, etc.
211 ;;;; A reporter function takes two mandatory arguments, RESULT and TEST, and
212 ;;;; possibly additional arguments depending on RESULT; its return value
213 ;;;; is ignored. RESULT has one of the following forms:
215 ;;;; pass - The test named TEST passed.
216 ;;;; Additional arguments are ignored.
217 ;;;; upass - The test named TEST passed unexpectedly.
218 ;;;; Additional arguments are ignored.
219 ;;;; fail - The test named TEST failed.
220 ;;;; Additional arguments are ignored.
221 ;;;; xfail - The test named TEST failed, as expected.
222 ;;;; Additional arguments are ignored.
223 ;;;; unresolved - The test named TEST did not perform as expected, for
224 ;;;; example the property that was to be tested could not be
225 ;;;; tested because something else went wrong.
226 ;;;; Additional arguments are ignored.
227 ;;;; untested - The test named TEST was not actually performed, for
228 ;;;; example because the test case is not complete yet.
229 ;;;; Additional arguments are ignored.
230 ;;;; unsupported - The test named TEST requires some feature that is not
231 ;;;; available in the configured testing environment.
232 ;;;; Additional arguments are ignored.
233 ;;;; error - An error occurred while the test named TEST was
234 ;;;; performed. Since this result means that the system caught
235 ;;;; an exception it could not handle, the exception arguments
236 ;;;; are passed as additional arguments.
238 ;;;; This library provides some standard reporters for logging results
239 ;;;; to a file, reporting interesting results to the user, and
240 ;;;; collecting totals.
242 ;;;; You can use the REGISTER-REPORTER function and friends to add
243 ;;;; whatever reporting functions you like. If you don't register any
244 ;;;; reporters, the library uses FULL-REPORTER, which simply writes
245 ;;;; all results to the standard output.
251 ;;; Define some exceptions which are commonly being tested for.
252 (define exception:syntax-pattern-unmatched
253 (cons 'syntax-error "source expression failed to match any pattern"))
254 (define exception:bad-variable
255 (cons 'syntax-error "Bad variable"))
256 (define exception:missing-expression
257 (cons 'misc-error "^missing or extra expression"))
258 (define exception:out-of-range
259 (cons 'out-of-range "^.*out of range"))
260 (define exception:unbound-var
261 (cons 'unbound-variable "^Unbound variable"))
262 (define exception:used-before-defined
263 (cons 'unbound-variable "^Variable used before given a value"))
264 (define exception:wrong-num-args
265 (cons 'wrong-number-of-args "^Wrong number of arguments"))
266 (define exception:wrong-type-arg
267 (cons 'wrong-type-arg "^Wrong type"))
268 (define exception:numerical-overflow
269 (cons 'numerical-overflow "^Numerical overflow"))
270 (define exception:struct-set!-denied
271 (cons 'misc-error "^set! denied for field"))
272 (define exception:system-error
273 (cons 'system-error ".*"))
274 (define exception:encoding-error
275 (cons 'encoding-error "(cannot convert to output locale|input locale conversion error)"))
276 (define exception:miscellaneous-error
277 (cons 'misc-error "^.*"))
278 (define exception:read-error
279 (cons 'read-error "^.*$"))
281 ;; as per throw in scm_to_locale_stringn()
282 (define exception:string-contains-nul
283 (cons 'misc-error "^string contains #\\\\nul character"))
286 ;;; Display all parameters to the default output port, followed by a newline.
287 (define (display-line . objs)
288 (for-each display objs)
291 ;;; Display all parameters to the given output port, followed by a newline.
292 (define (display-line-port port . objs)
293 (for-each (lambda (obj) (display obj port)) objs)
300 ;;; The central testing routine.
301 ;;; The idea is taken from Greg, the GNUstep regression test environment.
303 (let ((test-running #f))
304 (define (local-run-test name expect-pass thunk)
306 (error "Nested calls to run-test are not permitted.")
307 (let ((test-name (full-name name)))
308 (set! test-running #t)
311 (let ((result (thunk)))
312 (if (eq? result #t) (throw 'pass))
313 (if (eq? result #f) (throw 'fail))
314 (throw 'unresolved)))
318 (report (if expect-pass 'pass 'upass) test-name))
320 (report (if expect-pass 'fail 'xfail) test-name))
321 ((unresolved untested unsupported)
322 (report key test-name))
324 (report 'unresolved test-name)
327 (report 'error test-name (cons key args))))))
328 (set! test-running #f))))
329 (set! run-test local-run-test))
331 ;;; A short form for tests that are expected to pass, taken from Greg.
332 (define-syntax pass-if
335 ;; presume this is a simple test, i.e. (pass-if (even? 2))
336 ;; where the body should also be the name.
337 (run-test 'name #t (lambda () name)))
339 (run-test name #t (lambda () rest ...)))))
341 ;;; A short form for tests that are expected to fail, taken from Greg.
342 (define-syntax expect-fail
345 ;; presume this is a simple test, i.e. (expect-fail (even? 2))
346 ;; where the body should also be the name.
347 (run-test 'name #f (lambda () name)))
349 (run-test name #f (lambda () rest ...)))))
351 ;;; A helper function to implement the macros that test for exceptions.
352 (define (run-test-exception name exception expect-pass thunk)
353 (run-test name expect-pass
355 (stack-catch (car exception)
356 (lambda () (thunk) #f)
357 (lambda (key proc message . rest)
359 ;; handle explicit key
360 ((string-match (cdr exception) message)
362 ;; handle `(error ...)' which uses `misc-error' for key and doesn't
363 ;; yet format the message and args (we have to do it here).
364 ((and (eq? 'misc-error (car exception))
366 (string-match (cdr exception)
367 (apply simple-format #f message (car rest))))
369 ;; handle syntax errors which use `syntax-error' for key and don't
370 ;; yet format the message and args (we have to do it here).
371 ((and (eq? 'syntax-error (car exception))
373 (string-match (cdr exception)
374 (apply simple-format #f message (car rest))))
376 ;; unhandled; throw again
378 (apply throw key proc message rest))))))))
380 ;;; A short form for tests that expect a certain exception to be thrown.
381 (define-syntax pass-if-exception
383 ((_ name exception body rest ...)
384 (run-test-exception name exception #t (lambda () body rest ...)))))
386 ;;; A short form for tests expected to fail to throw a certain exception.
387 (define-syntax expect-fail-exception
389 ((_ name exception body rest ...)
390 (run-test-exception name exception #f (lambda () body rest ...)))))
396 ;;;; Turn a test name into a nice human-readable string.
397 (define (format-test-name name)
398 ;; Choose a Unicode-capable encoding so that the string port can contain any
399 ;; valid Unicode character.
400 (with-fluids ((%default-port-encoding "UTF-8"))
401 (call-with-output-string
403 (let loop ((name name)
407 (display separator port)
408 (display (car name) port)
409 (loop (cdr name) ": "))))))))
411 ;;;; For a given test-name, deliver the full name including all prefixes.
412 (define (full-name name)
413 (append (current-test-prefix) (list name)))
415 ;;; A fluid containing the current test prefix, as a list.
416 (define prefix-fluid (make-fluid))
417 (fluid-set! prefix-fluid '())
418 (define (current-test-prefix)
419 (fluid-ref prefix-fluid))
421 ;;; Postpend PREFIX to the current name prefix while evaluting THUNK.
422 ;;; The name prefix is only changed within the dynamic scope of the
423 ;;; call to with-test-prefix*. Return the value returned by THUNK.
424 (define (with-test-prefix* prefix thunk)
425 (with-fluids ((prefix-fluid
426 (append (fluid-ref prefix-fluid) (list prefix))))
429 ;;; (with-test-prefix PREFIX BODY ...)
430 ;;; Postpend PREFIX to the current name prefix while evaluating BODY ...
431 ;;; The name prefix is only changed within the dynamic scope of the
432 ;;; with-test-prefix expression. Return the value returned by the last
434 (defmacro with-test-prefix (prefix . body)
435 `(with-test-prefix* ,prefix (lambda () ,@body)))
437 ;;; Call THUNK using the debugging evaluator.
438 (define (with-debugging-evaluator* thunk)
442 (set! dopts (debug-options))
443 (debug-enable 'debug))
446 (debug-options dopts)))))
448 ;;; Evaluate BODY... using the debugging evaluator.
449 (define-macro (with-debugging-evaluator . body)
450 `(with-debugging-evaluator* (lambda () ,@body)))
452 ;;; Call THUNK with a given locale
453 (define (with-locale* nloc thunk)
457 (if (defined? 'setlocale)
460 (false-if-exception (setlocale LC_ALL nloc)))
462 (throw 'unresolved)))
463 (throw 'unresolved)))
466 (if (defined? 'setlocale)
467 (setlocale LC_ALL loc))))))
469 ;;; Evaluate BODY... using the given locale.
470 (define-macro (with-locale loc . body)
471 `(with-locale* ,loc (lambda () ,@body)))
477 ;;; The global list of reporters.
478 (define reporters '())
480 ;;; The default reporter, to be used only if no others exist.
481 (define default-reporter #f)
483 ;;; Add the procedure REPORTER to the current set of reporter functions.
484 ;;; Signal an error if that reporter procedure object is already registered.
485 (define (register-reporter reporter)
486 (if (memq reporter reporters)
487 (error "register-reporter: reporter already registered: " reporter))
488 (set! reporters (cons reporter reporters)))
490 ;;; Remove the procedure REPORTER from the current set of reporter
491 ;;; functions. Signal an error if REPORTER is not currently registered.
492 (define (unregister-reporter reporter)
493 (if (memq reporter reporters)
494 (set! reporters (delq! reporter reporters))
495 (error "unregister-reporter: reporter not registered: " reporter)))
497 ;;; Return true iff REPORTER is in the current set of reporter functions.
498 (define (reporter-registered? reporter)
499 (if (memq reporter reporters) #t #f))
501 ;;; Send RESULT to all currently registered reporter functions.
502 (define (report . args)
503 (if (pair? reporters)
504 (for-each (lambda (reporter) (apply reporter args))
506 (apply default-reporter args)))
509 ;;;; Some useful standard reporters:
510 ;;;; Count reporters count the occurrence of each test result type.
511 ;;;; Log reporters write all test results to a given log file.
512 ;;;; Full reporters write all test results to the standard output.
513 ;;;; User reporters write interesting test results to the standard output.
515 ;;; The complete list of possible test results.
517 '((pass "PASS" "passes: ")
518 (fail "FAIL" "failures: ")
519 (upass "UPASS" "unexpected passes: ")
520 (xfail "XFAIL" "expected failures: ")
521 (unresolved "UNRESOLVED" "unresolved test cases: ")
522 (untested "UNTESTED" "untested test cases: ")
523 (unsupported "UNSUPPORTED" "unsupported test cases: ")
524 (error "ERROR" "errors: ")))
526 ;;; The list of important test results.
527 (define important-result-tags
528 '(fail upass unresolved error))
530 ;;; Display a single test result in formatted form to the given port
531 (define (print-result port result name . args)
532 (let* ((tag (assq result result-tags))
533 (label (if tag (cadr tag) #f)))
538 (display (format-test-name name) port)
541 (display " - arguments: " port)
544 (error "(test-suite lib) FULL-REPORTER: unrecognized result: "
547 ;;; Return a list of the form (COUNTER RESULTS), where:
548 ;;; - COUNTER is a reporter procedure, and
549 ;;; - RESULTS is a procedure taking no arguments which returns the
550 ;;; results seen so far by COUNTER. The return value is an alist
551 ;;; mapping outcome symbols (`pass', `fail', etc.) onto counts.
552 (define (make-count-reporter)
553 (let ((counts (map (lambda (tag) (cons (car tag) 0)) result-tags)))
555 (lambda (result name . args)
556 (let ((pair (assq result counts)))
558 (set-cdr! pair (+ 1 (cdr pair)))
559 (error "count-reporter: unexpected test result: "
560 (cons result (cons name args))))))
562 (append counts '())))))
564 ;;; Print a count reporter's results nicely. Pass this function the value
565 ;;; returned by a count reporter's RESULTS procedure.
566 (define (print-counts results . port?)
567 (let ((port (if (pair? port?)
569 (current-output-port))))
571 (display-line-port port "Totals for this test run:")
574 (let ((result (assq (car tag) results)))
576 (display-line-port port (caddr tag) (cdr result))
577 (display-line-port port
579 "no total available for `" (car tag) "'"))))
583 ;;; Return a reporter procedure which prints all results to the file
584 ;;; FILE, in human-readable form. FILE may be a filename, or a port.
585 (define (make-log-reporter file)
586 (let ((port (if (output-port? file) file
587 (open-output-file file))))
589 (apply print-result port args)
590 (force-output port))))
592 ;;; A reporter that reports all results to the user.
593 (define (full-reporter . args)
594 (apply print-result (current-output-port) args))
596 ;;; A reporter procedure which shows interesting results (failures,
597 ;;; unexpected passes etc.) to the user.
598 (define (user-reporter result name . args)
599 (if (memq result important-result-tags)
600 (apply full-reporter result name args)))
602 (set! default-reporter full-reporter)