Initial code for refactoring formatters

This commit is contained in:
Michael Miller 2019-02-15 22:09:53 -07:00
parent 88c32b697b
commit 2012db85c5
4 changed files with 97 additions and 1 deletions

View file

@ -25,7 +25,7 @@ module Spectator
# The formatter that should be used,
# if one wasn't provided.
private def default_formatter
Formatters::DefaultFormatter.new
Formatters::DotsFormatter.new
end
# Creates a configuration.

View file

@ -0,0 +1,36 @@
module Spectator::Formatters
# Mix-in that provides methods for colorizing output.
module Color
# Symbol in `Colorize` representing success.
SUCCESS_COLOR = :green
# Symbol in `Colorize` representing failure.
FAILURE_COLOR = :red
# Symbol in `Colorize` representing an error.
ERROR_COLOR = :magenta
# Symbol in `Colorize` representing pending or skipped.
PENDING_COLOR = :yellow
# Colorizes some text with the success color.
private def success(text)
text.colorize(SUCCESS_COLOR)
end
# Colorizes some text with the failure color.
private def failure(text)
text.colorize(FAILURE_COLOR)
end
# Colorizes some text with the error color.
private def error(text)
text.colorize(ERROR_COLOR)
end
# Colorizes some text with the pending/skipped color.
private def pending(text)
text.colorize(PENDING_COLOR)
end
end
end

View file

@ -0,0 +1,44 @@
require "./formatter"
require "./suite_summary"
module Spectator::Formatters
# Produces a single character for each example.
# A dot is output for each successful example (hence the name).
# Other characters are output for non-successful results.
# At the end of the test suite, a summary of failures and results is displayed.
class DotsFormatter < Formatter
include SuiteSummary
include Color
# Character output for a successful example.
SUCCESS_CHAR = '.'
# Character output for a failed example.
FAILURE_CHAR = 'F'
# Character output for an errored example.
ERROR_CHAR = 'E'
# Character output for a pending or skipped example.
PENDING_CHAR = '*'
# Does nothing when an example is started.
def start_example(example)
# ...
end
# Produces a single character output based on a result.
def end_example(result)
case result
when ErroredResult
print error(ERROR_CHAR)
when PendingResult
print pending(PENDING_CHAR)
when SuccessfulResult
print success(SUCCESS_CHAR)
else # FailedResult
print failure(FAILURE_CHAR)
end
end
end
end

View file

@ -0,0 +1,16 @@
module Spectator::Formatters
# Mix-in for producing a human-readable summary of a test suite.
module SuiteSummary
# Does nothing when starting a test suite.
def start_suite(suite)
# ...
end
# Produces the summary of test suite from a report.
# A block describing each failure is displayed.
# At the end, the totals and runtime are printed.
def end_suite(report)
raise NotImplementedError.new("SuiteSummary#end_suite")
end
end
end