Introduce --koan=<name> to run a specific koan module.

This commit is contained in:
Felipe Sere
2016-04-04 20:57:17 +01:00
parent 734e608b07
commit 88ee80b73d
4 changed files with 35 additions and 3 deletions

View File

@@ -2,6 +2,23 @@ defmodule Display do
alias IO.ANSI
@current_dir File.cwd!
def invalid_koan(koan, modules) do
koans_names = module_names(modules)
IO.puts("Did not find koan #{name(koan)} in " <> koans_names )
exit(:normal)
end
defp module_names(modules) do
modules
|> Enum.map(&Atom.to_string/1)
|> Enum.map(&name/1)
|> Enum.join(", ")
|> format_red
end
defp name("Elixir." <> module), do: module
defp name(module), do: name(Atom.to_string(module))
def show_failure(failure, module, name) do
IO.puts("Now meditate upon #{format_module(module)}")
IO.puts("---------------------------------------")

View File

@@ -1,6 +1,7 @@
defmodule Options do
@defaults %{
clear_screen: false
clear_screen: false,
initial_koan: Equalities,
}
def start(args) do
@@ -13,12 +14,19 @@ defmodule Options do
end)
end
def initial_koan() do
Agent.get(__MODULE__, fn(options) ->
Map.fetch!(options, :initial_koan)
end)
end
defp parse(args) do
Enum.reduce(args, @defaults, fn(arg, acc) ->
Map.merge(acc, parse_argument(arg))
end)
end
def parse_argument("--koan="<>module), do: %{ initial_koan: String.to_atom("Elixir."<> module)}
def parse_argument("--clear-screen"), do: %{ clear_screen: true}
def parse_argument(_), do: %{}
end

View File

@@ -15,10 +15,11 @@ defmodule Runner do
]
def run do
run(Equalities)
Options.initial_koan
|>run
end
def run(start_module) do
def run(start_module) when start_module in @modules do
Display.clear_screen()
start_idx = Enum.find_index(@modules, &(&1 == start_module))
Enum.drop(@modules, start_idx)
@@ -26,6 +27,7 @@ defmodule Runner do
run_module(mod) == :passed
end)
end
def run(koan), do: Display.invalid_koan(koan, @modules)
def run_module(module) do
Display.considering(module)

View File

@@ -11,6 +11,11 @@ defmodule OptionTest do
assert Options.clear_screen?
end
test "can target specifc koans" do
Options.start(["--koan=Strings"])
assert Options.initial_koan() == Strings
end
test "ignores unknown options" do
Options.start(["--foo"])
refute Options.clear_screen?