Show error block for forced failure - `fail`

Fixes https://gitlab.com/arctic-fox/spectator/-/issues/78
This commit is contained in:
Michael Miller 2022-08-29 20:53:48 -06:00
parent 027521a7bc
commit 9d6d8de72f
No known key found for this signature in database
GPG Key ID: AC78B32D30CE34A2
2 changed files with 13 additions and 3 deletions

View File

@ -18,7 +18,10 @@ module Spectator::Formatting::Components
# Prefix for the second line of the block.
private def subtitle_label
"Error: ".colorize(:red)
case @error
when ExampleFailed then "Failed: "
else "Error: "
end.colorize(:red)
end
# Display error information.

View File

@ -63,10 +63,17 @@ 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)
error = example.result.as?(ErrorResult).try(&.error)
# Retrieve the ultimate reason for failing.
error = example.result.as?(FailResult).try(&.error)
# Prevent displaying duplicated output from expectation.
# Display `ExampleFailed` but not `ExpectationFailed`.
error = nil if error.responds_to?(:expectation)
# Gather all failed expectations.
failed_expectations = example.result.expectations.select(&.failed?)
block_count = failed_expectations.size
block_count += 1 if error
block_count += 1 if error # Add an extra block for final error if it's significant.
# Don't use sub-index if there was only one problem.
if block_count == 1