Cleanup how koan modules get their intros

This commit is contained in:
Felipe Sere
2016-05-10 22:47:47 +01:00
parent 7b494e16df
commit 149cc47195
3 changed files with 31 additions and 2 deletions

View File

@@ -82,11 +82,32 @@ defmodule Koans do
end
defmacro __before_compile__(env) do
koans = Module.get_attribute(env.module, :koans) |> Enum.reverse
koans = koans(env)
intro = extract_into(env)
quote do
def all_koans do
unquote(koans)
end
def intro do
unquote(intro)
end
end
end
defp koans(env) do
env.module
|> Module.get_attribute(:koans)
|> Enum.reverse
end
defp extract_into(env) do
env.module
|> Module.get_attribute(:intro)
|> default("")
|> String.strip
end
defp default(nil, n), do: n
defp default(n, _), do: n
end

View File

@@ -8,6 +8,10 @@ defmodule ExecuteTest do
test "stops at the first failing koan" do
{:failed, %{file: file, line: line}, SampleKoan, _name} = Execute.run_module(SampleKoan)
assert file == 'test/support/sample_koan.ex'
assert line == 4
assert line == 8
end
test "can access intro" do
assert SampleKoan.intro == "There is something"
end
end

View File

@@ -1,6 +1,10 @@
defmodule SampleKoan do
use Koans
@intro """
There is something
"""
koan "Thinking more than once" do
assert 3 == ___
assert 4 == ___