Hi Allen,
Looks pretty good to me.  I changed the name to "throws", which is
short, at least.  The (is :a :a) can be replaced by (count-assertion),
which is public, even though the counter itself is private.  I also
added an extra (catch...) clause so that there's a failure if the form
throws an exception other than the one that was expected.  Here's the
result, which I just committed:

(defmacro throws
  "Asserts that form throws an exception of the given class (or one of
  its subclasses)."
  ([class form]
     `(throws ~class ~form nil))
  ([class form message]
  `(try
    (count-assertion)
    (let [value# ~form]
      (failure (str "expected " ~(pr-str form) " to throw " ~class
                    ", but returned " value#) ~message))
    (catch ~class e# nil)  ; the correct exception was thrown
    (catch java.lang.Throwable e#  ; some other exception was thrown
      (failure (str "expected " ~(pr-str form) " to throw " ~class
                    ", but threw " e#) ~message)))))

-Stuart



On Sep 24, 1:17 am, Allen Rohner <[EMAIL PROTECTED]> wrote:
> Here's a first pass at an addition to test_is, the assert-raises
> macro. It asserts that the given expression throws an exception of the
> specified type.
>
> There are two things I don't like about this implementation: the name
> doesn't quite go with the test_is theme, and the (is :a :a) at the
> bottom. The problem I ran into was that the counters are all private,
> and it seems like this is more convenient as a macro than using
> lambda / apply. I figure this is a good starting point for discussion
> though.
>
> +++ src/clojure/contrib/test_is/test_is.clj     (working copy)
>
> +(defmacro assert-raises
> +  "asserts that the form raises the given expression"
> +  ([ex-class form]
> +     `(assert-raises ~ex-class ~form nil))
> +  ([ex-class form message]
> +  `(try
> +    (let [value# ~form]
> +      (failure (str "expected " ~(pr-str form) " to raise " ~ex-class
> ", but returned " value#) ~message))
> +    (catch ~ex-class e#
> +       (is :a :a))))) ; simple is just to bump the assertion count
> +
>
> Thoughts?
>
> Allen
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to