Fix reporting for should syntax

This commit is contained in:
Michael Miller 2021-01-16 11:12:41 -07:00
parent 97923d6bcd
commit 3ec267abbb
No known key found for this signature in database
GPG key ID: FB9F12F7C646A4AD

View file

@ -17,21 +17,19 @@ class Object
# require "spectator/should"
# ```
def should(matcher)
# First argument of the `Expectation` initializer is the expression label.
# However, since this isn't a macro and we can't "look behind" this method call
# to see what it was invoked on, the argument is an empty string.
# Additionally, the source file and line can't be obtained.
actual = ::Spectator::TestValue.new(self)
source = ::Spectator::Source.new(__FILE__, __LINE__)
::Spectator::Expectations::ExpectationPartial.new(actual, source).to(matcher)
actual = ::Spectator::Value.new(self)
match_data = matcher.match(actual)
expectation = ::Spectator::Expectation.new(match_data)
::Spectator::Harness.current.report(expectation)
end
# Works the same as `#should` except the condition is inverted.
# When `#should` succeeds, this method will fail, and vice-versa.
def should_not(matcher)
actual = ::Spectator::TestValue.new(self)
source = ::Spectator::Source.new(__FILE__, __LINE__)
::Spectator::Expectations::ExpectationPartial.new(actual, source).to_not(matcher)
actual = ::Spectator::Value.new(self)
match_data = matcher.negated_match(actual)
expectation = ::Spectator::Expectation.new(match_data)
::Spectator::Harness.current.report(expectation)
end
# Works the same as `#should` except that the condition check is postphoned.
@ -51,17 +49,19 @@ struct Proc(*T, R)
# Extension method to create an expectation for a block of code (proc).
# Depending on the matcher, the proc may be executed multiple times.
def should(matcher)
actual = ::Spectator::TestBlock.new(self)
source = ::Spectator::Source.new(__FILE__, __LINE__)
::Spectator::Expectations::ExpectationPartial.new(actual, source).to(matcher)
actual = ::Spectator::Block.new(self)
match_data = matcher.match(actual)
expectation = ::Spectator::Expectation.new(match_data)
::Spectator::Harness.current.report(expectation)
end
# Works the same as `#should` except the condition is inverted.
# When `#should` succeeds, this method will fail, and vice-versa.
def should_not(matcher)
actual = ::Spectator::TestBlock.new(self)
source = ::Spectator::Source.new(__FILE__, __LINE__)
::Spectator::Expectations::BlockExpectationPartial.new(actual, source).to_not(matcher)
actual = ::Spectator::Block.new(self)
match_data = matcher.negated_match(actual)
expectation = ::Spectator::Expectation.new(match_data)
::Spectator::Harness.current.report(expectation)
end
end