diff --git a/spec/spec_helper.cr b/spec/spec_helper.cr index c9176e6..6c9a806 100644 --- a/spec/spec_helper.cr +++ b/spec/spec_helper.cr @@ -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 diff --git a/src/spectator/dsl/expectations.cr b/src/spectator/dsl/expectations.cr index 290c716..b8a18e4 100644 --- a/src/spectator/dsl/expectations.cr +++ b/src/spectator/dsl/expectations.cr @@ -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. diff --git a/src/spectator/example_failed.cr b/src/spectator/example_failed.cr new file mode 100644 index 0000000..2170604 --- /dev/null +++ b/src/spectator/example_failed.cr @@ -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 diff --git a/src/spectator/expectation_failed.cr b/src/spectator/expectation_failed.cr index 67c6dea..2fe3941 100644 --- a/src/spectator/expectation_failed.cr +++ b/src/spectator/expectation_failed.cr @@ -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 diff --git a/src/spectator/harness.cr b/src/spectator/harness.cr index 7a4df88..5877d56 100644 --- a/src/spectator/harness.cr +++ b/src/spectator/harness.cr @@ -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)