summaryrefslogtreecommitdiff
path: root/test/test_delegate.rb
diff options
context:
space:
mode:
authorMasataka Pocke Kuwabara <kuwabara@pocke.me>2020-07-10 07:01:10 +0900
committerGitHub <noreply@github.com>2020-07-09 15:01:10 -0700
commitba81bc24e62d03cc0f4c05cf4e2b378696631568 (patch)
tree0466e8eb5b4f3b013109ad827691688308623ae7 /test/test_delegate.rb
parent7a479b30b647c38ded91e45c9e8e22d30b671e07 (diff)
Add instance_methods to class generated by DelegateClass
Also, make DelegateClass.instance_method fallback to superclass. Fixes [Bug #16982]
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/3221 Merged-By: jeremyevans <code@jeremyevans.net>
Diffstat (limited to 'test/test_delegate.rb')
-rw-r--r--test/test_delegate.rb34
1 files changed, 34 insertions, 0 deletions
diff --git a/test/test_delegate.rb b/test/test_delegate.rb
index 4a3445f..57480b1 100644
--- a/test/test_delegate.rb
+++ b/test/test_delegate.rb
@@ -106,6 +106,10 @@ class TestDelegateClass < Test::Unit::TestCase
protected
def parent_protected; end
+
+ private
+
+ def parent_private; end
end
class Child < DelegateClass(Parent)
@@ -117,6 +121,10 @@ class TestDelegateClass < Test::Unit::TestCase
protected
def parent_protected_added; end
+
+ private
+
+ def parent_private_added; end
end
def test_public_instance_methods
@@ -131,6 +139,32 @@ class TestDelegateClass < Test::Unit::TestCase
assert_equal([:parent_protected, :parent_protected_added], (Child.new(Parent.new).protected_methods - ignores).sort)
end
+ def test_instance_methods
+ ignores = Object.instance_methods | Delegator.instance_methods
+ assert_equal([:parent_protected, :parent_protected_added, :parent_public, :parent_public_added], (Child.instance_methods - ignores).sort)
+ assert_equal([:parent_protected, :parent_protected_added, :parent_public, :parent_public_added], (Child.new(Parent.new).methods - ignores).sort)
+ end
+
+ def test_DelegateClass_instance_method
+ assert_instance_of UnboundMethod, Child.instance_method(:parent_public)
+ assert_instance_of UnboundMethod, Child.instance_method(:parent_public_added)
+ assert_instance_of UnboundMethod, Child.instance_method(:parent_protected)
+ assert_instance_of UnboundMethod, Child.instance_method(:parent_protected_added)
+ assert_raise(NameError) { Child.instance_method(:parent_private) }
+ assert_raise(NameError) { Child.instance_method(:parent_private_added) }
+ assert_instance_of UnboundMethod, Child.instance_method(:to_s)
+ end
+
+ def test_DelegateClass_public_instance_method
+ assert_instance_of UnboundMethod, Child.public_instance_method(:parent_public)
+ assert_instance_of UnboundMethod, Child.public_instance_method(:parent_public_added)
+ assert_raise(NameError) { Child.public_instance_method(:parent_protected) }
+ assert_raise(NameError) { Child.public_instance_method(:parent_protected_added) }
+ assert_raise(NameError) { Child.instance_method(:parent_private) }
+ assert_raise(NameError) { Child.instance_method(:parent_private_added) }
+ assert_instance_of UnboundMethod, Child.public_instance_method(:to_s)
+ end
+
class IV < DelegateClass(Integer)
attr_accessor :var