Add support for sub-index in result blocks

This commit is contained in:
Michael Miller 2021-06-12 11:40:21 -06:00
parent dcdb87e31a
commit 88f0c23a3e
No known key found for this signature in database
GPG key ID: FB9F12F7C646A4AD
5 changed files with 21 additions and 12 deletions

View file

@ -7,8 +7,8 @@ module Spectator::Formatting::Components
# Displays information about an error result.
struct ErrorResultBlock < ResultBlock
# Creates the component.
def initialize(index : Int32, example : Example, @result : ErrorResult)
super(index, example)
def initialize(example : Example, index : Int32, @result : ErrorResult)
super(example, index)
end
# Content displayed on the second line of the block after the label.

View file

@ -11,8 +11,8 @@ module Spectator::Formatting::Components
@longest_key : Int32
# Creates the component.
def initialize(index : Int32, example : Example, result : FailResult)
super(index, example)
def initialize(example : Example, index : Int32, result : FailResult, subindex = 0)
super(example, index, subindex)
@expectation = result.expectations.find(&.failed?).not_nil!
@longest_key = @expectation.values.max_of { |(key, _value)| key.to_s.size }
end

View file

@ -7,8 +7,8 @@ module Spectator::Formatting::Components
# Displays information about a pending result.
struct PendingResultBlock < ResultBlock
# Creates the component.
def initialize(index : Int32, example : Example, @result : PendingResult)
super(index, example)
def initialize(example : Example, index : Int32, @result : PendingResult)
super(example, index)
end
# Content displayed on the second line of the block after the label.

View file

@ -14,7 +14,7 @@ module Spectator::Formatting::Components
# ```
abstract struct ResultBlock < Block
# Creates the block with the specified *index* and for the given *example*.
def initialize(@index : Int32, @example : Example)
def initialize(@example : Example, @index : Int32, @subindex : Int32 = 0)
super()
end
@ -55,7 +55,9 @@ module Spectator::Formatting::Components
# Produces the title line.
private def title_line(io)
line(io) do
io << @index << ") " << title
io << @index
io << '.' << @subindex if @subindex > 0
io << ") " << title
end
end
@ -80,7 +82,14 @@ module Spectator::Formatting::Components
# Computes the number of spaces the index takes
private def index_digit_count
(Math.log(@index.to_f + 1) / Math::LOG10).ceil.to_i
count = digit_count(@index)
count += 1 + digit_count(@subindex) if @subindex > 0
count
end
# Computes the number of spaces an integer takes.
private def digit_count(integer)
(Math.log(integer.to_f + 1) / Math::LOG10).ceil.to_i
end
end
end

View file

@ -25,7 +25,7 @@ module Spectator::Formatting
io.puts
examples.each_with_index(1) do |example, index|
result = example.result.as(PendingResult)
io.puts Components::PendingResultBlock.new(index, example, result)
io.puts Components::PendingResultBlock.new(example, index, result)
end
end
@ -39,9 +39,9 @@ module Spectator::Formatting
io.puts
examples.each_with_index(1) do |example, index|
if result = example.result.as?(ErrorResult)
io.puts Components::ErrorResultBlock.new(index, example, result)
io.puts Components::ErrorResultBlock.new(example, index, result)
elsif result = example.result.as?(FailResult)
io.puts Components::FailResultBlock.new(index, example, result)
io.puts Components::FailResultBlock.new(example, index, result)
end
end
end