Use Indent tracker type

This commit is contained in:
Michael Miller 2019-03-03 09:54:39 -07:00
parent 8cdc5f0b3f
commit 238dc38a36
1 changed files with 29 additions and 46 deletions

View File

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