Some work on FailureBlock

This commit is contained in:
Michael Miller 2019-02-17 23:15:58 -07:00
parent f2c17189fc
commit 4126ee3451

View file

@ -13,10 +13,19 @@ module Spectator::Formatters
# # spec/source_spec.cr:42
# ```
class FailureBlock
# Number of spaces to indent the block by.
INITIAL_INDENT = 2
# Number of spaces to add for each level of indentation.
INTENT_SIZE = 2
@index_length : Int32
# 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)
@index_length = integer_length(@index)
end
# Creates the block of text describing the failure.
@ -33,9 +42,10 @@ module Spectator::Formatters
# 1) Example name
# ```
private def title(io)
io << " "
INITIAL_INDENT.times { io << ' ' }
io << @index
io << ')'
io << ' '
io << @result.example
io.puts
end
@ -48,8 +58,10 @@ module Spectator::Formatters
# The indentation of this line starts directly under
# the example name from the title line.
private def message(io)
io << " Failure: "
io << @result.error
INITIAL_INDENT.times { io << ' ' }
(@index_length + 2).times { io << ' ' } # +2 for ) and space.
io << Color.failure("Failure: ")
io << Color.failure(@result.error)
io.puts
end
@ -63,8 +75,21 @@ module Spectator::Formatters
# Produces the source line of the failure block.
private def source(io)
io << " # "
@result.example.source.to_s(io)
INITIAL_INDENT.times { io << ' ' }
(@index_length + 2).times { io << ' ' } # +2 for ) and space.
io << '#'
io << ' '
io << Color.comment(@result.example.source)
end
# Gets the number of characters a positive integer spans in base 10.
private def integer_length(index)
count = 1
while index >= 10
index /= 10
count += 1
end
count
end
end
end