From df71e56b3e93377ce637334954b6cb3e2cb485d8 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Sun, 3 Mar 2019 10:19:17 -0700 Subject: [PATCH] Replace FailureMessage with LabeledText --- src/spectator/formatting/failure_block.cr | 6 ++-- src/spectator/formatting/failure_message.cr | 35 --------------------- src/spectator/formatting/labeled_text.cr | 15 +++++++++ 3 files changed, 18 insertions(+), 38 deletions(-) delete mode 100644 src/spectator/formatting/failure_message.cr create mode 100644 src/spectator/formatting/labeled_text.cr diff --git a/src/spectator/formatting/failure_block.cr b/src/spectator/formatting/failure_block.cr index d7d0d8b..0347c31 100644 --- a/src/spectator/formatting/failure_block.cr +++ b/src/spectator/formatting/failure_block.cr @@ -53,7 +53,7 @@ module Spectator::Formatting # Produces a list of unsatisfied expectations and their values. private def unsatisfied_expectations(indent) @result.expectations.each_unsatisfied do |expectation| - indent.line(FailureMessage.color(@result)) + indent.line(Color.failure(LabeledText.new("Failure", expectation.actual_message))) indent.line indent.increase do matcher_values(indent, expectation) @@ -72,9 +72,9 @@ module Spectator::Formatting # Produces the stack trace for an errored result. private def error_stacktrace(indent) - indent.line(FailureMessage.color(@result)) - indent.line error = @result.error + indent.line(Color.error(LabeledText.new("Error", error))) + indent.line indent.increase do loop do display_error(indent, error) diff --git a/src/spectator/formatting/failure_message.cr b/src/spectator/formatting/failure_message.cr deleted file mode 100644 index 5edb48a..0000000 --- a/src/spectator/formatting/failure_message.cr +++ /dev/null @@ -1,35 +0,0 @@ -module Spectator::Formatting - # Produces a stringified failure or error message. - private struct FailureMessage - # Creates the failure message. - def initialize(@result : FailedResult) - end - - # Appends the message to the output. - def to_s(io) - io << @result.call(Label) - io << @result.error - end - - # Creates a colorized version of the message. - def self.color(result) - result.call(Color) { |result| new(result) } - end - - # Interface for `Result#call` to invoke. - # This is used to get the correct prefix for failures and errors. - private module Label - extend self - - # Returns the prefix for a failure message. - def failure - "Failure: " - end - - # Returns the prefix for an error message. - def error - "Error: " - end - end - end -end diff --git a/src/spectator/formatting/labeled_text.cr b/src/spectator/formatting/labeled_text.cr new file mode 100644 index 0000000..b99152e --- /dev/null +++ b/src/spectator/formatting/labeled_text.cr @@ -0,0 +1,15 @@ +module Spectator::Formatting + # Produces a stringified message with a prefix. + private struct LabeledText(T) + # Creates the labeled text. + def initialize(@label : String, @text : T) + end + + # Appends the message to the output. + def to_s(io) + io << @label + io << ": " + io << @text + end + end +end