class RSpec::SleepingKingStudios::Matchers::Core::HavePredicateMatcher
Matcher for testing whether an object has a specific predicate, e.g. responds to :property?.
@since 2.2.0
Public Class Methods
@param [String, Symbol] expected The predicate to check for on the actual
object.
# 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
Public Instance Methods
Generates a description of the matcher expectation.
@return [String] The matcher description.
# File lib/rspec/sleeping_king_studios/matchers/core/have_predicate_matcher.rb, line 19 def description "have predicate :#{@expected}?" end
(see BaseMatcher#failure_message
)
# File lib/rspec/sleeping_king_studios/matchers/core/have_predicate_matcher.rb, line 63 def failure_message message = "expected #{@actual.inspect} to respond to :#{@expected}?" message << " and return #{value_to_string}" if @value_set if !@matches_predicate message << ", but did not respond to :#{@expected}?" elsif !@matches_predicate_value message << ", but returned #{@actual.send(:"#{@expected}?").inspect}" end # if-elsif message end
(see BaseMatcher#failure_message_when_negated
)
# File lib/rspec/sleeping_king_studios/matchers/core/have_predicate_matcher.rb, line 77 def failure_message_when_negated message = "expected #{@actual.inspect} not to respond to :#{@expected}?" message << " and return #{value_to_string}" if @value_set message end
Checks if the object responds to expected?. Additionally, if a value expectation is set, compares the value of expected to the specified value.
@param [Object] actual The object to check.
@return [Boolean] true If the object responds to expected and matches
the value expectation (if any); otherwise false.
RSpec::SleepingKingStudios::Matchers::BaseMatcher#matches?
# 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
Sets a value expectation. The matcher will compare the value from property? with the specified value.
@param [Object] value The value to compare.
@return [HaveReaderMatcher] self
# 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
Private Instance Methods
# File lib/rspec/sleeping_king_studios/matchers/core/have_predicate_matcher.rb, line 85 def apply_boolean_expectation matcher = BeBooleanMatcher.new aliased = RSpec::Matchers::AliasedMatcher.new matcher, ->(str) { 'true or false' } @value = aliased @value_set = true end
# File lib/rspec/sleeping_king_studios/matchers/core/have_predicate_matcher.rb, line 93 def strict_matching? RSpec.configure { |config| config.sleeping_king_studios.matchers }.strict_predicate_matching end