Generating structured maps with test.check

579 Views Asked by At

I'm playing around with test.check, and I'm testing a function which takes a map as an argument. These maps do have a defined structure, such as:

{:name "Bob" :age 42 :email "[email protected]" :admin true}

Key point, there is a set of expected keys, the values of which have differing clearly defined generators.

I took a look at gen/map, but it's not obvious how to use it for more structured key/value pairs:

(gen/sample (gen/map gen/keyword gen/boolean) 5)
;; => ({} {:z false} {:k true} {:v8Z false} {:9E false, :3uww false, :2s true})

This seems like a simple scenario, but I can't find an example.

How can I generate structured maps, such as the one described here, using test.check?

1

There are 1 best solutions below

0
On BEST ANSWER

Use gen/hash-map instead of gen/map.

=> (gen/sample (gen/hash-map :name gen/string
                             :age gen/int
                             :email email-gen     ; from test.check examples
                             :admin gen/boolean))
({:email "[email protected]", :age 0, :name "", :admin true}
 {:email "[email protected]", :age -1, :name "Á6", :admin false}
 {:email "[email protected]", :age 4, :name "z", :admin true})