Add argument to koan to allow it to be tested

This commit is contained in:
Felipe Sere
2016-03-09 23:31:27 +00:00
parent a212f674e0
commit fdb87b529f
5 changed files with 83 additions and 2 deletions

24
lib/ast_manger.ex Normal file
View File

@@ -0,0 +1,24 @@
defmodule ASTMangler do
def expand([do: thing], replacement) do
[do: expand(thing, replacement)]
end
def expand({fun, context, args}, replacement) do
new_args = replace(args, replacement)
{fun, context, new_args}
end
def replace(args, b) do
args
|> Enum.find_index(fn(x) -> x == :__ end)
|> replace(args, b)
end
def replace(nil, [], _b), do: []
def replace(nil, [ast], b) do
[expand(ast,b)]
end
def replace(index, list, b) do
List.update_at(list, index, fn(_)-> b end)
end
end

View File

@@ -1,11 +1,17 @@
defmodule Koans do
defmacro koan(name, body) do
compiled_name = String.to_atom(name)
x = quote do: answer
mangled_body = ASTMangler.expand(body, x)
quote do
@koans unquote(compiled_name)
def unquote(compiled_name)() do
def unquote(compiled_name)(answer \\ :nothing) do
try do
unquote(body)
if answer == :nothing do
unquote(body)
else
unquote(mangled_body)
end
:ok
rescue
e in _ -> e

View File

@@ -40,6 +40,13 @@ defmodule Runner do
end
end
def test_single_koan(module, name, answer) do
case apply(module, name, []) do
:ok -> :passed
error -> IO.inspect error
end
end
def run_koan(module, name) do
case apply(module, name, []) do
:ok -> :passed