Use location of the 'should' keyword for their expectation

This commit is contained in:
Michael Miller 2022-12-19 22:27:58 -07:00
parent faff2933e6
commit acf810553a
No known key found for this signature in database
GPG key ID: AC78B32D30CE34A2
2 changed files with 19 additions and 12 deletions

View file

@ -11,6 +11,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed ### Fixed
- Fix invalid syntax (unterminated call) when recording calls to stubs with an un-named splat. - Fix invalid syntax (unterminated call) when recording calls to stubs with an un-named splat.
### Changed
- Expectations using 'should' syntax report file and line where the 'should' keyword is instead of the test start.
## [0.11.5] - 2022-12-18 ## [0.11.5] - 2022-12-18
### Added ### Added
- Added support for mock modules and types that include mocked modules. - Added support for mock modules and types that include mocked modules.

View file

@ -22,51 +22,55 @@ class Object
# ``` # ```
# require "spectator/should" # require "spectator/should"
# ``` # ```
def should(matcher, message = nil) def should(matcher, message = nil, *, _file = __FILE__, _line = __LINE__)
actual = ::Spectator::Value.new(self) actual = ::Spectator::Value.new(self)
location = ::Spectator::Location.new(_file, _line)
match_data = matcher.match(actual) match_data = matcher.match(actual)
expectation = ::Spectator::Expectation.new(match_data, message: message) expectation = ::Spectator::Expectation.new(match_data, location, message)
::Spectator::Harness.current.report(expectation) ::Spectator::Harness.current.report(expectation)
end end
# Works the same as `#should` except the condition is inverted. # Works the same as `#should` except the condition is inverted.
# When `#should` succeeds, this method will fail, and vice-versa. # When `#should` succeeds, this method will fail, and vice-versa.
def should_not(matcher, message = nil) def should_not(matcher, message = nil, *, _file = __FILE__, _line = __LINE__)
actual = ::Spectator::Value.new(self) actual = ::Spectator::Value.new(self)
location = ::Spectator::Location.new(_file, _line)
match_data = matcher.negated_match(actual) match_data = matcher.negated_match(actual)
expectation = ::Spectator::Expectation.new(match_data, message: message) expectation = ::Spectator::Expectation.new(match_data, location, message)
::Spectator::Harness.current.report(expectation) ::Spectator::Harness.current.report(expectation)
end end
# Works the same as `#should` except that the condition check is postponed. # Works the same as `#should` except that the condition check is postponed.
# The expectation is checked after the example finishes and all hooks have run. # The expectation is checked after the example finishes and all hooks have run.
def should_eventually(matcher, message = nil) def should_eventually(matcher, message = nil, *, _file = __FILE__, _line = __LINE__)
::Spectator::Harness.current.defer { should(matcher, message) } ::Spectator::Harness.current.defer { should(matcher, message, _file: _file, _line: _line) }
end end
# Works the same as `#should_not` except that the condition check is postponed. # Works the same as `#should_not` except that the condition check is postponed.
# The expectation is checked after the example finishes and all hooks have run. # The expectation is checked after the example finishes and all hooks have run.
def should_never(matcher, message = nil) def should_never(matcher, message = nil, *, _file = __FILE__, _line = __LINE__)
::Spectator::Harness.current.defer { should_not(matcher, message) } ::Spectator::Harness.current.defer { should_not(matcher, message, _file: _file, _line: _line) }
end end
end end
struct Proc(*T, R) struct Proc(*T, R)
# Extension method to create an expectation for a block of code (proc). # Extension method to create an expectation for a block of code (proc).
# Depending on the matcher, the proc may be executed multiple times. # Depending on the matcher, the proc may be executed multiple times.
def should(matcher, message = nil) def should(matcher, message = nil, *, _file = __FILE__, _line = __LINE__)
actual = ::Spectator::Block.new(self) actual = ::Spectator::Block.new(self)
location = ::Spectator::Location.new(_file, _line)
match_data = matcher.match(actual) match_data = matcher.match(actual)
expectation = ::Spectator::Expectation.new(match_data, message: message) expectation = ::Spectator::Expectation.new(match_data, location, message)
::Spectator::Harness.current.report(expectation) ::Spectator::Harness.current.report(expectation)
end end
# Works the same as `#should` except the condition is inverted. # Works the same as `#should` except the condition is inverted.
# When `#should` succeeds, this method will fail, and vice-versa. # When `#should` succeeds, this method will fail, and vice-versa.
def should_not(matcher, message = nil) def should_not(matcher, message = nil, *, _file = __FILE__, _line = __LINE__)
actual = ::Spectator::Block.new(self) actual = ::Spectator::Block.new(self)
location = ::Spectator::Location.new(_file, _line)
match_data = matcher.negated_match(actual) match_data = matcher.negated_match(actual)
expectation = ::Spectator::Expectation.new(match_data, message: message) expectation = ::Spectator::Expectation.new(match_data, location, message)
::Spectator::Harness.current.report(expectation) ::Spectator::Harness.current.report(expectation)
end end
end end