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.
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.
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.
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
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)
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
41 exception:null-pointer-error
44 ;; Reporting passes and failures.
48 pass-if-exception expect-fail-exception
50 ;; Naming groups of tests in a regular fashion.
57 ;; Using the debugging evaluator.
58 with-debugging-evaluator with-debugging-evaluator*
60 ;; Clearing stale references on the C stack for GC-sensitive tests.
61 clear-stale-stack-references
63 ;; Using a given locale
64 with-locale with-locale* with-latin1-locale with-latin1-locale*
66 ;; Reporting results in various ways.
67 register-reporter unregister-reporter reporter-registered?
68 make-count-reporter print-counts
74 ;;;; If you're using Emacs's Scheme mode:
75 ;;;; (put 'with-test-prefix 'scheme-indent-function 1)
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:
89 ;;;; (run-test "integer addition" #t (lambda () (= 2 (+ 1 1))))
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
104 ;;;; Convenience macros for tests expected to pass or fail
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))
113 ;;;; (pass-if "integer addition" (= 2 (+ 1 1)))
116 ;;;; Convenience macros to test for exceptions
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".
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
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.
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.
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")
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.
152 ;;;; (pass-if "simple addition" (= 4 (+ 2 2)))
154 ;;;; In that case, the test name is the list ("simple addition").
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.
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))
168 ;;;; (pass-if (= 2 (+ 1 1)))
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:
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)))
180 ;;;; In that example, the three test names are:
181 ;;;; ("basic arithmetic" "addition"),
182 ;;;; ("basic arithmetic" "subtraction"), and
183 ;;;; ("multiplication").
185 ;;;; WITH-TEST-PREFIX can be nested. Each WITH-TEST-PREFIX appends
186 ;;;; a new element to the current prefix:
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))))
196 ;;;; The four test names here are:
197 ;;;; ("arithmetic" "addition" "integer")
198 ;;;; ("arithmetic" "addition" "complex")
199 ;;;; ("arithmetic" "subtraction" "integer")
200 ;;;; ("arithmetic" "subtraction" "complex")
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
206 ;;;; arithmetic: addition: integer
207 ;;;; arithmetic: addition: complex
208 ;;;; arithmetic: subtraction: integer
209 ;;;; arithmetic: subtraction: complex
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
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.
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:
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.
250 ;;;; This library provides some standard reporters for logging results
251 ;;;; to a file, reporting interesting results to the user, and
252 ;;;; collecting totals.
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.
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 "^.*$"))
297 ;; as per throw in scm_to_locale_stringn()
298 (define exception:string-contains-nul
299 (cons 'misc-error "^string contains #\\\\nul character"))
302 ;;; Display all parameters to the default output port, followed by a newline.
303 (define (display-line . objs)
304 (for-each display objs)
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)
316 ;;; The central testing routine.
317 ;;; The idea is taken from Greg, the GNUstep regression test environment.
319 (let ((test-running #f))
320 (lambda (name expect-pass thunk)
322 (error "Nested calls to run-test are not permitted."))
323 (let ((test-name (full-name name)))
324 (set! test-running #t)
327 (let ((result (thunk)))
328 (if (eq? result #t) (throw 'pass))
329 (if (eq? result #f) (throw 'fail))
330 (throw 'unresolved)))
334 (report (if expect-pass 'pass 'upass) test-name))
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)
341 ((unresolved untested unsupported)
342 (report key test-name))
344 (report 'unresolved test-name)
347 (report 'error test-name (cons key args))))))
348 (set! test-running #f)))))
350 ;;; A short form for tests that are expected to pass, taken from Greg.
351 (define-syntax pass-if
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)))
358 (run-test name #t (lambda () rest ...)))))
360 (define-syntax pass-if-equal
362 "Succeed if and only if BODY's return value is equal? to EXPECTED."
364 (pass-if-equal 'body expected body))
365 ((_ name expected body ...)
368 (let ((result (begin body ...)))
369 (or (equal? expected result)
371 'expected-value expected
372 'actual-value result))))))))
374 ;;; A short form for tests that are expected to fail, taken from Greg.
375 (define-syntax expect-fail
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)))
382 (run-test name #f (lambda () rest ...)))))
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
388 (stack-catch (car exception)
389 (lambda () (thunk) #f)
390 (lambda (key proc message . rest)
392 ;; handle explicit key
393 ((string-match (cdr exception) message)
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))
399 (string-match (cdr exception)
400 (apply simple-format #f message (car rest))))
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))
406 (string-match (cdr exception)
407 (apply simple-format #f message (car rest))))
409 ;; unhandled; throw again
411 (apply throw key proc message rest))))))))
413 ;;; A short form for tests that expect a certain exception to be thrown.
414 (define-syntax pass-if-exception
416 ((_ name exception body rest ...)
417 (run-test-exception name exception #t (lambda () body rest ...)))))
419 ;;; A short form for tests expected to fail to throw a certain exception.
420 (define-syntax expect-fail-exception
422 ((_ name exception body rest ...)
423 (run-test-exception name exception #f (lambda () body rest ...)))))
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
436 (let loop ((name name)
440 (display separator port)
441 (display (car name) port)
442 (loop (cdr name) ": "))))))))
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)))
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))
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))))
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
466 (define-syntax with-test-prefix
469 (with-test-prefix* prefix (lambda () body ...)))))
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)))))))
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
496 ((_ section-name exp ...)
497 (with-test-prefix section-name (c&e exp) ...))))
499 ;;; Call THUNK using the debugging evaluator.
500 (define (with-debugging-evaluator* thunk)
504 (set! dopts (debug-options)))
507 (debug-options dopts)))))
509 ;;; Evaluate BODY... using the debugging evaluator.
510 (define-macro (with-debugging-evaluator . body)
511 `(with-debugging-evaluator* (lambda () ,@body)))
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.)
518 (define* (clear-stale-stack-references #:optional (n 10))
520 (with-continuation-barrier
522 (clear-stale-stack-references (1- n))))))
524 ;;; Call THUNK with a given locale
525 (define (with-locale* nloc thunk)
529 (if (defined? 'setlocale)
531 (set! loc (false-if-exception (setlocale LC_ALL)))
533 (not (false-if-exception (setlocale LC_ALL nloc))))
534 (throw 'unresolved)))
535 (throw 'unresolved)))
538 (if (and (defined? 'setlocale) loc)
539 (setlocale LC_ALL loc))))))
541 ;;; Evaluate BODY... using the given locale.
542 (define-syntax with-locale
545 (with-locale* loc (lambda () body ...)))))
547 ;;; Try out several ISO-8859-1 locales and run THUNK under the one that works
549 (define (with-latin1-locale* thunk)
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")))
558 (let loop ((locales %locales))
563 (with-locale* (car locales) thunk))
565 (loop (cdr locales)))))))
567 ;;; Evaluate BODY... using an ISO-8859-1 locale or throw `unresolved' if none
569 (define-syntax with-latin1-locale
572 (with-latin1-locale* (lambda () body ...)))))
578 ;;; The global list of reporters.
579 (define reporters '())
581 ;;; The default reporter, to be used only if no others exist.
582 (define default-reporter #f)
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)))
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)))
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))
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))
607 (apply default-reporter args)))
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.
616 ;;; The complete list of possible test results.
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: ")))
627 ;;; The list of important test results.
628 (define important-result-tags
629 '(fail upass unresolved error))
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)))
639 (display (format-test-name name) port)
642 (display " - arguments: " port)
645 (error "(test-suite lib) FULL-REPORTER: unrecognized result: "
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)))
656 (lambda (result name . args)
657 (let ((pair (assq result counts)))
659 (set-cdr! pair (+ 1 (cdr pair)))
660 (error "count-reporter: unexpected test result: "
661 (cons result (cons name args))))))
663 (append counts '())))))
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?)
670 (current-output-port))))
672 (display-line-port port "Totals for this test run:")
675 (let ((result (assq (car tag) results)))
677 (display-line-port port (caddr tag) (cdr result))
678 (display-line-port port
680 "no total available for `" (car tag) "'"))))
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))))
690 (apply print-result port args)
691 (force-output port))))
693 ;;; A reporter that reports all results to the user.
694 (define (full-reporter . args)
695 (apply print-result (current-output-port) args))
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)))
703 (set! default-reporter full-reporter)