diff --git a/src/spectator/formatters/failure_block.cr b/src/spectator/formatters/failure_block.cr index f2efa76..6c8e334 100644 --- a/src/spectator/formatters/failure_block.cr +++ b/src/spectator/formatters/failure_block.cr @@ -7,10 +7,10 @@ module Spectator::Formatters # 1) Example name # Failure: Reason or message # - # Expected: value - # got: value + # Expected: value + # got: value # - # # spec/source_spec.cr:42 + # # spec/source_spec.cr:42 # ``` class FailureBlock # Default number of spaces to add for each level of indentation. @@ -26,6 +26,7 @@ module Spectator::Formatters # Creates the block of text describing the failure. def to_s(io) inner_indent = integer_length(@index) + 2 # +2 for ) and space after number. + indent do title(io) indent(inner_indent) do @@ -39,21 +40,21 @@ module Spectator::Formatters # Produces the title of the failure block. # The line takes the form: # ```text - # 1) Example name + # 1) Example name # ``` private def title(io) - line(io, "#{@index}) #{@result.example}") + line(io, NumberedItem.new(@index, @result.example)) end # Produces the message line of the failure block. # The line takes the form: # ```text - # Failure: Error message + # Failure: Error message # ``` # The indentation of this line starts directly under # the example name from the title line. private def message(io) - line(io, Color.failure("Failure: #{@result.error}")) + line(io, FailureMessage.color(@result)) end # Produces the values list of the failure block. diff --git a/src/spectator/formatters/failure_message.cr b/src/spectator/formatters/failure_message.cr new file mode 100644 index 0000000..a140303 --- /dev/null +++ b/src/spectator/formatters/failure_message.cr @@ -0,0 +1,35 @@ +module Spectator::Formatters + # 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/formatters/numbered_item.cr b/src/spectator/formatters/numbered_item.cr new file mode 100644 index 0000000..76afa1d --- /dev/null +++ b/src/spectator/formatters/numbered_item.cr @@ -0,0 +1,16 @@ +module Spectator::Formatters + # Produces a stringified value with a numerical prefix. + private struct NumberedItem(T) + # Creates the numbered item. + def initialize(@number : Int32, @text : T) + end + + # Appends the numbered item to the output. + def to_s(io) + io << @number + io << ')' + io << ' ' + io << @text + end + end +end