Class: RSpec::SleepingKingStudios::Matchers::Core::HaveReaderMatcher
- Inherits:
-
BaseMatcher
- Object
- BaseMatcher
- RSpec::SleepingKingStudios::Matchers::Core::HaveReaderMatcher
- Includes:
- Shared::MatchProperty
- Defined in:
- lib/rspec/sleeping_king_studios/matchers/core/have_reader_matcher.rb
Overview
Matcher for testing whether an object has a specific property reader, e.g. responds to :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) ⇒ HaveReaderMatcher
constructor
A new instance of HaveReaderMatcher.
-
#matches?(actual) ⇒ Boolean
Checks if the object responds to #expected.
-
#with(value) ⇒ HaveReaderMatcher
(also: #with_value)
Sets a value expectation.
Constructor Details
#initialize(expected, allow_private: false) ⇒ HaveReaderMatcher
Returns a new instance of HaveReaderMatcher.
19 20 21 22 |
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_reader_matcher.rb', line 19 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.
26 27 28 |
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_reader_matcher.rb', line 26 def allow_private? !!@allow_private end |
#description ⇒ Object
31 32 33 34 |
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_reader_matcher.rb', line 31 def description = value_to_string "have reader :#{@expected}#{@value_set ? " with value #{}" : ''}" end |
#does_not_match?(actual) ⇒ Boolean
Inverse of #matches? method.
37 38 39 40 41 |
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_reader_matcher.rb', line 37 def does_not_match? actual super matches_reader?(: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.
71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_reader_matcher.rb', line 71 def = "expected #{@actual.inspect} to respond to :#{@expected}" << " and return #{value_to_string}" if @value_set if !@matches_reader << ", but did not respond to :#{@expected}" elsif !@matches_reader_value << ", but returned #{@actual.send(@expected).inspect}" end # if 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.
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_reader_matcher.rb', line 85 def = "expected #{@actual.inspect} not to respond to :#{@expected}" << " and return #{value_to_string}" if @value_set errors = [] errors << "responded to :#{@expected}" if @matches_reader 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. Additionally, if a value expectation is set, compares the value of #expected to the specified value.
51 52 53 54 55 |
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_reader_matcher.rb', line 51 def matches? actual super matches_reader?(:all?) end |
#with(value) ⇒ HaveReaderMatcher Also known as: with_value
Sets a value expectation. The matcher will compare the value from #property with the specified value.
63 64 65 66 67 |
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_reader_matcher.rb', line 63 def with value @value = value @value_set = true self end |