在Clojure中,Midje无法处理宏



这是传递的版本代码:

正常函数:通过

(defn my-fn
  []
  (throw (IllegalStateException.)))
(fact
  (my-fn) => (throws IllegalStateException))

这是它的宏版本:

(defmacro my-fn
  []
  (throw (IllegalStateException.)))
(fact
  (my-fn) => (throws IllegalStateException))

where 失败下面是输出:

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

这是相同的代码,我只是改变defndefmacro

我不明白,为什么这是不工作?

事情是你的宏是错误的。当函数在运行时抛出错误时,宏会在编译时抛出错误。下面的代码可以修复该行为:

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

现在你的宏调用将被被抛出的异常所取代。像这样:

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

最新更新