From 11c738b941715c27023db1732a4e5a473e5a5f38 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Sun, 17 Feb 2019 14:48:54 -0700 Subject: [PATCH] Use Result#call instead of case-statement Color module is no longer mixed-in. --- src/spectator/formatters/color.cr | 12 +++--- src/spectator/formatters/dots_formatter.cr | 47 ++++++++++++---------- 2 files changed, 32 insertions(+), 27 deletions(-) diff --git a/src/spectator/formatters/color.cr b/src/spectator/formatters/color.cr index 87afe19..8230771 100644 --- a/src/spectator/formatters/color.cr +++ b/src/spectator/formatters/color.cr @@ -1,6 +1,8 @@ module Spectator::Formatters - # Mix-in that provides methods for colorizing output. + # Method for colorizing output. module Color + extend self + # Symbol in `Colorize` representing success. SUCCESS_COLOR = :green @@ -14,22 +16,22 @@ module Spectator::Formatters PENDING_COLOR = :yellow # Colorizes some text with the success color. - private def success(text) + def success(text) text.colorize(SUCCESS_COLOR) end # Colorizes some text with the failure color. - private def failure(text) + def failure(text) text.colorize(FAILURE_COLOR) end # Colorizes some text with the error color. - private def error(text) + def error(text) text.colorize(ERROR_COLOR) end # Colorizes some text with the pending/skipped color. - private def pending(text) + def pending(text) text.colorize(PENDING_COLOR) end end diff --git a/src/spectator/formatters/dots_formatter.cr b/src/spectator/formatters/dots_formatter.cr index 7aec714..ab8d0ad 100644 --- a/src/spectator/formatters/dots_formatter.cr +++ b/src/spectator/formatters/dots_formatter.cr @@ -8,19 +8,6 @@ module Spectator::Formatters # 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 = '*' # Creates the formatter. # By default, output is sent to `STDOUT`. @@ -34,15 +21,31 @@ module Spectator::Formatters # Produces a single character output based on a result. def end_example(result) - case result - when ErroredResult - @io.print error(ERROR_CHAR) - when PendingResult - @io.print pending(PENDING_CHAR) - when SuccessfulResult - @io.print success(SUCCESS_CHAR) - else # FailedResult - @io.print failure(FAILURE_CHAR) + @io.print result.call(Character) + end + + # Interface for `Result` to pick a character for output. + private module Character + extend self + + # Character output for a successful example. + def success(result) + Color.success('.') + end + + # Character output for a failed example. + def failure(result) + Color.failure('F') + end + + # Character output for an errored example. + def error(result) + Color.error('E') + end + + # Character output for a pending or skipped example. + def pending(result) + Color.pending('*') end end end