lib.scm 25 KB

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