Class: RSpec::SleepingKingStudios::Matchers::Core::HavePredicateMatcher
- Inherits:
-
BaseMatcher
- Object
- BaseMatcher
- RSpec::SleepingKingStudios::Matchers::Core::HavePredicateMatcher
- Includes:
- Matchers::Composable, 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.
26 27 28 29 30 |
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_predicate_matcher.rb', line 26 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.
20 21 22 |
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_predicate_matcher.rb', line 20 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.
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_predicate_matcher.rb', line 64 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.
78 79 80 81 82 |
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_predicate_matcher.rb', line 78 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.
40 41 42 43 44 |
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_predicate_matcher.rb', line 40 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.
52 53 54 55 56 57 58 59 60 |
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_predicate_matcher.rb', line 52 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 |