diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2019-01-28 11:45:21 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2019-01-28 11:45:21 +0000 |
commit | 2a4c87dc31b3774b8f8f2ab7b74f6d864f41a804 (patch) | |
tree | 7ebced0774e05f5a00f78f93d94e15c5d7b202e1 /test | |
parent | 2210709b79222ecd2d9758f7316fdbd7c068ea84 (diff) | |
download | ruby-2a4c87dc31b3774b8f8f2ab7b74f6d864f41a804.tar.gz |
Add refinements support to method/instance_method.
[Fix GH-2034]
From: manga_osyo <manga.osyo@gmail.com>
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66935 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test')
-rw-r--r-- | test/ruby/test_refinement.rb | 41 |
1 files changed, 39 insertions, 2 deletions
diff --git a/test/ruby/test_refinement.rb b/test/ruby/test_refinement.rb index 5c06266d6e..ba571670cf 100644 --- a/test/ruby/test_refinement.rb +++ b/test/ruby/test_refinement.rb @@ -118,6 +118,10 @@ class TestRefinement < Test::Unit::TestCase return foo.method(:z) end + def self.instance_method_z(foo) + return foo.class.instance_method(:z) + end + def self.invoke_call_x_on(foo) return foo.call_x end @@ -213,11 +217,44 @@ class TestRefinement < Test::Unit::TestCase assert_raise(NoMethodError) { FooExtClient.public_send_b_on(foo) } end - def test_method_should_not_use_refinements + module MethodIntegerPowEx + refine Integer do + def pow(*) + :refine_pow + end + end + end + def test_method_should_use_refinements foo = Foo.new assert_raise(NameError) { foo.method(:z) } - assert_raise(NameError) { FooExtClient.method_z(foo) } + assert_equal("FooExt#z", FooExtClient.method_z(foo).call) assert_raise(NameError) { foo.method(:z) } + assert_equal(8, eval(<<~EOS, Sandbox::BINDING)) + meth = 2.method(:pow) + using MethodIntegerPowEx + meth.call(3) + EOS + assert_equal(:refine_pow, eval_using(MethodIntegerPowEx, "2.pow(3)")) + end + + module InstanceMethodIntegerPowEx + refine Integer do + def abs + :refine_abs + end + end + end + def test_instance_method_should_use_refinements + foo = Foo.new + assert_raise(NameError) { Foo.instance_method(:z) } + assert_equal("FooExt#z", FooExtClient.instance_method_z(foo).bind(foo).call) + assert_raise(NameError) { Foo.instance_method(:z) } + assert_equal(4, eval(<<~EOS, Sandbox::BINDING)) + meth = Integer.instance_method(:abs) + using InstanceMethodIntegerPowEx + meth.bind(-4).call + EOS + assert_equal(:refine_abs, eval_using(InstanceMethodIntegerPowEx, "Integer.instance_method(:abs).bind(-4).call")) end def test_no_local_rebinding |