simple-check.generators documentation
any-printable
var
Like any, but avoids characters that the shell will interpret as actions, like 7 and 14 (bell and alternate character set command)
bind
fn
Create a new generator that passes the result of gen
into function
k
. k
should return a new generator. This allows you to create new
generators that depend on the value of other generators. For example,
to create a generator which first generates a vector of integers, and
then chooses a random element from that vector:
(gen/bind (gen/such-that not-empty (gen/vector gen/int))
;; this function takes a realized vector,
;; and then returns a new generator which
;; chooses a random element from it
gen/elements)
choose
fn
Create a generator that returns numbers in the range
min-range
to max-range
, inclusive.
elements
fn
Create a generator that randomly chooses an element from coll
.
Examples:
(gen/elements [:foo :bar :baz])
frequency
fn
Create a generator that chooses a generator from pairs
based on the
provided likelihoods. The likelihood of a given generator being chosen is
its likelihood divided by the sum of all likelihoods
Examples:
(gen/frequency [[5 gen/int] [3 (gen/vector gen/int)] [2 gen/boolean]])
hash-map
fn
Like clojure.core/hash-map, except the values are generators. Returns a generator that makes maps with the supplied keys and values generated using the supplied generators.
Examples:
(gen/hash-map :a gen/boolean :b gen/nat)
int
var
Generates a positive or negative integer bounded by the generator's
size
parameter.
(Really returns a long)
map
fn
Create a generator that generates maps, with keys chosen from
key-gen
and values chosen from val-gen
.
no-shrink
fn
Create a new generator that is just like gen
, except does not shrink
at all. This can be useful when shrinking is taking a long time or is not
applicable to the domain.
not-empty
var
Modifies a generator so that it doesn't generate empty collections.
Examples:
;; generate a vector of booleans, but never the empty vector
(gen/not-empty (gen/vector gen/boolean))
one-of
fn
Create a generator that randomly chooses a value from the list of provided generators. Shrinks toward choosing an earlier generator, as well as shrinking the value generated by the chosen generator.
Examples:
(one-of [gen/int gen/boolean (gen/vector gen/int)])
ratio
var
Generates a clojure.lang.Ratio
. Shrinks toward 0. Not all values generated
will be ratios, as many values returned by /
are not ratios.
return
fn
Create a generator that always returns value
,
and never shrinks. You can think of this as
the constantly
of generators.
rose-seq
fn
Create a lazy-seq of all of the (unique) nodes in a shrink-tree. This assumes that two nodes with the same value have the same children. While it's not common, it's possible to create trees that don't fit that description. This function is significantly faster than brute-force enumerating all of the nodes in a tree, as there will be many duplicates.
sample
fn
Return a sequence of num-samples
(default 10)
realized values from generator
.
shrink-2
fn
Create a new generator like gen
, but will consider nodes for shrinking
even if their parent passes the test (up to one additional level).
sized
fn
Create a generator that depends on the size parameter.
sized-gen
is a function that takes an integer and returns
a generator.
such-that
fn
Create a generator that generates values from gen
that satisfy predicate
f
. Care is needed to ensure there is a high chance gen
will satisfy f
,
otherwise it will keep trying forever. Eventually we will add another
generator combinator that only tries N times before giving up. In the Haskell
version this is called suchThatMaybe
.
Examples:
;; generate non-empty vectors of integers
(such-that not-empty (gen/vector gen/int))
tuple
fn
Create a generator that returns a vector, whose elements are chosen from the generators in the same position. The individual elements shrink according to their generator, but the value will never shrink in count.
Examples:
(def t (tuple gen/int gen/boolean))
(sample t)
;; => ([1 true] [2 true] [2 false] [1 false] [0 true] [-2 false] [-6 false]
;; => [3 true] [-4 false] [9 true]))
vector
fn
Create a generator whose elements are chosen from gen
. The count of the
vector will be bounded by the size
generator parameter.