test.check cheatsheet
So far this only documents functions in the generators namespace.
Dev utilities, misc
(gen/sample g)— returns 10 smallish samples fromg(gen/sample g n)— generatesnsamples fromg(gen/generate g)— generates a single moderately sized value fromg(gen/generate g size)— generates a value fromgwith the givensize(normally sizes range from 0 to 200)(gen/generator? g)— checks ifgis a generator
Simple Generators
(gen/return x)— A constant generator that always generatesxgen/boolean— generates booleans (trueandfalse)gen/uuid— generates uniformly random UUIDs, does not shrink(gen/elements coll)— generates elements fromcoll(which must be non-empty)(gen/shuffle coll)— generates vectors with the elements ofcollin random ordersgen/any— generates any clojure valuegen/any-printable— generates any printable clojure valuegen/any-equatable— generates any clojure value that can be equal to anothergen/any-printable-equatable— generates any printable clojure value that can be equal to anothergen/simple-type— likegen/anybut does not generate collectionsgen/simple-type-printable— likegen/any-printablebut does notgen/simple-type-equatable— likegen/any-equatablebut does not generate collectionsgen/simple-type-printable-equatable— likegen/any-printable-equatablebut does not generate collections
Numbers
gen/nat— generates small non-negative integers (useful for generating sizes of things)gen/small-integer— generates small integers, likegen/natbut also negativegen/large-integer— generates a large range of integers- variant with options:
(gen/large-integer* {:min x, :max y}) gen/size-bounded-bigint— generates bigints, up to2^(6*size)gen/double— generates a large range of doubles (w/ infinities &NaN)- variant with options:
(gen/double* {:min x, :max y, :infinite? true, :NaN? true}) gen/ratio— generates ratios (sometimes integers) using gen/small-integergen/big-ratio— generates ratios (sometimes integers) using gen/size-bounded-bigintgen/byte— generates aBytegen/choose— generates uniformly distributed integers between two (inclusive) values
Characters & Strings & Things
gen/char— generates charactersgen/char-ascii— generates printable ASCII charactersgen/char-alphanumeric— generates alphanumeric ASCII charactersgen/char-alpha— generates alphabetic ASCII charactersgen/string— generates a stringgen/string-ascii— generates a string usinggen/char-asciigen/string-alphanumeric— generates a string usinggen/char-alphanumericgen/keyword— generates keywordsgen/keyword-ns— generates namespaced keywordsgen/symbol— generates symbolsgen/symbol-ns— generates namespaced symbols
Heterogeneous Collections
(gen/tuple g1 g2 ...)— generates vectors[x1 x2 ...]wherex1is drawn fromg1,x2fromg2, etc.(gen/hash-map k1 g1, k2 g2, ...)— generates maps{k1 v1, k2 v2, ...}wherev1is drawn fromg1,v2fromg2, etc.
Homogeneous Collections
(gen/vector g)— generates vectors of elements fromg- Variants:
(gen/vector g num-elements)(gen/vector g min-elements max-elements)
(gen/list g)— generates lists of elements fromg(gen/set g)— generates sets of elements fromg- Variants:
(gen/set g {:num-elements x, :max-tries 20})(gen/set g {:min-elements x, :max-elements y, :max-tries 20})
(gen/map key-gen val-gen)— generates a map with keys fromkey-genand vals fromval-gen- same opts as
gen/setu (gen/sorted-set g)— just likegen/set, but generates sorted-sets(gen/vector-distinct g)— same signature asgen/set, but generates vectors of distinct elements(gen/list-distinct g)— same signature asgen/set, but generates lists of distinct elements(gen/vector-distinct-by key-fn g)— generates vectors of elements where(apply distinct? (map key-fn the-vector))- same opts as
gen/set (gen/list-distinct-by key-fn g)— generates list of elements where(apply distinct? (map key-fn the-list))- same opts as
gen/set gen/bytes— generates a byte array
Combinators
(gen/let [x g] y)— macro, likeclojure.core/let, where the right-hand bindings are generators and the left-hand are generated values; creates a generator- same functionality as
gen/fmapandgen/bind (gen/fmap f g)— creates a generator that generates(f x)forxgenerated fromg(gen/bind g f)— similar togen/fmap, but where(f x)is itself a generator and(gen/bind g f)generates values from(f x)(gen/such-that pred g)— returns a new generator that generates only elements fromgthat matchpred- Variants:
(gen/such-that pred g max-tries) (gen/one-of [g1 g2 ...])— generates elements from the given generators, picking generators at random(gen/frequency [[2 g1] [7 g2] ...])— generates elements from the given generators, using the given weights to determine the probability of picking any particular generator(gen/not-empty g)— given a generator that generates collections, returns a modified generator that never generates empty collections(gen/recursive-gen container-gen scalar-gen)— generates a tree of values, usingcontainer-gen(which is a function likegen/listwhich takes and returns a generator) andscalar-gen(a generator for the leaf values)
Sizing & shrinking control
(gen/resize n g)— creates a variant ofgwhosesizeparameter is alwaysn(gen/scale f g)— creates a variant ofgwhosesizeparameter is(f size)(gen/no-shrink g)— creates a variant ofgthat does not shrink