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 it {{description}} do
expect do expect do
{{block.body}} {{block.body}}
end.to raise_error(Spectator::ExpectationFailed) end.to raise_error(Spectator::ExampleFailed)
end end
end end

View file

@ -12,7 +12,7 @@ module Spectator::DSL
# Immediately fail the current test. # Immediately fail the current test.
# A reason can be specified with *message*. # A reason can be specified with *message*.
def fail(message = "Example failed", *, _file = __FILE__, _line = __LINE__) 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 end
# Mark the current test as pending and immediately abort. # 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" require "./expectation"
module Spectator module Spectator
# Exception that indicates an expectation from a test failed. # Exception that indicates an expectation from a test failed.
# When raised within a test, the test should abort. # When raised within a test, the test should abort.
class ExpectationFailed < Exception class ExpectationFailed < ExampleFailed
# Expectation that failed. # Expectation that failed.
getter expectation : Expectation getter expectation : Expectation
# Creates the exception. # Creates the exception.
def initialize(@expectation : Expectation, message : String? = nil, cause : Exception? = nil) def initialize(@expectation : Expectation, message : String? = nil, cause : Exception? = nil)
super(message, cause) super(expectation.location?, message, cause)
end end
end end
end end

View file

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