@api private Provides the implementation for `respond_to`. Not intended to be instantiated directly.
# File lib/rspec/matchers/built_in/respond_to.rb, line 10 def initialize(*names) @names = names @expected_arity = nil end
@api public No-op. Intended to be used as syntactic sugar when using `with`.
@example
expect(obj).to respond_to(:message).with(3).arguments
# File lib/rspec/matchers/built_in/respond_to.rb, line 30 def argument self end
@api private @return [String]
# File lib/rspec/matchers/built_in/respond_to.rb, line 59 def description "respond to #{pp_names}#{with_arity}" end
@private
# File lib/rspec/matchers/built_in/respond_to.rb, line 41 def does_not_match?(actual) find_failing_method_names(actual, :select).empty? end
@api private @return [String]
# File lib/rspec/matchers/built_in/respond_to.rb, line 47 def failure_message "expected #{@actual.inspect} to respond to #{@failing_method_names.collect {|name| name.inspect }.join(', ')}#{with_arity}" end
@api private @return [String]
# File lib/rspec/matchers/built_in/respond_to.rb, line 53 def failure_message_when_negated failure_message.sub(/to respond to/, 'not to respond to') end
@private
# File lib/rspec/matchers/built_in/respond_to.rb, line 36 def matches?(actual) find_failing_method_names(actual, :reject).empty? end
@private
# File lib/rspec/matchers/built_in/respond_to.rb, line 64 def supports_block_expectations? false end
@api public Specifies the number of expected arguments.
@example
expect(obj).to respond_to(:message).with(3).arguments
# File lib/rspec/matchers/built_in/respond_to.rb, line 20 def with(n) @expected_arity = n self end
# File lib/rspec/matchers/built_in/respond_to.rb, line 70 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
# File lib/rspec/matchers/built_in/respond_to.rb, line 77 def matches_arity?(actual, name) return true unless @expected_arity signature = Support::MethodSignature.new(actual.method(name)) Support::MethodSignatureVerifier.new(signature, Array.new(@expected_arity)).valid? end
# File lib/rspec/matchers/built_in/respond_to.rb, line 89 def pp_names # Ruby 1.9 returns the same thing for array.to_s as array.inspect, so just use array.inspect here @names.length == 1 ? "##{@names.first}" : @names.inspect end
# File lib/rspec/matchers/built_in/respond_to.rb, line 84 def with_arity @expected_arity.nil?? "" : " with #{@expected_arity} argument#{@expected_arity == 1 ? '' : 's'}" end