elm-community/elm-check/2.0.1

NOTE: this package is not compatible with Elm 0.19.1

Check.Producer

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.

Common Producers

bool : Producer Bool

A producer for bool values.

int : Producer Int

A producer for int values.

rangeInt : Int -> Int -> Producer Int

A producer for int values within between a given minimum and maximum value, inclusive. Shrunken values will also be within the range.

float : Producer Float

A producer for float values. It will never produce NaN, Infinity, or -Infinity.

rangeFloat : Float -> Float -> Producer Float

A producer for float values within between a given minimum and maximum value, inclusive. Shrunken values will also be within the range.

percentage : Producer Float

A producer for percentage values. Generates random floats between 0.0 and 1.0.

string : Producer String

A producer for string values. Generates random printable ascii strings whose length is between 0 and 10.

maybe : Producer a -> Producer (Maybe a)

Given a producer of a type, create a producer of a maybe for that type.

result : Producer error -> Producer value -> Producer (Result error value)

Given producers for an error type and a success type, createa a producer for a result.

list : Producer a -> Producer (List a)

Given a producer of a type, create a producer of a list of that type. Generates random lists of varying length, favoring shorter lists.

array : Producer a -> Producer (Array a)

Given a producer of a type, create a producer of an array of that type. Generates random arrays of varying length, favoring shorter arrays.

Tuple Producers

If your expected and actual functions need more than one input, pass them in as a tuple.

tuple : ( Producer a, Producer b ) -> Producer ( a, b )

Turn a tuple of producers into a producer of tuples.

tuple3 : ( Producer a, Producer b, Producer c ) -> Producer ( a, b, c )

Turn a 3-tuple of producers into a producer of 3-tuples.

( Producer a, Producer b, Producer c, Producer d )
-> Producer ( a, b, c, d )

Turn a 4-tuple of producers into a producer of 4-tuples.

( Producer a, Producer b, Producer c, Producer d, Producer e )
-> Producer ( a, b, c, d, e )

Turn a 5-tuple of producers into a producer of 5-tuples.

Working with Producers

type alias Producer a =
{ generator : Generator a
, shrinker : Shrinker a
}

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 shrinker
filter : (a -> Bool) -> Producer a -> Producer a

Filter 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 : (a -> b) -> (b -> a) -> Producer a -> Producer b

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 -> b) -> Producer a -> Producer b

Map a function over an producer. This works exactly like convert, except it does not require an inverse function, and consequently does no shrinking.

Uncommon Producers

unit : Producer ()

A producer for the unit value. Unit is a type with only one value, commonly used as a placeholder.

order : Producer Order

A producer for order values.

Character Producers

char : Producer Char

A producer for char values. Generates random ascii chars disregarding the control characters.

upperCaseChar : Producer Char

A producer for uppercase char values.

lowerCaseChar : Producer Char

A producer for lowercase char values.

ascii : Producer Char

A producer for ASCII char values.

unicode : Producer Char

A producer for unicode char values.