PropCheck.let
let
, go back to PropCheck module for more information.
Binds a generator to a name for use in another generator.
The binding
has the generator syntax x <- type
.
To produce an instance of this type, all appearances of the variables
in x
are replaced inside generator
by their corresponding values in a
randomly generated instance of type
. It's OK for the gen
part to
evaluate to a type - in that case, an instance of the inner type is
generated recursively.
iex> use PropCheck
iex> even = let n <- nat() do
...> n * 2
...> end
iex> quickcheck(
...> forall n <- even do
...> rem(n, 2) == 0
...> end)
true
If you require more than one type, put the pairs of variable and type into a list as shown in the example below.
iex> use PropCheck
iex> even_factor = let [n <- nat(), m <- nat()] do
...> n * m * 2
...> end
iex> quickcheck(
...> forall n <- even_factor do
...> rem(n, 2) == 0
...> end)
true
You also can refer to variables declared in the current let
scope with ^
.
Such variables are not allowed to form a dependency cycle.
You cannot apply ^
to variables declared outside of the let
macro.
iex> use PropCheck
iex> non_decreasing =
...> let [m <- integer(^l, :inf), l <- integer(), h <- integer(^m, :inf)] do
...> {l, m, h}
...> end
iex> quickcheck(
...> forall {l, m, h} <- non_decreasing do
...> l <= m and m <= h
...> end)
true
Similar to forall/2
, multiple types can also be put into tuples or lists:
iex> use PropCheck
iex> even_factor = let {n, m} <- {nat(), nat()} do
...> n * m * 2
...> end
iex> quickcheck(
...> forall n <- even_factor do
...> rem(n, 2) == 0
...> end)
true