summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorYusuke Endoh <mame@ruby-lang.org>2019-08-30 11:10:29 +0900
committerYusuke Endoh <mame@ruby-lang.org>2019-08-30 11:13:00 +0900
commit09c940b17fcbaec2f7d04e528b8869c8c51ff336 (patch)
tree92751ab1a7b7ba9f16e6328798b991dbc661ff2e /spec
parent83c6a1ef454c51ad1c0ca58e8a95fd67a033f710 (diff)
spec/ruby/core/unboundmethod/bind_call_spec.rb: Added
For UnboundMethod#bind_call [Feature #15955] introduced in 002e592e0d67bb0271d16314a32380ad947c9ae9.
Diffstat (limited to 'spec')
-rw-r--r--spec/ruby/core/unboundmethod/bind_call_spec.rb50
1 files changed, 50 insertions, 0 deletions
diff --git a/spec/ruby/core/unboundmethod/bind_call_spec.rb b/spec/ruby/core/unboundmethod/bind_call_spec.rb
new file mode 100644
index 0000000000..8f25f8bd0d
--- /dev/null
+++ b/spec/ruby/core/unboundmethod/bind_call_spec.rb
@@ -0,0 +1,50 @@
+require_relative '../../spec_helper'
+require_relative 'fixtures/classes'
+
+describe "UnboundMethod#bind_call" do
+ before :each do
+ @normal_um = UnboundMethodSpecs::Methods.new.method(:foo).unbind
+ @parent_um = UnboundMethodSpecs::Parent.new.method(:foo).unbind
+ @child1_um = UnboundMethodSpecs::Child1.new.method(:foo).unbind
+ @child2_um = UnboundMethodSpecs::Child2.new.method(:foo).unbind
+ end
+
+ it "raises TypeError if object is not kind_of? the Module the method defined in" do
+ -> { @normal_um.bind_call(UnboundMethodSpecs::B.new) }.should raise_error(TypeError)
+ end
+
+ it "binds and calls the method if object is kind_of the Module the method defined in" do
+ @normal_um.bind_call(UnboundMethodSpecs::Methods.new).should == true
+ end
+
+ it "binds and calls the method on any object when UnboundMethod is unbound from a module" do
+ UnboundMethodSpecs::Mod.instance_method(:from_mod).bind_call(Object.new).should == nil
+ end
+
+ it "binds and calls the method for any object kind_of? the Module the method is defined in" do
+ @parent_um.bind_call(UnboundMethodSpecs::Child1.new).should == nil
+ @child1_um.bind_call(UnboundMethodSpecs::Parent.new).should == nil
+ @child2_um.bind_call(UnboundMethodSpecs::Child1.new).should == nil
+ end
+
+ it "binds and calls a Kernel method retrieved from Object on BasicObject" do
+ Object.instance_method(:instance_of?).bind_call(BasicObject.new, BasicObject).should == true
+ end
+
+ it "binds and calls a Parent's class method to any Child's class methods" do
+ um = UnboundMethodSpecs::Parent.method(:class_method).unbind
+ um.bind_call(UnboundMethodSpecs::Child1).should == "I am UnboundMethodSpecs::Child1"
+ end
+
+ it "will raise when binding a an object singleton's method to another object" do
+ other = UnboundMethodSpecs::Parent.new
+ p = UnboundMethodSpecs::Parent.new
+ class << p
+ def singleton_method
+ :single
+ end
+ end
+ um = p.method(:singleton_method).unbind
+ ->{ um.bind_call(other) }.should raise_error(TypeError)
+ end
+end