Commits

Meikel Brandmeyer  committed 17df1c4

Basic generators

  • Participants
  • Parent commits 2d436c3

Comments (0)

Files changed (1)

File src/main/clojure/clojurecheck/core.clj

   (value  [this] (into (empty this) (for [[k v] this] [k (value v)])))
   (shrink [this] (for [[k v] this sv (shrink v)] (assoc this k sv))))
 
+;; ## Basic generators
+;;
+;; Here we define some basic generators which are basically the
+;; lowest level building blocks of our generator DSL.
 (extend-protocol Generator
+
+  ;; Functions may be used directly as generators. They generate
+  ;; a `SimpleValue` which is not shrinable.
   clojure.lang.Fn
-  (arbitrary [this size] (DomainValue. this (this size)))
+  (arbitrary [this size] (SimpleValue. (this size)))
 
+  ;; Vectors and sequences are treated as tupels of generators.
   clojure.lang.PersistentVector
   (arbitrary [this size] (arbitrary (seq this) size))
 
   clojure.lang.ISeq
-  (arbitrary
-    [this size]
-    (let [[generators values]
-          (reduce (fn [[generators values] gen]
-                    (let [dval (arbitrary gen size)]
-                      [(conj generators (.domain dval))
-                       (conj values (.value dval))]))
-                  [[] []] this)]
-      (DomainValue. generators values)))
+  (arbitrary [this size] (map #(generate % size) this))
 
+  ;; Maps generate static maps with fixed keys mapped to the
+  ;; values generated by the named generators.
   clojure.lang.IPersistentMap
-  (arbitrary
-    [this size]
-    (let [ks    (keys this)
-          gens  (vals this)
-          dvals (arbitrary gens size)]
-      (DomainValue. (zipmap ks (.domain dvals))
-                    (zipmap ks (.value dvals)))))
+  (arbitrary [this size]
+    (into (empty this) (for [[k gen] this] [k (generate gen size)])))
 
+  ;; Any other arbitrary object, which does not take part in the
+  ;; `Generator` protocol gives a generator which always generates
+  ;; the given value.
   Object
   (arbitrary [this size] (arbitrary (constantly this) size)))