Introduce non-expectation error ExampleFailed

Used by fail method.
Still todo: Output from failed example is missing because there are no 
expectations.
This commit is contained in:
Michael Miller 2021-07-10 03:32:55 -06:00
parent ccedcdac42
commit aa12cdf17c
No known key found for this signature in database
GPG key ID: F9A0C5C65B162436
5 changed files with 21 additions and 5 deletions

View file

@ -6,7 +6,7 @@ macro it_fails(description = nil, &block)
it {{description}} do
expect do
{{block.body}}
end.to raise_error(Spectator::ExpectationFailed)
end.to raise_error(Spectator::ExampleFailed)
end
end

View file

@ -12,7 +12,7 @@ module Spectator::DSL
# Immediately fail the current test.
# A reason can be specified with *message*.
def fail(message = "Example failed", *, _file = __FILE__, _line = __LINE__)
raise ExpectationFailed.new(Location.new(_file, _line), message)
raise ExampleFailed.new(Location.new(_file, _line), message)
end
# Mark the current test as pending and immediately abort.

View file

@ -0,0 +1,14 @@
require "./location"
module Spectator
# Exception that indicates an example failed.
# When raised within a test, the test should abort.
class ExampleFailed < Exception
getter! location : Location
# Creates the exception.
def initialize(@location : Location?, message : String? = nil, cause : Exception? = nil)
super(message, cause)
end
end
end

View file

@ -1,15 +1,16 @@
require "./example_failed"
require "./expectation"
module Spectator
# Exception that indicates an expectation from a test failed.
# When raised within a test, the test should abort.
class ExpectationFailed < Exception
class ExpectationFailed < ExampleFailed
# Expectation that failed.
getter expectation : Expectation
# Creates the exception.
def initialize(@expectation : Expectation, message : String? = nil, cause : Exception? = nil)
super(message, cause)
super(expectation.location?, message, cause)
end
end
end

View file

@ -1,4 +1,5 @@
require "./error_result"
require "./example_failed"
require "./example_pending"
require "./expectation"
require "./mocks"
@ -118,7 +119,7 @@ module Spectator
case error
when nil
PassResult.new(elapsed, @expectations)
when ExpectationFailed
when ExampleFailed
FailResult.new(elapsed, error, @expectations)
when ExamplePending
PendingResult.new(error.message || PendingResult::DEFAULT_REASON, elapsed, @expectations)