repl和测试运行程序之间的不一致 [英] inconsistency between repl and test runner

查看:61
本文介绍了repl和测试运行程序之间的不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在测试clojure宏时遇到一些问题.当我将代码放入repl时,它的行为符合预期,但是当我尝试在测试中期望这种行为时,我得到的却是nil.我觉得这与测试运行程序处理宏扩展的方式有关,但是我不确定到底发生了什么.感谢您提供任何建议/替代方法来测试此代码.

I'm having some issues with testing a clojure macro. When I put the code through the repl, it behaves as expected, but when I try to expect this behavior in a test, I'm getting back nil instead. I have a feeling it has to do with how the test runner handles macroexpansion, but I'm not sure what exactly is going on. Any advice/alternative ways to test this code is appreciated.

这是我要测试的宏的简化示例

Here is a simplified example of the macro I'm trying to test

(defmacro macro-with-some-validation
  [-name- & forms]
    (assert-symbols [-name-])
    `(defn ~-name- [] (println "You passed the validation")))

  (macroexpand-1 (read-string "(macro-with-some-validation my-name (forms))"))
  ;; ->
  (clojure.core/defn my-name [] (clojure.core/println "You passed the validation"))

传递给代表时

  (macroexpand-1 (read-string "(macro-with-some-validation 'not-symbol (forms))"))
  ;; ->
  rulesets.core-test=> Exception Non-symbol passed in to function.  chibi-1-0-0.core/assert-symbols (core.clj:140)

但是经过测试

  (deftest macro-with-some-validation-bad
    (testing "Passing in a non-symbol to the macro"
(is (thrown? Exception
             (macroexpand-1 (read-string "(macro-with-some-validation 'not-symbol (forms))"))))))

  ;; after a lein test ->
  FAIL in (macro-with-some-validation-bad) (core_test.clj:50)
  Passing in a non-symbol to the macro
  expected: (thrown? Exception (macroexpand-1 (read-string "(macro-with-some-validation 'not-symbol (forms))")))
    actual: nil

谢谢.

如果重要,则忘记包含断言符号的来源

forgot to include the source for assert-symbols in case it matters

(defn assert-symbol [symbol]
  (if (not (instance? clojure.lang.Symbol symbol))
        (throw (Exception. "Non-symbol passed in to function."))))

(defn assert-symbols [symbols]
  (if (not (every? #(instance? clojure.lang.Symbol %) symbols))
    (throw (Exception. "Non-symbol passed in to function."))))

推荐答案

将我的读取字符串更改为`之后,我可以使代码再次正常工作.不过,仍然很奇怪,读取字符串无法正常工作.感谢您的帮助.

After changing my read-strings to be ` instead, I'm able to get the code working again. Still strange that read-string wasn't working correctly, though. Thanks for the help.

这篇关于repl和测试运行程序之间的不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆