class RSpec::SleepingKingStudios::Matchers::BuiltIn::RespondToMatcher

Extensions to the built-in RSpec respond_to matcher.

Public Class Methods

new(*expected) click to toggle source
Calls superclass method
# File lib/rspec/sleeping_king_studios/matchers/built_in/respond_to_matcher.rb, line 12
def initialize *expected
  @include_all = [true, false].include?(expected.last) ? expected.pop : false

  super(*expected)
end

Public Instance Methods

description() click to toggle source

(see BaseMatcher#description)

# File lib/rspec/sleeping_king_studios/matchers/built_in/respond_to_matcher.rb, line 19
def description
  message = "respond to #{pp_names}"

  if method_signature_expectation?
    message << ' ' << method_signature_expectation.description
  end # if

  message
end
failure_message() click to toggle source

(see BaseMatcher#failure_message)

# File lib/rspec/sleeping_king_studios/matchers/built_in/respond_to_matcher.rb, line 30
def failure_message
  method_names = @failing_method_names || []
  messages     = []

  method_names.map do |method_name|
    message = "expected #{@actual.inspect} to respond to #{method_name.inspect}"
    reasons = @failing_method_reasons[method_name] || {}

    if reasons.key?(:does_not_respond_to_method)
      message << ", but #{@actual.inspect} does not respond to #{method_name.inspect}"
    elsif reasons.key?(:is_not_a_method)
      message << ", but #{@actual.inspect} does not define a method at #{method_name.inspect}"
    else
      errors = @failing_method_reasons[method_name]

      # TODO: Replace this with ", but received arguments did not match "\
      # " method signature:"
      message << " with arguments:\n" << format_errors(errors)
    end # if-elsif-else

    messages << message
  end # method

  messages.join "\n"
end
failure_message_when_negated() click to toggle source

(see BaseMatcher#failure_message_when_negated)

# File lib/rspec/sleeping_king_studios/matchers/built_in/respond_to_matcher.rb, line 57
def failure_message_when_negated
  @failing_method_names ||= []
  methods, messages = @failing_method_names, []

  methods.map do |method|
    message = "expected #{@actual.inspect} not to respond to #{method.inspect}"

    if method_signature_expectation?
      message << ' ' << method_signature_expectation.description
    end # if

    messages << message
  end # method

  messages.join "\n"
end

Private Instance Methods

find_failing_method_names(actual, filter_method) click to toggle source
# File lib/rspec/sleeping_king_studios/matchers/built_in/respond_to_matcher.rb, line 76
def find_failing_method_names actual, filter_method
  @actual = actual
  @failing_method_reasons = {}
  @failing_method_names   = @names.__send__(filter_method) do |method_name|
    unless @actual.respond_to?(method_name, @include_all)
      @failing_method_reasons[method_name] = {
        :does_not_respond_to_method => true
      } # end hash

      next false
    end # unless

    method =
      begin
        if actual.is_a?(Class) && method_name.intern == :new
          actual.instance_method(:initialize)
        else
          actual.method(method_name)
        end
      rescue NameError
        @failing_method_reasons[method_name] = {
          :is_not_a_method => true
        } # end hash

        next false
      end

    next true unless method_signature_expectation?

    unless check_method_signature(method)
      @failing_method_reasons[method_name] =
        method_signature_expectation.errors

      next false
    end # unless

    true
  end # send
end