Skip to content

Commit

Permalink
the PoC works
Browse files Browse the repository at this point in the history
  • Loading branch information
Qqwy committed Sep 23, 2021
1 parent 326e184 commit 85f583a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 11 deletions.
15 changes: 12 additions & 3 deletions lib/debug_example.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,17 @@ defmodule DebugExample do

@type! myparam :: integer()

@spec! stringify(myparam) :: binary()
def stringify(val) do
to_string(val)
@spec! stringify(myparam, boolean()) :: binary()
def stringify(val, _bool) do
if val > 10 do
val
else
to_string(val)
end
end

@spec! average(list(number())) :: number()
def average(vals) do
Enum.sum(vals) / Enum.count(vals)
end
end
32 changes: 25 additions & 7 deletions lib/type_check/ex_unit.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,41 @@ defmodule TypeCheck.ExUnit do
for {name, arity} <- module.__type_check__(:specs) do
spec = TypeCheck.Spec.lookup!(module, name, arity)
body = TypeCheck.ExUnit.__build_spectest__(module, name, arity, spec)
IO.puts(Macro.to_string(body))

# quote bind_quoted: [module: module, name: name, arity: arity, spec: spec, env: env, body: body] do
test_name = ExUnit.Case.register_test(env, :spectest, "#{name}/#{arity}", [:spectest])
IO.inspect(test_name)
def unquote(test_name), do: 42
# end
test_name = ExUnit.Case.register_test(env, :spectest, "#{TypeCheck.Inspect.inspect(spec)}", [:spectest])
def unquote(test_name)(_) do
import TypeCheck.Protocols.ToStreamData
unquote(body)
end
end
end

[req, tests]
quote do
unquote(req)
unquote(tests)
end
end

@doc false
def __build_spectest__(module, name, arity, spec) do
generators =
spec.param_types
# |> Enum.map(&TypeCheck.Protocols.ToStreamData.to_gen/1)
|> Enum.zip(Macro.generate_arguments(arity, module))
|> Enum.map(fn {type, arg} ->
quote do
unquote(arg) <- to_gen(unquote(Macro.escape(type)))
end
end)

quote do
require ExUnitProperties
ExUnitProperties.check all {unquote_splicing(Macro.generate_arguments(arity, module))} <- TypeCheck.Protocols.ToStreamData.to_gen(unquote(spec)) do
# arguments_generator = TypeCheck.Protocols.ToStreamData.to_gen(unquote(Macro.escape(spec)))
# ExUnitProperties.check all {unquote_splicing(Macro.generate_arguments(arity, module))} <- arguments_generator do
# unquote(module).unquote(name)(unquote_splicing(Macro.generate_arguments(arity, module)))
# end
ExUnitProperties.check all unquote_splicing(generators) do
unquote(module).unquote(name)(unquote_splicing(Macro.generate_arguments(arity, module)))
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/type_check/spec.ex
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ defmodule TypeCheck.Spec do
def to_gen(s) do
s.param_types
|> Enum.map(&TypeCheck.Protocols.ToStreamData.to_gen/1)
|> StreamData.fixed_list()
|> List.to_tuple
|> StreamData.tuple
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions test/spectest_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
defmodule SpectestTest do
use ExUnit.Case
import TypeCheck.ExUnit

spectest DebugExample
end

0 comments on commit 85f583a

Please sign in to comment.