Consistency with location and location?

This commit is contained in:
Michael Miller 2021-07-17 16:34:15 -06:00
parent e316dd8a11
commit 52a0ae938a
No known key found for this signature in database
GPG key ID: FB9F12F7C646A4AD
3 changed files with 11 additions and 4 deletions

View file

@ -41,12 +41,19 @@ module Spectator
# Attempts to retrieve the location where the example failed. # Attempts to retrieve the location where the example failed.
# This only works if the location of the failed expectation was reported. # This only works if the location of the failed expectation was reported.
# If available, returns a `Location`, otherwise `nil`. # If available, returns a `Location`, otherwise `nil`.
def location : Location? def location? : Location?
return unless error = @error.as?(ExampleFailed) return unless error = @error.as?(ExampleFailed)
error.location? error.location?
end end
# Attempts to retrieve the location where the example failed.
# This only works if the location of the failed expectation was reported.
# If available, returns a `Location`, otherwise raises `NilAssertionError`.
def location : Location
location? || raise(NilAssertionError.new("Source location of result unavailable"))
end
# One-word description of the result. # One-word description of the result.
def to_s(io) def to_s(io)
io << "fail" io << "fail"

View file

@ -71,8 +71,8 @@ module Spectator::Formatting::Components
# Produces the location line. # Produces the location line.
# This is where the result was determined. # This is where the result was determined.
private def location_line(io) private def location_line(io)
location = if (result = @example.result).responds_to?(:location) location = if (result = @example.result).responds_to?(:location?)
result.location result.location?
end end
location ||= @example.location? location ||= @example.location?
return unless location return unless location

View file

@ -11,7 +11,7 @@ module Spectator
getter reason : String getter reason : String
# Location the pending result was triggered from. # Location the pending result was triggered from.
getter location : Location? getter! location : Location
# Creates the result. # Creates the result.
# *elapsed* is the length of time it took to run the example. # *elapsed* is the length of time it took to run the example.