1 ;;;; test-suite/lib.scm --- generic support for testing
2 ;;;; Copyright (C) 1999, 2000, 2001, 2004, 2006, 2007, 2009 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:miscellaneous-error
34 exception:string-contains-nul
37 ;; Reporting passes and failures.
40 pass-if-exception expect-fail-exception
42 ;; Naming groups of tests in a regular fashion.
43 with-test-prefix with-test-prefix* current-test-prefix
46 ;; Using the debugging evaluator.
47 with-debugging-evaluator with-debugging-evaluator*
49 ;; Reporting results in various ways.
50 register-reporter unregister-reporter reporter-registered?
51 make-count-reporter print-counts
57 ;;;; If you're using Emacs's Scheme mode:
58 ;;;; (put 'with-test-prefix 'scheme-indent-function 1)
63 ;;;; The function (run-test name expected-result thunk) is the heart of the
64 ;;;; testing environment. The first parameter NAME is a unique name for the
65 ;;;; test to be executed (for an explanation of this parameter see below under
66 ;;;; TEST NAMES). The second parameter EXPECTED-RESULT is a boolean value
67 ;;;; that indicates whether the corresponding test is expected to pass. If
68 ;;;; EXPECTED-RESULT is #t the test is expected to pass, if EXPECTED-RESULT is
69 ;;;; #f the test is expected to fail. Finally, THUNK is the function that
70 ;;;; actually performs the test. For example:
72 ;;;; (run-test "integer addition" #t (lambda () (= 2 (+ 1 1))))
74 ;;;; To report success, THUNK should either return #t or throw 'pass. To
75 ;;;; report failure, THUNK should either return #f or throw 'fail. If THUNK
76 ;;;; returns a non boolean value or throws 'unresolved, this indicates that
77 ;;;; the test did not perform as expected. For example the property that was
78 ;;;; to be tested could not be tested because something else went wrong.
79 ;;;; THUNK may also throw 'untested to indicate that the test was deliberately
80 ;;;; not performed, for example because the test case is not complete yet.
81 ;;;; Finally, if THUNK throws 'unsupported, this indicates that this test
82 ;;;; requires some feature that is not available in the configured testing
83 ;;;; environment. All other exceptions thrown by THUNK are considered as
87 ;;;; Convenience macros for tests expected to pass or fail
89 ;;;; * (pass-if name body) is a short form for
90 ;;;; (run-test name #t (lambda () body))
91 ;;;; * (expect-fail name body) is a short form for
92 ;;;; (run-test name #f (lambda () body))
96 ;;;; (pass-if "integer addition" (= 2 (+ 1 1)))
99 ;;;; Convenience macros to test for exceptions
101 ;;;; The following macros take exception parameters which are pairs
102 ;;;; (type . message), where type is a symbol that denotes an exception type
103 ;;;; like 'wrong-type-arg or 'out-of-range, and message is a string holding a
104 ;;;; regular expression that describes the error message for the exception
105 ;;;; like "Argument .* out of range".
107 ;;;; * (pass-if-exception name exception body) will pass if the execution of
108 ;;;; body causes the given exception to be thrown. If no exception is
109 ;;;; thrown, the test fails. If some other exception is thrown, is is an
111 ;;;; * (expect-fail-exception name exception body) will pass unexpectedly if
112 ;;;; the execution of body causes the given exception to be thrown. If no
113 ;;;; exception is thrown, the test fails expectedly. If some other
114 ;;;; exception is thrown, it is an error.
119 ;;;; Every test in the test suite has a unique name, to help
120 ;;;; developers find tests that are failing (or unexpectedly passing),
121 ;;;; and to help gather statistics.
123 ;;;; A test name is a list of printable objects. For example:
124 ;;;; ("ports.scm" "file" "read and write back list of strings")
125 ;;;; ("ports.scm" "pipe" "read")
127 ;;;; Test names may contain arbitrary objects, but they always have
128 ;;;; the following properties:
129 ;;;; - Test names can be compared with EQUAL?.
130 ;;;; - Test names can be reliably stored and retrieved with the standard WRITE
131 ;;;; and READ procedures; doing so preserves their identity.
135 ;;;; (pass-if "simple addition" (= 4 (+ 2 2)))
137 ;;;; In that case, the test name is the list ("simple addition").
139 ;;;; In the case of simple tests the expression that is tested would often
140 ;;;; suffice as a test name by itself. Therefore, the convenience macros
141 ;;;; pass-if and expect-fail provide a shorthand notation that allows to omit
142 ;;;; a test name in such cases.
144 ;;;; * (pass-if expression) is a short form for
145 ;;;; (run-test 'expression #t (lambda () expression))
146 ;;;; * (expect-fail expression) is a short form for
147 ;;;; (run-test 'expression #f (lambda () expression))
151 ;;;; (pass-if (= 2 (+ 1 1)))
153 ;;;; The WITH-TEST-PREFIX syntax and WITH-TEST-PREFIX* procedure establish
154 ;;;; a prefix for the names of all tests whose results are reported
155 ;;;; within their dynamic scope. For example:
158 ;;;; (with-test-prefix "basic arithmetic"
159 ;;;; (pass-if "addition" (= (+ 2 2) 4))
160 ;;;; (pass-if "subtraction" (= (- 4 2) 2)))
161 ;;;; (pass-if "multiplication" (= (* 2 2) 4)))
163 ;;;; In that example, the three test names are:
164 ;;;; ("basic arithmetic" "addition"),
165 ;;;; ("basic arithmetic" "subtraction"), and
166 ;;;; ("multiplication").
168 ;;;; WITH-TEST-PREFIX can be nested. Each WITH-TEST-PREFIX postpends
169 ;;;; a new element to the current prefix:
171 ;;;; (with-test-prefix "arithmetic"
172 ;;;; (with-test-prefix "addition"
173 ;;;; (pass-if "integer" (= (+ 2 2) 4))
174 ;;;; (pass-if "complex" (= (+ 2+3i 4+5i) 6+8i)))
175 ;;;; (with-test-prefix "subtraction"
176 ;;;; (pass-if "integer" (= (- 2 2) 0))
177 ;;;; (pass-if "complex" (= (- 2+3i 1+2i) 1+1i))))
179 ;;;; The four test names here are:
180 ;;;; ("arithmetic" "addition" "integer")
181 ;;;; ("arithmetic" "addition" "complex")
182 ;;;; ("arithmetic" "subtraction" "integer")
183 ;;;; ("arithmetic" "subtraction" "complex")
185 ;;;; To print a name for a human reader, we DISPLAY its elements,
186 ;;;; separated by ": ". So, the last set of test names would be
189 ;;;; arithmetic: addition: integer
190 ;;;; arithmetic: addition: complex
191 ;;;; arithmetic: subtraction: integer
192 ;;;; arithmetic: subtraction: complex
194 ;;;; The Guile benchmarks use with-test-prefix to include the name of
195 ;;;; the source file containing the test in the test name, to help
196 ;;;; developers to find failing tests, and to provide each file with its
202 ;;;; A reporter is a function which we apply to each test outcome.
203 ;;;; Reporters can log results, print interesting results to the
204 ;;;; standard output, collect statistics, etc.
206 ;;;; A reporter function takes two mandatory arguments, RESULT and TEST, and
207 ;;;; possibly additional arguments depending on RESULT; its return value
208 ;;;; is ignored. RESULT has one of the following forms:
210 ;;;; pass - The test named TEST passed.
211 ;;;; Additional arguments are ignored.
212 ;;;; upass - The test named TEST passed unexpectedly.
213 ;;;; Additional arguments are ignored.
214 ;;;; fail - The test named TEST failed.
215 ;;;; Additional arguments are ignored.
216 ;;;; xfail - The test named TEST failed, as expected.
217 ;;;; Additional arguments are ignored.
218 ;;;; unresolved - The test named TEST did not perform as expected, for
219 ;;;; example the property that was to be tested could not be
220 ;;;; tested because something else went wrong.
221 ;;;; Additional arguments are ignored.
222 ;;;; untested - The test named TEST was not actually performed, for
223 ;;;; example because the test case is not complete yet.
224 ;;;; Additional arguments are ignored.
225 ;;;; unsupported - The test named TEST requires some feature that is not
226 ;;;; available in the configured testing environment.
227 ;;;; Additional arguments are ignored.
228 ;;;; error - An error occurred while the test named TEST was
229 ;;;; performed. Since this result means that the system caught
230 ;;;; an exception it could not handle, the exception arguments
231 ;;;; are passed as additional arguments.
233 ;;;; This library provides some standard reporters for logging results
234 ;;;; to a file, reporting interesting results to the user, and
235 ;;;; collecting totals.
237 ;;;; You can use the REGISTER-REPORTER function and friends to add
238 ;;;; whatever reporting functions you like. If you don't register any
239 ;;;; reporters, the library uses FULL-REPORTER, which simply writes
240 ;;;; all results to the standard output.
246 ;;; Define some exceptions which are commonly being tested for.
247 (define exception:bad-variable
248 (cons 'syntax-error "Bad variable"))
249 (define exception:missing-expression
250 (cons 'misc-error "^missing or extra expression"))
251 (define exception:out-of-range
252 (cons 'out-of-range "^.*out of range"))
253 (define exception:unbound-var
254 (cons 'unbound-variable "^Unbound variable"))
255 (define exception:used-before-defined
256 (cons 'unbound-variable "^Variable used before given a value"))
257 (define exception:wrong-num-args
258 (cons 'wrong-number-of-args "^Wrong number of arguments"))
259 (define exception:wrong-type-arg
260 (cons 'wrong-type-arg "^Wrong type"))
261 (define exception:numerical-overflow
262 (cons 'numerical-overflow "^Numerical overflow"))
263 (define exception:struct-set!-denied
264 (cons 'misc-error "^set! denied for field"))
265 (define exception:system-error
266 (cons 'system-error ".*"))
267 (define exception:miscellaneous-error
268 (cons 'misc-error "^.*"))
269 (define exception:read-error
270 (cons 'read-error "^.*$"))
272 ;; as per throw in scm_to_locale_stringn()
273 (define exception:string-contains-nul
274 (cons 'misc-error "^string contains #\\\\nul character"))
277 ;;; Display all parameters to the default output port, followed by a newline.
278 (define (display-line . objs)
279 (for-each display objs)
282 ;;; Display all parameters to the given output port, followed by a newline.
283 (define (display-line-port port . objs)
284 (for-each (lambda (obj) (display obj port)) objs)
291 ;;; The central testing routine.
292 ;;; The idea is taken from Greg, the GNUstep regression test environment.
294 (let ((test-running #f))
295 (define (local-run-test name expect-pass thunk)
297 (error "Nested calls to run-test are not permitted.")
298 (let ((test-name (full-name name)))
299 (set! test-running #t)
302 (let ((result (thunk)))
303 (if (eq? result #t) (throw 'pass))
304 (if (eq? result #f) (throw 'fail))
305 (throw 'unresolved)))
309 (report (if expect-pass 'pass 'upass) test-name))
311 (report (if expect-pass 'fail 'xfail) test-name))
312 ((unresolved untested unsupported)
313 (report key test-name))
315 (report 'unresolved test-name)
318 (report 'error test-name (cons key args))))))
319 (set! test-running #f))))
320 (set! run-test local-run-test))
322 ;;; A short form for tests that are expected to pass, taken from Greg.
323 (define-syntax pass-if
326 ;; presume this is a simple test, i.e. (pass-if (even? 2))
327 ;; where the body should also be the name.
328 (run-test 'name #t (lambda () name)))
330 (run-test name #t (lambda () rest ...)))))
332 ;;; A short form for tests that are expected to fail, taken from Greg.
333 (define-syntax expect-fail
336 ;; presume this is a simple test, i.e. (expect-fail (even? 2))
337 ;; where the body should also be the name.
338 (run-test 'name #f (lambda () name)))
340 (run-test name #f (lambda () rest ...)))))
342 ;;; A helper function to implement the macros that test for exceptions.
343 (define (run-test-exception name exception expect-pass thunk)
344 (run-test name expect-pass
346 (stack-catch (car exception)
347 (lambda () (thunk) #f)
348 (lambda (key proc message . rest)
350 ;; handle explicit key
351 ((string-match (cdr exception) message)
353 ;; handle `(error ...)' which uses `misc-error' for key and doesn't
354 ;; yet format the message and args (we have to do it here).
355 ((and (eq? 'misc-error (car exception))
357 (string-match (cdr exception)
358 (apply simple-format #f message (car rest))))
360 ;; handle syntax errors which use `syntax-error' for key and don't
361 ;; yet format the message and args (we have to do it here).
362 ((and (eq? 'syntax-error (car exception))
364 (string-match (cdr exception)
365 (apply simple-format #f message (car rest))))
367 ;; unhandled; throw again
369 (apply throw key proc message rest))))))))
371 ;;; A short form for tests that expect a certain exception to be thrown.
372 (define-syntax pass-if-exception
374 ((_ name exception body rest ...)
375 (run-test-exception name exception #t (lambda () body rest ...)))))
377 ;;; A short form for tests expected to fail to throw a certain exception.
378 (define-syntax expect-fail-exception
380 ((_ name exception body rest ...)
381 (run-test-exception name exception #f (lambda () body rest ...)))))
387 ;;;; Turn a test name into a nice human-readable string.
388 (define (format-test-name name)
389 (call-with-output-string
391 (let loop ((name name)
395 (display separator port)
396 (display (car name) port)
397 (loop (cdr name) ": ")))))))
399 ;;;; For a given test-name, deliver the full name including all prefixes.
400 (define (full-name name)
401 (append (current-test-prefix) (list name)))
403 ;;; A fluid containing the current test prefix, as a list.
404 (define prefix-fluid (make-fluid))
405 (fluid-set! prefix-fluid '())
406 (define (current-test-prefix)
407 (fluid-ref prefix-fluid))
409 ;;; Postpend PREFIX to the current name prefix while evaluting THUNK.
410 ;;; The name prefix is only changed within the dynamic scope of the
411 ;;; call to with-test-prefix*. Return the value returned by THUNK.
412 (define (with-test-prefix* prefix thunk)
413 (with-fluids ((prefix-fluid
414 (append (fluid-ref prefix-fluid) (list prefix))))
417 ;;; (with-test-prefix PREFIX BODY ...)
418 ;;; Postpend PREFIX to the current name prefix while evaluating BODY ...
419 ;;; The name prefix is only changed within the dynamic scope of the
420 ;;; with-test-prefix expression. Return the value returned by the last
422 (defmacro with-test-prefix (prefix . body)
423 `(with-test-prefix* ,prefix (lambda () ,@body)))
425 ;;; Call THUNK using the debugging evaluator.
426 (define (with-debugging-evaluator* thunk)
430 (set! dopts (debug-options))
431 (debug-enable 'debug))
434 (debug-options dopts)))))
436 ;;; Evaluate BODY... using the debugging evaluator.
437 (define-macro (with-debugging-evaluator . body)
438 `(with-debugging-evaluator* (lambda () ,@body)))
445 ;;; The global list of reporters.
446 (define reporters '())
448 ;;; The default reporter, to be used only if no others exist.
449 (define default-reporter #f)
451 ;;; Add the procedure REPORTER to the current set of reporter functions.
452 ;;; Signal an error if that reporter procedure object is already registered.
453 (define (register-reporter reporter)
454 (if (memq reporter reporters)
455 (error "register-reporter: reporter already registered: " reporter))
456 (set! reporters (cons reporter reporters)))
458 ;;; Remove the procedure REPORTER from the current set of reporter
459 ;;; functions. Signal an error if REPORTER is not currently registered.
460 (define (unregister-reporter reporter)
461 (if (memq reporter reporters)
462 (set! reporters (delq! reporter reporters))
463 (error "unregister-reporter: reporter not registered: " reporter)))
465 ;;; Return true iff REPORTER is in the current set of reporter functions.
466 (define (reporter-registered? reporter)
467 (if (memq reporter reporters) #t #f))
469 ;;; Send RESULT to all currently registered reporter functions.
470 (define (report . args)
471 (if (pair? reporters)
472 (for-each (lambda (reporter) (apply reporter args))
474 (apply default-reporter args)))
477 ;;;; Some useful standard reporters:
478 ;;;; Count reporters count the occurrence of each test result type.
479 ;;;; Log reporters write all test results to a given log file.
480 ;;;; Full reporters write all test results to the standard output.
481 ;;;; User reporters write interesting test results to the standard output.
483 ;;; The complete list of possible test results.
485 '((pass "PASS" "passes: ")
486 (fail "FAIL" "failures: ")
487 (upass "UPASS" "unexpected passes: ")
488 (xfail "XFAIL" "expected failures: ")
489 (unresolved "UNRESOLVED" "unresolved test cases: ")
490 (untested "UNTESTED" "untested test cases: ")
491 (unsupported "UNSUPPORTED" "unsupported test cases: ")
492 (error "ERROR" "errors: ")))
494 ;;; The list of important test results.
495 (define important-result-tags
496 '(fail upass unresolved error))
498 ;;; Display a single test result in formatted form to the given port
499 (define (print-result port result name . args)
500 (let* ((tag (assq result result-tags))
501 (label (if tag (cadr tag) #f)))
506 (display (format-test-name name) port)
509 (display " - arguments: " port)
512 (error "(test-suite lib) FULL-REPORTER: unrecognized result: "
515 ;;; Return a list of the form (COUNTER RESULTS), where:
516 ;;; - COUNTER is a reporter procedure, and
517 ;;; - RESULTS is a procedure taking no arguments which returns the
518 ;;; results seen so far by COUNTER. The return value is an alist
519 ;;; mapping outcome symbols (`pass', `fail', etc.) onto counts.
520 (define (make-count-reporter)
521 (let ((counts (map (lambda (tag) (cons (car tag) 0)) result-tags)))
523 (lambda (result name . args)
524 (let ((pair (assq result counts)))
526 (set-cdr! pair (+ 1 (cdr pair)))
527 (error "count-reporter: unexpected test result: "
528 (cons result (cons name args))))))
530 (append counts '())))))
532 ;;; Print a count reporter's results nicely. Pass this function the value
533 ;;; returned by a count reporter's RESULTS procedure.
534 (define (print-counts results . port?)
535 (let ((port (if (pair? port?)
537 (current-output-port))))
539 (display-line-port port "Totals for this test run:")
542 (let ((result (assq (car tag) results)))
544 (display-line-port port (caddr tag) (cdr result))
545 (display-line-port port
547 "no total available for `" (car tag) "'"))))
551 ;;; Return a reporter procedure which prints all results to the file
552 ;;; FILE, in human-readable form. FILE may be a filename, or a port.
553 (define (make-log-reporter file)
554 (let ((port (if (output-port? file) file
555 (open-output-file file))))
557 (apply print-result port args)
558 (force-output port))))
560 ;;; A reporter that reports all results to the user.
561 (define (full-reporter . args)
562 (apply print-result (current-output-port) args))
564 ;;; A reporter procedure which shows interesting results (failures,
565 ;;; unexpected passes etc.) to the user.
566 (define (user-reporter result name . args)
567 (if (memq result important-result-tags)
568 (apply full-reporter result name args)))
570 (set! default-reporter full-reporter)