The FSF has a new address.
[bpt/guile.git] / test-suite / lib.scm
1 ;;;; test-suite/lib.scm --- generic support for testing
2 ;;;; Copyright (C) 1999, 2000, 2001, 2004 Free Software Foundation, Inc.
3 ;;;;
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.
8 ;;;;
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.
13 ;;;;
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
16 ;;;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 ;;;; Boston, MA 02110-1301 USA
18
19 (define-module (test-suite lib)
20 :use-module (ice-9 stack-catch)
21 :use-module (ice-9 regex)
22 :export (
23
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
32 ;; Reporting passes and failures.
33 run-test
34 pass-if expect-fail
35 pass-if-exception expect-fail-exception
36
37 ;; Naming groups of tests in a regular fashion.
38 with-test-prefix with-test-prefix* current-test-prefix
39 format-test-name
40
41 ;; Reporting results in various ways.
42 register-reporter unregister-reporter reporter-registered?
43 make-count-reporter print-counts
44 make-log-reporter
45 full-reporter
46 user-reporter))
47
48
49 ;;;; If you're using Emacs's Scheme mode:
50 ;;;; (put 'with-test-prefix 'scheme-indent-function 1)
51
52 \f
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 ;;;;
78 ;;;;
79 ;;;; Convenience macros for tests expected to pass or fail
80 ;;;;
81 ;;;; * (pass-if name body) is a short form for
82 ;;;; (run-test name #t (lambda () body))
83 ;;;; * (expect-fail name body) is a short form for
84 ;;;; (run-test name #f (lambda () body))
85 ;;;;
86 ;;;; For example:
87 ;;;;
88 ;;;; (pass-if "integer addition" (= 2 (+ 1 1)))
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.
107
108 \f
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.
124 ;;;;
125 ;;;; For example:
126 ;;;;
127 ;;;; (pass-if "simple addition" (= 4 (+ 2 2)))
128 ;;;;
129 ;;;; In that case, the test name is the list ("simple addition").
130 ;;;;
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 ;;;;
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:
148 ;;;;
149 ;;;; (begin
150 ;;;; (with-test-prefix "basic arithmetic"
151 ;;;; (pass-if "addition" (= (+ 2 2) 4))
152 ;;;; (pass-if "subtraction" (= (- 4 2) 2)))
153 ;;;; (pass-if "multiplication" (= (* 2 2) 4)))
154 ;;;;
155 ;;;; In that example, the three test names are:
156 ;;;; ("basic arithmetic" "addition"),
157 ;;;; ("basic arithmetic" "subtraction"), and
158 ;;;; ("multiplication").
159 ;;;;
160 ;;;; WITH-TEST-PREFIX can be nested. Each WITH-TEST-PREFIX postpends
161 ;;;; a new element to the current prefix:
162 ;;;;
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))))
170 ;;;;
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:
180 ;;;;
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
193 ;;;;
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.
197 ;;;;
198 ;;;; A reporter function takes two mandatory arguments, RESULT and TEST, and
199 ;;;; possibly additional arguments depending on RESULT; its return value
200 ;;;; is ignored. RESULT has one of the following forms:
201 ;;;;
202 ;;;; pass - The test named TEST passed.
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
215 ;;;; example because the test case is not complete yet.
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.
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.
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.
233
234 \f
235 ;;;; MISCELLANEOUS
236 ;;;;
237
238 ;;; Define some exceptions which are commonly being tested for.
239 (define exception:bad-variable
240 (cons 'syntax-error "Bad variable"))
241 (define exception:missing-expression
242 (cons 'misc-error "^missing or extra expression"))
243 (define exception:out-of-range
244 (cons 'out-of-range "^.*out of range"))
245 (define exception:unbound-var
246 (cons 'unbound-variable "^Unbound variable"))
247 (define exception:used-before-defined
248 (cons 'unbound-variable "^Variable used before given a value"))
249 (define exception:wrong-num-args
250 (cons 'wrong-number-of-args "^Wrong number of arguments"))
251 (define exception:wrong-type-arg
252 (cons 'wrong-type-arg "^Wrong type"))
253 (define exception:numerical-overflow
254 (cons 'numerical-overflow "^Numerical overflow"))
255
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
287 ((pass)
288 (report (if expect-pass 'pass 'upass) test-name))
289 ((fail)
290 (report (if expect-pass 'fail 'xfail) test-name))
291 ((unresolved untested unsupported)
292 (report key test-name))
293 ((quit)
294 (report 'unresolved test-name)
295 (quit))
296 (else
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.
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.
306 `(run-test ',name #t (lambda () ,name))
307 `(run-test ,name #t (lambda () ,@rest))))
308
309 ;;; A short form for tests that are expected to fail, taken from Greg.
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.
314 `(run-test ',name #f (lambda () ,name))
315 `(run-test ,name #f (lambda () ,@rest))))
316
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 ()
321 (stack-catch (car exception)
322 (lambda () (thunk) #f)
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)
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)
342 ;; unhandled; throw again
343 (else
344 (apply throw key proc message rest))))))))
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
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)))
373
374 ;;; A fluid containing the current test prefix, as a list.
375 (define prefix-fluid (make-fluid))
376 (fluid-set! prefix-fluid '())
377 (define (current-test-prefix)
378 (fluid-ref prefix-fluid))
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
396 \f
397 ;;;; REPORTERS
398 ;;;;
399
400 ;;; The global list of reporters.
401 (define reporters '())
402
403 ;;; The default reporter, to be used only if no others exist.
404 (define default-reporter #f)
405
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
424 ;;; Send RESULT to all currently registered reporter functions.
425 (define (report . args)
426 (if (pair? reporters)
427 (for-each (lambda (reporter) (apply reporter args))
428 reporters)
429 (apply default-reporter args)))
430
431 \f
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.
439 (define result-tags
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.
450 (define important-result-tags
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))))
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)
476 (let ((counts (map (lambda (tag) (cons (car tag) 0)) result-tags)))
477 (list
478 (lambda (result name . args)
479 (let ((pair (assq result counts)))
480 (if pair
481 (set-cdr! pair (+ 1 (cdr pair)))
482 (error "count-reporter: unexpected test result: "
483 (cons result (cons name args))))))
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.
489 (define (print-counts results . port?)
490 (let ((port (if (pair? port?)
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)))
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))))
511 (lambda args
512 (apply print-result port args)
513 (force-output port))))
514
515 ;;; A reporter that reports all results to the user.
516 (define (full-reporter . args)
517 (apply print-result (current-output-port) args))
518
519 ;;; A reporter procedure which shows interesting results (failures,
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)))
524
525 (set! default-reporter full-reporter)