Packages
espec
0.7.2
1.10.0
1.9.2
1.9.1
1.9.0
1.8.3
1.8.2
1.8.1
1.8.0
1.7.0
1.6.5
1.6.4
1.6.3
1.6.2
1.6.1
1.6.0
1.5.1
1.5.0
1.4.6
1.4.5
1.4.4
1.4.3
1.4.2
1.4.1
1.4.0
1.3.4
1.3.3
1.3.2
1.3.1
1.3.0
1.2.2
1.2.1
1.2.0
1.1.2
1.1.1
1.1.0
1.0.1
1.0.0
0.8.28
0.8.27
0.8.26
0.8.25
0.8.24
0.8.23
0.8.22
0.8.21
0.8.20
0.8.19
0.8.18
0.8.17
0.8.16
0.8.15
0.8.14
0.8.13
0.8.12
0.8.11
0.8.10
0.8.9
0.8.8
0.8.7
0.8.6
0.8.5
0.8.1
0.8.0
0.7.2
0.7.1
0.7.0
0.6.4
0.6.3
0.6.2
0.6.1
0.6.0
0.5.1
0.5.0
0.4.2
0.4.1
0.4.0
0.3.7
0.3.5
0.3.0
0.2.0
0.1.0
BDD testing framework for Elixir inspired by RSpec.
Current section
Files
Jump to
Current section
Files
lib/espec/output/doc.ex
defmodule ESpec.Output.Doc do
@moduledoc """
Generates plain colored text output.
"""
@green IO.ANSI.green
@red IO.ANSI.red
@cyan IO.ANSI.cyan
@yellow IO.ANSI.yellow
@blue IO.ANSI.blue
@reset IO.ANSI.reset
@status_colors [success: @green, failure: @red, pending: @yellow]
@status_symbols [success: ".", failure: "F", pending: "*"]
alias ESpec.Example
@doc "Formats the final result."
def format_result(examples, times, _opts) do
pending = Example.pendings(examples)
string = ""
if Enum.any?(pending), do: string = string <> format_pending(pending)
failed = Example.failure(examples)
if Enum.any?(failed), do: string = string <> format_failed(failed)
string = string <> format_footer(examples, failed, pending)
string <> format_times(times, failed, pending)
end
@doc "Formats an example result."
def format_example(example, opts) do
color = color_for_status(example.status)
symbol = symbol_for_status(example.status)
if opts[:details] do
trace_description(example)
else
"#{color}#{symbol}#{@reset}"
end
end
defp format_failed(failed) do
res = failed |> Enum.with_index
|> Enum.map fn({example, index}) ->
do_format_example(example, example.error.message, index)
end
Enum.join(res, "\n")
end
defp format_pending(pending) do
res = pending |> Enum.with_index
|> Enum.map fn({example, index}) ->
do_format_example(example, example.result, index)
end
Enum.join(res, "\n")
end
defp do_format_example(example, info, index) do
color = color_for_status(example.status)
decription = one_line_description(example)
[
"\n",
"\t#{index + 1}) #{decription}",
"\t#{@cyan}#{example.file}:#{example.line}#{@reset}",
"\t#{color}#{info}#{@reset}",
]
|> Enum.join("\n")
end
defp format_footer(examples, failed, pending) do
color = get_color(failed, pending)
parts = ["#{Enum.count(examples)} examples", "#{Enum.count(failed)} failures"]
if Enum.any?(pending), do: parts = parts ++ ["#{Enum.count(pending)} pending"]
"\n\n\t#{color}#{Enum.join(parts, ", ")}#{@reset}"
end
defp format_times({start_loading_time, finish_loading_time, finish_specs_time}, failed, pending) do
color = get_color(failed, pending)
load_time = :timer.now_diff(finish_loading_time, start_loading_time)
spec_time = :timer.now_diff(finish_specs_time, finish_loading_time)
"\n\n\t#{color}Finished in #{us_to_sec(load_time + spec_time)} seconds"
<> " (#{us_to_sec(load_time)}s on load, #{us_to_sec(spec_time)}s on specs)#{@reset}\n\n"
end
defp us_to_sec(us), do: div(us, 10000) / 100
defp get_color(failed, pending) do
if Enum.any?(failed) do
@red
else
if Enum.any?(pending), do: @yellow, else: @green
end
end
defp one_line_description(example) do
module = "#{example.module}" |> String.replace("Elixir.", "")
[ module | Example.context_descriptions(example)] ++ [example.description]
|> Enum.join(" ") |> String.rstrip
end
defp trace_description(example) do
color = color_for_status(example.status)
ex_desc = if String.length(example.description) > 0 do
"#{color}#{example.description}#{@reset}"
else
if example.status == :failure do
"#{color}#{example.error.message}#{@reset}"
else
"#{color}#{inspect example.result}#{@reset}"
end
end
array = Example.context_descriptions(example) ++ [ex_desc]
{result, _} = Enum.reduce(array, {"", ""}, fn(description, acc) ->
{d, w} = acc
{d <> w <> "#{description}" <> "\n", w <> " "}
end)
result
end
defp color_for_status(status), do: Keyword.get(@status_colors, status)
defp symbol_for_status(status), do: Keyword.get(@status_symbols, status)
end