Add argument to koan to allow it to be tested
This commit is contained in:
24
lib/ast_manger.ex
Normal file
24
lib/ast_manger.ex
Normal 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
|
||||
10
lib/koans.ex
10
lib/koans.ex
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user