简体   繁体   English

Midje在Clojure中的Macros上失败

[英]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 . 这与我刚刚将defn更改为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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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