Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I try to test the throw of an exception:

;;; src
;; Courtesy https://github.com/clojure/clojure/blob/master/src/clj/clojure/core.clj
(defmacro assert-args [& pairs]
  `(do (when-not ~(first pairs)
         (throw (IllegalArgumentException.
                 (str (first ~'&form) " requires " ~(second pairs) " in " ~'*ns* ":" (:line (meta ~'&form))))))
       ~(let [more (nnext pairs)]
          (when more
            (list* `assert-args more)))))

(defmacro my-macro []
  (assert-args false "foobar")
  #_(...))

;;; test
(deftest x-fn-test
  (is (thrown-with-msg? IllegalArgumentException #"foo" (my-macro))))

But none of the exceptions are caught by the test framework, but are all thrown.

share|improve this question

Your throw expression is malformed, you want

(throw (IllegalArgumentException. "foo"))

Full working code:

(ns mytest.core
  (:use clojure.test))

(defn x-fn []
  (throw (IllegalArgumentException. "foo")))

(deftest x-fn-test
  (is (thrown-with-msg? IllegalArgumentException           #"foo"     (x-fn)))
  (is (thrown-with-msg? IllegalArgumentException           #".*foo.*" (x-fn)))
  (is (thrown-with-msg? IllegalArgumentException           #"^foo$"   (x-fn)))
  (is (thrown-with-msg? java.lang.IllegalArgumentException #"foo"     (x-fn)))
  (is (thrown-with-msg? java.lang.IllegalArgumentException #".*foo.*" (x-fn)))
  (is (thrown-with-msg? java.lang.IllegalArgumentException #"^foo$"   (x-fn))))


; nREPL 0.1.8-preview
user> 
#<Namespace mytest.core>
mytest.core> (run-tests)

Testing mytest.core

Ran 1 tests containing 6 assertions.
0 failures, 0 errors.
{:type :summary, :pass 6, :test 1, :error 0, :fail 0}
share|improve this answer
    
Thanks Joost, but it was just a typo while posting it. The issue remains. Or does it then work for you? – Kreisquadratur Jun 28 '13 at 16:55
    
@Kreisquadratur fixing x-fn makes it work me me. See update – Joost Diepenmaat Jun 28 '13 at 17:08
    
Hej Joost, I got trapped by the "example fallacy" and simplified my example code to an extend that it didn't illustrate the issue I had. Thus the example worked for you. Now I posted a pretty close example version of my code and that should expose the misassumption. During the process of converging the example to the original code I finally understood what was going on. I still up voted your answer to credit your efforts in helping out. – Kreisquadratur Jul 10 '13 at 8:50
up vote 1 down vote accepted

The reason why the exception is still thrown, is because the macro (my-macro) gets expanded during compile time, which calls the (assert-args) macro, thus the exception is thrown during compile time. My mistake was to assume that calling (my-macro) in the test will trigger the exception at run time and let the test's (thrown-with-msg?) catch it.

So the misassumption was that code for nested macros is only expanded during compile time, not executed. If (my-macro) would be a function, this would have worked.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.