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,32 +195,35 @@
|
|||
#t
|
||||
(fail-check)))))]))
|
||||
|
||||
(define-check (check-exn pred thunk)
|
||||
(let/ec succeed
|
||||
(with-handlers
|
||||
(;; catch the exception we are looking for and
|
||||
;; succeed
|
||||
[pred
|
||||
(lambda (exn) (succeed #t))]
|
||||
;; rethrow check failures if we aren't looking
|
||||
;; for them
|
||||
[exn:test:check?
|
||||
(lambda (exn)
|
||||
(refail-check exn))]
|
||||
;; catch any other exception and raise an check
|
||||
;; failure
|
||||
[exn:fail?
|
||||
(lambda (exn)
|
||||
(with-check-info*
|
||||
(list
|
||||
(make-check-message "Wrong exception raised")
|
||||
(make-check-info 'exn-message (exn-message exn))
|
||||
(make-check-info 'exn exn))
|
||||
(lambda () (fail-check))))])
|
||||
(thunk))
|
||||
(with-check-info*
|
||||
(list (make-check-message "No exception raised"))
|
||||
(lambda () (fail-check)))))
|
||||
(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
|
||||
(with-handlers
|
||||
(;; catch the exception we are looking for and
|
||||
;; succeed
|
||||
[pred
|
||||
(lambda (exn) (succeed #t))]
|
||||
;; rethrow check failures if we aren't looking
|
||||
;; for them
|
||||
[exn:test:check?
|
||||
(lambda (exn)
|
||||
(refail-check exn))]
|
||||
;; catch any other exception and raise an check
|
||||
;; failure
|
||||
[exn:fail?
|
||||
(lambda (exn)
|
||||
(with-check-info*
|
||||
(list
|
||||
(make-check-message "Wrong exception raised")
|
||||
(make-check-info 'exn-message (exn-message exn))
|
||||
(make-check-info 'exn exn))
|
||||
(lambda () (fail-check))))])
|
||||
(thunk))
|
||||
(with-check-info*
|
||||
(list (make-check-message "No exception raised"))
|
||||
(lambda () (fail-check))))))
|
||||
|
||||
(define-check (check-not-exn thunk)
|
||||
(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]{
|
||||
|
||||
Checks that @racket[thunk] raises an exception for which
|
||||
@racket[exn-predicate] returns @racket[#t]. The optional
|
||||
Checks that @racket[thunk] raises an exception and that
|
||||
either @racket[exn-predicate] returns @racket[#t] if it is a function,
|
||||
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
|
||||
fails. A common error is to use an expression instead of a
|
||||
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:
|
||||
|
||||
@racketblock[
|
||||
(check-exn exn?
|
||||
(check-exn exn:fail?
|
||||
(lambda ()
|
||||
(raise (make-exn "Hi there"
|
||||
(current-continuation-marks)))))
|
||||
(code:comment "Forgot to wrap the expression in a thunk. Don't do this!")
|
||||
(check-exn exn?
|
||||
(raise (make-exn "Hi there"
|
||||
(current-continuation-marks))))
|
||||
(check-exn exn:fail?
|
||||
(error 'hi "there"))
|
||||
]
|
||||
|
||||
@defproc[(check-not-exn (thunk (-> any)) (message string? "")) #t]{
|
||||
|
|
Loading…
Reference in New Issue
Block a user