From 9d6d8de72f44f40f3f4e265fe4b21178231ce1a8 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Mon, 29 Aug 2022 20:53:48 -0600 Subject: [PATCH] Show error block for forced failure - `fail` Fixes https://gitlab.com/arctic-fox/spectator/-/issues/78 --- .../formatting/components/error_result_block.cr | 5 ++++- src/spectator/formatting/summary.cr | 11 +++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/spectator/formatting/components/error_result_block.cr b/src/spectator/formatting/components/error_result_block.cr index 2f9d749..3eb96fc 100644 --- a/src/spectator/formatting/components/error_result_block.cr +++ b/src/spectator/formatting/components/error_result_block.cr @@ -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. diff --git a/src/spectator/formatting/summary.cr b/src/spectator/formatting/summary.cr index b1ed247..b866307 100644 --- a/src/spectator/formatting/summary.cr +++ b/src/spectator/formatting/summary.cr @@ -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