Generate test methods depending on the number of arguments.
This commit is contained in:
31
lib/koans.ex
31
lib/koans.ex
@@ -1,10 +1,12 @@
|
|||||||
defmodule Koans do
|
defmodule Koans do
|
||||||
defmacro koan(name, body) do
|
defmacro koan(name, body) do
|
||||||
compiled_name = String.to_atom(name)
|
compiled_name = String.to_atom(name)
|
||||||
expanded_answers = expand(ASTMangler.count(body))
|
number_of_args = ASTMangler.count(body)
|
||||||
mangled_body = ASTMangler.expand(body, expanded_answers)
|
|
||||||
quote do
|
quote do
|
||||||
@koans unquote(compiled_name)
|
@koans unquote(compiled_name)
|
||||||
|
|
||||||
|
generate_test_method(unquote(compiled_name), unquote(number_of_args), unquote(body))
|
||||||
|
|
||||||
def unquote(compiled_name)() do
|
def unquote(compiled_name)() do
|
||||||
try do
|
try do
|
||||||
unquote(body)
|
unquote(body)
|
||||||
@@ -13,17 +15,34 @@ defmodule Koans do
|
|||||||
e in _ -> e
|
e in _ -> e
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def unquote(compiled_name)(answer) when is_list(answer) do
|
defmacro generate_test_method(_name, 0, _body), do: false
|
||||||
converted = List.to_tuple(answer)
|
defmacro generate_test_method(name, 1, body) do
|
||||||
unquote(mangled_body)
|
single_var = ASTMangler.expand(body, Macro.var(:answer, Koans))
|
||||||
|
quote do
|
||||||
|
def unquote(name)(answer) do
|
||||||
|
converted = {answer}
|
||||||
|
unquote(single_var)
|
||||||
|
:ok
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
defmacro generate_test_method(name, number_of_args, body) do
|
||||||
|
answer_placeholders = expand(number_of_args)
|
||||||
|
multi_var = ASTMangler.expand(body, answer_placeholders)
|
||||||
|
quote do
|
||||||
|
def unquote(name)({:multiple, answers}) do
|
||||||
|
converted = List.to_tuple(answers)
|
||||||
|
unquote(multi_var)
|
||||||
:ok
|
:ok
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def expand(amount) do
|
def expand(amount) do
|
||||||
Enum.map(0..amount, fn (idx) -> {:elem, [context: Koans, import: Kernel], [{:converted, [], Koans}, idx]} end)
|
Enum.map(0..amount, fn (idx) -> quote do: elem(converted, unquote(idx)) end)
|
||||||
end
|
end
|
||||||
|
|
||||||
defmacro __using__(_opts) do
|
defmacro __using__(_opts) do
|
||||||
|
|||||||
Reference in New Issue
Block a user