You can to use gen/fmap to build up generators. Here's an example:

;; generates a millisecond using gen/choose, then maps that to a time instance using my u/ms-to-time function.
(def result-gen
  (->> (gen/choose 0 u/max-time)
       (gen/fmap u/ms-to-time)))

;; Generates EITHER a result or nil, then maps that into an event entry data structure:
(def event-entry-gen
  (->> (gen/one-of [(gen/return nil) result-gen])
       (gen/fmap (fn [result]
                   (merge {:_id "id"
                           :_rev "rev"
                           :type "event-entry"
                           :division {:age-group "U17"
                                      :boat-type "K1"
                                      :gender "male"}
                           :event-id "event-id"
                           :regatta-id "regatta-id"
                           :athletes []
                           :racer-number 10}
                          (when result
                            {:result {:division-size 10
                                      :rank {:overall 1
                                             :division 1}
                                      :time result}}))))))

Once you have a generator instance you can copy the rest of the test.check examples, run gen/sample on the generator directly... all the good stuff.

Timothy Washington <mailto:twash...@gmail.com>
August 10, 2014 at 12:38 PM
Hi there,

I'm trying to get my head wrapped around test.check <https://github.com/clojure/test.check/>. My current stumbling block is custom generators. Of course I've combed through the docs <https://github.com/clojure/test.check/blob/master/doc/intro.md#record-generators> and source <https://github.com/clojure/test.check/blob/master/src/main/clojure/clojure/test/check/generators.clj>. And I'm pretty sure I need to use */gen-bind/* to get this all working. But it's just not clicking yet.


Let's say I want to create a */user/* and a */group/*, where a user can belong to many groups. So let's say I have a generic */create-user/* function.

      (defn */create-user/* [opts]
        (merge {:id (mu/generate-uuid)
                :username ""
                :password (crypto/base64 12)
                :first-name ""
                :last-name ""
                :email ""
                :country {}}
               opts))


And a generic */create-group/* function.

      (defn create [opts]
        (merge {:id (mu/generate-uuid)
                :name ""
                :users []}
               opts))



 1. Now, if I try `*/(gen/sample create-user)/*`, or `*/(gen/sample
    (gen/vector du/create))/*`it will fail.
 2. I also want to do the same thing with groups (ie /(gen/sample
    create-user)/).
 3. Additionally however, I want to assert that a created group will
    always have at least 1 user in the `*/:users []/*` k/v field.


How can I make test.check generators for these data structures?



Tim Washington
Interruptsoftware.com <http://interruptsoftware.com>


--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com <mailto:clojure+unsubscr...@googlegroups.com>.
For more options, visit https://groups.google.com/d/optout.

--
Sam Ritchie (@sritchie)
Paddleguru Co-Founder
703.863.8561
www.paddleguru.com <http://www.paddleguru.com/>
Twitter <http://twitter.com/paddleguru>// Facebook <http://facebook.com/paddleguru>

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to