From 5a2a71ffe894cdc9f40b0424e142c83aa05df9d7 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Wed, 9 Jun 2021 22:15:15 -0600 Subject: [PATCH] Pass and output along reason for pending/skip result --- src/spectator/example.cr | 2 +- src/spectator/formatting/components/junit/test_case.cr | 4 ++-- .../formatting/components/pending_result_block.cr | 2 +- src/spectator/formatting/tap_formatter.cr | 6 +++++- src/spectator/harness.cr | 2 +- src/spectator/pending_result.cr | 10 +++++++--- 6 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/spectator/example.cr b/src/spectator/example.cr index d3ac530..cc9805c 100644 --- a/src/spectator/example.cr +++ b/src/spectator/example.cr @@ -26,7 +26,7 @@ module Spectator # Result of the last time the example ran. # Is pending if the example hasn't run. - getter result : Result = PendingResult.new + getter result : Result = PendingResult.new(Time::Span::ZERO, "Example not run") # Creates the example. # An instance to run the test code in is given by *context*. diff --git a/src/spectator/formatting/components/junit/test_case.cr b/src/spectator/formatting/components/junit/test_case.cr index f57cd5f..5545fd4 100644 --- a/src/spectator/formatting/components/junit/test_case.cr +++ b/src/spectator/formatting/components/junit/test_case.cr @@ -78,8 +78,8 @@ module Spectator::Formatting::Components::JUnit end # Adds a skipped element to the test case node. - def pending(_result) - @xml.element("skipped") # TODO: Populate message attribute with reason from result. + def pending(result) + @xml.element("skipped", message: result.reason) end end end diff --git a/src/spectator/formatting/components/pending_result_block.cr b/src/spectator/formatting/components/pending_result_block.cr index 12ee2a1..ea916c6 100644 --- a/src/spectator/formatting/components/pending_result_block.cr +++ b/src/spectator/formatting/components/pending_result_block.cr @@ -13,7 +13,7 @@ module Spectator::Formatting::Components # Content displayed on the second line of the block after the label. private def subtitle - "No reason given" # TODO: Get reason from result. + @result.reason end # Prefix for the second line of the block. diff --git a/src/spectator/formatting/tap_formatter.cr b/src/spectator/formatting/tap_formatter.cr index fe5392f..e5991a3 100644 --- a/src/spectator/formatting/tap_formatter.cr +++ b/src/spectator/formatting/tap_formatter.cr @@ -33,7 +33,11 @@ module Spectator::Formatting # TODO: Skipped tests should report ok. @io << "not ok " << @counter << " - " @io << notification.example << " # TODO " - @io.puts "No reason given" # TODO: Get reason from result. + + # This should never be false. + if (result = notification.example.result).responds_to?(:reason) + @io.puts result.reason + end end # Invoked after an example fails. diff --git a/src/spectator/harness.cr b/src/spectator/harness.cr index f5ffd96..13ee409 100644 --- a/src/spectator/harness.cr +++ b/src/spectator/harness.cr @@ -121,7 +121,7 @@ module Spectator when ExpectationFailed FailResult.new(elapsed, error, @expectations) when ExamplePending - PendingResult.new(elapsed, @expectations) + PendingResult.new(elapsed, error.message || "No reason given", @expectations) else ErrorResult.new(elapsed, error, @expectations) end diff --git a/src/spectator/pending_result.cr b/src/spectator/pending_result.cr index 11ae93e..5cd61ef 100644 --- a/src/spectator/pending_result.cr +++ b/src/spectator/pending_result.cr @@ -5,10 +5,14 @@ module Spectator # A pending result means the example is not ready to run yet. # This can happen when the functionality to be tested is not implemented yet. class PendingResult < Result + # Reason the example was skipped or marked pending. + getter reason : String + # Creates the result. # *elapsed* is the length of time it took to run the example. - def initialize(elapsed = Time::Span::ZERO, expectations = [] of Expectation) - super + # A *reason* for the skip/pending result can be specified. + def initialize(elapsed = Time::Span::ZERO, @reason = "No reason given", expectations = [] of Expectation) + super(elapsed, expectations) end # Calls the `pending` method on the *visitor*. @@ -40,7 +44,7 @@ module Spectator def to_json(json : JSON::Builder) super json.field("status", "pending") - json.field("pending_message", "Not implemented") # TODO: Provide pending message. + json.field("pending_message", @reason) end end end