class RSpec::Matchers::BuiltIn::DynamicPredicate

@api private Provides the implementation for dynamic predicate matchers. Not intended to be inherited directly.

Public Class Methods

new(method_name, *args, &block) click to toggle source
# File lib/rspec/matchers/built_in/has.rb, line 10
def initialize(method_name, *args, &block)
  @method_name, @args, @block = method_name, args, block
end

Public Instance Methods

description() click to toggle source

@api private @return [String]

# File lib/rspec/matchers/built_in/has.rb, line 43
def description
  "#{method_description}#{args_to_sentence}"
end
does_not_match?(actual, &block) click to toggle source

@private

# File lib/rspec/matchers/built_in/has.rb, line 23
def does_not_match?(actual, &block)
  @actual = actual
  @block ||= block
  predicate_accessible? && predicate_matches?(false)
end
failure_message() click to toggle source

@api private @return [String]

# File lib/rspec/matchers/built_in/has.rb, line 31
def failure_message
  failure_message_expecting(true)
end
failure_message_when_negated() click to toggle source

@api private @return [String]

# File lib/rspec/matchers/built_in/has.rb, line 37
def failure_message_when_negated
  failure_message_expecting(false)
end
matches?(actual, &block) click to toggle source

@private

# File lib/rspec/matchers/built_in/has.rb, line 16
def matches?(actual, &block)
  @actual = actual
  @block ||= block
  predicate_accessible? && predicate_matches?
end

Private Instance Methods

expectation_of(value) click to toggle source
# File lib/rspec/matchers/built_in/has.rb, line 126
def expectation_of(value)
  if RSpec::Expectations.configuration.strict_predicate_matchers?
    "return #{value}"
  elsif value
    "be truthy"
  else
    "be falsey"
  end
end
failure_message_expecting(value) click to toggle source
# File lib/rspec/matchers/built_in/has.rb, line 121
def failure_message_expecting(value)
  validity_message ||
    "expected `#{actual_formatted}.#{predicate}#{args_to_s}` to #{expectation_of value}, got #{description_of @predicate_result}"
end
failure_to_respond_explanation() click to toggle source
# File lib/rspec/matchers/built_in/has.rb, line 142
def failure_to_respond_explanation
  if private_predicate?
    " but `#{predicate}` is a private method"
  end
end
method_description() click to toggle source
# File lib/rspec/matchers/built_in/has.rb, line 117
def method_description
  EnglishPhrasing.split_words(@method_name)
end
methods_include?(method) click to toggle source
# File lib/rspec/matchers/built_in/has.rb, line 79
def methods_include?(method)
  @actual.methods.include?(method.to_s)
end
predicate_accessible?() click to toggle source
# File lib/rspec/matchers/built_in/has.rb, line 68
def predicate_accessible?
  really_responds_to?(predicate)
end
predicate_matches?(value=true) click to toggle source
# File lib/rspec/matchers/built_in/has.rb, line 101
def predicate_matches?(value=true)
  if RSpec::Expectations.configuration.strict_predicate_matchers?
    value == predicate_result
  else
    value == !!predicate_result
  end
end
predicate_method_name() click to toggle source
# File lib/rspec/matchers/built_in/has.rb, line 97
def predicate_method_name
  predicate
end
predicate_result() click to toggle source
# File lib/rspec/matchers/built_in/has.rb, line 93
def predicate_result
  @predicate_result = actual.__send__(predicate_method_name, *@args, &@block)
end
private_predicate?() click to toggle source

:nocov:

# File lib/rspec/matchers/built_in/has.rb, line 75
def private_predicate?
  @actual.private_methods.include? predicate.to_s
end
really_responds_to?(method) click to toggle source
# File lib/rspec/matchers/built_in/has.rb, line 53
def really_responds_to?(method)
  if RSpec::Mocks::Double === @actual
    @actual.respond_to?(method) && methods_include?(method)
  else
    @actual.respond_to?(method)
  end
end
root() click to toggle source
# File lib/rspec/matchers/built_in/has.rb, line 109
def root
  # On 1.9, there appears to be a bug where String#match can return `false`
  # rather than the match data object. Changing to Regex#match appears to
  # work around this bug. For an example of this bug, see:
  # https://travis-ci.org/rspec/rspec-expectations/jobs/27549635
  self.class::REGEX.match(@method_name.to_s).captures.first
end
validity_message() click to toggle source
# File lib/rspec/matchers/built_in/has.rb, line 136
def validity_message
  return nil if predicate_accessible?

  "expected #{actual_formatted} to respond to `#{predicate}`#{failure_to_respond_explanation}"
end