Fork me on GitHub
#test-check
<
2020-09-27
>
colinkahn16:09:55

Is it possible to make a vector generator that always generates X number of items but can shrink down to only 1 of those items?

gfredericks16:09:40

What's the use case?

colinkahn16:09:17

I have a test that has some expensive setup and I want to run the max items through it (each item of the vector) but still want to be able to get it to shrink to a single item if it fails

gfredericks16:09:57

you can get approximately this behavior by modifying the size parameter that won't specifically guarantee large vectors, but it does express the idea of "I want to generate big inputs for whatever reason"

colinkahn16:09:43

But will that affect the size of other gens or can I modify per-gen?

gfredericks16:09:25

ummmmmmhm let's see something like

(defn gen-vector-with-shallow-min-size
  [min-size element-gen & other-args]
  (gen/sized (fn [size] (gen/scale #(min min-size %) (apply gen/vector (gen/resize size element-gen) other-args)))))

gfredericks16:09:42

kind of hairy, but I believe that's what you're describing

gfredericks16:09:00

again, doesn't guarantee any minimum number of elements, but it will skew the distribution larger

gfredericks16:09:37

(which is intentional -- it's nice to still test empty/small vectors occasionally just in case)

colinkahn16:09:15

Nice! Thanks! I’ll give it a shot!

👍 3