Track source location of pending result

This commit is contained in:
Michael Miller 2021-07-17 16:25:32 -06:00
parent 7cb1545e83
commit 6c6dff363b
No known key found for this signature in database
GPG key ID: FB9F12F7C646A4AD
4 changed files with 19 additions and 6 deletions

View file

@ -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.

View file

@ -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

View file

@ -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

View file

@ -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