generalized check-exn so that you can supply a regular expression as the predicate (which matches the exception message)
This commit is contained in:
parent
12b345dc99
commit
b56019c275
|
@ -195,7 +195,10 @@
|
||||||
#t
|
#t
|
||||||
(fail-check)))))]))
|
(fail-check)))))]))
|
||||||
|
|
||||||
(define-check (check-exn pred thunk)
|
(define-check (check-exn raw-pred thunk)
|
||||||
|
(let ([pred (if (regexp? raw-pred)
|
||||||
|
(λ (x) (and (exn:fail? x) (regexp-match raw-pred (exn-message x))))
|
||||||
|
raw-pred)])
|
||||||
(let/ec succeed
|
(let/ec succeed
|
||||||
(with-handlers
|
(with-handlers
|
||||||
(;; catch the exception we are looking for and
|
(;; catch the exception we are looking for and
|
||||||
|
@ -220,7 +223,7 @@
|
||||||
(thunk))
|
(thunk))
|
||||||
(with-check-info*
|
(with-check-info*
|
||||||
(list (make-check-message "No exception raised"))
|
(list (make-check-message "No exception raised"))
|
||||||
(lambda () (fail-check)))))
|
(lambda () (fail-check))))))
|
||||||
|
|
||||||
(define-check (check-not-exn thunk)
|
(define-check (check-not-exn thunk)
|
||||||
(with-handlers
|
(with-handlers
|
||||||
|
|
|
@ -98,11 +98,14 @@ For example, the following checks all fail:
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
@defproc[(check-exn (exn-predicate (-> any (or/c #t #f))) (thunk (-> any)) (message string? ""))
|
@defproc[(check-exn (exn-predicate (or/c (-> any (or/c #t #f)) regexp?)) (thunk (-> any)) (message string? ""))
|
||||||
#t]{
|
#t]{
|
||||||
|
Checks that @racket[thunk] raises an exception and that
|
||||||
Checks that @racket[thunk] raises an exception for which
|
either @racket[exn-predicate] returns @racket[#t] if it is a function,
|
||||||
@racket[exn-predicate] returns @racket[#t]. The optional
|
or that it matches the message in the exception if @racket[exn-predicate]
|
||||||
|
is a regexp. In the latter case, the exception raised must be
|
||||||
|
an @racket[exn:fail?].
|
||||||
|
The optional
|
||||||
@racket[message] is included in the output if the check
|
@racket[message] is included in the output if the check
|
||||||
fails. A common error is to use an expression instead of a
|
fails. A common error is to use an expression instead of a
|
||||||
function of no arguments for @racket[thunk]. Remember that
|
function of no arguments for @racket[thunk]. Remember that
|
||||||
|
@ -111,14 +114,13 @@ checks are conceptually functions.}
|
||||||
Here are two example, one showing a test that succeeds, and one showing a common error:
|
Here are two example, one showing a test that succeeds, and one showing a common error:
|
||||||
|
|
||||||
@racketblock[
|
@racketblock[
|
||||||
(check-exn exn?
|
(check-exn exn:fail?
|
||||||
(lambda ()
|
(lambda ()
|
||||||
(raise (make-exn "Hi there"
|
(raise (make-exn "Hi there"
|
||||||
(current-continuation-marks)))))
|
(current-continuation-marks)))))
|
||||||
(code:comment "Forgot to wrap the expression in a thunk. Don't do this!")
|
(code:comment "Forgot to wrap the expression in a thunk. Don't do this!")
|
||||||
(check-exn exn?
|
(check-exn exn:fail?
|
||||||
(raise (make-exn "Hi there"
|
(error 'hi "there"))
|
||||||
(current-continuation-marks))))
|
|
||||||
]
|
]
|
||||||
|
|
||||||
@defproc[(check-not-exn (thunk (-> any)) (message string? "")) #t]{
|
@defproc[(check-not-exn (thunk (-> any)) (message string? "")) #t]{
|
||||||
|
|
Loading…
Reference in New Issue
Block a user