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

new(expected) click to toggle source

@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

description() click to toggle source

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
failure_message() click to toggle source

(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
failure_message_when_negated() click to toggle source

(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
matches?(actual) click to toggle source

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.
# 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)
Alias for: with_value
with_value(value) click to toggle source

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
Also aliased as: with

Private Instance Methods

apply_boolean_expectation() click to toggle source
# 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
strict_matching?() click to toggle source
# 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