/collects/tests/rackunit/text-ui-test.rkt

http://github.com/gmarceau/PLT · Racket · 224 lines · 171 code · 29 blank · 24 comment · 0 complexity · 4e8ff1538c917d174c03063342e7a233 MD5 · raw file

  1. ;;;
  2. ;;; Time-stamp: <2010-03-29 13:56:54 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/runtime-path
  26. racket/pretty
  27. racket/port
  28. srfi/1
  29. srfi/13
  30. rackunit
  31. rackunit/text-ui)
  32. (provide text-ui-tests)
  33. (define-syntax-rule (with-all-output-to-string e ...)
  34. (with-all-output-to-string* (lambda () e ...)))
  35. (define (with-all-output-to-string* thnk)
  36. (with-output-to-string
  37. (lambda ()
  38. (parameterize ([current-error-port (current-output-port)])
  39. (thnk)))))
  40. (define-runtime-path here ".")
  41. ;; with-silent-output (() -> any) -> any
  42. (define (with-silent-output thunk)
  43. (parameterize ([current-output-port (open-output-nowhere)]
  44. [current-error-port (open-output-nowhere)])
  45. (thunk)))
  46. (define (failing-test)
  47. (run-tests
  48. (test-suite
  49. "Dummy"
  50. (test-case "Dummy" (check-equal? 1 2)))))
  51. (define (failing-binary-test/complex-params)
  52. (run-tests
  53. (test-suite
  54. "Dummy"
  55. (test-case "Dummy"
  56. (check-equal?
  57. (list (iota 15) (iota 15) (iota 15))
  58. 1)))))
  59. (define (failing-test/complex-params)
  60. (run-tests
  61. (test-suite
  62. "Dummy"
  63. (test-case "Dummy"
  64. (check-false
  65. (list (iota 15) (iota 15) (iota 15)))))))
  66. (define (quiet-failing-test)
  67. (run-tests
  68. (test-suite
  69. "Dummy"
  70. (test-case "Dummy" (check-equal? 1 2)))
  71. 'quiet))
  72. (define (quiet-error-test)
  73. (run-tests
  74. (test-suite
  75. "Dummy"
  76. (test-case "Dummy" (error "kabloom!")))
  77. 'quiet))
  78. (define text-ui-tests
  79. (test-suite
  80. "All tests for text-ui"
  81. (test-case
  82. "Binary check displays actual and expected in failure error message"
  83. (let ((op (with-all-output-to-string (failing-test))))
  84. (check string-contains
  85. op
  86. "expected")
  87. (check string-contains
  88. op
  89. "actual")))
  90. (test-case
  91. "Binary check doesn't display params"
  92. (let ((op (with-all-output-to-string (failing-test))))
  93. (check (lambda (out str) (not (string-contains out str)))
  94. op
  95. "params")))
  96. (test-case
  97. "Binary check output is pretty printed"
  98. (let ([op (parameterize ([pretty-print-columns 80])
  99. (with-all-output-to-string (failing-binary-test/complex-params)))])
  100. (check string-contains
  101. op
  102. "'((0 1 2 3 4 5 6 7 8 9 10 11 12 13 14)
  103. (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14)
  104. (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14))")))
  105. (test-case
  106. "Non-binary check output is pretty printed"
  107. (let ([op (parameterize ([pretty-print-columns 80])
  108. (with-all-output-to-string (failing-test/complex-params)))])
  109. (check string-contains
  110. op
  111. "'((0 1 2 3 4 5 6 7 8 9 10 11 12 13 14)
  112. (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14)
  113. (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14))")))
  114. (test-case
  115. "Location trimmed when file is under current directory"
  116. (parameterize ((current-directory here))
  117. (let ((op (with-all-output-to-string (failing-test))))
  118. (check string-contains
  119. op
  120. "location: text-ui-test.rkt"))))
  121. (test-case
  122. "Name and location displayed before actual/expected"
  123. (let ((op (with-all-output-to-string (failing-test))))
  124. (let ((name-idx (string-contains op "name:"))
  125. (loc-idx (string-contains op "location:"))
  126. (actual-idx (string-contains op "actual:"))
  127. (expected-idx (string-contains op "expected:")))
  128. (check < name-idx loc-idx)
  129. (check < loc-idx actual-idx)
  130. (check < actual-idx expected-idx))))
  131. (test-case
  132. "Quiet mode is quiet"
  133. (let ((op1 (with-all-output-to-string (quiet-failing-test)))
  134. (op2 (with-all-output-to-string (quiet-error-test))))
  135. (check string=? op1 "")
  136. (check string=? op2 "")))
  137. (test-case
  138. "Number of unsuccessful tests returned"
  139. (check-equal? (with-silent-output failing-test) 1)
  140. (check-equal? (with-silent-output quiet-failing-test) 1)
  141. (check-equal? (with-silent-output quiet-error-test) 1)
  142. (check-equal? (with-silent-output
  143. (lambda ()
  144. (run-tests
  145. (test-suite
  146. "Dummy"
  147. (test-case "Dummy" (check-equal? 1 1)))
  148. 'quiet)))
  149. 0))
  150. (test-case
  151. "run-tests runs suite before/after actions in quiet mode"
  152. (with-silent-output
  153. (λ ()
  154. (let ([foo 1])
  155. (run-tests
  156. (test-suite
  157. "Foo"
  158. #:before (lambda () (set! foo 2))
  159. #:after (lambda () (set! foo 3))
  160. (test-case
  161. "Foo check"
  162. (check = foo 2)))
  163. 'quiet)
  164. (check = foo 3)))))
  165. (test-case
  166. "run-tests runs suite before/after actions in normal mode"
  167. (with-silent-output
  168. (λ ()
  169. (let ([foo 1])
  170. (run-tests
  171. (test-suite
  172. "Foo"
  173. #:before (lambda () (set! foo 2))
  174. #:after (lambda () (set! foo 3))
  175. (test-case
  176. "Foo check"
  177. (check = foo 2)))
  178. 'normal)
  179. (check = foo 3)))))
  180. (test-case
  181. "run-tests runs suite before/after actions in verbose mode"
  182. (with-silent-output
  183. (λ ()
  184. (let ([foo 1])
  185. (run-tests
  186. (test-suite
  187. "Foo"
  188. #:before (lambda () (set! foo 2))
  189. #:after (lambda () (set! foo 3))
  190. (test-case
  191. "Foo check"
  192. (check = foo 2)))
  193. 'verbose)
  194. (check = foo 3)))))
  195. ))