class RSpec::Matchers::BuiltIn::RespondTo
Not intended to be instantiated directly.
Provides the implementation for ‘respond_to`.
@api private
def argument
- Api: - public
def argument self end
def description
-
(String)
-
Other tags:
- Api: - private
def description "respond to #{pp_names}#{with_arity}" end
def does_not_match?(actual)
- Private: -
def does_not_match?(actual) find_failing_method_names(actual, :select).empty? end
def failure_message
-
(String)
-
Other tags:
- Api: - private
def failure_message "expected #{actual_formatted} to respond to #{@failing_method_names.map { |name| description_of(name) }.join(', ')}#{with_arity}" end
def failure_message_when_negated
-
(String)
-
Other tags:
- Api: - private
def failure_message_when_negated failure_message.sub(/to respond to/, 'not to respond to') end
def find_failing_method_names(actual, filter_method)
def find_failing_method_names(actual, filter_method) @actual = actual @failing_method_names = @names.__send__(filter_method) do |name| @actual.respond_to?(name) && matches_arity?(actual, name) end end
def ignoring_method_signature_failure!
- Api: - private
def ignoring_method_signature_failure! @ignoring_method_signature_failure = true end
def initialize(*names)
def initialize(*names) @names = names @expected_arity = nil @expected_keywords = [] @ignoring_method_signature_failure = false @unlimited_arguments = nil @arbitrary_keywords = nil end
def matches?(actual)
- Private: -
def matches?(actual) find_failing_method_names(actual, :reject).empty? end
def matches_arity?(actual, name)
def matches_arity?(actual, name) ArityCheck.new(@expected_arity, @expected_keywords, @arbitrary_keywords, @unlimited_arguments).matches?(actual, name) rescue NameError return true if @ignoring_method_signature_failure raise ArgumentError, "The #{matcher_name} matcher requires that " \ "the actual object define the method(s) in " \ "order to check arity, but the method " \ "`#{name}` is not defined. Remove the arity " \ "check or define the method to continue." end
def pp_names
def pp_names @names.length == 1 ? "##{@names.first}" : description_of(@names) end
def with(n)
- Api: - public
def with(n) @expected_arity = n self end
def with_any_keywords
- Api: - public
def with_any_keywords @arbitrary_keywords = true self end
def with_arity
def with_arity str = ''.dup str << " with #{with_arity_string}" if @expected_arity str << " #{str.length == 0 ? 'with' : 'and'} #{with_keywords_string}" if @expected_keywords && @expected_keywords.count > 0 str << " #{str.length == 0 ? 'with' : 'and'} unlimited arguments" if @unlimited_arguments str << " #{str.length == 0 ? 'with' : 'and'} any keywords" if @arbitrary_keywords str end
def with_arity_string
def with_arity_string "#{@expected_arity} argument#{@expected_arity == 1 ? '' : 's'}" end
def with_keywords(*keywords)
- Example: with an expected number of arguments -
Other tags:
- Api: - public
def with_keywords(*keywords) @expected_keywords = keywords self end
def with_keywords_string
def with_keywords_string kw_str = case @expected_keywords.count when 1 @expected_keywords.first.inspect when 2 @expected_keywords.map(&:inspect).join(' and ') else "#{@expected_keywords[0...-1].map(&:inspect).join(', ')}, and #{@expected_keywords.last.inspect}" end "keyword#{@expected_keywords.count == 1 ? '' : 's'} #{kw_str}" end
def with_unlimited_arguments
- Api: - public
def with_unlimited_arguments @unlimited_arguments = true self end