mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Rewrite to use indentation methods
This commit is contained in:
parent
1cd0f0249e
commit
8f85a6436f
2 changed files with 33 additions and 29 deletions
|
@ -13,27 +13,27 @@ 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.
|
# Default number of spaces to add for each level of indentation.
|
||||||
INITIAL_INDENT = 2
|
INDENT_SIZE = 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)
|
@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)
|
||||||
title(io)
|
inner_indent = integer_length(@index) + 2 # +2 for ) and space after number.
|
||||||
message(io)
|
indent do
|
||||||
values(io)
|
title(io)
|
||||||
source(io)
|
indent(inner_indent) do
|
||||||
|
message(io)
|
||||||
|
values(io)
|
||||||
|
source(io)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Produces the title of the failure block.
|
# Produces the title of the failure block.
|
||||||
|
@ -42,12 +42,7 @@ module Spectator::Formatters
|
||||||
# 1) Example name
|
# 1) Example name
|
||||||
# ```
|
# ```
|
||||||
private def title(io)
|
private def title(io)
|
||||||
INITIAL_INDENT.times { io << ' ' }
|
line(io, "#{@index}) #{@result.example}")
|
||||||
io << @index
|
|
||||||
io << ')'
|
|
||||||
io << ' '
|
|
||||||
io << @result.example
|
|
||||||
io.puts
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Produces the message line of the failure block.
|
# Produces the message line of the failure block.
|
||||||
|
@ -58,26 +53,36 @@ 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)
|
||||||
INITIAL_INDENT.times { io << ' ' }
|
line(io, Color.failure("Failure: #{@result.error}"))
|
||||||
(@index_length + 2).times { io << ' ' } # +2 for ) and space.
|
|
||||||
io << Color.failure("Failure: ")
|
|
||||||
io << Color.failure(@result.error)
|
|
||||||
io.puts
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Produces the values list of the failure block.
|
# Produces the values list of the failure block.
|
||||||
private def values(io)
|
private def values(io)
|
||||||
io.puts
|
io.puts
|
||||||
io.puts " Expected: TODO"
|
indent do
|
||||||
io.puts " got: TODO"
|
line(io, "Expected: TODO")
|
||||||
|
line(io, " got: TODO")
|
||||||
|
end
|
||||||
io.puts
|
io.puts
|
||||||
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(io)
|
||||||
INITIAL_INDENT.times { io << ' ' }
|
line(io, Comment.color(@result.example.source))
|
||||||
(@index_length + 2).times { io << ' ' } # +2 for ) and space.
|
end
|
||||||
io << Comment.color(@result.example.source)
|
|
||||||
|
# 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.
|
||||||
|
|
|
@ -25,7 +25,6 @@ module Spectator::Formatters
|
||||||
@io.puts
|
@io.puts
|
||||||
failures.each_with_index do |result, index|
|
failures.each_with_index do |result, index|
|
||||||
@io.puts FailureBlock.new(index + 1, result)
|
@io.puts FailureBlock.new(index + 1, result)
|
||||||
@io.puts
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue