Use Indent tracker type

This commit is contained in:
Michael Miller 2019-03-03 09:54:39 -07:00
parent 8cdc5f0b3f
commit 238dc38a36

View file

@ -13,26 +13,23 @@ module Spectator::Formatting
# # spec/source_spec.cr:42
# ```
private struct FailureBlock
# Default number of spaces to add for each level of indentation.
INDENT_SIZE = 2
# Creates the failure block.
# The *index* uniquely identifies the failure in the output.
# The *result* is the outcome of the failed example.
def initialize(@index : Int32, @result : FailedResult)
@indent = 0
end
# Creates the block of text describing the failure.
def to_s(io)
indent = Indent.new(io)
inner_indent = integer_length(@index) + 2 # +2 for ) and space after number.
indent do
title(io)
indent(inner_indent) do
message(io)
content(io)
source(io)
indent.increase do
title(indent)
indent.increase(inner_indent) do
message(indent)
content(indent)
source(indent)
end
end
end
@ -42,8 +39,8 @@ module Spectator::Formatting
# ```text
# 1) Example name
# ```
private def title(io)
line(io, NumberedItem.new(@index, @result.example))
private def title(indent)
indent.line(NumberedItem.new(@index, @result.example))
end
# Produces the message line of the failure block.
@ -53,43 +50,43 @@ module Spectator::Formatting
# ```
# The indentation of this line starts directly under
# the example name from the title line.
private def message(io)
line(io, FailureMessage.color(@result))
private def message(indent)
indent.line(FailureMessage.color(@result))
end
# Produces the main content of the failure block.
# Any failed expectations are displayed,
# then an error stacktrace if an error occurred.
private def content(io)
io.puts
indent do
unsatisfied_expectations(io)
error_stacktrace(io) if @result.is_a?(ErroredResult)
private def content(indent)
indent.line
indent.increase do
unsatisfied_expectations(indent)
error_stacktrace(indent) if @result.is_a?(ErroredResult)
end
io.puts
indent.line
end
# Produces a list of unsatisfied expectations and their values.
private def unsatisfied_expectations(io)
private def unsatisfied_expectations(indent)
@result.expectations.each_unsatisfied do |expectation|
# TODO: Failure message for this expectation.
matcher_values(io, expectation)
matcher_values(indent, expectation)
end
end
# Produces the values list for an expectation
private def matcher_values(io, expectation)
private def matcher_values(indent, expectation)
MatchDataValues.new(expectation.values).each do |pair|
# TODO: Not all expectations will be failures (color green and red).
line(io, Color.failure(pair))
indent.line(Color.failure(pair))
end
end
# Produces the stack trace for an errored result.
private def error_stacktrace(io)
private def error_stacktrace(indent)
error = @result.error
loop do
display_error(io, error)
display_error(indent, error)
if (next_error = error.cause)
error = next_error
else
@ -99,32 +96,18 @@ module Spectator::Formatting
end
# Display a single error and its stacktrace.
private def display_error(io, error) : Nil
line(io, Color.error("Caused by: #{error.message} (#{error.class})"))
indent do
private def display_error(indent, error) : Nil
indent.line(Color.error("Caused by: #{error.message} (#{error.class})"))
indent.increase do
error.backtrace.each do |frame|
line(io, Color.error(frame))
indent.line(Color.error(frame))
end
end
end
# Produces the source line of the failure block.
private def source(io)
line(io, Comment.color(@result.example.source))
end
# Increases the indentation for a block of text.
private def indent(amount = INDENT_SIZE)
@indent += amount
yield
ensure
@indent -= amount
end
# Produces a line of text with a leading indent.
private def line(io, text)
@indent.times { io << ' ' }
io.puts text
private def source(indent)
indent.line(Comment.color(@result.example.source))
end
# Gets the number of characters a positive integer spans in base 10.