@@ -33,6 +33,7 @@ defmodule Display do
|
|||||||
|
|
||||||
def considering(module) do
|
def considering(module) do
|
||||||
IO.puts("Considering #{format_module(module)}...")
|
IO.puts("Considering #{format_module(module)}...")
|
||||||
|
module
|
||||||
end
|
end
|
||||||
|
|
||||||
def clear_screen do
|
def clear_screen do
|
||||||
@@ -42,36 +43,20 @@ defmodule Display do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp format_failure(%ExUnit.AssertionError{expr: expr}) do
|
defp format_failure(%{error: %ExUnit.AssertionError{expr: expr}, file: file, line: line}) do
|
||||||
"""
|
"""
|
||||||
#{format_cyan("Assertion failed in #{last_failure_location}")}
|
#{format_cyan("Assertion failed in #{file}:#{line}")}
|
||||||
#{format_red(Macro.to_string(expr))}
|
#{format_red(Macro.to_string(expr))}
|
||||||
"""
|
"""
|
||||||
end
|
end
|
||||||
|
|
||||||
defp format_failure(error) do
|
defp format_failure(%{error: error, file: file, line: line}) do
|
||||||
"""
|
"""
|
||||||
#{format_cyan("Error in #{last_failure_location}")}
|
#{format_cyan("Error in #{file}:#{line}")}
|
||||||
#{format_error(error)}
|
#{format_error(error)}
|
||||||
"""
|
"""
|
||||||
end
|
end
|
||||||
|
|
||||||
defp last_failure_location do
|
|
||||||
{file, line} = System.stacktrace
|
|
||||||
|> Enum.drop_while(&in_ex_unit?/1)
|
|
||||||
|> List.first
|
|
||||||
|> extract_file_and_line
|
|
||||||
|
|
||||||
"#{file}:#{line}"
|
|
||||||
end
|
|
||||||
|
|
||||||
defp in_ex_unit?({ExUnit.Assertions, _, _, _}), do: true
|
|
||||||
defp in_ex_unit?(_), do: false
|
|
||||||
|
|
||||||
defp extract_file_and_line({_, _, _, [file: file, line: line]}) do
|
|
||||||
{file, line}
|
|
||||||
end
|
|
||||||
|
|
||||||
defp format_error(error) do
|
defp format_error(error) do
|
||||||
trace = System.stacktrace |> Enum.take(2)
|
trace = System.stacktrace |> Enum.take(2)
|
||||||
format_red(Exception.format(:error, error, trace))
|
format_red(Exception.format(:error, error, trace))
|
||||||
|
|||||||
44
lib/execute.ex
Normal file
44
lib/execute.ex
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
defmodule Execute do
|
||||||
|
def run_module(module) do
|
||||||
|
Enum.reduce_while(module.all_koans, :passed, fn(koan, _) ->
|
||||||
|
module
|
||||||
|
|> run_koan(koan)
|
||||||
|
|> continue?
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp continue?(:passed), do: {:cont, :passed}
|
||||||
|
defp continue?(result), do: {:halt, result}
|
||||||
|
|
||||||
|
def run_koan(module, name, args \\ []) do
|
||||||
|
parent = self()
|
||||||
|
spawn(fn -> exec(module, name, args, parent) end)
|
||||||
|
receive do
|
||||||
|
:ok -> :passed
|
||||||
|
error -> {:failed, error, module, name}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp exec(module, name, args, parent) do
|
||||||
|
result = apply(module, name, args)
|
||||||
|
send parent, expand(result)
|
||||||
|
Process.exit(self(), :kill)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp expand(:ok), do: :ok
|
||||||
|
defp expand(error) do
|
||||||
|
{file, line} = System.stacktrace
|
||||||
|
|> Enum.drop_while(&in_ex_unit?/1)
|
||||||
|
|> List.first
|
||||||
|
|> extract_file_and_line
|
||||||
|
|
||||||
|
%{error: error, file: file, line: line}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp in_ex_unit?({ExUnit.Assertions, _, _, _}), do: true
|
||||||
|
defp in_ex_unit?(_), do: false
|
||||||
|
|
||||||
|
defp extract_file_and_line({_, _, _, [file: file, line: line]}) do
|
||||||
|
{file, line}
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -14,48 +14,35 @@ defmodule Runner do
|
|||||||
Tasks,
|
Tasks,
|
||||||
]
|
]
|
||||||
|
|
||||||
def koan?(module) do
|
def koan?(koan), do: Enum.member?(@modules, koan)
|
||||||
Enum.member?(@modules, module)
|
|
||||||
end
|
|
||||||
|
|
||||||
def run do
|
def run do
|
||||||
Options.initial_koan
|
Options.initial_koan
|
||||||
|>run
|
|>run
|
||||||
end
|
end
|
||||||
|
|
||||||
def run(start_module) when start_module in @modules do
|
def run(start_module) when start_module in @modules, do: run(start_module, @modules)
|
||||||
|
def run(start_module), do: Display.invalid_koan(start_module, @modules)
|
||||||
|
|
||||||
|
def run(start_module, modules) do
|
||||||
Display.clear_screen()
|
Display.clear_screen()
|
||||||
start_idx = Enum.find_index(@modules, &(&1 == start_module))
|
|
||||||
Enum.drop(@modules, start_idx)
|
modules
|
||||||
|> Enum.take_while(fn(mod) ->
|
|> Enum.drop_while( &(&1 != start_module))
|
||||||
run_module(mod) == :passed
|
|> Enum.take_while( &(run_module(&1) == :passed))
|
||||||
end)
|
|
||||||
end
|
end
|
||||||
def run(koan), do: Display.invalid_koan(koan, @modules)
|
|
||||||
|
|
||||||
def run_module(module) do
|
def run_module(module) do
|
||||||
Display.considering(module)
|
module
|
||||||
|
|> Display.considering
|
||||||
|
|> Execute.run_module
|
||||||
|
|> display
|
||||||
|
|
||||||
koans = module.all_koans
|
|
||||||
passed = Enum.take_while(koans, fn(name) ->
|
|
||||||
case run_koan(module, name) do
|
|
||||||
:passed -> true
|
|
||||||
{:failed, error, module, name} -> Display.show_failure(error, module, name)
|
|
||||||
false
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
|
|
||||||
if Enum.count(koans) == Enum.count(passed) do
|
|
||||||
:passed
|
|
||||||
else
|
|
||||||
:failed
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def run_koan(module, name, args \\ []) do
|
defp display({:failed, error, module, name}) do
|
||||||
case apply(module, name, args) do
|
Display.show_failure(error, module, name)
|
||||||
:ok -> :passed
|
:failed
|
||||||
error -> {:failed, error, module, name}
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
defp display(_), do: :passed
|
||||||
end
|
end
|
||||||
|
|||||||
13
test/executor_test.exs
Normal file
13
test/executor_test.exs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
defmodule ExecuteTest do
|
||||||
|
use ExUnit.Case
|
||||||
|
|
||||||
|
test "passes a koan" do
|
||||||
|
assert :passed == Execute.run_module(PassingKoan)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "stops at the first failing koan" do
|
||||||
|
{:failed, %{error: _, file: file, line: line}, SampleKoan, _name} = Execute.run_module(SampleKoan)
|
||||||
|
assert file == 'test/support/sample_koan.ex'
|
||||||
|
assert line == 5
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -232,6 +232,6 @@ defmodule KoansHarnessTest do
|
|||||||
end
|
end
|
||||||
|
|
||||||
def run_all(pairs, module) do
|
def run_all(pairs, module) do
|
||||||
Enum.map(pairs, fn ({koan, answer}) -> Runner.run_koan(module, koan, [answer]) end)
|
Enum.map(pairs, fn ({koan, answer}) -> Execute.run_koan(module, koan, [answer]) end)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
7
test/support/passing_koan.ex
Normal file
7
test/support/passing_koan.ex
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
defmodule PassingKoan do
|
||||||
|
use Koans
|
||||||
|
|
||||||
|
koan "hi there" do
|
||||||
|
assert 1 == 1
|
||||||
|
end
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user