Midje fails on Macros in Clojure

87 views Asked by At

Here is the passing version of the code:

Normal function: That passes

(defn my-fn
  []
  (throw (IllegalStateException.)))

(fact
  (my-fn) => (throws IllegalStateException))

Here is the macro version of it:

(defmacro my-fn
  []
  (throw (IllegalStateException.)))

(fact
  (my-fn) => (throws IllegalStateException))

Which fails here is the output:

LOAD FAILURE for example.dsl.is-test
java.lang.IllegalStateException, compiling:(example/dsl/is_test.clj:14:3)
The exception has been stored in #'*me, so `(pst *me)` will show the stack trace.
FAILURE: 1 check failed.  (But 12 succeeded.)

It is the same code I just changed defn to defmacro.

I did not understand that why this is not working?

1

There are 1 answers

1
leetwinski On BEST ANSWER

the thing is your macro is wrong. While your function was throwing an error in runtime, the macro throws it in compile-time. The following should fix that behavior:

(defmacro my-fn
  []
  `(throw (IllegalStateException.)))

now your macro call would be substituted with the exception being thrown. Something like that:

(fact
  (throw (IllegalStateException.)) => (throws IllegalStateException))