开发者

Shared definitions for with-test in Clojure

开发者 https://www.devze.com 2023-03-28 04:10 出处:网络
I am struggling to see how I can define values for use in multiple tests when using with-test.What I want to do is something like:

I am struggling to see how I can define values for use in multiple tests when using with-test. What I want to do is something like:

(with-test

  (defn myfunction [arg...]
    (code to test))

  (let [a (something-complex ...)
        b (something-else ...)]
    (is (= '(...) (myfunction a ....)))
    (is (= '(...) (myfunction a b ..)))
    (is (= '(...) (myfunction b ....))))

wher开发者_JAVA百科e i define a and b once, but use them in multiple tests (multiple assertions; this may be a single test - even that is not clear to me).

Please - how can I do this?


Your assumption is correct, the body of code after the definition given to with-test will get evaluated like any normal body of Clojure code, so you can use construct like let.

One good tip is to use macroexpand to look at the code generated by a macro like with-test. How much trust you can have in this is harder to determine. I would say that you should use your common sense to figure out what is the actual purpose of the macro you're using. Macros are powerful tools and the frontier between normally using a macro and relying on an implementation detail is much fuzzier than for a function. In this case you should be quite confident about the behavior of with-test by looking at the argument list, the last argument, namely body, implies that it accept any number of forms in the same way a function body, a let or a do would work.

0

精彩评论

暂无评论...
验证码 换一张
取 消