2

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 "bob@example.com" :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?

Brad Koch
  • 19,267
  • 19
  • 110
  • 137

1 Answers1

5

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 "00w@hotmail.com", :age 0, :name "", :admin true}
 {:email "mi6@computer.org", :age -1, :name "Á6", :admin false}
 {:email "Ux4gp@hotmail.com", :age 4, :name "z", :admin true})
Brad Koch
  • 19,267
  • 19
  • 110
  • 137