[racket] Help with exception raising and testing

From: George Rudolph (rudolphg1 at citadel.edu)
Date: Tue Aug 27 19:48:47 EDT 2013

I understand the idea of refactoring code to make it testable.
I very much appreciate the clarification on usage. Thank you.
But...
As I see it, this approach to testing code is beyond counterintuitive,
at least at first glance.  It takes a moment to realize that wrapping 
the test call in a lambda definition or a thunk is basically creating a 
delayed unit of computation. Not something I want to teach my students about
during the first week of class, I think.

What I intend to do is require them to unit test their code before they submit it.
I have never used rackunit before, so I thought I would give it a try before I make them use it.
IS testing really this complex, or am I missing some larger idea here?

George

-----Original Message-----
From: Asumu Takikawa [mailto:asumu at ccs.neu.edu] 
Sent: Tuesday, August 27, 2013 4:35 PM
To: George Rudolph
Cc: users at racket-lang.org
Subject: Re: [racket] Help with exception raising and testing

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.