Merge pull request #117 from elixirkoans/split-display

Extract smaller modules from display.
This commit is contained in:
Felipe Seré
2016-05-15 17:58:52 +02:00
11 changed files with 198 additions and 96 deletions

View File

@@ -1,13 +0,0 @@
defmodule Colours do
alias IO.ANSI
def red(str), do: colourize(ANSI.red, str)
def cyan(str), do: colourize(ANSI.cyan, str)
def green(str), do: colourize(ANSI.green, str)
defp colourize(color, message) do
Enum.join([color, message, ANSI.reset], "")
end
end

View File

@@ -1,75 +1,39 @@
defmodule Display do
alias IO.ANSI
@no_value :ex_unit_no_meaningful_value
@progress_bar_length 30
alias Display.ProgressBar
alias Display.Intro
alias Display.Failure
alias Display.Notifications
def invalid_koan(koan, modules) do
koans_names = module_names(modules)
IO.puts("Did not find koan #{name(koan)} in " <> koans_names )
Notifications.invalid_koan(koan, modules)
|> IO.puts
end
defp module_names(modules) do
modules
|> Enum.map(&Atom.to_string/1)
|> Enum.map(&name/1)
|> Enum.join(", ")
|> Colours.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(format(failure, module, name))
format(failure, module, name)
|> IO.puts
end
def format(failure, module, name) do
"""
#{intro(module, Tracker.visited)}
#{Intro.intro(module, Tracker.visited)}
Now meditate upon #{format_module(module)}
#{progress_bar(Tracker.summarize)}
#{ProgressBar.progress_bar(Tracker.summarize)}
----------------------------------------
#{name}
#{format_failure(failure)}
#{Failure.format_failure(failure)}
"""
end
defp intro(module, modules) do
if not module in modules do
show_intro(module.intro)
end
end
defp show_intro(""), do: nil
defp show_intro(message) do
message <> "\n"
|> Colours.green
def show_compile_error(error) do
Failure.show_compile_error(error)
|> IO.puts
end
def progress_bar(%{current: current, total: total}) do
arrow = caluculate_progress(current, total) |> build_arrow
"|" <> String.ljust(arrow, @progress_bar_length) <> "| #{current} of #{total}"
end
defp caluculate_progress(current, total) do
round( (current/total) * @progress_bar_length)
end
defp build_arrow(0), do: ""
defp build_arrow(length) do
String.duplicate("=", length-1) <> ">"
end
def show_compile_error(error) do
IO.puts("")
format_error(error) |> IO.puts
end
def congratulate do
IO.puts(Colours.green("\nYou have learned much. You must find your own path now."))
Notifications.congratulate
|> IO.puts
end
def clear_screen do
@@ -79,34 +43,6 @@ defmodule Display do
end
end
defp format_failure(%{error: %ExUnit.AssertionError{expr: @no_value, message: message}, file: file, line: line}) do
"""
#{Colours.cyan("Assertion failed in #{file}:#{line}")}
#{Colours.red(message)}
"""
end
defp format_failure(%{error: %ExUnit.AssertionError{expr: expr}, file: file, line: line}) do
format_assertion_error(expr, file, line)
end
defp format_failure(%{error: error, file: file, line: line}) do
"""
#{Colours.cyan("Error in #{file}:#{line}")}
#{format_error(error)}
"""
end
defp format_assertion_error(error, file, line) do
"""
#{Colours.cyan("Assertion failed in #{file}:#{line}")}
#{Colours.red(Macro.to_string(error))}
"""
end
defp format_error(error) do
trace = System.stacktrace |> Enum.take(2)
Colours.red(Exception.format(:error, error, trace))
end
defp format_module(module) do
Module.split(module) |> List.last
end

36
lib/display/colours.ex Normal file
View File

@@ -0,0 +1,36 @@
defmodule Display.Paint do
def red(str), do: painter().red(str)
def cyan(str), do: painter().cyan(str)
def green(str), do: painter().green(str)
defp painter do
case Mix.env do
:test -> Display.Uncoloured
_ -> Display.Colours
end
end
end
defmodule Display.Colours do
alias IO.ANSI
def red(str), do: colourize(ANSI.red, str)
def cyan(str), do: colourize(ANSI.cyan, str)
def green(str), do: colourize(ANSI.green, str)
defp colourize(color, message) do
Enum.join([color, message, ANSI.reset], "")
end
end
defmodule Display.Uncoloured do
def red(str), do: str
def cyan(str), do: str
def green(str), do: str
end

37
lib/display/failure.ex Normal file
View File

@@ -0,0 +1,37 @@
defmodule Display.Failure do
alias Display.Paint
@no_value :ex_unit_no_meaningful_value
def format_failure(%{error: %ExUnit.AssertionError{expr: @no_value, message: message}, file: file, line: line}) do
"""
#{Paint.cyan("Assertion failed in #{file}:#{line}")}
#{Paint.red(message)}
"""
end
def format_failure(%{error: %ExUnit.AssertionError{expr: expr}, file: file, line: line}) do
format_assertion_error(expr, file, line)
end
def format_failure(%{error: error, file: file, line: line}) do
"""
#{Paint.cyan("Error in #{file}:#{line}")}
#{format_error(error)}
"""
end
defp format_assertion_error(error, file, line) do
"""
#{Paint.cyan("Assertion failed in #{file}:#{line}")}
#{Paint.red(Macro.to_string(error))}
"""
end
defp format_error(error) do
trace = System.stacktrace |> Enum.take(2)
Paint.red(Exception.format(:error, error, trace))
end
def show_compile_error(error) do
format_error(error)
end
end

16
lib/display/intro.ex Normal file
View File

@@ -0,0 +1,16 @@
defmodule Display.Intro do
alias Display.Paint
def intro(module, modules) do
if not module in modules do
show_intro(module.intro)
else
""
end
end
def show_intro(message) do
message <> "\n"
|> Paint.green
end
end

View File

@@ -0,0 +1,23 @@
defmodule Display.Notifications do
alias Display.Paint
def congratulate do
Paint.green("\nYou have learned much. You must find your own path now.")
end
def invalid_koan(koan, modules) do
koans_names = module_names(modules)
"Did not find koan #{name(koan)} in " <> koans_names
end
defp module_names(modules) do
modules
|> Enum.map(&Atom.to_string/1)
|> Enum.map(&name/1)
|> Enum.join(", ")
|> Paint.red
end
defp name("Elixir." <> module), do: module
defp name(module), do: name(Atom.to_string(module))
end

View File

@@ -0,0 +1,19 @@
defmodule Display.ProgressBar do
@progress_bar_length 30
def progress_bar(%{current: current, total: total}) do
arrow = caluculate_progress(current, total) |> build_arrow
"|" <> String.ljust(arrow, @progress_bar_length) <> "| #{current} of #{total}"
end
defp caluculate_progress(current, total) do
round( (current/total) * @progress_bar_length)
end
defp build_arrow(0), do: ""
defp build_arrow(length) do
String.duplicate("=", length-1) <> ">"
end
end

View File

@@ -0,0 +1,24 @@
defmodule FailureTests do
use ExUnit.Case
alias Display.Failure
test "assertion failure with proper expression" do
error = error(%ExUnit.AssertionError{expr: "hi"})
assert Failure.format_failure(error) == "Assertion failed in some_file.ex:42\n\"hi\"\n"
end
test "assertion failure with message" do
error = error(%ExUnit.AssertionError{expr: :ex_unit_no_meaningful_value, message: "hola"})
assert Failure.format_failure(error) == "Assertion failed in some_file.ex:42\nhola\n"
end
defp error(error) do
%{
error: error,
file: "some_file.ex",
line: 42
}
end
end

View File

@@ -0,0 +1,13 @@
defmodule IntroTest do
use ExUnit.Case
alias Display.Intro
test "module not visited yet" do
assert Intro.intro(SampleKoan, []) == "There is something\n\n"
end
test "module has been visited" do
assert Intro.intro(SampleKoan, [SampleKoan]) == ""
end
end

View File

@@ -0,0 +1,9 @@
defmodule NotificationTest do
use ExUnit.Case
alias Display.Notifications
test "shows possible koans when a koan can not be found" do
message = Notifications.invalid_koan(SampleKoan, [PassingKoan])
assert message == "Did not find koan SampleKoan in PassingKoan"
end
end

View File

@@ -1,19 +1,21 @@
defmodule DisplayTest do
defmodule ProgressBarTest do
use ExUnit.Case
alias Display.ProgressBar
test "empty bar" do
bar = Display.progress_bar(%{total: 12, current: 0})
bar = ProgressBar.progress_bar(%{total: 12, current: 0})
assert bar == "| | 0 of 12"
end
test "puts counter on the right until half the koans are complete" do
bar = Display.progress_bar(%{total: 12, current: 3})
bar = ProgressBar.progress_bar(%{total: 12, current: 3})
assert bar == "|=======> | 3 of 12"
end
test "full bar" do
bar = Display.progress_bar(%{total: 12, current: 12})
bar = ProgressBar.progress_bar(%{total: 12, current: 12})
assert bar == "|=============================>| 12 of 12"
end
end