From 027521a7bc5d3e1aa524df2c7711780c76c206eb Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Mon, 29 Aug 2022 18:00:32 -0600 Subject: [PATCH] ErrorResultBlock only needs the exception, not an ErrorResult --- .../components/error_result_block.cr | 29 +++++++++---------- src/spectator/formatting/summary.cr | 10 +++---- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/src/spectator/formatting/components/error_result_block.cr b/src/spectator/formatting/components/error_result_block.cr index 353c096..2f9d749 100644 --- a/src/spectator/formatting/components/error_result_block.cr +++ b/src/spectator/formatting/components/error_result_block.cr @@ -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| diff --git a/src/spectator/formatting/summary.cr b/src/spectator/formatting/summary.cr index 50a2b1b..b1ed247 100644 --- a/src/spectator/formatting/summary.cr +++ b/src/spectator/formatting/summary.cr @@ -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