Skip to content

Instantly share code, notes, and snippets.

@jeffkreeftmeijer
Created August 26, 2017 20:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffkreeftmeijer/0e656c69be1e904cb10a824ee033e01b to your computer and use it in GitHub Desktop.
Save jeffkreeftmeijer/0e656c69be1e904cb10a824ee033e01b to your computer and use it in GitHub Desktop.
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