Move more failure block chunks to their own structs

This commit is contained in:
Michael Miller 2019-02-20 21:28:11 -07:00
parent bef3243c6c
commit e752f901a5
3 changed files with 59 additions and 7 deletions

View file

@ -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.

View file

@ -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

View file

@ -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