merge from 1.8 branch
[bpt/guile.git] / test-suite / lib.scm
CommitLineData
000ee07f 1;;;; test-suite/lib.scm --- generic support for testing
6e7d5622 2;;;; Copyright (C) 1999, 2000, 2001, 2004, 2006 Free Software Foundation, Inc.
bba2d190 3;;;;
000ee07f
JB
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.
bba2d190 8;;;;
000ee07f
JB
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.
bba2d190 13;;;;
000ee07f
JB
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
92205699
MV
16;;;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17;;;; Boston, MA 02110-1301 USA
000ee07f 18
6b4113af 19(define-module (test-suite lib)
8bc4547c 20 :use-module (ice-9 stack-catch)
1a179b03
MD
21 :use-module (ice-9 regex)
22 :export (
000ee07f 23
6b4113af 24 ;; Exceptions which are commonly being tested for.
d6754c23 25 exception:bad-variable
d6e04e7c 26 exception:missing-expression
08c608e1 27 exception:out-of-range exception:unbound-var
0ac46745 28 exception:used-before-defined
08c608e1 29 exception:wrong-num-args exception:wrong-type-arg
0825ae0b 30 exception:numerical-overflow
6b4113af 31
000ee07f 32 ;; Reporting passes and failures.
6b4113af
DH
33 run-test
34 pass-if expect-fail
35 pass-if-exception expect-fail-exception
000ee07f
JB
36
37 ;; Naming groups of tests in a regular fashion.
38 with-test-prefix with-test-prefix* current-test-prefix
d6e04e7c 39 format-test-name
000ee07f
JB
40
41 ;; Reporting results in various ways.
42 register-reporter unregister-reporter reporter-registered?
43 make-count-reporter print-counts
bba2d190 44 make-log-reporter
087dab1c 45 full-reporter
d6e04e7c 46 user-reporter))
000ee07f
JB
47
48
49;;;; If you're using Emacs's Scheme mode:
000ee07f
JB
50;;;; (put 'with-test-prefix 'scheme-indent-function 1)
51
52\f
57e7f270
DH
53;;;; CORE FUNCTIONS
54;;;;
55;;;; The function (run-test name expected-result thunk) is the heart of the
56;;;; testing environment. The first parameter NAME is a unique name for the
57;;;; test to be executed (for an explanation of this parameter see below under
58;;;; TEST NAMES). The second parameter EXPECTED-RESULT is a boolean value
59;;;; that indicates whether the corresponding test is expected to pass. If
60;;;; EXPECTED-RESULT is #t the test is expected to pass, if EXPECTED-RESULT is
61;;;; #f the test is expected to fail. Finally, THUNK is the function that
62;;;; actually performs the test. For example:
63;;;;
64;;;; (run-test "integer addition" #t (lambda () (= 2 (+ 1 1))))
65;;;;
66;;;; To report success, THUNK should either return #t or throw 'pass. To
67;;;; report failure, THUNK should either return #f or throw 'fail. If THUNK
68;;;; returns a non boolean value or throws 'unresolved, this indicates that
69;;;; the test did not perform as expected. For example the property that was
70;;;; to be tested could not be tested because something else went wrong.
71;;;; THUNK may also throw 'untested to indicate that the test was deliberately
72;;;; not performed, for example because the test case is not complete yet.
73;;;; Finally, if THUNK throws 'unsupported, this indicates that this test
74;;;; requires some feature that is not available in the configured testing
75;;;; environment. All other exceptions thrown by THUNK are considered as
76;;;; errors.
77;;;;
6b4113af
DH
78;;;;
79;;;; Convenience macros for tests expected to pass or fail
80;;;;
bba2d190 81;;;; * (pass-if name body) is a short form for
57e7f270 82;;;; (run-test name #t (lambda () body))
bba2d190 83;;;; * (expect-fail name body) is a short form for
57e7f270
DH
84;;;; (run-test name #f (lambda () body))
85;;;;
bba2d190 86;;;; For example:
57e7f270
DH
87;;;;
88;;;; (pass-if "integer addition" (= 2 (+ 1 1)))
6b4113af
DH
89;;;;
90;;;;
91;;;; Convenience macros to test for exceptions
92;;;;
93;;;; The following macros take exception parameters which are pairs
94;;;; (type . message), where type is a symbol that denotes an exception type
95;;;; like 'wrong-type-arg or 'out-of-range, and message is a string holding a
96;;;; regular expression that describes the error message for the exception
97;;;; like "Argument .* out of range".
98;;;;
99;;;; * (pass-if-exception name exception body) will pass if the execution of
100;;;; body causes the given exception to be thrown. If no exception is
101;;;; thrown, the test fails. If some other exception is thrown, is is an
102;;;; error.
103;;;; * (expect-fail-exception name exception body) will pass unexpectedly if
104;;;; the execution of body causes the given exception to be thrown. If no
105;;;; exception is thrown, the test fails expectedly. If some other
106;;;; exception is thrown, it is an error.
57e7f270
DH
107
108\f
000ee07f
JB
109;;;; TEST NAMES
110;;;;
111;;;; Every test in the test suite has a unique name, to help
112;;;; developers find tests that are failing (or unexpectedly passing),
113;;;; and to help gather statistics.
114;;;;
115;;;; A test name is a list of printable objects. For example:
116;;;; ("ports.scm" "file" "read and write back list of strings")
117;;;; ("ports.scm" "pipe" "read")
118;;;;
119;;;; Test names may contain arbitrary objects, but they always have
120;;;; the following properties:
121;;;; - Test names can be compared with EQUAL?.
122;;;; - Test names can be reliably stored and retrieved with the standard WRITE
123;;;; and READ procedures; doing so preserves their identity.
bba2d190 124;;;;
000ee07f 125;;;; For example:
bba2d190 126;;;;
57e7f270 127;;;; (pass-if "simple addition" (= 4 (+ 2 2)))
bba2d190 128;;;;
000ee07f
JB
129;;;; In that case, the test name is the list ("simple addition").
130;;;;
e46083d5
DH
131;;;; In the case of simple tests the expression that is tested would often
132;;;; suffice as a test name by itself. Therefore, the convenience macros
133;;;; pass-if and expect-fail provide a shorthand notation that allows to omit
134;;;; a test name in such cases.
135;;;;
136;;;; * (pass-if expression) is a short form for
137;;;; (run-test 'expression #t (lambda () expression))
138;;;; * (expect-fail expression) is a short form for
139;;;; (run-test 'expression #f (lambda () expression))
140;;;;
141;;;; For example:
142;;;;
143;;;; (pass-if (= 2 (+ 1 1)))
144;;;;
000ee07f
JB
145;;;; The WITH-TEST-PREFIX syntax and WITH-TEST-PREFIX* procedure establish
146;;;; a prefix for the names of all tests whose results are reported
147;;;; within their dynamic scope. For example:
bba2d190 148;;;;
000ee07f
JB
149;;;; (begin
150;;;; (with-test-prefix "basic arithmetic"
151;;;; (pass-if "addition" (= (+ 2 2) 4))
05c4ba00 152;;;; (pass-if "subtraction" (= (- 4 2) 2)))
000ee07f 153;;;; (pass-if "multiplication" (= (* 2 2) 4)))
bba2d190 154;;;;
000ee07f
JB
155;;;; In that example, the three test names are:
156;;;; ("basic arithmetic" "addition"),
05c4ba00 157;;;; ("basic arithmetic" "subtraction"), and
000ee07f
JB
158;;;; ("multiplication").
159;;;;
160;;;; WITH-TEST-PREFIX can be nested. Each WITH-TEST-PREFIX postpends
161;;;; a new element to the current prefix:
bba2d190 162;;;;
000ee07f
JB
163;;;; (with-test-prefix "arithmetic"
164;;;; (with-test-prefix "addition"
165;;;; (pass-if "integer" (= (+ 2 2) 4))
166;;;; (pass-if "complex" (= (+ 2+3i 4+5i) 6+8i)))
167;;;; (with-test-prefix "subtraction"
168;;;; (pass-if "integer" (= (- 2 2) 0))
169;;;; (pass-if "complex" (= (- 2+3i 1+2i) 1+1i))))
bba2d190 170;;;;
000ee07f
JB
171;;;; The four test names here are:
172;;;; ("arithmetic" "addition" "integer")
173;;;; ("arithmetic" "addition" "complex")
174;;;; ("arithmetic" "subtraction" "integer")
175;;;; ("arithmetic" "subtraction" "complex")
176;;;;
177;;;; To print a name for a human reader, we DISPLAY its elements,
178;;;; separated by ": ". So, the last set of test names would be
179;;;; reported as:
bba2d190 180;;;;
000ee07f
JB
181;;;; arithmetic: addition: integer
182;;;; arithmetic: addition: complex
183;;;; arithmetic: subtraction: integer
184;;;; arithmetic: subtraction: complex
185;;;;
186;;;; The Guile benchmarks use with-test-prefix to include the name of
187;;;; the source file containing the test in the test name, to help
188;;;; developers to find failing tests, and to provide each file with its
189;;;; own namespace.
190
191\f
192;;;; REPORTERS
bba2d190 193;;;;
000ee07f
JB
194;;;; A reporter is a function which we apply to each test outcome.
195;;;; Reporters can log results, print interesting results to the
196;;;; standard output, collect statistics, etc.
bba2d190 197;;;;
57e7f270
DH
198;;;; A reporter function takes two mandatory arguments, RESULT and TEST, and
199;;;; possibly additional arguments depending on RESULT; its return value
000ee07f
JB
200;;;; is ignored. RESULT has one of the following forms:
201;;;;
bba2d190 202;;;; pass - The test named TEST passed.
57e7f270
DH
203;;;; Additional arguments are ignored.
204;;;; upass - The test named TEST passed unexpectedly.
205;;;; Additional arguments are ignored.
206;;;; fail - The test named TEST failed.
207;;;; Additional arguments are ignored.
208;;;; xfail - The test named TEST failed, as expected.
209;;;; Additional arguments are ignored.
210;;;; unresolved - The test named TEST did not perform as expected, for
211;;;; example the property that was to be tested could not be
212;;;; tested because something else went wrong.
213;;;; Additional arguments are ignored.
214;;;; untested - The test named TEST was not actually performed, for
bba2d190 215;;;; example because the test case is not complete yet.
57e7f270
DH
216;;;; Additional arguments are ignored.
217;;;; unsupported - The test named TEST requires some feature that is not
218;;;; available in the configured testing environment.
219;;;; Additional arguments are ignored.
220;;;; error - An error occurred while the test named TEST was
221;;;; performed. Since this result means that the system caught
222;;;; an exception it could not handle, the exception arguments
223;;;; are passed as additional arguments.
000ee07f
JB
224;;;;
225;;;; This library provides some standard reporters for logging results
226;;;; to a file, reporting interesting results to the user, and
227;;;; collecting totals.
087dab1c
JB
228;;;;
229;;;; You can use the REGISTER-REPORTER function and friends to add
230;;;; whatever reporting functions you like. If you don't register any
231;;;; reporters, the library uses FULL-REPORTER, which simply writes
232;;;; all results to the standard output.
000ee07f
JB
233
234\f
57e7f270
DH
235;;;; MISCELLANEOUS
236;;;;
237
6b4113af 238;;; Define some exceptions which are commonly being tested for.
d6754c23
DH
239(define exception:bad-variable
240 (cons 'syntax-error "Bad variable"))
d6e04e7c
DH
241(define exception:missing-expression
242 (cons 'misc-error "^missing or extra expression"))
6b4113af 243(define exception:out-of-range
a47d845f 244 (cons 'out-of-range "^.*out of range"))
08c608e1
DH
245(define exception:unbound-var
246 (cons 'unbound-variable "^Unbound variable"))
0ac46745
MV
247(define exception:used-before-defined
248 (cons 'unbound-variable "^Variable used before given a value"))
08c608e1
DH
249(define exception:wrong-num-args
250 (cons 'wrong-number-of-args "^Wrong number of arguments"))
6b4113af 251(define exception:wrong-type-arg
a47d845f 252 (cons 'wrong-type-arg "^Wrong type"))
0825ae0b
KR
253(define exception:numerical-overflow
254 (cons 'numerical-overflow "^Numerical overflow"))
6b4113af 255
57e7f270
DH
256;;; Display all parameters to the default output port, followed by a newline.
257(define (display-line . objs)
258 (for-each display objs)
259 (newline))
260
261;;; Display all parameters to the given output port, followed by a newline.
262(define (display-line-port port . objs)
263 (for-each (lambda (obj) (display obj port)) objs)
264 (newline port))
265
266\f
267;;;; CORE FUNCTIONS
268;;;;
269
270;;; The central testing routine.
271;;; The idea is taken from Greg, the GNUstep regression test environment.
272(define run-test #f)
273(let ((test-running #f))
274 (define (local-run-test name expect-pass thunk)
275 (if test-running
276 (error "Nested calls to run-test are not permitted.")
277 (let ((test-name (full-name name)))
278 (set! test-running #t)
279 (catch #t
280 (lambda ()
281 (let ((result (thunk)))
282 (if (eq? result #t) (throw 'pass))
283 (if (eq? result #f) (throw 'fail))
284 (throw 'unresolved)))
285 (lambda (key . args)
286 (case key
bba2d190 287 ((pass)
57e7f270 288 (report (if expect-pass 'pass 'upass) test-name))
bba2d190 289 ((fail)
57e7f270 290 (report (if expect-pass 'fail 'xfail) test-name))
bba2d190 291 ((unresolved untested unsupported)
57e7f270 292 (report key test-name))
bba2d190 293 ((quit)
57e7f270
DH
294 (report 'unresolved test-name)
295 (quit))
bba2d190 296 (else
57e7f270
DH
297 (report 'error test-name (cons key args))))))
298 (set! test-running #f))))
299 (set! run-test local-run-test))
300
301;;; A short form for tests that are expected to pass, taken from Greg.
b4690a66
RB
302(defmacro pass-if (name . rest)
303 (if (and (null? rest) (pair? name))
304 ;; presume this is a simple test, i.e. (pass-if (even? 2))
305 ;; where the body should also be the name.
e46083d5 306 `(run-test ',name #t (lambda () ,name))
b4690a66 307 `(run-test ,name #t (lambda () ,@rest))))
57e7f270
DH
308
309;;; A short form for tests that are expected to fail, taken from Greg.
b4690a66
RB
310(defmacro expect-fail (name . rest)
311 (if (and (null? rest) (pair? name))
312 ;; presume this is a simple test, i.e. (expect-fail (even? 2))
313 ;; where the body should also be the name.
e46083d5 314 `(run-test ',name #f (lambda () ,name))
b4690a66 315 `(run-test ,name #f (lambda () ,@rest))))
57e7f270 316
6b4113af
DH
317;;; A helper function to implement the macros that test for exceptions.
318(define (run-test-exception name exception expect-pass thunk)
319 (run-test name expect-pass
320 (lambda ()
8bc4547c 321 (stack-catch (car exception)
6b4113af 322 (lambda () (thunk) #f)
bba2d190
TTN
323 (lambda (key proc message . rest)
324 (cond
325 ;; handle explicit key
326 ((string-match (cdr exception) message)
327 #t)
328 ;; handle `(error ...)' which uses `misc-error' for key and doesn't
329 ;; yet format the message and args (we have to do it here).
330 ((and (eq? 'misc-error (car exception))
331 (list? rest)
332 (string-match (cdr exception)
333 (apply simple-format #f message (car rest))))
334 #t)
e6729603
DH
335 ;; handle syntax errors which use `syntax-error' for key and don't
336 ;; yet format the message and args (we have to do it here).
337 ((and (eq? 'syntax-error (car exception))
338 (list? rest)
339 (string-match (cdr exception)
340 (apply simple-format #f message (car rest))))
341 #t)
bba2d190
TTN
342 ;; unhandled; throw again
343 (else
344 (apply throw key proc message rest))))))))
6b4113af
DH
345
346;;; A short form for tests that expect a certain exception to be thrown.
347(defmacro pass-if-exception (name exception body . rest)
348 `(,run-test-exception ,name ,exception #t (lambda () ,body ,@rest)))
349
350;;; A short form for tests expected to fail to throw a certain exception.
351(defmacro expect-fail-exception (name exception body . rest)
352 `(,run-test-exception ,name ,exception #f (lambda () ,body ,@rest)))
353
57e7f270
DH
354\f
355;;;; TEST NAMES
356;;;;
357
358;;;; Turn a test name into a nice human-readable string.
359(define (format-test-name name)
360 (call-with-output-string
361 (lambda (port)
362 (let loop ((name name)
363 (separator ""))
364 (if (pair? name)
365 (begin
366 (display separator port)
367 (display (car name) port)
368 (loop (cdr name) ": ")))))))
369
370;;;; For a given test-name, deliver the full name including all prefixes.
371(define (full-name name)
372 (append (current-test-prefix) (list name)))
000ee07f
JB
373
374;;; A fluid containing the current test prefix, as a list.
375(define prefix-fluid (make-fluid))
376(fluid-set! prefix-fluid '())
57e7f270
DH
377(define (current-test-prefix)
378 (fluid-ref prefix-fluid))
000ee07f
JB
379
380;;; Postpend PREFIX to the current name prefix while evaluting THUNK.
381;;; The name prefix is only changed within the dynamic scope of the
382;;; call to with-test-prefix*. Return the value returned by THUNK.
383(define (with-test-prefix* prefix thunk)
384 (with-fluids ((prefix-fluid
385 (append (fluid-ref prefix-fluid) (list prefix))))
386 (thunk)))
387
388;;; (with-test-prefix PREFIX BODY ...)
389;;; Postpend PREFIX to the current name prefix while evaluating BODY ...
390;;; The name prefix is only changed within the dynamic scope of the
391;;; with-test-prefix expression. Return the value returned by the last
392;;; BODY expression.
393(defmacro with-test-prefix (prefix . body)
394 `(with-test-prefix* ,prefix (lambda () ,@body)))
395
000ee07f 396\f
57e7f270 397;;;; REPORTERS
bba2d190 398;;;;
000ee07f
JB
399
400;;; The global list of reporters.
401(define reporters '())
402
087dab1c
JB
403;;; The default reporter, to be used only if no others exist.
404(define default-reporter #f)
405
000ee07f
JB
406;;; Add the procedure REPORTER to the current set of reporter functions.
407;;; Signal an error if that reporter procedure object is already registered.
408(define (register-reporter reporter)
409 (if (memq reporter reporters)
410 (error "register-reporter: reporter already registered: " reporter))
411 (set! reporters (cons reporter reporters)))
412
413;;; Remove the procedure REPORTER from the current set of reporter
414;;; functions. Signal an error if REPORTER is not currently registered.
415(define (unregister-reporter reporter)
416 (if (memq reporter reporters)
417 (set! reporters (delq! reporter reporters))
418 (error "unregister-reporter: reporter not registered: " reporter)))
419
420;;; Return true iff REPORTER is in the current set of reporter functions.
421(define (reporter-registered? reporter)
422 (if (memq reporter reporters) #t #f))
423
000ee07f 424;;; Send RESULT to all currently registered reporter functions.
57e7f270 425(define (report . args)
087dab1c 426 (if (pair? reporters)
57e7f270 427 (for-each (lambda (reporter) (apply reporter args))
087dab1c 428 reporters)
57e7f270 429 (apply default-reporter args)))
000ee07f
JB
430
431\f
57e7f270
DH
432;;;; Some useful standard reporters:
433;;;; Count reporters count the occurrence of each test result type.
434;;;; Log reporters write all test results to a given log file.
435;;;; Full reporters write all test results to the standard output.
436;;;; User reporters write interesting test results to the standard output.
437
438;;; The complete list of possible test results.
bba2d190 439(define result-tags
57e7f270
DH
440 '((pass "PASS" "passes: ")
441 (fail "FAIL" "failures: ")
442 (upass "UPASS" "unexpected passes: ")
443 (xfail "XFAIL" "expected failures: ")
444 (unresolved "UNRESOLVED" "unresolved test cases: ")
445 (untested "UNTESTED" "untested test cases: ")
446 (unsupported "UNSUPPORTED" "unsupported test cases: ")
447 (error "ERROR" "errors: ")))
448
449;;; The list of important test results.
bba2d190 450(define important-result-tags
57e7f270
DH
451 '(fail upass unresolved error))
452
453;;; Display a single test result in formatted form to the given port
454(define (print-result port result name . args)
455 (let* ((tag (assq result result-tags))
456 (label (if tag (cadr tag) #f)))
457 (if label
458 (begin
459 (display label port)
460 (display ": " port)
461 (display (format-test-name name) port)
462 (if (pair? args)
463 (begin
464 (display " - arguments: " port)
465 (write args port)))
466 (newline port))
467 (error "(test-suite lib) FULL-REPORTER: unrecognized result: "
468 result))))
000ee07f
JB
469
470;;; Return a list of the form (COUNTER RESULTS), where:
471;;; - COUNTER is a reporter procedure, and
472;;; - RESULTS is a procedure taking no arguments which returns the
473;;; results seen so far by COUNTER. The return value is an alist
474;;; mapping outcome symbols (`pass', `fail', etc.) onto counts.
475(define (make-count-reporter)
57e7f270 476 (let ((counts (map (lambda (tag) (cons (car tag) 0)) result-tags)))
000ee07f 477 (list
57e7f270
DH
478 (lambda (result name . args)
479 (let ((pair (assq result counts)))
bba2d190 480 (if pair
57e7f270 481 (set-cdr! pair (+ 1 (cdr pair)))
bba2d190 482 (error "count-reporter: unexpected test result: "
57e7f270 483 (cons result (cons name args))))))
000ee07f
JB
484 (lambda ()
485 (append counts '())))))
486
487;;; Print a count reporter's results nicely. Pass this function the value
488;;; returned by a count reporter's RESULTS procedure.
57e7f270 489(define (print-counts results . port?)
bba2d190 490 (let ((port (if (pair? port?)
57e7f270
DH
491 (car port?)
492 (current-output-port))))
493 (newline port)
494 (display-line-port port "Totals for this test run:")
495 (for-each
496 (lambda (tag)
497 (let ((result (assq (car tag) results)))
498 (if result
499 (display-line-port port (caddr tag) (cdr result))
500 (display-line-port port
501 "Test suite bug: "
502 "no total available for `" (car tag) "'"))))
503 result-tags)
504 (newline port)))
000ee07f
JB
505
506;;; Return a reporter procedure which prints all results to the file
507;;; FILE, in human-readable form. FILE may be a filename, or a port.
508(define (make-log-reporter file)
509 (let ((port (if (output-port? file) file
510 (open-output-file file))))
57e7f270
DH
511 (lambda args
512 (apply print-result port args)
000ee07f
JB
513 (force-output port))))
514
087dab1c 515;;; A reporter that reports all results to the user.
57e7f270
DH
516(define (full-reporter . args)
517 (apply print-result (current-output-port) args))
087dab1c
JB
518
519;;; A reporter procedure which shows interesting results (failures,
57e7f270
DH
520;;; unexpected passes etc.) to the user.
521(define (user-reporter result name . args)
522 (if (memq result important-result-tags)
523 (apply full-reporter result name args)))
087dab1c
JB
524
525(set! default-reporter full-reporter)