1e78c71cfdc5863c46e1369de2f023468c5cce08
[bpt/guile.git] / test-suite / lib.scm
1 ;;;; test-suite/lib.scm --- generic support for testing
2 ;;;; Copyright (C) 1999, 2000, 2001, 2004, 2006, 2007, 2009, 2010 Free Software Foundation, Inc.
3 ;;;;
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.
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 Lesser General Public License for more details.
13 ;;;;
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
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 exception:struct-set!-denied
32 exception:system-error
33 exception:encoding-error
34 exception:miscellaneous-error
35 exception:string-contains-nul
36 exception:read-error
37
38 ;; Reporting passes and failures.
39 run-test
40 pass-if expect-fail
41 pass-if-exception expect-fail-exception
42
43 ;; Naming groups of tests in a regular fashion.
44 with-test-prefix with-test-prefix* current-test-prefix
45 format-test-name
46
47 ;; Using the debugging evaluator.
48 with-debugging-evaluator with-debugging-evaluator*
49
50 ;; Using a given locale
51 with-locale with-locale*
52
53 ;; Reporting results in various ways.
54 register-reporter unregister-reporter reporter-registered?
55 make-count-reporter print-counts
56 make-log-reporter
57 full-reporter
58 user-reporter))
59
60
61 ;;;; If you're using Emacs's Scheme mode:
62 ;;;; (put 'with-test-prefix 'scheme-indent-function 1)
63
64 \f
65 ;;;; CORE FUNCTIONS
66 ;;;;
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:
75 ;;;;
76 ;;;; (run-test "integer addition" #t (lambda () (= 2 (+ 1 1))))
77 ;;;;
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
88 ;;;; errors.
89 ;;;;
90 ;;;;
91 ;;;; Convenience macros for tests expected to pass or fail
92 ;;;;
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))
97 ;;;;
98 ;;;; For example:
99 ;;;;
100 ;;;; (pass-if "integer addition" (= 2 (+ 1 1)))
101 ;;;;
102 ;;;;
103 ;;;; Convenience macros to test for exceptions
104 ;;;;
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".
110 ;;;;
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
114 ;;;; error.
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.
119
120 \f
121 ;;;; TEST NAMES
122 ;;;;
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.
126 ;;;;
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")
130 ;;;;
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.
136 ;;;;
137 ;;;; For example:
138 ;;;;
139 ;;;; (pass-if "simple addition" (= 4 (+ 2 2)))
140 ;;;;
141 ;;;; In that case, the test name is the list ("simple addition").
142 ;;;;
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.
147 ;;;;
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))
152 ;;;;
153 ;;;; For example:
154 ;;;;
155 ;;;; (pass-if (= 2 (+ 1 1)))
156 ;;;;
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:
160 ;;;;
161 ;;;; (begin
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)))
166 ;;;;
167 ;;;; In that example, the three test names are:
168 ;;;; ("basic arithmetic" "addition"),
169 ;;;; ("basic arithmetic" "subtraction"), and
170 ;;;; ("multiplication").
171 ;;;;
172 ;;;; WITH-TEST-PREFIX can be nested. Each WITH-TEST-PREFIX postpends
173 ;;;; a new element to the current prefix:
174 ;;;;
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))))
182 ;;;;
183 ;;;; The four test names here are:
184 ;;;; ("arithmetic" "addition" "integer")
185 ;;;; ("arithmetic" "addition" "complex")
186 ;;;; ("arithmetic" "subtraction" "integer")
187 ;;;; ("arithmetic" "subtraction" "complex")
188 ;;;;
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
191 ;;;; reported as:
192 ;;;;
193 ;;;; arithmetic: addition: integer
194 ;;;; arithmetic: addition: complex
195 ;;;; arithmetic: subtraction: integer
196 ;;;; arithmetic: subtraction: complex
197 ;;;;
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
201 ;;;; own namespace.
202
203 \f
204 ;;;; REPORTERS
205 ;;;;
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.
209 ;;;;
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:
213 ;;;;
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.
236 ;;;;
237 ;;;; This library provides some standard reporters for logging results
238 ;;;; to a file, reporting interesting results to the user, and
239 ;;;; collecting totals.
240 ;;;;
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.
245
246 \f
247 ;;;; MISCELLANEOUS
248 ;;;;
249
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 "^.*$"))
277
278 ;; as per throw in scm_to_locale_stringn()
279 (define exception:string-contains-nul
280 (cons 'misc-error "^string contains #\\\\nul character"))
281
282
283 ;;; Display all parameters to the default output port, followed by a newline.
284 (define (display-line . objs)
285 (for-each display objs)
286 (newline))
287
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)
291 (newline port))
292
293 \f
294 ;;;; CORE FUNCTIONS
295 ;;;;
296
297 ;;; The central testing routine.
298 ;;; The idea is taken from Greg, the GNUstep regression test environment.
299 (define run-test #f)
300 (let ((test-running #f))
301 (define (local-run-test name expect-pass thunk)
302 (if test-running
303 (error "Nested calls to run-test are not permitted.")
304 (let ((test-name (full-name name)))
305 (set! test-running #t)
306 (catch #t
307 (lambda ()
308 (let ((result (thunk)))
309 (if (eq? result #t) (throw 'pass))
310 (if (eq? result #f) (throw 'fail))
311 (throw 'unresolved)))
312 (lambda (key . args)
313 (case key
314 ((pass)
315 (report (if expect-pass 'pass 'upass) test-name))
316 ((fail)
317 (report (if expect-pass 'fail 'xfail) test-name))
318 ((unresolved untested unsupported)
319 (report key test-name))
320 ((quit)
321 (report 'unresolved test-name)
322 (quit))
323 (else
324 (report 'error test-name (cons key args))))))
325 (set! test-running #f))))
326 (set! run-test local-run-test))
327
328 ;;; A short form for tests that are expected to pass, taken from Greg.
329 (define-syntax pass-if
330 (syntax-rules ()
331 ((_ name)
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)))
335 ((_ name rest ...)
336 (run-test name #t (lambda () rest ...)))))
337
338 ;;; A short form for tests that are expected to fail, taken from Greg.
339 (define-syntax expect-fail
340 (syntax-rules ()
341 ((_ name)
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)))
345 ((_ name rest ...)
346 (run-test name #f (lambda () rest ...)))))
347
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
351 (lambda ()
352 (stack-catch (car exception)
353 (lambda () (thunk) #f)
354 (lambda (key proc message . rest)
355 (cond
356 ;; handle explicit key
357 ((string-match (cdr exception) message)
358 #t)
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))
362 (list? rest)
363 (string-match (cdr exception)
364 (apply simple-format #f message (car rest))))
365 #t)
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))
369 (list? rest)
370 (string-match (cdr exception)
371 (apply simple-format #f message (car rest))))
372 #t)
373 ;; unhandled; throw again
374 (else
375 (apply throw key proc message rest))))))))
376
377 ;;; A short form for tests that expect a certain exception to be thrown.
378 (define-syntax pass-if-exception
379 (syntax-rules ()
380 ((_ name exception body rest ...)
381 (run-test-exception name exception #t (lambda () body rest ...)))))
382
383 ;;; A short form for tests expected to fail to throw a certain exception.
384 (define-syntax expect-fail-exception
385 (syntax-rules ()
386 ((_ name exception body rest ...)
387 (run-test-exception name exception #f (lambda () body rest ...)))))
388
389 \f
390 ;;;; TEST NAMES
391 ;;;;
392
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
399 (lambda (port)
400 (let loop ((name name)
401 (separator ""))
402 (if (pair? name)
403 (begin
404 (display separator port)
405 (display (car name) port)
406 (loop (cdr name) ": "))))))))
407
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)))
411
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))
417
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))))
424 (thunk)))
425
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
430 ;;; BODY expression.
431 (defmacro with-test-prefix (prefix . body)
432 `(with-test-prefix* ,prefix (lambda () ,@body)))
433
434 ;;; Call THUNK using the debugging evaluator.
435 (define (with-debugging-evaluator* thunk)
436 (let ((dopts #f))
437 (dynamic-wind
438 (lambda ()
439 (set! dopts (debug-options))
440 (debug-enable 'debug))
441 thunk
442 (lambda ()
443 (debug-options dopts)))))
444
445 ;;; Evaluate BODY... using the debugging evaluator.
446 (define-macro (with-debugging-evaluator . body)
447 `(with-debugging-evaluator* (lambda () ,@body)))
448
449 ;;; Call THUNK with a given locale
450 (define (with-locale* nloc thunk)
451 (let ((loc #f))
452 (dynamic-wind
453 (lambda ()
454 (if (defined? 'setlocale)
455 (begin
456 (set! loc
457 (false-if-exception (setlocale LC_ALL nloc)))
458 (if (not loc)
459 (throw 'unresolved)))
460 (throw 'unresolved)))
461 thunk
462 (lambda ()
463 (if (defined? 'setlocale)
464 (setlocale LC_ALL loc))))))
465
466 ;;; Evaluate BODY... using the given locale.
467 (define-macro (with-locale loc . body)
468 `(with-locale* ,loc (lambda () ,@body)))
469
470 \f
471 ;;;; REPORTERS
472 ;;;;
473
474 ;;; The global list of reporters.
475 (define reporters '())
476
477 ;;; The default reporter, to be used only if no others exist.
478 (define default-reporter #f)
479
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)))
486
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)))
493
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))
497
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))
502 reporters)
503 (apply default-reporter args)))
504
505 \f
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.
511
512 ;;; The complete list of possible test results.
513 (define result-tags
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: ")))
522
523 ;;; The list of important test results.
524 (define important-result-tags
525 '(fail upass unresolved error))
526
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)))
531 (if label
532 (begin
533 (display label port)
534 (display ": " port)
535 (display (format-test-name name) port)
536 (if (pair? args)
537 (begin
538 (display " - arguments: " port)
539 (write args port)))
540 (newline port))
541 (error "(test-suite lib) FULL-REPORTER: unrecognized result: "
542 result))))
543
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)))
551 (list
552 (lambda (result name . args)
553 (let ((pair (assq result counts)))
554 (if pair
555 (set-cdr! pair (+ 1 (cdr pair)))
556 (error "count-reporter: unexpected test result: "
557 (cons result (cons name args))))))
558 (lambda ()
559 (append counts '())))))
560
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?)
565 (car port?)
566 (current-output-port))))
567 (newline port)
568 (display-line-port port "Totals for this test run:")
569 (for-each
570 (lambda (tag)
571 (let ((result (assq (car tag) results)))
572 (if result
573 (display-line-port port (caddr tag) (cdr result))
574 (display-line-port port
575 "Test suite bug: "
576 "no total available for `" (car tag) "'"))))
577 result-tags)
578 (newline port)))
579
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))))
585 (lambda args
586 (apply print-result port args)
587 (force-output port))))
588
589 ;;; A reporter that reports all results to the user.
590 (define (full-reporter . args)
591 (apply print-result (current-output-port) args))
592
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)))
598
599 (set! default-reporter full-reporter)