lib.scm 23 KB

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