Handle undefined methods with have_attributes matcher

Instead of producing a compilation error on missing/undefind methods, 
substitute in an "undefined" value.
This shows better output to the user.
This commit is contained in:
Michael Miller 2021-09-26 12:50:17 -06:00
parent 20e17851c6
commit 99ced17516
No known key found for this signature in database
GPG key ID: F9A0C5C65B162436
2 changed files with 14 additions and 1 deletions

View file

@ -8,6 +8,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- Fix usage of `be ===` and `be =~` [#34](https://github.com/icy-arctic-fox/spectator/issues/34)
### Changed
- Elegantly handle missing/undefined methods with `have_attributes` matcher.
## [0.10.1] - 2021-09-16
### Fixed
- Fix `Spectator.configure` block calls to `filter_run_excluding` and `filter_run_including`. [#61](https://gitlab.com/arctic-fox/spectator/-/issues/61)

View file

@ -10,6 +10,16 @@ module Spectator::Matchers
# Each key in the tuple is the attribute/method name,
# and the corresponding value is the expected value to match against.
struct AttributesMatcher(ExpectedType) < Matcher
# Stand-in for undefined methods on types.
private module Undefined
extend self
# Text displayed when a method is undefined.
def inspect(io)
io << "<Method undefined>"
end
end
# Expected value and label.
private getter expected
@ -51,7 +61,7 @@ module Spectator::Matchers
{% begin %}
{
{% for attribute in ExpectedType.keys %}
{{attribute}}: object.{{attribute}},
{{attribute}}: object.responds_to?({{attribute.symbolize}}) ? object.{{attribute}} : Undefined,
{% end %}
}
{% end %}