[racket] Help with exception raising and testing

From: George Rudolph (rudolphg1 at citadel.edu)
Date: Tue Aug 27 14:33:54 EDT 2013

 

I am having trouble figuring out how to write code that raises an
argument error

and code that tests for that error using rackunit.

 

I have the following function, which raises and exception when 

pennies is negative, in a file:

-----------------------------------

#lang racket

 

(define (sum-coins pennies nickels dimes quarters )

  (cond 

    [(negative? pennies) (raise-argument-error 'sum-coins "negative?" 0
pennies)

      ])

  (+ pennies (* 5 nickels) (* 10 dimes) (* 25 quarters)))

 

(provide sum-coins)

------------------------------

In a separate test file, I have the following test case:

 

--------------------

#lang racket

(require rackunit "mybasic.rkt")

(test-exn "negative coin" negative? (sum-coins -1 3 5 7) )

 

---------------------

 

When I run the test, I get the following error:

 

--------------------

negative coin

ERROR

. . sum-coins: contract violation

  expected: negative?

  given: -1

 

--------------------

 

This is not what I expect. I expect the test to succeed if the argument
error is raised.

I believe that there is something not quite correct about both the code
that raises the exception

and the call that tests it.  The examples in the Racket documentation
online for raising an argument error

and for testing for the error/exception are not clear to me, obviously.

 

Can someone help?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20130827/f5ea2e9a/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: image/gif
Size: 92 bytes
Desc: image001.gif
URL: <http://lists.racket-lang.org/users/archive/attachments/20130827/f5ea2e9a/attachment.gif>

Posted on the users mailing list.