Skip to content

Commit

Permalink
Merge pull request #533 from freerange/consistent-definitions-for-res…
Browse files Browse the repository at this point in the history
…pond-to

Consistent definitions for respond_to?
  • Loading branch information
floehopper authored Aug 20, 2022
2 parents b7a7d23 + 514fc6c commit 1ade384
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 27 deletions.
4 changes: 2 additions & 2 deletions lib/mocha/class_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ def stubba_class
@stubba_object
end

def respond_to?(method)
@stubba_object.allocate.respond_to?(method.to_sym)
def respond_to?(symbol, include_all = false)
@stubba_object.allocate.respond_to?(symbol.to_sym, include_all)
end

attr_reader :stubba_object
Expand Down
12 changes: 4 additions & 8 deletions lib/mocha/mock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -322,22 +322,18 @@ def method_missing(symbol, *arguments, &block) # rubocop:disable Style/MethodMis
end

# @private
def respond_to_missing?(symbol, include_private = false)
def respond_to_missing?(symbol, include_all)
if @responder
if @responder.method(:respond_to?).arity > 1
@responder.respond_to?(symbol, include_private)
else
@responder.respond_to?(symbol)
end
@responder.respond_to?(symbol, include_all)
else
@everything_stubbed || all_expectations.matches_method?(symbol)
end
end

if PRE_RUBY_V19
# @private
def respond_to?(symbol, include_private = false)
respond_to_missing?(symbol, include_private)
def respond_to?(symbol, include_all = false)
respond_to_missing?(symbol, include_all)
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def existing_public_method; end
def test_should_allow_stubbing_method_to_which_any_instance_responds
Mocha.configure { |c| c.stubbing_non_existent_method = :prevent }
klass = Class.new do
def respond_to?(method, _include_private = false)
def respond_to?(method, _include_all = false)
(method == :method_to_which_instance_responds)
end
end
Expand All @@ -82,7 +82,7 @@ def initialize(attrs = {})
@attributes = attrs
end

def respond_to?(method, _include_private = false)
def respond_to?(method, _include_all = false)
@attributes.key?(method) ? @attributes[method] : super
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/acceptance/stubbing_non_existent_class_method_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def test_should_allow_stubbing_method_to_which_class_responds
Mocha.configure { |c| c.stubbing_non_existent_method = :prevent }
klass = Class.new do
class << self
def respond_to?(method, _include_private = false)
def respond_to?(method, _include_all = false)
(method == :method_to_which_class_responds)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def existing_public_method; end
def test_should_allow_stubbing_method_to_which_instance_responds
Mocha.configure { |c| c.stubbing_non_existent_method = :prevent }
klass = Class.new do
def respond_to?(method, _include_private = false)
def respond_to?(method, _include_all = false)
(method == :method_to_which_instance_responds)
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/acceptance/stubbing_non_public_class_method_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def test_should_allow_stubbing_method_to_which_class_responds
Mocha.configure { |c| c.stubbing_non_public_method = :prevent }
klass = Class.new do
class << self
def respond_to?(method, _include_private_methods = false)
def respond_to?(method, _include_all = false)
(method == :method_to_which_class_responds)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def public_method; end
def test_should_allow_stubbing_method_to_which_instance_responds
Mocha.configure { |c| c.stubbing_non_public_method = :prevent }
instance = Class.new do
def respond_to?(method, _include_private_methods = false)
def respond_to?(method, _include_all = false)
(method == :method_to_which_instance_responds)
end
end.new
Expand Down
16 changes: 5 additions & 11 deletions test/unit/mock_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -247,35 +247,31 @@ def test_should_not_respond_to_unexpected_method

def test_should_respond_to_methods_which_the_responder_does_responds_to
instance = Class.new do
define_method(:respond_to?) { |_symbol| true }
define_method(:invoked_method) {}
end.new
mock = build_mock
mock.responds_like(instance)
assert_equal true, mock.respond_to?(:invoked_method)
end

def test_should_not_respond_to_methods_which_the_responder_does_not_responds_to
instance = Class.new do
define_method(:respond_to?) { |_symbol| false }
end.new
instance = Class.new.new
mock = build_mock
mock.responds_like(instance)
assert_equal false, mock.respond_to?(:invoked_method)
end

def test_should_respond_to_methods_which_the_responder_instance_does_responds_to
klass = Class.new do
define_method(:respond_to?) { |_symbol| true }
define_method(:invoked_method) {}
end
mock = build_mock
mock.responds_like_instance_of(klass)
assert_equal true, mock.respond_to?(:invoked_method)
end

def test_should_not_respond_to_methods_which_the_responder_instance_does_not_responds_to
klass = Class.new do
define_method(:respond_to?) { |_symbol| false }
end
klass = Class.new
mock = build_mock
mock.responds_like_instance_of(klass)
assert_equal false, mock.respond_to?(:invoked_method)
Expand All @@ -299,7 +295,7 @@ def test_should_not_raise_no_method_error_if_mock_is_not_restricted_to_respond_l

def test_should_not_raise_no_method_error_if_responder_does_respond_to_invoked_method
instance = Class.new do
define_method(:respond_to?) { |_symbol| true }
define_method(:invoked_method) {}
end.new
mock = build_mock
mock.responds_like(instance)
Expand All @@ -309,7 +305,6 @@ def test_should_not_raise_no_method_error_if_responder_does_respond_to_invoked_m

def test_should_raise_no_method_error_if_responder_does_not_respond_to_invoked_method
instance = Class.new do
define_method(:respond_to?) { |_symbol| false }
define_method(:mocha_inspect) { 'mocha_inspect' }
end.new
mock = build_mock
Expand All @@ -320,7 +315,6 @@ def test_should_raise_no_method_error_if_responder_does_not_respond_to_invoked_m

def test_should_raise_no_method_error_with_message_indicating_that_mock_is_constrained_to_respond_like_responder
instance = Class.new do
define_method(:respond_to?) { |_symbol| false }
define_method(:mocha_inspect) { 'mocha_inspect' }
end.new
mock = build_mock
Expand Down

0 comments on commit 1ade384

Please sign in to comment.