Class: RSpec::SleepingKingStudios::Matchers::Core::HavePredicateMatcher
- Inherits:
-
BaseMatcher
- Object
- BaseMatcher
- RSpec::SleepingKingStudios::Matchers::Core::HavePredicateMatcher
- Includes:
- Shared::MatchProperty
- Defined in:
- lib/rspec/sleeping_king_studios/matchers/core/have_predicate_matcher.rb
Overview
Matcher for testing whether an object has a specific predicate, 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
-
#description ⇒ String
Generates a description of the matcher expectation.
-
#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) ⇒ HavePredicateMatcher
constructor
A new instance of HavePredicateMatcher.
-
#matches?(actual) ⇒ Boolean
Checks if the object responds to #expected?.
-
#with_value(value) ⇒ HaveReaderMatcher
(also: #with)
Sets a value expectation.
Methods inherited from BaseMatcher
Constructor Details
#initialize(expected) ⇒ HavePredicateMatcher
Returns a new instance of HavePredicateMatcher.
25 26 27 28 29 |
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_predicate_matcher.rb', line 25 def initialize expected @expected = expected.to_s.gsub(/\?$/, '').intern apply_boolean_expectation if strict_matching? end |
Instance Method Details
#description ⇒ String
Generates a description of the matcher expectation.
19 20 21 |
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_predicate_matcher.rb', line 19 def description "have predicate :#{@expected}?" 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.
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_predicate_matcher.rb', line 63 def = "expected #{@actual.inspect} to respond to :#{@expected}?" << " and return #{value_to_string}" if @value_set if !@matches_predicate << ", but did not respond to :#{@expected}?" elsif !@matches_predicate_value << ", but returned #{@actual.send(:"#{@expected}?").inspect}" end # if-elsif 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.
77 78 79 80 81 |
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_predicate_matcher.rb', line 77 def = "expected #{@actual.inspect} not to respond to :#{@expected}?" << " and return #{value_to_string}" if @value_set 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.
39 40 41 42 43 |
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_predicate_matcher.rb', line 39 def matches? actual super responds_to_predicate? && matches_predicate_value? end |
#with_value(value) ⇒ HaveReaderMatcher Also known as: with
Sets a value expectation. The matcher will compare the value from #property? with the specified value.
51 52 53 54 55 56 57 58 59 |
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_predicate_matcher.rb', line 51 def with_value value if strict_matching? && !(value === true || value === false) raise ArgumentError.new 'predicate must return true or false' end # if @value = value @value_set = true self end |