/collects/rackunit/text-ui.rkt

http://github.com/gmarceau/PLT · Racket · 246 lines · 193 code · 21 blank · 32 comment · 19 complexity · 2266af6413ada874a5db2a7068b580a4 MD5 · raw file

  1. ;;;
  2. ;;; Time-stamp: <2009-06-11 17:11:22 noel>
  3. ;;;
  4. ;;; Copyright (C) 2005 by Noel Welsh.
  5. ;;;
  6. ;;; This library is free software; you can redistribute it
  7. ;;; and/or modify it under the terms of the GNU Lesser
  8. ;;; General Public License as published by the Free Software
  9. ;;; Foundation; either version 2.1 of the License, or (at
  10. ;;; your option) any later version.
  11. ;;; This library is distributed in the hope that it will be
  12. ;;; useful, but WITHOUT ANY WARRANTY; without even the
  13. ;;; implied warranty of MERCHANTABILITY or FITNESS FOR A
  14. ;;; PARTICULAR PURPOSE. See the GNU Lesser General Public
  15. ;;; License for more details.
  16. ;;; You should have received a copy of the GNU Lesser
  17. ;;; General Public License along with this library; if not,
  18. ;;; write to the Free Software Foundation, Inc., 59 Temple
  19. ;;; Place, Suite 330, Boston, MA 02111-1307 USA
  20. ;;; Author: Noel Welsh <noelwelsh@yahoo.com>
  21. ;;
  22. ;;
  23. ;; Commentary:
  24. #lang racket/base
  25. (require racket/match
  26. racket/pretty
  27. srfi/13
  28. srfi/26
  29. "main.rkt"
  30. "private/base.rkt"
  31. "private/counter.rkt"
  32. "private/format.rkt"
  33. "private/location.rkt"
  34. "private/result.rkt"
  35. "private/check-info.rkt"
  36. "private/monad.rkt"
  37. "private/hash-monad.rkt"
  38. "private/name-collector.rkt"
  39. "private/text-ui-util.rkt")
  40. (provide run-tests
  41. display-context
  42. display-exn
  43. display-summary+return
  44. display-ticker
  45. display-result)
  46. ;; display-ticker : test-result -> void
  47. ;;
  48. ;; Prints a summary of the test result
  49. (define (display-ticker result)
  50. (cond
  51. ((test-error? result)
  52. (display "!"))
  53. ((test-failure? result)
  54. (display "-"))
  55. (else
  56. (display "."))))
  57. ;; display-test-preamble : test-result -> (hash-monad-of void)
  58. (define (display-test-preamble result)
  59. (lambda (hash)
  60. (if (test-success? result)
  61. hash
  62. (begin
  63. (display-delimiter)
  64. hash))))
  65. ;; display-test-postamble : test-result -> (hash-monad-of void)
  66. (define (display-test-postamble result)
  67. (lambda (hash)
  68. (if (test-success? result)
  69. hash
  70. (begin
  71. (display-delimiter)
  72. hash))))
  73. ;; display-result : test-result -> void
  74. (define (display-result result)
  75. (cond
  76. ((test-error? result)
  77. (display-test-name (test-result-test-case-name result))
  78. (display-error)
  79. (newline))
  80. ((test-failure? result)
  81. (display-test-name (test-result-test-case-name result))
  82. (display-failure)
  83. (newline))
  84. (else
  85. (void))))
  86. ;; display-context : test-result [(U #t #f)] -> void
  87. (define (display-context result [verbose? #f])
  88. (cond
  89. [(test-failure? result)
  90. (let* ([exn (test-failure-result result)]
  91. [stack (exn:test:check-stack exn)])
  92. (textui-display-check-info-stack stack verbose?)
  93. (when #t
  94. ((error-display-handler) (exn-message exn) exn)))]
  95. [(test-error? result)
  96. (let ([exn (test-error-result result)])
  97. (when (exn? exn)
  98. (textui-display-check-info-stack (check-info-stack (exn-continuation-marks exn))))
  99. (display-exn exn))]
  100. [else (void)]))
  101. (define (textui-display-check-info-stack stack [verbose? #f])
  102. (for-each
  103. (lambda (info)
  104. (cond
  105. [(check-name? info)
  106. (display-check-info info)]
  107. [(check-location? info)
  108. (display-check-info-name-value
  109. 'location
  110. (trim-current-directory
  111. (location->string
  112. (check-info-value info)))
  113. display)]
  114. [(check-params? info)
  115. (display-check-info-name-value
  116. 'params
  117. (check-info-value info)
  118. (lambda (v) (map pretty-print v)))]
  119. [(check-actual? info)
  120. (display-check-info-name-value
  121. 'actual
  122. (check-info-value info)
  123. pretty-print)]
  124. [(check-expected? info)
  125. (display-check-info-name-value
  126. 'expected
  127. (check-info-value info)
  128. pretty-print)]
  129. [(and (check-expression? info)
  130. (not verbose?))
  131. (void)]
  132. [else
  133. (display-check-info info)]))
  134. (if verbose?
  135. stack
  136. (strip-redundant-params stack))))
  137. ;; display-verbose-check-info : test-result -> void
  138. (define (display-verbose-check-info result)
  139. (cond
  140. ((test-failure? result)
  141. (let* ((exn (test-failure-result result))
  142. (stack (exn:test:check-stack exn)))
  143. (for-each
  144. (lambda (info)
  145. (cond
  146. ((check-location? info)
  147. (display "location: ")
  148. (display (trim-current-directory
  149. (location->string
  150. (check-info-value info)))))
  151. (else
  152. (display (check-info-name info))
  153. (display ": ")
  154. (write (check-info-value info))))
  155. (newline))
  156. stack)))
  157. ((test-error? result)
  158. (display-exn (test-error-result result)))
  159. (else
  160. (void))))
  161. (define (std-test/text-ui display-context test)
  162. (parameterize ([current-output-port (current-error-port)])
  163. (fold-test-results
  164. (lambda (result seed)
  165. ((sequence* (update-counter! result)
  166. (display-test-preamble result)
  167. (display-test-case-name result)
  168. (lambda (hash)
  169. (display-result result)
  170. (display-context result)
  171. hash)
  172. (display-test-postamble result))
  173. seed))
  174. ((sequence
  175. (put-initial-counter)
  176. (put-initial-name))
  177. (make-empty-hash))
  178. test
  179. #:fdown (lambda (name seed) ((push-suite-name! name) seed))
  180. #:fup (lambda (name kid-seed) ((pop-suite-name!) kid-seed)))))
  181. (define (display-summary+return monad)
  182. (monad-value
  183. ((compose
  184. (sequence*
  185. (display-counter*)
  186. (counter->vector))
  187. (match-lambda
  188. ((vector s f e)
  189. (return-hash (+ f e)))))
  190. monad)))
  191. (define (display-counter*)
  192. (compose (counter->vector)
  193. (match-lambda
  194. [(vector s f e)
  195. (if (and (zero? f) (zero? e))
  196. (display-counter)
  197. (lambda args
  198. (parameterize ([current-output-port (current-error-port)])
  199. (apply (display-counter) args))))])))
  200. ;; run-tests : test [(U 'quiet 'normal 'verbose)] -> integer
  201. (define (run-tests test [mode 'normal])
  202. (monad-value
  203. ((compose
  204. (sequence*
  205. (case mode
  206. [(normal verbose)
  207. (display-counter*)]
  208. [(quiet)
  209. (lambda (a) a)])
  210. (counter->vector))
  211. (match-lambda
  212. ((vector s f e)
  213. (return-hash (+ f e)))))
  214. (case mode
  215. ((quiet)
  216. (fold-test-results
  217. (lambda (result seed)
  218. ((update-counter! result) seed))
  219. ((put-initial-counter)
  220. (make-empty-hash))
  221. test))
  222. ((normal) (std-test/text-ui display-context test))
  223. ((verbose) (std-test/text-ui
  224. (cut display-context <> #t)
  225. test))))))