To improve testing, add a layer of indirection around colours.
This commit is contained in:
@@ -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
|
|
||||||
36
lib/display/colours.ex
Normal file
36
lib/display/colours.ex
Normal 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
|
||||||
@@ -1,10 +1,12 @@
|
|||||||
defmodule Display.Failure do
|
defmodule Display.Failure do
|
||||||
|
alias Display.Paint
|
||||||
|
|
||||||
@no_value :ex_unit_no_meaningful_value
|
@no_value :ex_unit_no_meaningful_value
|
||||||
|
|
||||||
def format_failure(%{error: %ExUnit.AssertionError{expr: @no_value, message: message}, file: file, line: line}) do
|
def format_failure(%{error: %ExUnit.AssertionError{expr: @no_value, message: message}, file: file, line: line}) do
|
||||||
"""
|
"""
|
||||||
#{Colours.cyan("Assertion failed in #{file}:#{line}")}
|
#{Paint.cyan("Assertion failed in #{file}:#{line}")}
|
||||||
#{Colours.red(message)}
|
#{Paint.red(message)}
|
||||||
"""
|
"""
|
||||||
end
|
end
|
||||||
def format_failure(%{error: %ExUnit.AssertionError{expr: expr}, file: file, line: line}) do
|
def format_failure(%{error: %ExUnit.AssertionError{expr: expr}, file: file, line: line}) do
|
||||||
@@ -12,21 +14,21 @@ defmodule Display.Failure do
|
|||||||
end
|
end
|
||||||
def format_failure(%{error: error, file: file, line: line}) do
|
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)}
|
#{format_error(error)}
|
||||||
"""
|
"""
|
||||||
end
|
end
|
||||||
|
|
||||||
defp format_assertion_error(error, file, line) do
|
defp format_assertion_error(error, file, line) do
|
||||||
"""
|
"""
|
||||||
#{Colours.cyan("Assertion failed in #{file}:#{line}")}
|
#{Paint.cyan("Assertion failed in #{file}:#{line}")}
|
||||||
#{Colours.red(Macro.to_string(error))}
|
#{Paint.red(Macro.to_string(error))}
|
||||||
"""
|
"""
|
||||||
end
|
end
|
||||||
|
|
||||||
defp format_error(error) do
|
defp format_error(error) do
|
||||||
trace = System.stacktrace |> Enum.take(2)
|
trace = System.stacktrace |> Enum.take(2)
|
||||||
Colours.red(Exception.format(:error, error, trace))
|
Paint.red(Exception.format(:error, error, trace))
|
||||||
end
|
end
|
||||||
|
|
||||||
def show_compile_error(error) do
|
def show_compile_error(error) do
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
defmodule Display.Intro do
|
defmodule Display.Intro do
|
||||||
|
alias Display.Colours
|
||||||
|
|
||||||
def intro(module, modules) do
|
def intro(module, modules) do
|
||||||
if not module in modules do
|
if not module in modules do
|
||||||
show_intro(module.intro)
|
show_intro(module.intro)
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
defmodule Display.Notifications do
|
defmodule Display.Notifications do
|
||||||
|
alias Display.Paint
|
||||||
|
|
||||||
def congratulate do
|
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
|
end
|
||||||
|
|
||||||
def invalid_koan(koan, modules) do
|
def invalid_koan(koan, modules) do
|
||||||
@@ -13,7 +15,7 @@ defmodule Display.Notifications do
|
|||||||
|> Enum.map(&Atom.to_string/1)
|
|> Enum.map(&Atom.to_string/1)
|
||||||
|> Enum.map(&name/1)
|
|> Enum.map(&name/1)
|
||||||
|> Enum.join(", ")
|
|> Enum.join(", ")
|
||||||
|> Colours.red
|
|> Paint.red
|
||||||
end
|
end
|
||||||
|
|
||||||
defp name("Elixir." <> module), do: module
|
defp name("Elixir." <> module), do: module
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
defmodule Display.ProgressBar do
|
defmodule Display.ProgressBar do
|
||||||
|
alias Display.Colours
|
||||||
|
|
||||||
@progress_bar_length 30
|
@progress_bar_length 30
|
||||||
|
|
||||||
def progress_bar(%{current: current, total: total}) do
|
def progress_bar(%{current: current, total: total}) do
|
||||||
|
|||||||
@@ -5,13 +5,13 @@ defmodule FailureTests do
|
|||||||
test "assertion failure with proper expression" do
|
test "assertion failure with proper expression" do
|
||||||
error = error(%ExUnit.AssertionError{expr: "hi"})
|
error = error(%ExUnit.AssertionError{expr: "hi"})
|
||||||
|
|
||||||
assert Failure.format_failure(error) == "\e[36mAssertion failed in some_file.ex:42\e[0m\n\e[31m\"hi\"\e[0m\n"
|
assert Failure.format_failure(error) == "Assertion failed in some_file.ex:42\n\"hi\"\n"
|
||||||
end
|
end
|
||||||
|
|
||||||
test "assertion failure with message" do
|
test "assertion failure with message" do
|
||||||
error = error(%ExUnit.AssertionError{expr: :ex_unit_no_meaningful_value, message: "hola"})
|
error = error(%ExUnit.AssertionError{expr: :ex_unit_no_meaningful_value, message: "hola"})
|
||||||
|
|
||||||
assert Failure.format_failure(error) == "\e[36mAssertion failed in some_file.ex:42\e[0m\n\e[31mhola\e[0m\n"
|
assert Failure.format_failure(error) == "Assertion failed in some_file.ex:42\nhola\n"
|
||||||
end
|
end
|
||||||
|
|
||||||
defp error(error) do
|
defp error(error) do
|
||||||
|
|||||||
@@ -4,6 +4,6 @@ defmodule NotificationTest do
|
|||||||
|
|
||||||
test "shows possible koans when a koan can not be found" do
|
test "shows possible koans when a koan can not be found" do
|
||||||
message = Notifications.invalid_koan(SampleKoan, [PassingKoan])
|
message = Notifications.invalid_koan(SampleKoan, [PassingKoan])
|
||||||
assert message == "Did not find koan SampleKoan in \e[31mPassingKoan\e[0m"
|
assert message == "Did not find koan SampleKoan in PassingKoan"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user