Class: RSpec::SleepingKingStudios::Matchers::Core::HavePropertyMatcher
- Inherits:
-
BaseMatcher
- Object
- BaseMatcher
- RSpec::SleepingKingStudios::Matchers::Core::HavePropertyMatcher
- Includes:
- Shared::MatchProperty
- Defined in:
- lib/rspec/sleeping_king_studios/matchers/core/have_property_matcher.rb
Overview
Matcher for testing whether an object has a specific property, e.g. responds to #property and #property= and has the specified value for #property.
Constant Summary
Constants included from Description
Description::DEFAULT_EXPECTED_ITEMS
Instance Attribute Summary
Attributes inherited from BaseMatcher
Instance Method Summary collapse
-
#allow_private? ⇒ Boolean
True if the matcher matches private reader methods, otherwise false.
- #description ⇒ Object
-
#does_not_match?(actual) ⇒ Boolean
Inverse of #matches? method.
-
#failure_message ⇒ Object
Message for when the object does not match, but was expected to.
-
#failure_message_when_negated ⇒ Object
Message for when the object matches, but was expected not to.
-
#initialize(expected, allow_private: false) ⇒ HavePropertyMatcher
constructor
A new instance of HavePropertyMatcher.
-
#matches?(actual) ⇒ Boolean
Checks if the object responds to #expected and #expected=.
-
#with(value) ⇒ HavePropertyMatcher
(also: #with_value)
Sets a value expectation.
Constructor Details
#initialize(expected, allow_private: false) ⇒ HavePropertyMatcher
Returns a new instance of HavePropertyMatcher.
20 21 22 23 |
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_property_matcher.rb', line 20 def initialize expected, allow_private: false @expected = expected.intern @allow_private = allow_private end |
Instance Method Details
#allow_private? ⇒ Boolean
Returns True if the matcher matches private reader methods, otherwise false.
27 28 29 |
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_property_matcher.rb', line 27 def allow_private? !!@allow_private end |
#description ⇒ Object
32 33 34 35 |
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_property_matcher.rb', line 32 def description = value_to_string "have property :#{@expected}#{@value_set ? " with value #{}" : ''}" end |
#does_not_match?(actual) ⇒ Boolean
Inverse of #matches? method.
38 39 40 41 42 |
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_property_matcher.rb', line 38 def does_not_match? actual @actual = actual matches_property?(:none?) end |
#failure_message ⇒ Object
Message for when the object does not match, but was expected to. Make sure to always call #matches? first to set up the matcher state.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_property_matcher.rb', line 72 def methods = [] methods << ":#{@expected}" unless @matches_reader methods << ":#{@expected}=" unless @matches_writer = "expected #{@actual.inspect} to respond to :#{@expected} and :#{@expected}=" << " and return #{value_to_string}" if @value_set errors = [] errors << "did not respond to #{methods.join " or "}" unless methods.empty? if @matches_reader errors << "returned #{@actual.send(@expected).inspect}" unless @matches_reader_value || !@value_set end # if << ", but #{errors.join(" and ")}" end |
#failure_message_when_negated ⇒ Object
Message for when the object matches, but was expected not to. Make sure to always call #matches? first to set up the matcher state.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_property_matcher.rb', line 92 def methods = [] methods << ":#{@expected}" if @matches_reader methods << ":#{@expected}=" if @matches_writer = "expected #{@actual.inspect} not to respond to :#{@expected} or :#{@expected}=" << " and return #{value_to_string}" if @value_set errors = [] errors << "responded to #{methods.join " and "}" unless methods.empty? errors << "returned #{@actual.send(@expected).inspect}" if @matches_reader_value << ", but #{errors.join(" and ")}" end |
#matches?(actual) ⇒ Boolean
Checks if the object responds to #expected and #expected=. Additionally, if a value expectation is set, compares the result of calling :expected to the value.
52 53 54 55 56 |
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_property_matcher.rb', line 52 def matches? actual super matches_property?(:all?) end |
#with(value) ⇒ HavePropertyMatcher Also known as: with_value
Sets a value expectation. The matcher will compare the value to the result of calling #property.
64 65 66 67 68 |
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_property_matcher.rb', line 64 def with value @value = value @value_set = true self end |