From 6c6dff363b8e80a704d34abf3f0db4697e3c2839 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Sat, 17 Jul 2021 16:25:32 -0600 Subject: [PATCH] Track source location of pending result --- src/spectator/dsl/expectations.cr | 8 ++++---- src/spectator/example_pending.cr | 8 ++++++++ src/spectator/harness.cr | 2 +- src/spectator/pending_result.cr | 7 ++++++- 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/spectator/dsl/expectations.cr b/src/spectator/dsl/expectations.cr index b8a18e4..c42d272 100644 --- a/src/spectator/dsl/expectations.cr +++ b/src/spectator/dsl/expectations.cr @@ -17,14 +17,14 @@ module Spectator::DSL # Mark the current test as pending and immediately abort. # A reason can be specified with *message*. - def pending(message = PendingResult::DEFAULT_REASON) - raise ExamplePending.new(message) + def pending(message = PendingResult::DEFAULT_REASON, *, _file = __FILE__, _line = __LINE__) + raise ExamplePending.new(Location.new(_file, _line), message) end # Mark the current test as skipped and immediately abort. # A reason can be specified with *message*. - def skip(message = PendingResult::DEFAULT_REASON) - raise ExamplePending.new(message) + def skip(message = PendingResult::DEFAULT_REASON, *, _file = __FILE__, _line = __LINE__) + raise ExamplePending.new(Location.new(_file, _line), message) end # Starts an expectation. diff --git a/src/spectator/example_pending.cr b/src/spectator/example_pending.cr index 6099fed..16c0709 100644 --- a/src/spectator/example_pending.cr +++ b/src/spectator/example_pending.cr @@ -2,5 +2,13 @@ module Spectator # Exception that indicates an example is pending and should be skipped. # When raised within a test, the test should abort. class ExamplePending < Exception + # Location of where the example was aborted. + getter location : Location? + + # Creates the exception. + # Specify *location* to where the example was aborted. + def initialize(@location : Location? = nil, message : String? = nil, cause : Exception? = nil) + super(message, cause) + end end end diff --git a/src/spectator/harness.cr b/src/spectator/harness.cr index 47e2b0f..8034262 100644 --- a/src/spectator/harness.cr +++ b/src/spectator/harness.cr @@ -123,7 +123,7 @@ module Spectator when ExampleFailed FailResult.new(elapsed, error, @expectations) when ExamplePending - PendingResult.new(error.message || PendingResult::DEFAULT_REASON, elapsed, @expectations) + PendingResult.new(error.message || PendingResult::DEFAULT_REASON, error.location, elapsed, @expectations) else ErrorResult.new(elapsed, error, @expectations) end diff --git a/src/spectator/pending_result.cr b/src/spectator/pending_result.cr index 0854fdc..29b0fbc 100644 --- a/src/spectator/pending_result.cr +++ b/src/spectator/pending_result.cr @@ -10,10 +10,15 @@ module Spectator # Reason the example was skipped or marked pending. getter reason : String + # Location the pending result was triggered from. + getter location : Location? + # Creates the result. # *elapsed* is the length of time it took to run the example. # A *reason* for the skip/pending result can be specified. - def initialize(@reason = DEFAULT_REASON, elapsed = Time::Span::ZERO, expectations = [] of Expectation) + # If the pending result was triggered inside of an example, then *location* can be set. + def initialize(@reason = DEFAULT_REASON, @location = nil, + elapsed = Time::Span::ZERO, expectations = [] of Expectation) super(elapsed, expectations) end