simple-check.generators documentation

any

var

A recursive generator that will generate many different, often nested, values

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

[generator k]

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)

boolean

var

Generates one of true or false. Shrinks to false.

byte

var

Generates java.lang.Bytes, using the full byte-range.

bytes

var

Generates byte-arrays.

char

var

Generates character from 0-255.

char-alpha-numeric

var

Generate alpha-numeric characters.

char-ascii

var

Generate only ascii character.

choose

fn

[lower upper]

Create a generator that returns numbers in the range min-range to max-range, inclusive.

elements

fn

[coll]

Create a generator that randomly chooses an element from coll.

Examples:

(gen/elements [:foo :bar :baz])

frequency

fn

[pairs]

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

[& kvs]

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)

keyword

var

Generate keywords.

list

fn

[generator]

Like vector, but generators lists.

map

fn

[key-gen val-gen]

Create a generator that generates maps, with keys chosen from key-gen and values chosen from val-gen.

nat

var

Generates natural numbers, starting at zero. Shrinks to zero.

neg-int

var

Generate negative integers bounded by the generator's size parameter.

no-shrink

fn

[gen]

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

[generators]

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)])

pos-int

var

Generate positive integers bounded by the generator's size parameter.

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.

resize

fn

[n {gen :gen}]

Create a new generator with size always bound to n.

return

fn

[value]

Create a generator that always returns value, and never shrinks. You can think of this as the constantly of generators.

rose-seq

fn

[root]

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.

s-neg-int

var

Generate strictly negative integers bounded by the generator's size parameter.

s-pos-int

var

Generate strictly positive integers bounded by the generator's size parameter.

sample

fn

[generator]
[generator num-samples]

Return a sequence of num-samples (default 10) realized values from generator.

sample-seq

fn

[generator]
[generator max-size]

Return a sequence of realized values from generator.

shrink-2

fn

[gen]

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

[sized-gen]

Create a generator that depends on the size parameter. sized-gen is a function that takes an integer and returns a generator.

string

var

Generate strings. May generate unprintable characters.

string-alpha-numeric

var

Generate alpha-numeric strings.

string-ascii

var

Generate ascii strings.

such-that

fn

[pred gen]

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

[& generators]

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

[generator]
[generator num-elements]
[generator min-elements max-elements]

Create a generator whose elements are chosen from gen. The count of the vector will be bounded by the size generator parameter.