I'm using test.check to generate a hashmap. I want one key to be a random numeric keywords This is what I tried
(gen/hash-map
:16 gen/nat
:1041 gen/string
(keyword (str gen/nat)) gen/string)
This works fine for the first two keys, but gen/sample returns a function for the last key instead of a value. An output from gen/sample {:16 1, :1041 "»", :clojure.test.check.generators.Generator@3daf97b7 "u"}
Here is an example using
gen/bind
And here is one using
gen/fmap
In such cases, you have to use either
gen/fmap
orgen/bind
. Both of them allow you to manipulate the output of a generator. If you want to return a generator, then usegen/bind
. If you want to return a value, then usegen/fmap
. Bothgen/fmap
andgen/bind
take two arguments, one of which is a generator, and the other argument is a function that takes the value generated by the first as its input and returns either a generator (forgen/bind
) or a value (forgen/fmap
).