Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
POC for type spec tests with StreamData.
defmodule Math do
@spec square(integer) :: integer
def square(a) do
a * a
end
end
defmodule TypeTest do
use ExUnit.Case
import PropertyTest
property "types" do
module = Math
specs = Kernel.Typespec.beam_specs(module)
for {{name, _}, _} = spec <- specs, name != :__info__ do
{{function_name, _arity}, specs} = spec
for {:type, _, :fun, types} <- specs do
[{:type, _, :product, arguments}, result] = types
result_type = type(result)
[argument] = arguments
argument_gen = argument
|> type
|> generator
check all a <- argument_gen do
result = :erlang.apply(module, function_name, [a])
checker(result_type).(result)
end
end
end
end
def generator(:integer), do: StreamData.integer()
def checker(:integer), do: fn(result) ->
assert is_integer(result)
end
def type({:type, _, type, _}), do: type
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.