This is a library of Producers you can use to supply values to your tests.
You can typically pick out which ones you need according to their types.
A Producer a knows how to create values of type a. It can create them
randomly, and it can shrink them to more minimal values. Producers can be
filtered and mapped over.
A producer for int values.
A producer for int values within between a given minimum and maximum value, inclusive. Shrunken values will also be within the range.
A producer for float values. It will never produce NaN, Infinity, or -Infinity.
A producer for float values within between a given minimum and maximum value, inclusive. Shrunken values will also be within the range.
A producer for percentage values. Generates random floats between 0.0 and
1.0.
A producer for string values. Generates random printable ascii strings whose length is between 0 and 10.
Given a producer of a type, create a producer of a maybe for that type.
Given producers for an error type and a success type, createa a producer for a result.
Given a producer of a type, create a producer of a list of that type. Generates random lists of varying length, favoring shorter lists.
Given a producer of a type, create a producer of an array of that type. Generates random arrays of varying length, favoring shorter arrays.
If your expected and actual functions need more than one input, pass them in as a tuple.
Turn a tuple of producers into a producer of tuples.
Turn a 3-tuple of producers into a producer of 3-tuples.
Turn a 4-tuple of producers into a producer of 4-tuples.
Turn a 5-tuple of producers into a producer of 5-tuples.
An Producer type is a
Random
Generator paired with a shrinking strategy, or Shrinker. Shrinkers are defined
in elm-community/shrink.
You will need to be familiar with both libraries to write custom producers for your own types.
Here is an example for a record:
type alias Position =
{ x : Int, y : Int }
position : Producer Position
position =
Producer
(Random.map2 Position (Random.int 0 1919) (Random.int 0 1079))
(\{ x, y } -> Shrink.map Position (Shrink.int x) `Shrink.andMap` (Shrink.int y))Here is an example for a union type:
type Question
= Name String
| Age Int
question =
let
generator =
Random.bool `Random.andThen` (\b ->
if b then
Random.map Name string.generator
else
Random.map Age (Random.int 0 120)
)
shrinker question =
case question of
Name n ->
Shrink.string n |> Shrink.map Name
Age i ->
Shrink.int i |> Shrink.map Age
in
Producer generator shrinkerFilter the values from a Producer. The resulting Producer will only generate random test values or shrunken values that satisfy the predicate. The predicate must be satisfiable.
Convert the output of one producer to another type. This is useful if you're testing a function that expects a large model record, but you only need to randomize a few fields. You might do this several different ways for a single model, so you generate and shrink only the fields relevant to each test.
type alias Person =
{ first : String, last : String, age : String }
spy : Producer Person
spy = convert (\age -> Person "James" "Bond" age) .age (rangeInt 0 120)In order for shrinking to work, you need to pass an inverse function of the function being mapped.
Map a function over an producer. This works exactly like convert,
except it does not require an inverse function, and consequently does no
shrinking.
A producer for the unit value. Unit is a type with only one value, commonly used as a placeholder.
A producer for order values.
A producer for char values. Generates random ascii chars disregarding the control characters.
A producer for uppercase char values.
A producer for lowercase char values.
A producer for ASCII char values.
A producer for unicode char values.
A producer for bool values.