Rename and clean the ASTMangler to Blanks
This commit is contained in:
@@ -1,25 +0,0 @@
|
||||
defmodule ASTMangler do
|
||||
|
||||
def expand([do: remainder], replacements) when is_list(replacements) do
|
||||
[do: expand(remainder, replacements)]
|
||||
end
|
||||
def expand(ast, replacements) when is_list(replacements) do
|
||||
{node, _acc} = Macro.prewalk(ast, replacements, fn(node, acc) -> pre(node, acc) end)
|
||||
node
|
||||
end
|
||||
def expand(ast, replacement) do
|
||||
expand(ast, [replacement])
|
||||
end
|
||||
|
||||
def pre(:__, [first | remainder]), do: {first, remainder}
|
||||
def pre(node, acc), do: {node, acc}
|
||||
|
||||
|
||||
def count(ast) do
|
||||
{_node, acc} = Macro.prewalk(ast, 0, fn(node, acc) -> count(node, acc) end)
|
||||
acc
|
||||
end
|
||||
|
||||
def count(:__, acc), do: {node, acc+1}
|
||||
def count(node, acc), do: {node, acc}
|
||||
end
|
21
lib/blanks.ex
Normal file
21
lib/blanks.ex
Normal file
@@ -0,0 +1,21 @@
|
||||
defmodule Blanks do
|
||||
def replace(ast, replacement) when not is_list(replacement), do: replace(ast, [replacement])
|
||||
def replace([do: ast], replacements), do: [do: replace(ast, replacements)]
|
||||
def replace(ast, replacements) do
|
||||
ast
|
||||
|> Macro.prewalk(replacements, &pre/2)
|
||||
|> elem(0)
|
||||
end
|
||||
|
||||
def pre(:__, [first | remainder]), do: {first, remainder}
|
||||
def pre(node, acc), do: {node, acc}
|
||||
|
||||
def count(ast) do
|
||||
ast
|
||||
|> Macro.prewalk(0, &count/2)
|
||||
|> elem(1)
|
||||
end
|
||||
|
||||
def count(:__, acc), do: {node, acc+1}
|
||||
def count(node, acc), do: {node, acc}
|
||||
end
|
10
lib/koans.ex
10
lib/koans.ex
@@ -1,7 +1,7 @@
|
||||
defmodule Koans do
|
||||
defmacro koan(name, body) do
|
||||
compiled_name = String.to_atom(name)
|
||||
number_of_args = ASTMangler.count(body)
|
||||
number_of_args = Blanks.count(body)
|
||||
quote do
|
||||
@koans unquote(compiled_name)
|
||||
|
||||
@@ -20,7 +20,7 @@ defmodule Koans do
|
||||
|
||||
defmacro generate_test_method(_name, 0, _body), do: false
|
||||
defmacro generate_test_method(name, 1, body) do
|
||||
single_var = ASTMangler.expand(body, Macro.var(:answer, Koans))
|
||||
single_var = Blanks.replace(body, Macro.var(:answer, Koans))
|
||||
quote do
|
||||
def unquote(name)(answer) do
|
||||
converted = {answer}
|
||||
@@ -30,8 +30,8 @@ defmodule Koans do
|
||||
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)
|
||||
answer_placeholders = create_vars(number_of_args)
|
||||
multi_var = Blanks.replace(body, answer_placeholders)
|
||||
quote do
|
||||
def unquote(name)({:multiple, answers}) do
|
||||
converted = List.to_tuple(answers)
|
||||
@@ -41,7 +41,7 @@ defmodule Koans do
|
||||
end
|
||||
end
|
||||
|
||||
def expand(amount) do
|
||||
def create_vars(amount) do
|
||||
Enum.map(0..amount, fn (idx) -> quote do: elem(converted, unquote(idx)) end)
|
||||
end
|
||||
|
||||
|
@@ -1,11 +1,11 @@
|
||||
defmodule ASTManglerTest do
|
||||
defmodule BlanksTest do
|
||||
use ExUnit.Case, async: true
|
||||
|
||||
test "simple replacement" do
|
||||
ast = quote do: 1 + :__
|
||||
|
||||
mangled = ASTMangler.expand(ast, 37)
|
||||
assert {:+, [context: ASTManglerTest, import: Kernel], [1, 37]} == mangled
|
||||
mangled = Blanks.replace(ast, 37)
|
||||
assert {:+, [context: BlanksTest, import: Kernel], [1, 37]} == mangled
|
||||
end
|
||||
|
||||
test "Work with multiple different replacements" do
|
||||
@@ -31,24 +31,24 @@ defmodule ASTManglerTest do
|
||||
test "complex example" do
|
||||
ast = [do: {:assert, [line: 5], [{:==, [line: 5], [true, :__]}]}]
|
||||
|
||||
assert [do: {:assert, [line: 5], [{:==, [line: 5], [true, true]}]}] == ASTMangler.expand(ast, true)
|
||||
assert [do: {:assert, [line: 5], [{:==, [line: 5], [true, true]}]}] == Blanks.replace(ast, true)
|
||||
end
|
||||
|
||||
test "multiple arguments" do
|
||||
ast = [do: {:assert, [line: 5], [{:==, [line: 5], [:__, :__]}]}]
|
||||
|
||||
assert [do: {:assert, [line: 5], [{:==, [line: 5], [true, false]}]}] == ASTMangler.expand(ast, [true, false])
|
||||
assert [do: {:assert, [line: 5], [{:==, [line: 5], [true, false]}]}] == Blanks.replace(ast, [true, false])
|
||||
end
|
||||
|
||||
test "counts simple blanks" do
|
||||
ast = quote do: 1 + :__
|
||||
|
||||
assert ASTMangler.count(ast) == 1
|
||||
assert Blanks.count(ast) == 1
|
||||
end
|
||||
|
||||
test "counts multiple blanks" do
|
||||
ast = [do: {:assert, [line: 5], [{:==, [line: 5], [:__, :__]}]}]
|
||||
|
||||
assert ASTMangler.count(ast) == 2
|
||||
assert Blanks.count(ast) == 2
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user