Skip to main content

test.gen

Jump to function (34)

test.gen/boolean#

Generator of booleans.

test.gen/char#

Generator of printable ASCII characters (space through ~).

test.gen/char-alpha#

Generator of ASCII letters.

test.gen/char-alphanumeric#

Generator of ASCII letters and digits.

test.gen/choose#

(choose lo hi)

Generator of integers in the closed interval [lo hi].

Example:

((choose 1 6) 100) ; => 1..6

test.gen/default-num-tests#

Number of trials quick-check runs by default.

test.gen/default-size#

Magnitude used when no :size option is supplied.

test.gen/defspec#

(defspec name options args-gen property)

Defines a property test. The generated deftest runs quick-check and asserts the result is :pass. options is a hash-map accepting :num-tests, :size, :seed and :shrink?.

Attach ^:no-shrink metadata to the name to skip shrinking on failure. The test-name symbol's metadata is also forwarded to the underlying deftest, so tag-based selectors apply as usual.

On failure a :defspec-failed event is sent to the reporter set before the surrounding is assertion records a :failed event. Reporters that want rich shrink information can subscribe to it; the built-in reporters keep rendering their usual failure summary.

Shape: (defspec name options args-gen property).

Example:

(defspec addition-commutes {:num-tests 200}
              (tuple int int)
              (fn [a b] (= (+ a b) (+ b a))))

test.gen/elements#

(elements coll)

Generator that picks a random element from coll.

Example:

((elements [:a :b :c]) 100) ; => :a, :b or :c

test.gen/float#

Generator of floats in [0, 1).

test.gen/fmap#

(fmap f g)

Returns a generator that applies f to values produced by g.

Example:

((fmap inc (return 1)) 100) ; => 2

test.gen/frequency#

(frequency pairs)

Generator that picks one of the [weight gen] pairs with probability proportional to weight.

Example:

((frequency [[9 (return :a)] [1 (return :b)]]) 100)

test.gen/generate#

(generate g)
(generate g {:size size, :seed seed})

Runs g once and returns a single value. Accepts :size and :seed.

Example:

(generate int) ; => 42

test.gen/int#

Generator of integers in [-size, size].

test.gen/keyword#

Generator of keywords with alphabetic names, length in [1, max(1, size)].

test.gen/large-int#

Generator of arbitrary PHP-range integers.

test.gen/list-of#

(list-of g)

Generator of lists whose elements come from g. Length in [0, size].

Example:

((list-of int) 3) ; => (-1 2 0)

test.gen/map-of#

(map-of kg vg)

Generator of hash-maps with keys from kg and values from vg. Number of entries is in [0, size].

Example:

((map-of keyword int) 2) ; => {:a 1 :b -3}

test.gen/nat#

Generator of non-negative integers in [0, size].

test.gen/one-of#

(one-of gens)

Generator that selects uniformly from gens and runs the chosen one.

Example:

((one-of [int boolean]) 100)

test.gen/quick-check#

(quick-check num-tests args-gen property)
(quick-check num-tests args-gen property {:size size, :seed seed, :shrink? shrink?})

Runs property for num-tests trials, drawing each trial's arguments from args-gen (a generator returning a vector of arguments). Returns a hash-map with :result (:pass, :failed, or :error), :num-tests, and the effective :seed for replay. On failure/error the failing :args (and :exception) are also included.

Options:

  • :size (default 100): magnitude passed to the generator.
  • :seed (default: fresh per-run): PRNG seed.
  • :shrink? (default: true): when true, a failing trial is shrunk to a minimal counterexample; the resulting map carries :shrunk-args, :original-args and :shrink-steps.

Example:

(quick-check 50 (tuple int int) (fn [a b] (= (+ a b) (+ b a))))

test.gen/resize#

(resize n g)

Returns a generator equivalent to g but with size forced to n.

Example:

((resize 5 nat) 1000) ; => value in [0, 5]

test.gen/return#

(return x)

Generator that always yields x.

Example:

((return 42) 100) ; => 42

test.gen/sample#

(sample g)
(sample g num-samples)
(sample g num-samples {:size size, :seed seed})

Runs g num-samples times (default 10) and returns a vector of values. Accepts :size and :seed.

Example:

(sample int 5) ; => [3 -7 0 1 -2]

test.gen/set-of#

(set-of g)

Generator of hash-sets with elements from g. Cardinality in [0, size].

Example:

((set-of nat) 3) ; => (set 1 2 3)

test.gen/sized#

(sized f)

Builds a generator from a function f of size. The returned generator forwards its size into f, which must yield another generator that is then invoked with the same size.

Example:

(sized (fn [n] (return n)))

test.gen/spec-failure-message#

(spec-failure-message name-str res)

Renders the human-readable failure message for a defspec outcome map. Includes the shrink summary when the trial's arguments were shrunk to a smaller counterexample.

Example:

(spec-failure-message "my-spec" {:result :failed ...})

test.gen/string#

Generator of printable ASCII strings, length in [0, size].

test.gen/string-alpha#

Generator of ASCII alphabetic strings, length in [0, size].

test.gen/string-alphanumeric#

Generator of ASCII alphanumeric strings, length in [0, size].

test.gen/such-that#

(such-that pred g)
(such-that pred g max-tries)

Generator yielding only values from g that satisfy pred. Retries up to max-tries (default 100) before throwing.

Example:

((such-that even? nat) 100)

test.gen/symbol#

Generator of symbols with alphabetic names, length in [1, max(1, size)].

test.gen/tuple#

(tuple & gens)

Generator of a fixed-length vector produced by running each of the given generators in order. Accepts either a vector of generators or variadic generator arguments.

Example:

((tuple int boolean) 10) ; => [3 true]

test.gen/vector-of#

(vector-of g)
(vector-of g n)
(vector-of g lo hi)

Generator of vectors. Length is [0, size] with one arg, exactly n with (vector-of g n), or [lo, hi] with (vector-of g lo hi).

Example:

((vector-of int) 3) ; => [-1 2 0]