ErrorResultBlock only needs the exception, not an ErrorResult

This commit is contained in:
Michael Miller 2022-08-29 18:00:32 -06:00
parent d10531430c
commit 027521a7bc
No known key found for this signature in database
GPG Key ID: AC78B32D30CE34A2
2 changed files with 19 additions and 20 deletions

View File

@ -7,13 +7,13 @@ module Spectator::Formatting::Components
# Displays information about an error result.
struct ErrorResultBlock < ResultBlock
# Creates the component.
def initialize(example : Example, index : Int32, @result : ErrorResult, subindex = 0)
def initialize(example : Example, index : Int32, @error : Exception, subindex = 0)
super(example, index, subindex)
end
# Content displayed on the second line of the block after the label.
private def subtitle
@result.error.message.try(&.each_line.first)
@error.message.try(&.each_line.first)
end
# Prefix for the second line of the block.
@ -24,19 +24,18 @@ module Spectator::Formatting::Components
# Display error information.
private def content(io)
# Fetch the error and message.
error = @result.error
lines = error.message.try(&.lines)
lines = @error.message.try(&.lines)
# Write the error and message if available.
case
when lines.nil? then write_error_class(io, error)
when lines.size == 1 then write_error_message(io, error, lines.first)
when lines.size > 1 then write_multiline_error_message(io, error, lines)
else write_error_class(io, error)
when lines.nil? then write_error_class(io)
when lines.size == 1 then write_error_message(io, lines.first)
when lines.size > 1 then write_multiline_error_message(io, lines)
else write_error_class(io)
end
# Display the backtrace if it's available.
if backtrace = error.backtrace?
if backtrace = @error.backtrace?
indent { write_backtrace(io, backtrace) }
end
@ -44,24 +43,24 @@ module Spectator::Formatting::Components
end
# Display just the error type.
private def write_error_class(io, error)
private def write_error_class(io)
line(io) do
io << error.class.colorize(:red)
io << @error.class.colorize(:red)
end
end
# Display the error type and first line of the message.
private def write_error_message(io, error, message)
private def write_error_message(io, message)
line(io) do
io << "#{error.class}: ".colorize(:red)
io << "#{@error.class}: ".colorize(:red)
io << message
end
end
# Display the error type and its multi-line message.
private def write_multiline_error_message(io, error, lines)
private def write_multiline_error_message(io, lines)
# Use the normal formatting for the first line.
write_error_message(io, error, lines.first)
write_error_message(io, lines.first)
# Display additional lines after the first.
lines.skip(1).each do |entry|

View File

@ -63,15 +63,15 @@ module Spectator::Formatting
# Displays one or more blocks for a failed example.
# Each block is a failed expectation or error raised in the example.
private def dump_failed_example(example, index)
result = example.result.as?(ErrorResult)
error = example.result.as?(ErrorResult).try(&.error)
failed_expectations = example.result.expectations.select(&.failed?)
block_count = failed_expectations.size
block_count += 1 if result
block_count += 1 if error
# Don't use sub-index if there was only one problem.
if block_count == 1
if result
io.puts Components::ErrorResultBlock.new(example, index, result)
if error
io.puts Components::ErrorResultBlock.new(example, index, error)
else
io.puts Components::FailResultBlock.new(example, index, failed_expectations.first)
end
@ -79,7 +79,7 @@ module Spectator::Formatting
failed_expectations.each_with_index(1) do |expectation, subindex|
io.puts Components::FailResultBlock.new(example, index, expectation, subindex)
end
io.puts Components::ErrorResultBlock.new(example, index, result, block_count) if result
io.puts Components::ErrorResultBlock.new(example, index, error, block_count) if error
end
end
end