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 # # spec/source_spec.cr:42
# ``` # ```
class FailureBlock 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. # 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)
@index_length = integer_length(@index)
end end
# Creates the block of text describing the failure. # Creates the block of text describing the failure.
@ -33,9 +42,10 @@ module Spectator::Formatters
# 1) Example name # 1) Example name
# ``` # ```
private def title(io) private def title(io)
io << " " INITIAL_INDENT.times { io << ' ' }
io << @index io << @index
io << ')' io << ')'
io << ' '
io << @result.example io << @result.example
io.puts io.puts
end end
@ -48,8 +58,10 @@ module Spectator::Formatters
# 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(io)
io << " Failure: " INITIAL_INDENT.times { io << ' ' }
io << @result.error (@index_length + 2).times { io << ' ' } # +2 for ) and space.
io << Color.failure("Failure: ")
io << Color.failure(@result.error)
io.puts io.puts
end end
@ -63,8 +75,21 @@ module Spectator::Formatters
# Produces the source line of the failure block. # Produces the source line of the failure block.
private def source(io) private def source(io)
io << " # " INITIAL_INDENT.times { io << ' ' }
@result.example.source.to_s(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 end
end end