-
Notifications
You must be signed in to change notification settings - Fork 0
/
generators.clj
27 lines (26 loc) · 1.05 KB
/
generators.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
(ns instacheck.generators
(:require [clojure.test.check.generators :as gen]))
(defn freq
"Alternate version of standard frequency generator with the the
weights sorted so that shrinking is to the higher weights first and
then the earlier weights among those that are equal (Clojure sort is
stable and will not re-order equal elements). To support removal of
generator branches via weights this version also allows all the
weights to be zero and will throw at runtime rather than
a definition time."
[id pairs]
(if (= 0 (reduce + (map first pairs)))
(gen/fmap
(fn [g] (throw (ex-info
(str "Invalid zero weight call to "
"(instacheck.generators/freq " id " ...)")
{:pairs pairs})))
(gen/return ""))
(gen/frequency (sort (comparator (fn [a b] (> (first a) (first b))))
pairs))))
(defn vector+
"Version of standard vector generator with at least one item"
[gen]
(gen/let [v (gen/vector gen)
itm gen]
(conj v itm)))