Merge pull request #25 from ukutaht/clear-screen
Clear screen on every run.
This commit is contained in:
@@ -2,8 +2,9 @@ defmodule Display do
|
||||
alias IO.ANSI
|
||||
@current_dir File.cwd!
|
||||
|
||||
def show_failure(%{expr: expr}, module, name) do
|
||||
IO.puts("")
|
||||
def show_failure(%{expr: expr}, module, name, options) do
|
||||
clear_screen(options)
|
||||
|
||||
IO.puts("Now meditate upon #{display_module(module)}")
|
||||
IO.puts("---------------------------------------")
|
||||
IO.puts(format_cyan(display_failed_assertion(module, expr)))
|
||||
@@ -15,9 +16,10 @@ defmodule Display do
|
||||
IO.puts("Considering #{display_module(module)}...")
|
||||
end
|
||||
|
||||
def before_run do
|
||||
IO.puts("")
|
||||
IO.puts("")
|
||||
def clear_screen(%{ clear_screen: false }), do: false
|
||||
def clear_screen(_) do
|
||||
IO.puts(ANSI.clear)
|
||||
IO.puts(ANSI.home)
|
||||
end
|
||||
|
||||
def display_failed_assertion(module, expr) do
|
||||
|
||||
@@ -1,11 +1,15 @@
|
||||
defmodule Mix.Tasks.Meditate do
|
||||
use Mix.Task
|
||||
alias Options
|
||||
|
||||
def run(_) do
|
||||
def run(args) do
|
||||
Application.ensure_all_started(:elixir_koans)
|
||||
Code.compiler_options(ignore_module_conflict: true)
|
||||
Watcher.start
|
||||
Runner.run
|
||||
|
||||
options = Options.parse(args)
|
||||
Runner.run(options)
|
||||
|
||||
:timer.sleep(:infinity)
|
||||
end
|
||||
end
|
||||
|
||||
16
lib/options.ex
Normal file
16
lib/options.ex
Normal file
@@ -0,0 +1,16 @@
|
||||
defmodule Options do
|
||||
|
||||
def parse([]), do: defaults
|
||||
def parse(args) do
|
||||
Enum.reduce(args, defaults, fn(arg, acc) ->
|
||||
Map.merge(acc, parse_argument(arg))
|
||||
end)
|
||||
end
|
||||
|
||||
def parse_argument("--clear-screen"), do: %{ clear_screen: true}
|
||||
def parse_argument(_), do: %{}
|
||||
|
||||
defp defaults do
|
||||
%{ clear_screen: false}
|
||||
end
|
||||
end
|
||||
@@ -8,27 +8,25 @@ defmodule Runner do
|
||||
Enums
|
||||
]
|
||||
|
||||
def run do
|
||||
run(Equalities)
|
||||
def run(options) do
|
||||
run(Equalities, options)
|
||||
end
|
||||
|
||||
def run(start_module) do
|
||||
Display.before_run
|
||||
|
||||
def run(start_module, options) do
|
||||
start_idx = Enum.find_index(@modules, &(&1 == start_module))
|
||||
Enum.drop(@modules, start_idx)
|
||||
|> Enum.take_while(fn(mod) ->
|
||||
run_module(mod) == :passed
|
||||
run_module(mod, options) == :passed
|
||||
end)
|
||||
end
|
||||
|
||||
def run_module(module) do
|
||||
def run_module(module, options) do
|
||||
Display.considering(module)
|
||||
|
||||
koans = extract_koans_from(module)
|
||||
|
||||
passed = Enum.take_while(koans, fn(name) ->
|
||||
run_koan(module, name) == :passed
|
||||
run_koan(module, name, options) == :passed
|
||||
end)
|
||||
|
||||
if Enum.count(koans) == Enum.count(passed) do
|
||||
@@ -38,11 +36,11 @@ defmodule Runner do
|
||||
end
|
||||
end
|
||||
|
||||
def run_koan(module, name) do
|
||||
def run_koan(module, name, options) do
|
||||
case apply(module, name, []) do
|
||||
:ok -> :passed
|
||||
error ->
|
||||
Display.show_failure(error, module, name)
|
||||
Display.show_failure(error, module, name, options)
|
||||
:failed
|
||||
end
|
||||
end
|
||||
|
||||
18
test/options_test.exs
Normal file
18
test/options_test.exs
Normal file
@@ -0,0 +1,18 @@
|
||||
defmodule OptionTest do
|
||||
use ExUnit.Case, async: true
|
||||
|
||||
test "has default options" do
|
||||
options = Options.parse([])
|
||||
refute Map.fetch!(options, :clear_screen)
|
||||
end
|
||||
|
||||
test "override clearing of screen" do
|
||||
options = Options.parse(["--clear-screen"])
|
||||
assert Map.fetch!(options, :clear_screen)
|
||||
end
|
||||
|
||||
test "ignores unknown options" do
|
||||
options = Options.parse(["--foo"])
|
||||
refute Map.fetch!(options, :clear_screen)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user