简体   繁体   中英

Midje fails on Macros in Clojure

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?

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM