Skip to content

Commit 202d41c

Browse files
soraweesamth
authored andcommitted
Treat exceptions thrown by arguments to check as test failures (again)
This PR corrects the wrong fix in #123. It restores the functionality that #109 is meant to implement while correctly reporting test results.
1 parent 8c06d86 commit 202d41c

File tree

4 files changed

+144
-5
lines changed

4 files changed

+144
-5
lines changed

rackunit-lib/rackunit/private/check.rkt

+13-5
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@
9292

9393
(define-simple-macro (make-check-func (name:id formal:id ...) #:public-name pub:id body:expr ...)
9494
(λ (#:location [location (list 'unknown #f #f #f #f)]
95-
#:expression [expression 'unknown])
95+
#:expression [expression 'unknown]
96+
#:suppress-check? [suppress-check? #f])
9697
(procedure-rename
9798
(λ (formal ... [message #f])
9899
(define infos
@@ -102,7 +103,11 @@
102103
(make-check-params (list formal ...))
103104
(and message (make-check-message message))))
104105
(with-default-check-info* infos
105-
(λ () ((current-check-around) (λ () body ... (void))))))
106+
(λ ()
107+
(define (the-body-thunk) body ... (void))
108+
(cond
109+
[suppress-check? (the-body-thunk)]
110+
[else ((current-check-around) the-body-thunk)]))))
106111
'pub)))
107112

108113

@@ -120,9 +125,12 @@
120125
(make-check-expression '(chk . args)))
121126
#,(syntax/loc #'loc
122127
(λ ()
123-
((check-impl #:location location
124-
#:expression '(chk . args))
125-
. args)))))]
128+
((current-check-around)
129+
(λ ()
130+
((check-impl #:location location
131+
#:expression '(chk . args)
132+
#:suppress-check? #t)
133+
. args)))))))]
126134
[chk:id
127135
#'(check-impl #:location (syntax->location #'loc)
128136
#:expression 'chk)])))))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#lang racket/base
2+
3+
(require rackunit
4+
rackunit/text-ui
5+
racket/port
6+
tests/eli-tester)
7+
8+
(define output
9+
(with-output-to-string
10+
(lambda ()
11+
(parameterize ([current-error-port (current-output-port)])
12+
(run-tests (test-suite "tests"
13+
(check-equal? 1 1)
14+
(check-equal? (1) 1)
15+
(check-equal? 1 2)
16+
(check-equal? 1 1)))
17+
(run-tests (test-suite "tests2"
18+
(check-equal? 5 5)
19+
(check-equal? 4 4)
20+
(check-equal? 3 3)
21+
(check-equal? 2 2)
22+
(check-equal? 1 1)))))))
23+
24+
(test
25+
(regexp-match
26+
(regexp (regexp-quote "2 success(es) 1 failure(s) 1 error(s) 4 test(s) run\n"))
27+
output))
28+
29+
(test
30+
(regexp-match
31+
(regexp (regexp-quote "5 success(es) 0 failure(s) 0 error(s) 5 test(s) run\n"))
32+
output))
33+
34+
(test
35+
(with-handlers ([exn:fail? (λ (e)
36+
(regexp-match
37+
(regexp (regexp-quote "given: 3"))
38+
(exn-message e)))])
39+
(define my-check-equal? check-equal?)
40+
(run-tests (test-suite "tests"
41+
(my-check-equal? 1 1)
42+
(my-check-equal? (3) 1)
43+
(my-check-equal? 1 2)))
44+
#f))
45+
46+
(module test racket/base
47+
(require syntax/location)
48+
;; Use a separate namespace to avoid logging results
49+
;; in this namespace (where `raco test` would see errors).
50+
(parameterize ([current-namespace (make-base-namespace)])
51+
(dynamic-require (quote-module-path "..") #f)))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
;;;
2+
;;; Time-stamp: <2008-06-06 15:32:49 noel>
3+
;;;
4+
;;; Copyright (C) by Noel Welsh.
5+
;;;
6+
7+
;;; This library is free software; you can redistribute it
8+
;;; and/or modify it under the terms of the GNU Lesser
9+
;;; General Public License as published by the Free Software
10+
;;; Foundation; either version 2.1 of the License, or (at
11+
;;; your option) any later version.
12+
13+
;;; This library is distributed in the hope that it will be
14+
;;; useful, but WITHOUT ANY WARRANTY; without even the
15+
;;; implied warranty of MERCHANTABILITY or FITNESS FOR A
16+
;;; PARTICULAR PURPOSE. See the GNU Lesser General Public
17+
;;; License for more details.
18+
19+
;;; You should have received a copy of the GNU Lesser
20+
;;; General Public License along with this library; if not,
21+
;;; write to the Free Software Foundation, Inc., 59 Temple
22+
;;; Place, Suite 330, Boston, MA 02111-1307 USA
23+
24+
;;; Author: Noel Welsh <[email protected]>
25+
26+
27+
;; Here we check the standalone (not within a test-case or
28+
;; test-suite) semantics of checks. These tests are not
29+
;; part of the standard test suite and must be run
30+
;; separately.
31+
32+
#lang racket/base
33+
34+
(require rackunit/private/check)
35+
36+
;; Don't run this test automatically:
37+
(module test racket/base
38+
(displayln "run as program for tests"))
39+
40+
(define my-check check)
41+
42+
;; This check should succeed
43+
(my-check = 1 1 0.0)
44+
45+
;; This check should display an error including the message "Outta here!"
46+
((values check-pred) (procedure-rename (lambda (x) (error "Outta here!")) 'proc) 'foo)
47+
48+
49+
;; This check should display a failure
50+
(my-check = 1 2 0.0)
51+
52+
;; This check should display "Oh HAI!"
53+
(parameterize
54+
([current-check-handler (lambda (e) (display "Oh HAI!\n"))])
55+
(my-check = 1 2 0.0))
56+
57+
;; This check should display "I didn't run"
58+
(parameterize
59+
([current-check-around (lambda (t) (display "I didn't run\n"))])
60+
(my-check = 1 1 0.0))

rackunit-test/tests/rackunit/standalone.rkt

+20
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,26 @@ message: 0.0
5656
--------------------
5757
")
5858

59+
(test-file "standalone-check-higher-order-test.rkt"
60+
#"Oh HAI!\nI didn't run\n"
61+
#"\
62+
--------------------
63+
ERROR
64+
name: check-pred
65+
location: standalone-check-higher-order-test.rkt:46:9
66+
params: '(#<procedure:proc> foo)
67+
68+
Outta here!
69+
--------------------
70+
--------------------
71+
FAILURE
72+
name: check
73+
location: standalone-check-higher-order-test.rkt:40:17
74+
params: '(#<procedure:=> 1 2)
75+
message: 0.0
76+
--------------------
77+
")
78+
5979
(test-file "standalone-test-case-test.rkt"
6080
#""
6181
#"\

0 commit comments

Comments
 (0)