TypeCheck.Type.StreamData (TypeCheck v0.10.1) View Source

Transforms types into StreamData generators.

With the exception of wrap_with_gen/2, methods in this module are only compiled when the optional dependency :stream_data is added to your project's dependencies.

Link to this section Summary

Functions

When given a type, it is transformed to a StreamData generator that can be used in a property test.

Customizes a type with a custom generator.

Link to this section Functions

Link to this function

arbitrary_primitive_type_gen()

View Source

When given a type, it is transformed to a StreamData generator that can be used in a property test.

iex> import TypeCheck.Type.StreamData
iex> generator = TypeCheck.Type.build({:ok | :error, integer()}) |> to_gen()
iex> StreamData.seeded(generator, 42) |> Enum.take(10)
[
{:ok, -1},
{:ok, 2},
{:ok, -2},
{:ok, -4},
{:ok, 1},
{:ok, 1},
{:ok, 2},
{:ok, 4},
{:ok, -7},
{:ok, 5}
]
Link to this function

wrap_with_gen(type, generator_function)

View Source

Customizes a type with a custom generator.

generator_function can be a arity-zero function (in which case it should simply return a StreamData generator)

or a arity-one function, in which case it is passed the value that would be generated by default and it can be altered by e.g. using StreamData.map/2 or StreamData.bind/2.

Note that these functions must be of the form &Module.function/arity because this is the only form of function capture that can be stored at compile-time.

Example:

iex> defmodule IntString do
...>   use TypeCheck
...>   import TypeCheck.Type.StreamData
...>   @type! t() :: ((val :: binary()) when Integer.parse(val) != :error)
...>                 |> wrap_with_gen(&IntString.gen/0)
...>
...>   def gen() do
...>     StreamData.integer()
...>     |> StreamData.map(&to_string/1)
...>   end
...> end
...>
...> IntString.t() |> TypeCheck.Type.StreamData.to_gen() |> StreamData.seeded(42) |> Enum.take(10)
["0", "2", "1", "-3", "-5", "-4", "-3", "-4", "3", "-6"]