class RSpec::Matchers::BuiltIn::Has

@api private Provides the implementation for `has_<predicate>`. Not intended to be instantiated directly.

Public Class Methods

new(method_name, *args, &block) click to toggle source
# File lib/rspec/matchers/built_in/has.rb, line 8
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 40
def description
  [method_description, args_description].compact.join(' ')
end
does_not_match?(actual, &block) click to toggle source

@private

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

@api private @return [String]

# File lib/rspec/matchers/built_in/has.rb, line 28
def failure_message
  validity_message || "expected ##{predicate}#{failure_message_args_description} to return true, got false"
end
failure_message_when_negated() click to toggle source

@api private @return [String]

# File lib/rspec/matchers/built_in/has.rb, line 34
def failure_message_when_negated
  validity_message || "expected ##{predicate}#{failure_message_args_description} to return false, got true"
end
matches?(actual, &block) click to toggle source

@private

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

Private Instance Methods

args_description() click to toggle source
# File lib/rspec/matchers/built_in/has.rb, line 83
def args_description
  return nil if @args.empty?
  @args.map { |arg| RSpec::Support::ObjectFormatter.format(arg) }.join(', ')
end
failure_message_args_description() click to toggle source
# File lib/rspec/matchers/built_in/has.rb, line 88
def failure_message_args_description
  desc = args_description
  "(#{desc})" if desc
end
method_description() click to toggle source
# File lib/rspec/matchers/built_in/has.rb, line 79
def method_description
  @method_name.to_s.gsub('_', ' ')
end
predicate() click to toggle source
# File lib/rspec/matchers/built_in/has.rb, line 71
def predicate
  # 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
  @predicate ||= :"has_#{Matchers::HAS_REGEX.match(@method_name.to_s).captures.first}?"
end
predicate_accessible?() click to toggle source
# File lib/rspec/matchers/built_in/has.rb, line 46
def predicate_accessible?
  !private_predicate? && predicate_exists?
end
predicate_exists?() click to toggle source
# File lib/rspec/matchers/built_in/has.rb, line 63
def predicate_exists?
  @actual.respond_to? predicate
end
predicate_matches?() click to toggle source
# File lib/rspec/matchers/built_in/has.rb, line 67
def predicate_matches?
  @actual.__send__(predicate, *@args, &@block)
end
private_predicate?() click to toggle source

:nocov:

# File lib/rspec/matchers/built_in/has.rb, line 53
def private_predicate?
  @actual.private_methods.include? predicate.to_s
end
validity_message() click to toggle source
# File lib/rspec/matchers/built_in/has.rb, line 93
def validity_message
  if private_predicate?
    "expected #{@actual} to respond to `#{predicate}` but `#{predicate}` is a private method"
  elsif !predicate_exists?
    "expected #{@actual} to respond to `#{predicate}`"
  end
end