Use a tuple to get indexed access to a arguments
This commit is contained in:
10
lib/koans.ex
10
lib/koans.ex
@@ -1,7 +1,8 @@
|
||||
defmodule Koans do
|
||||
defmacro koan(name, body) do
|
||||
compiled_name = String.to_atom(name)
|
||||
mangled_body = ASTMangler.expand(body, quote do: answer)
|
||||
expanded_answers = expand(ASTMangler.count(body))
|
||||
mangled_body = ASTMangler.expand(body, expanded_answers)
|
||||
quote do
|
||||
@koans unquote(compiled_name)
|
||||
def unquote(compiled_name)() do
|
||||
@@ -13,13 +14,18 @@ defmodule Koans do
|
||||
end
|
||||
end
|
||||
|
||||
def unquote(compiled_name)(answer) do
|
||||
def unquote(compiled_name)(answer) when is_list(answer) do
|
||||
converted = List.to_tuple(answer)
|
||||
unquote(mangled_body)
|
||||
:ok
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def expand(amount) do
|
||||
Enum.map(0..amount, fn (idx) -> {:elem, [context: Koans, import: Kernel], [{:converted, [], Koans}, idx]} end)
|
||||
end
|
||||
|
||||
defmacro __using__(_opts) do
|
||||
quote do
|
||||
@compile :nowarn_unused_vars
|
||||
|
||||
@@ -8,6 +8,11 @@ defmodule ASTManglerTest do
|
||||
assert {:+, [context: ASTManglerTest, import: Kernel], [1, 37]} == mangled
|
||||
end
|
||||
|
||||
test "Work with multiple different replacements" do
|
||||
[koan | _] = SampleKoan.all_koans
|
||||
assert :ok == apply(SampleKoan, koan, [{:multiple, [3,4]}])
|
||||
end
|
||||
|
||||
def complex_example do
|
||||
[head | tail] = [1,2,3,4]
|
||||
|
||||
|
||||
8
test/sample_koan_test.exs
Normal file
8
test/sample_koan_test.exs
Normal file
@@ -0,0 +1,8 @@
|
||||
defmodule SampleKoan do
|
||||
use Koans
|
||||
|
||||
koan "thinking more than once" do
|
||||
assert 3 == :__
|
||||
assert 4 == :__
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user