Following on this question, and the blog post referenced there, is there a reason why prop/for-all
does not just roll in this sort of capability directly? E.g. something like:
(require '[clojure.test.check.generators :as gen])
(require '[clojure.test.check.properties :as prop])
(require '[clojure.test.check.clojure-test :refer :all])
(defspec some-props-test
(prop/for-all [n (gen/choose 1 10)
v (gen/vector gen/int n) ;; treat n like its produced value
e (gen/element v)]
... do stuff with n, v & e ...
))
Basically, I want to re-use the value produced by one generator in another generator and then reference both values produced within the actual test code. This would essentially extend the sugar/magic of for-all
into allowing references to the generated values within the let-like block provided by the macro, as it works within the expression blocks below.
Please let me know if I'm missing something else that makes this possible or it just wouldn't make sense for some reason to implement.
I agree that this functionality would probably be more useful than what
for-all
currently does. The primary reason it hasn't been changed is for backwards-compatibility (though admittedly code using the old style wouldn't break, it would just not shrink as well as it used to).But you have more options available than just monads:
gen/let
, which useslet
-style bindings (it isn't a drop-in replacement forfor-all
but you can use them together)com.gfredericks.test.chuck.generators/for
defined in the test.chuck helper library — it's like a fancier version ofgen/let
com.gfredericks.test.chuck.properties/for-all
, in the same library, which is a drop-in replacement forfor-all