[racket] Help with exception raising and testing

From: Asumu Takikawa (asumu at ccs.neu.edu)
Date: Tue Aug 27 16:35:06 EDT 2013

On 2013-08-27 14:33:54 -0400, George Rudolph wrote:
>    #lang racket
>
>    (require rackunit "mybasic.rkt")
>
>    (test-exn "negative coin" negative? (sum-coins -1 3 5 7) )

There is a common mistake here that myself and other Racket programmers
frequently make. The various rackunit functions that deal with
exceptions take a *thunk* that contains the code to run.

So `(sum-coins -1 3 5 7)` should be `(λ () (sum-coins -1 3 5 7))` or
`(thunk (sum-coins -1 3 5 7))`.

In addition, the test predicate should take an exception value so
`negative?` won't work. You probably want a predicate like
`(λ (e) (regexp-match #rx"negative?" (exn-message e)))` instead.

Cheers,
Asumu

Posted on the users mailing list.