[racket] Help with exception raising and testing

From: Neil Van Dyke (neil at neilvandyke.org)
Date: Tue Aug 27 20:50:04 EDT 2013

George, Matthias's suggestion to have students use one of the teaching 
languages is generally a good idea.

For experienced programmers and software test engineers, however, there 
are a number of different test engines available.  Below is your program 
using the Overeasy test engine, which I like better than current 
RackUnit.  More info is at "http://www.neilvandyke.org/overeasy/".  It's 
currently in the middle of changes, which is why the documentation 
doesn't talk about this convention of putting tests inline with 
"(module+ test ...)", and how it now uses the logger and error 
facilities, rather than text reports.

Neil V.

#lang racket/base

(module+ test
   (require (planet neil/overeasy:3)))

(provide sum-coins)
(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)))

(module+ test

   (test #:id   'negative-coin-with-exn-string
         #:code (sum-coins -1 3 5 7)
         #:exn  "sum-coins: contract violation\n  expected: negative?\n  
given: -1")

   (test #:id   'negative-coin-with-exn-regexp
         #:code (sum-coins -1 3 5 7)
         #:exn  #rx"negative")

   (test #:id   'negative-coin-with-exn-predicate
         #:code (sum-coins -1 3 5 7)
         #:exn  exn:fail:contract?)

   (test 'all-zeroes
         (sum-coins 0 0 0 0)

   (test 'all-ones
         (sum-coins 1 1 1 1)

   (test 'barf-test
         (sum-coins 1 1 1 1)

Posted on the users mailing list.