Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rackunit: in-depth check of exception type

In Rackunit, I know how to assert that an exception is thrown:

#lang racket

(module+ test
  (require rackunit)
  (check-exn exn:fail:contract? (lambda () (3 + 4))))

However, I cannot find a way to assert something more specific. Looking at the exception hierarchy in Racket, exn:fail:contract could mean a number of things: a wrong arity, a division by zero ...

I would like to assert in the test that this particular exception is one that reads:

; application: not a procedure;
;  expected a procedure that can be applied to arguments

in its printed message. How do you accomplish this?

like image 757
logc Avatar asked Dec 08 '25 05:12

logc


1 Answers

The predicate doesn't have to be a built-in exception predicate. You can use your own, like this:

(check-exn (lambda (e)
             (and (exn:fail:contract? e)
                  (regexp-match #rx"not a procedure" (exn-message e))))
           (lambda () (3 + 4)))

Rackunit's check-exn also accepts a regexp in place of an exception predicate. In that case, it checks for an exn:fail (or any of its subtypes) whose message matches the regexp. So you could also write this:

(check-exn #rx"not a procedure" (lambda () (3 + 4)))
like image 118
Ryan Culpepper Avatar answered Dec 10 '25 21:12

Ryan Culpepper