more / more-> cause multiple evaluation; more-of does not
seancorfield opened this issue · 3 comments
seancorfield commented
Downloading: com/bhauman/rebel-readline/maven-metadata.xml from clojars
[Rebel readline] Type :repl/help for online help info
user=> (require '[expectations.clojure.test :refer :all])
nil
user=> (def c (atom 0))
#'user/c
user=> (defn f [] (swap! c inc))
#'user/f
user=> (expect (more-of a number? a number? a number? a) (f))
true
user=> @c
1
user=> (expect (more number? number? number?) (f))
true
user=> @c
4
user=> (expect (more-> number? identity number? identity number? identity) (f))
true
user=> @c
7
user=>
holyjak commented
For me this was quite unexpected (and caused weird failures when I had more then 1 tests in the more-> - my test code changes the DB and is thus not side-effect free). IMO it would be optimal if the actual expression only was evaluated once. If that is not practical then it would be nice to make this behavior very clear and perhaps demonstrate a workaround (such as (let [actual (delay ...)] (expect ....))). Thank you!
seancorfield commented
Here's the result now:
dev=> (def c (atom 0))
#'dev/c
dev=> (defn f [] (swap! c inc))
#'dev/f
dev=> (expect (more-of a number? a number? a number? a) (f))
true
dev=> @c
1
dev=> (expect (more number? number? number?) (f))
true
dev=> @c
2
dev=> (expect (more-> number? identity number? identity number? identity) (f))
true
dev=> @c
3
dev=> holyjak commented
Awesome! Thanks a lot!