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:bad-variable
26 exception:missing-expression
27 exception:out-of-range exception:unbound-var
28 exception:used-before-defined
29 exception:wrong-num-args exception:wrong-type-arg
30 exception:numerical-overflow
31 exception:struct-set!-denied
32 exception:system-error
33 exception:encoding-error
34 exception:miscellaneous-error
35 exception:string-contains-nul
38 ;; Reporting passes and failures.
41 pass-if-exception expect-fail-exception
43 ;; Naming groups of tests in a regular fashion.
44 with-test-prefix with-test-prefix* current-test-prefix
47 ;; Using the debugging evaluator.
48 with-debugging-evaluator with-debugging-evaluator*
50 ;; Using a given locale
51 with-locale with-locale*
53 ;; Reporting results in various ways.
54 register-reporter unregister-reporter reporter-registered?
55 make-count-reporter print-counts
61 ;;;; If you're using Emacs's Scheme mode:
62 ;;;; (put 'with-test-prefix 'scheme-indent-function 1)
67 ;;;; The function (run-test name expected-result thunk) is the heart of the
68 ;;;; testing environment. The first parameter NAME is a unique name for the
69 ;;;; test to be executed (for an explanation of this parameter see below under
70 ;;;; TEST NAMES). The second parameter EXPECTED-RESULT is a boolean value
71 ;;;; that indicates whether the corresponding test is expected to pass. If
72 ;;;; EXPECTED-RESULT is #t the test is expected to pass, if EXPECTED-RESULT is
73 ;;;; #f the test is expected to fail. Finally, THUNK is the function that
74 ;;;; actually performs the test. For example:
76 ;;;; (run-test "integer addition" #t (lambda () (= 2 (+ 1 1))))
78 ;;;; To report success, THUNK should either return #t or throw 'pass. To
79 ;;;; report failure, THUNK should either return #f or throw 'fail. If THUNK
80 ;;;; returns a non boolean value or throws 'unresolved, this indicates that
81 ;;;; the test did not perform as expected. For example the property that was
82 ;;;; to be tested could not be tested because something else went wrong.
83 ;;;; THUNK may also throw 'untested to indicate that the test was deliberately
84 ;;;; not performed, for example because the test case is not complete yet.
85 ;;;; Finally, if THUNK throws 'unsupported, this indicates that this test
86 ;;;; requires some feature that is not available in the configured testing
87 ;;;; environment. All other exceptions thrown by THUNK are considered as
91 ;;;; Convenience macros for tests expected to pass or fail
93 ;;;; * (pass-if name body) is a short form for
94 ;;;; (run-test name #t (lambda () body))
95 ;;;; * (expect-fail name body) is a short form for
96 ;;;; (run-test name #f (lambda () body))
100 ;;;; (pass-if "integer addition" (= 2 (+ 1 1)))
103 ;;;; Convenience macros to test for exceptions
105 ;;;; The following macros take exception parameters which are pairs
106 ;;;; (type . message), where type is a symbol that denotes an exception type
107 ;;;; like 'wrong-type-arg or 'out-of-range, and message is a string holding a
108 ;;;; regular expression that describes the error message for the exception
109 ;;;; like "Argument .* out of range".
111 ;;;; * (pass-if-exception name exception body) will pass if the execution of
112 ;;;; body causes the given exception to be thrown. If no exception is
113 ;;;; thrown, the test fails. If some other exception is thrown, is is an
115 ;;;; * (expect-fail-exception name exception body) will pass unexpectedly if
116 ;;;; the execution of body causes the given exception to be thrown. If no
117 ;;;; exception is thrown, the test fails expectedly. If some other
118 ;;;; exception is thrown, it is an error.
123 ;;;; Every test in the test suite has a unique name, to help
124 ;;;; developers find tests that are failing (or unexpectedly passing),
125 ;;;; and to help gather statistics.
127 ;;;; A test name is a list of printable objects. For example:
128 ;;;; ("ports.scm" "file" "read and write back list of strings")
129 ;;;; ("ports.scm" "pipe" "read")
131 ;;;; Test names may contain arbitrary objects, but they always have
132 ;;;; the following properties:
133 ;;;; - Test names can be compared with EQUAL?.
134 ;;;; - Test names can be reliably stored and retrieved with the standard WRITE
135 ;;;; and READ procedures; doing so preserves their identity.
139 ;;;; (pass-if "simple addition" (= 4 (+ 2 2)))
141 ;;;; In that case, the test name is the list ("simple addition").
143 ;;;; In the case of simple tests the expression that is tested would often
144 ;;;; suffice as a test name by itself. Therefore, the convenience macros
145 ;;;; pass-if and expect-fail provide a shorthand notation that allows to omit
146 ;;;; a test name in such cases.
148 ;;;; * (pass-if expression) is a short form for
149 ;;;; (run-test 'expression #t (lambda () expression))
150 ;;;; * (expect-fail expression) is a short form for
151 ;;;; (run-test 'expression #f (lambda () expression))
155 ;;;; (pass-if (= 2 (+ 1 1)))
157 ;;;; The WITH-TEST-PREFIX syntax and WITH-TEST-PREFIX* procedure establish
158 ;;;; a prefix for the names of all tests whose results are reported
159 ;;;; within their dynamic scope. For example:
162 ;;;; (with-test-prefix "basic arithmetic"
163 ;;;; (pass-if "addition" (= (+ 2 2) 4))
164 ;;;; (pass-if "subtraction" (= (- 4 2) 2)))
165 ;;;; (pass-if "multiplication" (= (* 2 2) 4)))
167 ;;;; In that example, the three test names are:
168 ;;;; ("basic arithmetic" "addition"),
169 ;;;; ("basic arithmetic" "subtraction"), and
170 ;;;; ("multiplication").
172 ;;;; WITH-TEST-PREFIX can be nested. Each WITH-TEST-PREFIX postpends
173 ;;;; a new element to the current prefix:
175 ;;;; (with-test-prefix "arithmetic"
176 ;;;; (with-test-prefix "addition"
177 ;;;; (pass-if "integer" (= (+ 2 2) 4))
178 ;;;; (pass-if "complex" (= (+ 2+3i 4+5i) 6+8i)))
179 ;;;; (with-test-prefix "subtraction"
180 ;;;; (pass-if "integer" (= (- 2 2) 0))
181 ;;;; (pass-if "complex" (= (- 2+3i 1+2i) 1+1i))))
183 ;;;; The four test names here are:
184 ;;;; ("arithmetic" "addition" "integer")
185 ;;;; ("arithmetic" "addition" "complex")
186 ;;;; ("arithmetic" "subtraction" "integer")
187 ;;;; ("arithmetic" "subtraction" "complex")
189 ;;;; To print a name for a human reader, we DISPLAY its elements,
190 ;;;; separated by ": ". So, the last set of test names would be
193 ;;;; arithmetic: addition: integer
194 ;;;; arithmetic: addition: complex
195 ;;;; arithmetic: subtraction: integer
196 ;;;; arithmetic: subtraction: complex
198 ;;;; The Guile benchmarks use with-test-prefix to include the name of
199 ;;;; the source file containing the test in the test name, to help
200 ;;;; developers to find failing tests, and to provide each file with its
206 ;;;; A reporter is a function which we apply to each test outcome.
207 ;;;; Reporters can log results, print interesting results to the
208 ;;;; standard output, collect statistics, etc.
210 ;;;; A reporter function takes two mandatory arguments, RESULT and TEST, and
211 ;;;; possibly additional arguments depending on RESULT; its return value
212 ;;;; is ignored. RESULT has one of the following forms:
214 ;;;; pass - The test named TEST passed.
215 ;;;; Additional arguments are ignored.
216 ;;;; upass - The test named TEST passed unexpectedly.
217 ;;;; Additional arguments are ignored.
218 ;;;; fail - The test named TEST failed.
219 ;;;; Additional arguments are ignored.
220 ;;;; xfail - The test named TEST failed, as expected.
221 ;;;; Additional arguments are ignored.
222 ;;;; unresolved - The test named TEST did not perform as expected, for
223 ;;;; example the property that was to be tested could not be
224 ;;;; tested because something else went wrong.
225 ;;;; Additional arguments are ignored.
226 ;;;; untested - The test named TEST was not actually performed, for
227 ;;;; example because the test case is not complete yet.
228 ;;;; Additional arguments are ignored.
229 ;;;; unsupported - The test named TEST requires some feature that is not
230 ;;;; available in the configured testing environment.
231 ;;;; Additional arguments are ignored.
232 ;;;; error - An error occurred while the test named TEST was
233 ;;;; performed. Since this result means that the system caught
234 ;;;; an exception it could not handle, the exception arguments
235 ;;;; are passed as additional arguments.
237 ;;;; This library provides some standard reporters for logging results
238 ;;;; to a file, reporting interesting results to the user, and
239 ;;;; collecting totals.
241 ;;;; You can use the REGISTER-REPORTER function and friends to add
242 ;;;; whatever reporting functions you like. If you don't register any
243 ;;;; reporters, the library uses FULL-REPORTER, which simply writes
244 ;;;; all results to the standard output.
250 ;;; Define some exceptions which are commonly being tested for.
251 (define exception:bad-variable
252 (cons 'syntax-error "Bad variable"))
253 (define exception:missing-expression
254 (cons 'misc-error "^missing or extra expression"))
255 (define exception:out-of-range
256 (cons 'out-of-range "^.*out of range"))
257 (define exception:unbound-var
258 (cons 'unbound-variable "^Unbound variable"))
259 (define exception:used-before-defined
260 (cons 'unbound-variable "^Variable used before given a value"))
261 (define exception:wrong-num-args
262 (cons 'wrong-number-of-args "^Wrong number of arguments"))
263 (define exception:wrong-type-arg
264 (cons 'wrong-type-arg "^Wrong type"))
265 (define exception:numerical-overflow
266 (cons 'numerical-overflow "^Numerical overflow"))
267 (define exception:struct-set!-denied
268 (cons 'misc-error "^set! denied for field"))
269 (define exception:system-error
270 (cons 'system-error ".*"))
271 (define exception:encoding-error
272 (cons 'encoding-error "(cannot convert to output locale|input locale conversion error)"))
273 (define exception:miscellaneous-error
274 (cons 'misc-error "^.*"))
275 (define exception:read-error
276 (cons 'read-error "^.*$"))
278 ;; as per throw in scm_to_locale_stringn()
279 (define exception:string-contains-nul
280 (cons 'misc-error "^string contains #\\\\nul character"))
283 ;;; Display all parameters to the default output port, followed by a newline.
284 (define (display-line . objs)
285 (for-each display objs)
288 ;;; Display all parameters to the given output port, followed by a newline.
289 (define (display-line-port port . objs)
290 (for-each (lambda (obj) (display obj port)) objs)
297 ;;; The central testing routine.
298 ;;; The idea is taken from Greg, the GNUstep regression test environment.
300 (let ((test-running #f))
301 (define (local-run-test name expect-pass thunk)
303 (error "Nested calls to run-test are not permitted.")
304 (let ((test-name (full-name name)))
305 (set! test-running #t)
308 (let ((result (thunk)))
309 (if (eq? result #t) (throw 'pass))
310 (if (eq? result #f) (throw 'fail))
311 (throw 'unresolved)))
315 (report (if expect-pass 'pass 'upass) test-name))
317 (report (if expect-pass 'fail 'xfail) test-name))
318 ((unresolved untested unsupported)
319 (report key test-name))
321 (report 'unresolved test-name)
324 (report 'error test-name (cons key args))))))
325 (set! test-running #f))))
326 (set! run-test local-run-test))
328 ;;; A short form for tests that are expected to pass, taken from Greg.
329 (define-syntax pass-if
332 ;; presume this is a simple test, i.e. (pass-if (even? 2))
333 ;; where the body should also be the name.
334 (run-test 'name #t (lambda () name)))
336 (run-test name #t (lambda () rest ...)))))
338 ;;; A short form for tests that are expected to fail, taken from Greg.
339 (define-syntax expect-fail
342 ;; presume this is a simple test, i.e. (expect-fail (even? 2))
343 ;; where the body should also be the name.
344 (run-test 'name #f (lambda () name)))
346 (run-test name #f (lambda () rest ...)))))
348 ;;; A helper function to implement the macros that test for exceptions.
349 (define (run-test-exception name exception expect-pass thunk)
350 (run-test name expect-pass
352 (stack-catch (car exception)
353 (lambda () (thunk) #f)
354 (lambda (key proc message . rest)
356 ;; handle explicit key
357 ((string-match (cdr exception) message)
359 ;; handle `(error ...)' which uses `misc-error' for key and doesn't
360 ;; yet format the message and args (we have to do it here).
361 ((and (eq? 'misc-error (car exception))
363 (string-match (cdr exception)
364 (apply simple-format #f message (car rest))))
366 ;; handle syntax errors which use `syntax-error' for key and don't
367 ;; yet format the message and args (we have to do it here).
368 ((and (eq? 'syntax-error (car exception))
370 (string-match (cdr exception)
371 (apply simple-format #f message (car rest))))
373 ;; unhandled; throw again
375 (apply throw key proc message rest))))))))
377 ;;; A short form for tests that expect a certain exception to be thrown.
378 (define-syntax pass-if-exception
380 ((_ name exception body rest ...)
381 (run-test-exception name exception #t (lambda () body rest ...)))))
383 ;;; A short form for tests expected to fail to throw a certain exception.
384 (define-syntax expect-fail-exception
386 ((_ name exception body rest ...)
387 (run-test-exception name exception #f (lambda () body rest ...)))))
393 ;;;; Turn a test name into a nice human-readable string.
394 (define (format-test-name name)
395 ;; Choose a Unicode-capable encoding so that the string port can contain any
396 ;; valid Unicode character.
397 (with-fluids ((%default-port-encoding "UTF-8"))
398 (call-with-output-string
400 (let loop ((name name)
404 (display separator port)
405 (display (car name) port)
406 (loop (cdr name) ": "))))))))
408 ;;;; For a given test-name, deliver the full name including all prefixes.
409 (define (full-name name)
410 (append (current-test-prefix) (list name)))
412 ;;; A fluid containing the current test prefix, as a list.
413 (define prefix-fluid (make-fluid))
414 (fluid-set! prefix-fluid '())
415 (define (current-test-prefix)
416 (fluid-ref prefix-fluid))
418 ;;; Postpend PREFIX to the current name prefix while evaluting THUNK.
419 ;;; The name prefix is only changed within the dynamic scope of the
420 ;;; call to with-test-prefix*. Return the value returned by THUNK.
421 (define (with-test-prefix* prefix thunk)
422 (with-fluids ((prefix-fluid
423 (append (fluid-ref prefix-fluid) (list prefix))))
426 ;;; (with-test-prefix PREFIX BODY ...)
427 ;;; Postpend PREFIX to the current name prefix while evaluating BODY ...
428 ;;; The name prefix is only changed within the dynamic scope of the
429 ;;; with-test-prefix expression. Return the value returned by the last
431 (defmacro with-test-prefix (prefix . body)
432 `(with-test-prefix* ,prefix (lambda () ,@body)))
434 ;;; Call THUNK using the debugging evaluator.
435 (define (with-debugging-evaluator* thunk)
439 (set! dopts (debug-options))
440 (debug-enable 'debug))
443 (debug-options dopts)))))
445 ;;; Evaluate BODY... using the debugging evaluator.
446 (define-macro (with-debugging-evaluator . body)
447 `(with-debugging-evaluator* (lambda () ,@body)))
449 ;;; Call THUNK with a given locale
450 (define (with-locale* nloc thunk)
454 (if (defined? 'setlocale)
457 (false-if-exception (setlocale LC_ALL nloc)))
459 (throw 'unresolved)))
460 (throw 'unresolved)))
463 (if (defined? 'setlocale)
464 (setlocale LC_ALL loc))))))
466 ;;; Evaluate BODY... using the given locale.
467 (define-macro (with-locale loc . body)
468 `(with-locale* ,loc (lambda () ,@body)))
474 ;;; The global list of reporters.
475 (define reporters '())
477 ;;; The default reporter, to be used only if no others exist.
478 (define default-reporter #f)
480 ;;; Add the procedure REPORTER to the current set of reporter functions.
481 ;;; Signal an error if that reporter procedure object is already registered.
482 (define (register-reporter reporter)
483 (if (memq reporter reporters)
484 (error "register-reporter: reporter already registered: " reporter))
485 (set! reporters (cons reporter reporters)))
487 ;;; Remove the procedure REPORTER from the current set of reporter
488 ;;; functions. Signal an error if REPORTER is not currently registered.
489 (define (unregister-reporter reporter)
490 (if (memq reporter reporters)
491 (set! reporters (delq! reporter reporters))
492 (error "unregister-reporter: reporter not registered: " reporter)))
494 ;;; Return true iff REPORTER is in the current set of reporter functions.
495 (define (reporter-registered? reporter)
496 (if (memq reporter reporters) #t #f))
498 ;;; Send RESULT to all currently registered reporter functions.
499 (define (report . args)
500 (if (pair? reporters)
501 (for-each (lambda (reporter) (apply reporter args))
503 (apply default-reporter args)))
506 ;;;; Some useful standard reporters:
507 ;;;; Count reporters count the occurrence of each test result type.
508 ;;;; Log reporters write all test results to a given log file.
509 ;;;; Full reporters write all test results to the standard output.
510 ;;;; User reporters write interesting test results to the standard output.
512 ;;; The complete list of possible test results.
514 '((pass "PASS" "passes: ")
515 (fail "FAIL" "failures: ")
516 (upass "UPASS" "unexpected passes: ")
517 (xfail "XFAIL" "expected failures: ")
518 (unresolved "UNRESOLVED" "unresolved test cases: ")
519 (untested "UNTESTED" "untested test cases: ")
520 (unsupported "UNSUPPORTED" "unsupported test cases: ")
521 (error "ERROR" "errors: ")))
523 ;;; The list of important test results.
524 (define important-result-tags
525 '(fail upass unresolved error))
527 ;;; Display a single test result in formatted form to the given port
528 (define (print-result port result name . args)
529 (let* ((tag (assq result result-tags))
530 (label (if tag (cadr tag) #f)))
535 (display (format-test-name name) port)
538 (display " - arguments: " port)
541 (error "(test-suite lib) FULL-REPORTER: unrecognized result: "
544 ;;; Return a list of the form (COUNTER RESULTS), where:
545 ;;; - COUNTER is a reporter procedure, and
546 ;;; - RESULTS is a procedure taking no arguments which returns the
547 ;;; results seen so far by COUNTER. The return value is an alist
548 ;;; mapping outcome symbols (`pass', `fail', etc.) onto counts.
549 (define (make-count-reporter)
550 (let ((counts (map (lambda (tag) (cons (car tag) 0)) result-tags)))
552 (lambda (result name . args)
553 (let ((pair (assq result counts)))
555 (set-cdr! pair (+ 1 (cdr pair)))
556 (error "count-reporter: unexpected test result: "
557 (cons result (cons name args))))))
559 (append counts '())))))
561 ;;; Print a count reporter's results nicely. Pass this function the value
562 ;;; returned by a count reporter's RESULTS procedure.
563 (define (print-counts results . port?)
564 (let ((port (if (pair? port?)
566 (current-output-port))))
568 (display-line-port port "Totals for this test run:")
571 (let ((result (assq (car tag) results)))
573 (display-line-port port (caddr tag) (cdr result))
574 (display-line-port port
576 "no total available for `" (car tag) "'"))))
580 ;;; Return a reporter procedure which prints all results to the file
581 ;;; FILE, in human-readable form. FILE may be a filename, or a port.
582 (define (make-log-reporter file)
583 (let ((port (if (output-port? file) file
584 (open-output-file file))))
586 (apply print-result port args)
587 (force-output port))))
589 ;;; A reporter that reports all results to the user.
590 (define (full-reporter . args)
591 (apply print-result (current-output-port) args))
593 ;;; A reporter procedure which shows interesting results (failures,
594 ;;; unexpected passes etc.) to the user.
595 (define (user-reporter result name . args)
596 (if (memq result important-result-tags)
597 (apply full-reporter result name args)))
599 (set! default-reporter full-reporter)