RTypes v0.3.2 RTypes.Generator behaviour View Source
Generator module provides the make/4 function and make/2 macro
which allow to atomatically derive a data generator to be used with
property-based frameworks.
For example, to write a unit test for a pure function with a given
spec to use with StreamData framework one could write something
along the lines:
defmodule MyTest do
use ExUnit.Case
use ExUnitProperties
require RTypes
require RTypes.Generator, as: Generator
# @spec f(arg_type) :: result_type
arg_type_gen = Generator.make(arg_type, Generator.StreamData)
result_type? = RTypes.make_predicate(result_type)
property "for any parameter `f/1` returs value of `result_type`" do
check all value <- arg_type_gen do
assert result_type?.(f(value))
end
end
end
The above would generate 100 (by default) values that belong to
arg_type and verify that the result belongs to result_type.
Link to this section Summary
Functions
Make a data generator for the given type and backend.
Make a data generator for the given type AST and backend.
Link to this section Functions
Make a data generator for the given type and backend.
typeis a literal type expression, e.g.:inet.port_number()backendis a module implementing a particular property-based backend, e.g.RTypes.Generator.StreamData
make(mod, type_name, type_args, backend)
View Sourcemake(module(), atom(), [RTypes.Extractor.type()], module()) :: generator when generator: term()
Make a data generator for the given type AST and backend.
modis a module name implementing a type,type_nameis the type nametype_argsis a list of type arguments in AST formbackenda module implementing a particular property-based backend, e.g.RTypes.Generator.StreamData
For example
iex> alias RTypes.Generator.StreamData, as: SD
iex> g = RTypes.Generator.make(:inet, :port_number, [], SD)
Link to this section Callbacks
derive(arg1)
View Sourcederive(RTypes.Extractor.type()) :: generator when generator: term()