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)
— generatesn
samples fromg
(gen/generate g)
— generates a single moderately sized value fromg
(gen/generate g size)
— generates a value fromg
with the givensize
(normally sizes range from 0 to 200)(gen/generator? g)
— checks ifg
is a generator
Simple Generators
(gen/return x)
— A constant generator that always generatesx
gen/boolean
— generates booleans (true
andfalse
)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 ofcoll
in 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/any
but does not generate collectionsgen/simple-type-printable
— likegen/any-printable
but does notgen/simple-type-equatable
— likegen/any-equatable
but does not generate collectionsgen/simple-type-printable-equatable
— likegen/any-printable-equatable
but 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/nat
but 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 aByte
gen/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-ascii
gen/string-alphanumeric
— generates a string usinggen/char-alphanumeric
gen/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 ...]
wherex1
is drawn fromg1
,x2
fromg2
, etc.(gen/hash-map k1 g1, k2 g2, ...)
— generates maps{k1 v1, k2 v2, ...}
wherev1
is drawn fromg1
,v2
fromg2
, 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-gen
and vals fromval-gen
- same opts as
gen/set
u (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/fmap
andgen/bind
(gen/fmap f g)
— creates a generator that generates(f x)
forx
generated 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 fromg
that 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/list
which takes and returns a generator) andscalar-gen
(a generator for the leaf values)
Sizing & shrinking control
(gen/resize n g)
— creates a variant ofg
whosesize
parameter is alwaysn
(gen/scale f g)
— creates a variant ofg
whosesize
parameter is(f size)
(gen/no-shrink g)
— creates a variant ofg
that does not shrink