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?

<<image001.gif>>

____________________
  Racket Users list:
  http://lists.racket-lang.org/users

Reply via email to