To improve testing, add a layer of indirection around colours.

This commit is contained in:
Felipe Sere
2016-05-14 00:12:42 +02:00
parent 0936ed56e2
commit ddcd971ede
8 changed files with 55 additions and 24 deletions

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

View File

@@ -1,10 +1,12 @@
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
"""
#{Colours.cyan("Assertion failed in #{file}:#{line}")}
#{Colours.red(message)}
#{Paint.cyan("Assertion failed in #{file}:#{line}")}
#{Paint.red(message)}
"""
end
def format_failure(%{error: %ExUnit.AssertionError{expr: expr}, file: file, line: line}) do
@@ -12,21 +14,21 @@ defmodule Display.Failure do
end
def format_failure(%{error: error, file: file, line: line}) do
"""
#{Colours.cyan("Error in #{file}:#{line}")}
#{Paint.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))}
#{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)
Colours.red(Exception.format(:error, error, trace))
Paint.red(Exception.format(:error, error, trace))
end
def show_compile_error(error) do

View File

@@ -1,4 +1,6 @@
defmodule Display.Intro do
alias Display.Colours
def intro(module, modules) do
if not module in modules do
show_intro(module.intro)

View File

@@ -1,6 +1,8 @@
defmodule Display.Notifications do
alias Display.Paint
def congratulate do
Colours.green("\nYou have learned much. You must find your own path now.")
Paint.green("\nYou have learned much. You must find your own path now.")
end
def invalid_koan(koan, modules) do
@@ -13,7 +15,7 @@ defmodule Display.Notifications do
|> Enum.map(&Atom.to_string/1)
|> Enum.map(&name/1)
|> Enum.join(", ")
|> Colours.red
|> Paint.red
end
defp name("Elixir." <> module), do: module

View File

@@ -1,4 +1,6 @@
defmodule Display.ProgressBar do
alias Display.Colours
@progress_bar_length 30
def progress_bar(%{current: current, total: total}) do