Show how to test koans for Equalities
This commit is contained in:
@@ -1,24 +0,0 @@
|
||||
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
|
||||
@@ -9,6 +9,7 @@ defmodule ASTMangler do
|
||||
end
|
||||
def expand(n, _), do: n
|
||||
|
||||
def replace(nil, _), do: nil
|
||||
def replace(args, b) do
|
||||
args
|
||||
|> Enum.find_index(fn(x) -> x == :__ end)
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
defmodule Koans do
|
||||
defmacro koan(name, body) do
|
||||
compiled_name = String.to_atom(name)
|
||||
x = quote do: answer
|
||||
mangled_body = ASTMangler.expand(body, x)
|
||||
mangled_body = ASTMangler.expand(body, quote do: answer)
|
||||
quote do
|
||||
@koans unquote(compiled_name)
|
||||
def unquote(compiled_name)(answer \\ :nothing) do
|
||||
|
||||
@@ -40,13 +40,6 @@ defmodule Runner do
|
||||
end
|
||||
end
|
||||
|
||||
def test_single_koan(module, name, answer) do
|
||||
case apply(module, name, [answer]) do
|
||||
:ok -> :passed
|
||||
error -> IO.inspect error
|
||||
end
|
||||
end
|
||||
|
||||
def run_koan(module, name) do
|
||||
case apply(module, name, []) do
|
||||
:ok -> :passed
|
||||
|
||||
@@ -23,10 +23,6 @@ defmodule ASTManglerTest do
|
||||
end
|
||||
end
|
||||
|
||||
test "something" do
|
||||
n = 1
|
||||
end
|
||||
|
||||
test "complex example" do
|
||||
ast = [do: {:assert, [line: 5], [{:==, [line: 5], [true, :__]}]}]
|
||||
|
||||
|
||||
22
test/koans_harness_test.exs
Normal file
22
test/koans_harness_test.exs
Normal file
@@ -0,0 +1,22 @@
|
||||
defmodule KoansHarnessTest do
|
||||
use ExUnit.Case
|
||||
|
||||
test "Equalities" do
|
||||
answers = [true, 1, 2, 1, :something]
|
||||
|
||||
assert all_pass?(Equalities, answers) == [:passed, :passed, :passed, :passed, :passed]
|
||||
end
|
||||
|
||||
def all_pass?(module, answers) do
|
||||
module.all_koans
|
||||
|> Enum.zip(answers)
|
||||
|> Enum.map(fn({koan, answer}) -> KoansHarnessTest.test_single_koan(module, koan, answer) end)
|
||||
end
|
||||
|
||||
def test_single_koan(module, name, answer) do
|
||||
case apply(module, name, [answer]) do
|
||||
:ok -> :passed
|
||||
error -> {:error, name, error}
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,11 +0,0 @@
|
||||
defmodule PowerTest do
|
||||
use ExUnit.Case
|
||||
|
||||
test "something" do
|
||||
koans = Equalities.all_koans
|
||||
answers = [true, 1]
|
||||
|
||||
combined = Enum.zip(koans, answers)
|
||||
Enum.each(combined, fn({koan, answer}) -> assert :passed == Runner.test_single_koan(Equalities, koan, answer) end)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user