metamon/generator
Generators of random test inputs, with shrinking.
A Generator(a) produces values of type a from a seed and a size,
together with smaller alternatives the runner tries when a property
fails, and a short list of edge values the runner tries first. The
size grows from 0 on the first random input to Config.max_size on the
last.
let names =
generator.string(
of: generator.Alphabetic,
length: generator.range(from: 1, to: 12),
)
let ages = generator.int(generator.range(from: 0, to: 120))
let users = generator.map2(names, ages, with: User)
Types
A set of characters for character and string.
pub type Characters {
Ascii
PrintableAscii
Alphabetic
Alphanumeric
Digits
Unicode
}
Constructors
-
AsciiEvery ASCII character,
U+0000toU+007F, control characters included. -
PrintableAsciiThe printable ASCII characters, from the space
U+0020to~U+007E. -
AlphabeticThe ASCII letters
a-zandA-Z. -
AlphanumericThe ASCII letters and the digits
0-9. -
DigitsThe digits
0-9. -
UnicodeEvery Unicode scalar value:
U+0000toU+10FFFFwithout the surrogatesU+D800-U+DFFF, which a GleamStringcannot hold.
A generator of values of type a. Build one with the functions of
this module.
pub opaque type Generator(a)
Values
pub fn bind(
generator: Generator(a),
with function: fn(a) -> Generator(b),
) -> Generator(b)
Use each value of generator to choose the generator of the result.
Shrinking shrinks the first value, then the second. The edge values
are those of the generators chosen for the edge values of
generator, and they shrink within the chosen generator only.
pub fn bit_array(bytes length: Range) -> Generator(BitArray)
Return a generator of byte-aligned bit arrays whose length in bytes
lies in bytes. Every byte is uniform in 0-255.
pub fn bit_array_unaligned(
bits length: Range,
) -> Generator(BitArray)
Return a generator of bit arrays whose length in bits lies in bits,
so the length need not be a multiple of 8. Use it for code that must
handle or reject a trailing partial byte.
pub fn bool() -> Generator(Bool)
Return a generator of True and False with equal chance. True
shrinks to False.
pub fn character(of characters: Characters) -> Generator(String)
Return a generator of one-character strings from characters. Values
shrink toward the lowest code point of the set.
pub fn dict_of(
keys: Generator(k),
values: Generator(v),
size size: Range,
) -> Generator(dict.Dict(k, v))
Return a generator of dicts with keys from keys and values from
values, built from a list of entries whose length lies in size.
A repeated key keeps its last value, so a dict can be smaller than
the lower bound of size.
pub fn element_of(values: List(a)) -> Generator(a)
Pick one of values with equal chance. Every value is an edge value.
Panics
Panics when values is empty.
pub fn filter(
generator: Generator(a),
keeping predicate: fn(a) -> Bool,
) -> Generator(a)
Keep only the values of generator that satisfy predicate, retrying
with a new seed for each rejected value. Shrinks and edge values that
fail predicate are dropped too.
Panics
Panics when predicate rejects 100 values in a row: the predicate is
too strict for the generator, which should then produce the wanted
values directly.
pub fn float(from from: Float, to to: Float) -> Generator(Float)
Return a generator of finite floats between from and to, both
included, in either order. Values shrink toward 0.0 when the range
contains it, otherwise toward the lower bound, and toward whole numbers
on the way. It never produces NaN or infinities; see float_special.
pub fn float_special() -> Generator(Float)
Return a generator of the IEEE 754 values float never produces: NaN,
positive and negative infinity, 0.0, -0.0, 1.0, the smallest
positive denormal and the largest finite double. Every value is also
an edge value.
On JavaScript the non-finite values are real NaN and infinities. On Erlang, which cannot build them, NaN and positive infinity are the largest finite double and negative infinity is its negation.
pub fn frequency(
weighted: List(#(Int, Generator(a))),
) -> Generator(a)
Pick a generator with a chance proportional to its weight, so
frequency([#(3, a), #(1, b)]) picks a three times as often as b.
Panics
Panics when weighted is empty or a weight is below 1.
pub fn int(range: Range) -> Generator(Int)
Return a generator of integers in range. Values shrink toward the
origin of the range. The edge values are 0, 1, -1, the bounds and
their neighbours, and the largest safe JavaScript integers, when they
lie in the range.
pub fn linear_range(from from: Int, to to: Int) -> Range
Return a range between from and to that grows with the size: at
size 0 it holds only its origin (0 when the range contains it,
otherwise the lower bound), and at the largest size it spans every
value. Early runs thus try small values first.
pub fn list_of(
generator: Generator(a),
length length: Range,
) -> Generator(List(a))
Return a generator of lists of values of generator, with a length
in length. Shrinking removes elements, largest blocks first and
never below the lower bound of length, then shrinks the remaining
elements. The edge values are [], one edge element and all edge
elements, when their lengths fit.
pub fn map(
generator: Generator(a),
with function: fn(a) -> b,
) -> Generator(b)
Apply function to every value of generator, its shrinks and its
edge values.
pub fn map2(
g1: Generator(a),
g2: Generator(b),
with function: fn(a, b) -> c,
) -> Generator(c)
Combine the values of two independent generators with function.
Shrinking shrinks one value while keeping the other; the edge values
are the combinations of their edge values, at most 16.
pub fn map3(
g1: Generator(a),
g2: Generator(b),
g3: Generator(c),
with function: fn(a, b, c) -> d,
) -> Generator(d)
Combine three independent generators like map2.
pub fn map4(
g1: Generator(a),
g2: Generator(b),
g3: Generator(c),
g4: Generator(d),
with function: fn(a, b, c, d) -> e,
) -> Generator(e)
Combine four independent generators like map2.
pub fn map5(
g1: Generator(a),
g2: Generator(b),
g3: Generator(c),
g4: Generator(d),
g5: Generator(e),
with function: fn(a, b, c, d, e) -> f,
) -> Generator(f)
Combine five independent generators like map2.
pub fn map6(
g1: Generator(a),
g2: Generator(b),
g3: Generator(c),
g4: Generator(d),
g5: Generator(e),
g6: Generator(f),
with function: fn(a, b, c, d, e, f) -> g,
) -> Generator(g)
Combine six independent generators like map2. For more values,
nest the tuples, for example map2(tuple3(..), tuple4(..), with: ..).
pub fn no_edges(generator: Generator(a)) -> Generator(a)
Remove the edge values of generator, so the runner starts with
random inputs.
pub fn one_of(generators: List(Generator(a))) -> Generator(a)
Pick one of generators with equal chance for every value. The edge
values are those of all the generators, at most 16.
Panics
Panics when generators is empty.
pub fn option_of(
generator: Generator(a),
) -> Generator(option.Option(a))
Return a generator of None and of Some with the values of
generator, Some three times as often. Some shrinks to None
first, then shrinks its value.
pub fn range(from from: Int, to to: Int) -> Range
Return the range of integers between from and to, both included,
at every size. The bounds may come in either order. Values shrink
toward 0 when the range contains it, otherwise toward the lower bound.
pub fn recursive(
base: Generator(a),
step: fn(Generator(a)) -> Generator(a),
) -> Generator(a)
Build a recursive generator, such as one for trees. At size 0 it is
base; at a larger size it is step applied to the same generator at
half the size, so the values stay finite.
pub fn result_of(
ok: Generator(a),
error: Generator(e),
) -> Generator(Result(a, e))
Return a generator of Ok with the values of ok and of Error with
the values of error, Ok three times as often.
pub fn return(value: a) -> Generator(a)
Return a generator that always produces value. It does not shrink,
and value is its only edge value.
pub fn sample(
generator: Generator(a),
count count: Int,
seed seed: Int,
) -> List(a)
Return count values of generator drawn from seed, without
running a property. The same seed always gives the same values. The
size grows from 0 to 99 over the values like in a run, and the edge
values are not included. A count below 1 gives [].
let digits = generator.int(generator.range(from: 0, to: 9))
generator.sample(digits, count: 3, seed: 42)
// -> three integers between 0 and 9
pub fn set_of(
generator: Generator(a),
size size: Range,
) -> Generator(set.Set(a))
Return a generator of sets of values of generator, built from a list
whose length lies in size. Repeated values collapse, so a set can be
smaller than the lower bound of size.
pub fn sized(builder: fn(Int) -> Generator(a)) -> Generator(a)
Build the generator from the current size, which grows from 0 on the
first random input to Config.max_size on the last. The edge values
are those of the generator built for size 0.
pub fn string(
of characters: Characters,
length length: Range,
) -> Generator(String)
Return a generator of strings of characters whose length in
characters lies in length. Shrinking removes characters, then
shrinks the remaining ones. The edge values include short strings
known to break parsers ("", " ", "\n", keywords, an emoji for
Unicode) when they fit length.
generator.string(
of: generator.Alphanumeric,
length: generator.range(from: 1, to: 16),
)
pub fn string_from(
characters generator: Generator(String),
length length: Range,
) -> Generator(String)
Return a generator of strings built from the strings of generator,
with length of them. Use it for character sets outside
Characters, for example element_of(["a", "b", "-"]).
pub fn tuple2(
g1: Generator(a),
g2: Generator(b),
) -> Generator(#(a, b))
Pair the values of two independent generators, shrinking like map2.
pub fn tuple3(
g1: Generator(a),
g2: Generator(b),
g3: Generator(c),
) -> Generator(#(a, b, c))
Combine three independent generators into a tuple.
pub fn tuple4(
g1: Generator(a),
g2: Generator(b),
g3: Generator(c),
g4: Generator(d),
) -> Generator(#(a, b, c, d))
Combine four independent generators into a tuple.
pub fn tuple5(
g1: Generator(a),
g2: Generator(b),
g3: Generator(c),
g4: Generator(d),
g5: Generator(e),
) -> Generator(#(a, b, c, d, e))
Combine five independent generators into a tuple.
pub fn tuple6(
g1: Generator(a),
g2: Generator(b),
g3: Generator(c),
g4: Generator(d),
g5: Generator(e),
g6: Generator(f),
) -> Generator(#(a, b, c, d, e, f))
Combine six independent generators into a tuple.