diff options
Diffstat (limited to 'spec/ruby/core/unboundmethod')
-rw-r--r-- | spec/ruby/core/unboundmethod/bind_call_spec.rb | 82 | ||||
-rw-r--r-- | spec/ruby/core/unboundmethod/bind_spec.rb | 8 | ||||
-rw-r--r-- | spec/ruby/core/unboundmethod/clone_spec.rb | 13 | ||||
-rw-r--r-- | spec/ruby/core/unboundmethod/dup_spec.rb | 15 | ||||
-rw-r--r-- | spec/ruby/core/unboundmethod/equal_value_spec.rb | 76 | ||||
-rw-r--r-- | spec/ruby/core/unboundmethod/fixtures/classes.rb | 20 | ||||
-rw-r--r-- | spec/ruby/core/unboundmethod/hash_spec.rb | 7 | ||||
-rw-r--r-- | spec/ruby/core/unboundmethod/owner_spec.rb | 7 | ||||
-rw-r--r-- | spec/ruby/core/unboundmethod/private_spec.rb | 28 | ||||
-rw-r--r-- | spec/ruby/core/unboundmethod/protected_spec.rb | 28 | ||||
-rw-r--r-- | spec/ruby/core/unboundmethod/public_spec.rb | 28 | ||||
-rw-r--r-- | spec/ruby/core/unboundmethod/shared/dup.rb | 32 | ||||
-rw-r--r-- | spec/ruby/core/unboundmethod/shared/to_s.rb | 16 | ||||
-rw-r--r-- | spec/ruby/core/unboundmethod/source_location_spec.rb | 9 | ||||
-rw-r--r-- | spec/ruby/core/unboundmethod/super_method_spec.rb | 21 |
15 files changed, 332 insertions, 58 deletions
diff --git a/spec/ruby/core/unboundmethod/bind_call_spec.rb b/spec/ruby/core/unboundmethod/bind_call_spec.rb index c5d392156c..80b2095d86 100644 --- a/spec/ruby/core/unboundmethod/bind_call_spec.rb +++ b/spec/ruby/core/unboundmethod/bind_call_spec.rb @@ -1,52 +1,58 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' -ruby_version_is '2.7' do - 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 +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 + @normal_um_super = UnboundMethodSpecs::Mod.instance_method(:foo_super) + @parent_um_super = UnboundMethodSpecs::Parent.new.method(:foo_super).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 "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 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 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 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 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 "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 + 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 - um = p.method(:singleton_method).unbind - ->{ um.bind_call(other) }.should raise_error(TypeError) end + um = p.method(:singleton_method).unbind + ->{ um.bind_call(other) }.should raise_error(TypeError) + end + + it "allows calling super for module methods bound to hierarchies that do not already have that module" do + p = UnboundMethodSpecs::Parent.new + + @normal_um_super.bind_call(p).should == true end end diff --git a/spec/ruby/core/unboundmethod/bind_spec.rb b/spec/ruby/core/unboundmethod/bind_spec.rb index 03aaa22e74..7658b664e5 100644 --- a/spec/ruby/core/unboundmethod/bind_spec.rb +++ b/spec/ruby/core/unboundmethod/bind_spec.rb @@ -7,6 +7,8 @@ describe "UnboundMethod#bind" do @parent_um = UnboundMethodSpecs::Parent.new.method(:foo).unbind @child1_um = UnboundMethodSpecs::Child1.new.method(:foo).unbind @child2_um = UnboundMethodSpecs::Child2.new.method(:foo).unbind + @normal_um_super = UnboundMethodSpecs::Mod.instance_method(:foo_super) + @parent_um_super = UnboundMethodSpecs::Parent.new.method(:foo_super).unbind end it "raises TypeError if object is not kind_of? the Module the method defined in" do @@ -58,4 +60,10 @@ describe "UnboundMethod#bind" do um = p.method(:singleton_method).unbind ->{ um.bind(other) }.should raise_error(TypeError) end + + it "allows calling super for module methods bound to hierarchies that do not already have that module" do + p = UnboundMethodSpecs::Parent.new + + @normal_um_super.bind(p).call.should == true + end end diff --git a/spec/ruby/core/unboundmethod/clone_spec.rb b/spec/ruby/core/unboundmethod/clone_spec.rb index 098ee61476..1e7fb18744 100644 --- a/spec/ruby/core/unboundmethod/clone_spec.rb +++ b/spec/ruby/core/unboundmethod/clone_spec.rb @@ -1,12 +1,13 @@ require_relative '../../spec_helper' -require_relative 'fixtures/classes' +require_relative 'shared/dup' describe "UnboundMethod#clone" do - it "returns a copy of the UnboundMethod" do - um1 = UnboundMethodSpecs::Methods.instance_method(:foo) - um2 = um1.clone + it_behaves_like :unboundmethod_dup, :clone - (um1 == um2).should == true - um1.bind(UnboundMethodSpecs::Methods.new).call.should == um2.bind(UnboundMethodSpecs::Methods.new).call + it "preserves frozen status" do + method = Class.instance_method(:instance_method) + method.freeze + method.frozen?.should == true + method.clone.frozen?.should == true end end diff --git a/spec/ruby/core/unboundmethod/dup_spec.rb b/spec/ruby/core/unboundmethod/dup_spec.rb new file mode 100644 index 0000000000..5a78dd8e36 --- /dev/null +++ b/spec/ruby/core/unboundmethod/dup_spec.rb @@ -0,0 +1,15 @@ +require_relative '../../spec_helper' +require_relative 'shared/dup' + +describe "UnboundMethod#dup" do + ruby_version_is "3.4" do + it_behaves_like :unboundmethod_dup, :dup + + it "resets frozen status" do + method = Class.instance_method(:instance_method) + method.freeze + method.frozen?.should == true + method.dup.frozen?.should == false + end + end +end diff --git a/spec/ruby/core/unboundmethod/equal_value_spec.rb b/spec/ruby/core/unboundmethod/equal_value_spec.rb index 6242b04884..036c6b7f8c 100644 --- a/spec/ruby/core/unboundmethod/equal_value_spec.rb +++ b/spec/ruby/core/unboundmethod/equal_value_spec.rb @@ -76,19 +76,38 @@ describe "UnboundMethod#==" do (@identical_body == @original_body).should == false end - it "returns false if same method but one extracted from a subclass" do - (@parent == @child1).should == false - (@child1 == @parent).should == false - end + ruby_version_is ""..."3.2" do + it "returns false if same method but one extracted from a subclass" do + (@parent == @child1).should == false + (@child1 == @parent).should == false + end + + it "returns false if same method but extracted from two different subclasses" do + (@child2 == @child1).should == false + (@child1 == @child2).should == false + end - it "returns false if same method but extracted from two different subclasses" do - (@child2 == @child1).should == false - (@child1 == @child2).should == false + it "returns false if methods are the same but added from an included Module" do + (@includee == @includer).should == false + (@includer == @includee).should == false + end end - it "returns false if methods are the same but added from an included Module" do - (@includee == @includer).should == false - (@includer == @includee).should == false + ruby_version_is "3.2" do + it "returns true if same method but one extracted from a subclass" do + (@parent == @child1).should == true + (@child1 == @parent).should == true + end + + it "returns false if same method but extracted from two different subclasses" do + (@child2 == @child1).should == true + (@child1 == @child2).should == true + end + + it "returns true if methods are the same but added from an included Module" do + (@includee == @includer).should == true + (@includer == @includee).should == true + end end it "returns false if both have same Module, same name, identical body but not the same" do @@ -98,4 +117,41 @@ describe "UnboundMethod#==" do (@discard_1 == UnboundMethodSpecs::Methods.instance_method(:discard_1)).should == false end + + it "considers methods through aliasing equal" do + c = Class.new do + class << self + alias_method :n, :new + end + end + + c.method(:new).should == c.method(:n) + c.method(:n).should == Class.instance_method(:new).bind(c) + end + + # On CRuby < 3.2, the 2 specs below pass due to method/instance_method skipping zsuper methods. + # We are interested in the general pattern working, i.e. the combination of method/instance_method + # and #== exposes the wanted behavior. + it "considers methods through visibility change equal" do + c = Class.new do + class << self + private :new + end + end + + c.method(:new).should == Class.instance_method(:new).bind(c) + end + + it "considers methods through aliasing and visibility change equal" do + c = Class.new do + class << self + alias_method :n, :new + private :new + end + end + + c.method(:new).should == c.method(:n) + c.method(:n).should == Class.instance_method(:new).bind(c) + c.method(:new).should == Class.instance_method(:new).bind(c) + end end diff --git a/spec/ruby/core/unboundmethod/fixtures/classes.rb b/spec/ruby/core/unboundmethod/fixtures/classes.rb index 46b1c51669..28d8e0a965 100644 --- a/spec/ruby/core/unboundmethod/fixtures/classes.rb +++ b/spec/ruby/core/unboundmethod/fixtures/classes.rb @@ -22,6 +22,7 @@ module UnboundMethodSpecs module Mod def from_mod; end + def foo_super; super; end end class Methods @@ -53,10 +54,19 @@ module UnboundMethodSpecs def discard_1(); :discard; end def discard_2(); :discard; end + + def my_public_method; end + def my_protected_method; end + def my_private_method; end + protected :my_protected_method + private :my_private_method end class Parent def foo; end + def foo_super + true + end def self.class_method "I am #{name}" end @@ -84,4 +94,14 @@ module UnboundMethodSpecs class C < B def overridden; end end + + module HashSpecs + class SuperClass + def foo + end + end + + class SubClass < SuperClass + end + end end diff --git a/spec/ruby/core/unboundmethod/hash_spec.rb b/spec/ruby/core/unboundmethod/hash_spec.rb index 12dce0020f..6888675bc1 100644 --- a/spec/ruby/core/unboundmethod/hash_spec.rb +++ b/spec/ruby/core/unboundmethod/hash_spec.rb @@ -12,4 +12,11 @@ describe "UnboundMethod#hash" do to_s, inspect = Array.instance_method(:to_s), Array.instance_method(:inspect) to_s.hash.should == inspect.hash end + + it "equals a hash of the same method in the superclass" do + foo_in_superclass = UnboundMethodSpecs::HashSpecs::SuperClass.instance_method(:foo) + foo = UnboundMethodSpecs::HashSpecs::SubClass.instance_method(:foo) + + foo.hash.should == foo_in_superclass.hash + end end diff --git a/spec/ruby/core/unboundmethod/owner_spec.rb b/spec/ruby/core/unboundmethod/owner_spec.rb index 5f1f4646b3..e8a734dac4 100644 --- a/spec/ruby/core/unboundmethod/owner_spec.rb +++ b/spec/ruby/core/unboundmethod/owner_spec.rb @@ -1,5 +1,6 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' +require_relative '../method/fixtures/classes' describe "UnboundMethod#owner" do it "returns the owner of the method" do @@ -23,4 +24,10 @@ describe "UnboundMethod#owner" do child_singleton_class.instance_method(:class_method).owner.should == parent_singleton_class child_singleton_class.instance_method(:another_class_method).owner.should == child_singleton_class end + + ruby_version_is "3.2" do + it "returns the class on which public was called for a private method in ancestor" do + MethodSpecs::InheritedMethods::C.instance_method(:derp).owner.should == MethodSpecs::InheritedMethods::C + end + end end diff --git a/spec/ruby/core/unboundmethod/private_spec.rb b/spec/ruby/core/unboundmethod/private_spec.rb new file mode 100644 index 0000000000..8ea50bb5d4 --- /dev/null +++ b/spec/ruby/core/unboundmethod/private_spec.rb @@ -0,0 +1,28 @@ +require_relative '../../spec_helper' +require_relative 'fixtures/classes' + +describe "UnboundMethod#private?" do + ruby_version_is "3.1"..."3.2" do + it "returns false when the method is public" do + obj = UnboundMethodSpecs::Methods.new + obj.method(:my_public_method).unbind.private?.should == false + end + + it "returns false when the method is protected" do + obj = UnboundMethodSpecs::Methods.new + obj.method(:my_protected_method).unbind.private?.should == false + end + + it "returns true when the method is private" do + obj = UnboundMethodSpecs::Methods.new + obj.method(:my_private_method).unbind.private?.should == true + end + end + + ruby_version_is "3.2" do + it "has been removed" do + obj = UnboundMethodSpecs::Methods.new + obj.method(:my_private_method).unbind.should_not.respond_to?(:private?) + end + end +end diff --git a/spec/ruby/core/unboundmethod/protected_spec.rb b/spec/ruby/core/unboundmethod/protected_spec.rb new file mode 100644 index 0000000000..0c215d8638 --- /dev/null +++ b/spec/ruby/core/unboundmethod/protected_spec.rb @@ -0,0 +1,28 @@ +require_relative '../../spec_helper' +require_relative 'fixtures/classes' + +describe "UnboundMethod#protected?" do + ruby_version_is "3.1"..."3.2" do + it "returns false when the method is public" do + obj = UnboundMethodSpecs::Methods.new + obj.method(:my_public_method).unbind.protected?.should == false + end + + it "returns true when the method is protected" do + obj = UnboundMethodSpecs::Methods.new + obj.method(:my_protected_method).unbind.protected?.should == true + end + + it "returns false when the method is private" do + obj = UnboundMethodSpecs::Methods.new + obj.method(:my_private_method).unbind.protected?.should == false + end + end + + ruby_version_is "3.2" do + it "has been removed" do + obj = UnboundMethodSpecs::Methods.new + obj.method(:my_protected_method).unbind.should_not.respond_to?(:protected?) + end + end +end diff --git a/spec/ruby/core/unboundmethod/public_spec.rb b/spec/ruby/core/unboundmethod/public_spec.rb new file mode 100644 index 0000000000..552bbf6eab --- /dev/null +++ b/spec/ruby/core/unboundmethod/public_spec.rb @@ -0,0 +1,28 @@ +require_relative '../../spec_helper' +require_relative 'fixtures/classes' + +describe "UnboundMethod#public?" do + ruby_version_is "3.1"..."3.2" do + it "returns true when the method is public" do + obj = UnboundMethodSpecs::Methods.new + obj.method(:my_public_method).unbind.public?.should == true + end + + it "returns false when the method is protected" do + obj = UnboundMethodSpecs::Methods.new + obj.method(:my_protected_method).unbind.public?.should == false + end + + it "returns false when the method is private" do + obj = UnboundMethodSpecs::Methods.new + obj.method(:my_private_method).unbind.public?.should == false + end + end + + ruby_version_is "3.2" do + it "has been removed" do + obj = UnboundMethodSpecs::Methods.new + obj.method(:my_public_method).unbind.should_not.respond_to?(:public?) + end + end +end diff --git a/spec/ruby/core/unboundmethod/shared/dup.rb b/spec/ruby/core/unboundmethod/shared/dup.rb new file mode 100644 index 0000000000..943a7faaa3 --- /dev/null +++ b/spec/ruby/core/unboundmethod/shared/dup.rb @@ -0,0 +1,32 @@ +describe :unboundmethod_dup, shared: true do + it "returns a copy of self" do + a = Class.instance_method(:instance_method) + b = a.send(@method) + + a.should == b + a.should_not equal(b) + end + + ruby_version_is "3.4" do + it "copies instance variables" do + method = Class.instance_method(:instance_method) + method.instance_variable_set(:@ivar, 1) + cl = method.send(@method) + cl.instance_variables.should == [:@ivar] + end + + it "copies the finalizer" do + code = <<-RUBY + obj = Class.instance_method(:instance_method) + + ObjectSpace.define_finalizer(obj, Proc.new { STDOUT.write "finalized\n" }) + + obj.clone + + exit 0 + RUBY + + ruby_exe(code).lines.sort.should == ["finalized\n", "finalized\n"] + end + end +end diff --git a/spec/ruby/core/unboundmethod/shared/to_s.rb b/spec/ruby/core/unboundmethod/shared/to_s.rb index 38503c3c60..b92bb0b207 100644 --- a/spec/ruby/core/unboundmethod/shared/to_s.rb +++ b/spec/ruby/core/unboundmethod/shared/to_s.rb @@ -20,12 +20,22 @@ describe :unboundmethod_to_s, shared: true do it "the String shows the method name, Module defined in and Module extracted from" do @from_module.send(@method).should =~ /\bfrom_mod\b/ @from_module.send(@method).should =~ /\bUnboundMethodSpecs::Mod\b/ - @from_method.send(@method).should =~ /\bUnboundMethodSpecs::Methods\b/ + + ruby_version_is ""..."3.2" do + @from_method.send(@method).should =~ /\bUnboundMethodSpecs::Methods\b/ + end end it "returns a String including all details" do - @from_module.send(@method).should.start_with? "#<UnboundMethod: UnboundMethodSpecs::Methods(UnboundMethodSpecs::Mod)#from_mod" - @from_method.send(@method).should.start_with? "#<UnboundMethod: UnboundMethodSpecs::Methods(UnboundMethodSpecs::Mod)#from_mod" + ruby_version_is ""..."3.2" do + @from_module.send(@method).should.start_with? "#<UnboundMethod: UnboundMethodSpecs::Methods(UnboundMethodSpecs::Mod)#from_mod" + @from_method.send(@method).should.start_with? "#<UnboundMethod: UnboundMethodSpecs::Methods(UnboundMethodSpecs::Mod)#from_mod" + end + + ruby_version_is "3.2" do + @from_module.send(@method).should.start_with? "#<UnboundMethod: UnboundMethodSpecs::Mod#from_mod" + @from_method.send(@method).should.start_with? "#<UnboundMethod: UnboundMethodSpecs::Mod#from_mod" + end end it "does not show the defining module if it is the same as the origin" do diff --git a/spec/ruby/core/unboundmethod/source_location_spec.rb b/spec/ruby/core/unboundmethod/source_location_spec.rb index 96933a5d40..5c2f14362c 100644 --- a/spec/ruby/core/unboundmethod/source_location_spec.rb +++ b/spec/ruby/core/unboundmethod/source_location_spec.rb @@ -9,7 +9,7 @@ describe "UnboundMethod#source_location" do it "sets the first value to the path of the file in which the method was defined" do file = @method.source_location.first file.should be_an_instance_of(String) - file.should == File.realpath('../fixtures/classes.rb', __FILE__) + file.should == File.realpath('fixtures/classes.rb', __dir__) end it "sets the last value to an Integer representing the line on which the method was defined" do @@ -49,4 +49,11 @@ describe "UnboundMethod#source_location" do method.source_location[0].should =~ /#{__FILE__}/ method.source_location[1].should == line end + + it "works for eval with a given line" do + c = Class.new do + eval('def m; end', nil, "foo", 100) + end + c.instance_method(:m).source_location.should == ["foo", 100] + end end diff --git a/spec/ruby/core/unboundmethod/super_method_spec.rb b/spec/ruby/core/unboundmethod/super_method_spec.rb index c9fa1ec533..aa7c129377 100644 --- a/spec/ruby/core/unboundmethod/super_method_spec.rb +++ b/spec/ruby/core/unboundmethod/super_method_spec.rb @@ -1,5 +1,6 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' +require_relative '../method/fixtures/classes' describe "UnboundMethod#super_method" do it "returns the method that would be called by super in the method" do @@ -25,4 +26,24 @@ describe "UnboundMethod#super_method" do method.super_method.should == nil end + + # https://github.com/jruby/jruby/issues/7240 + context "after changing an inherited methods visibility" do + it "calls the proper super method" do + method = MethodSpecs::InheritedMethods::C.instance_method(:derp) + method.bind(MethodSpecs::InheritedMethods::C.new).call.should == 'BA' + end + + it "returns the expected super_method" do + method = MethodSpecs::InheritedMethods::C.instance_method(:derp) + method.super_method.owner.should == MethodSpecs::InheritedMethods::A + end + end + + context "after aliasing an inherited method" do + it "returns the expected super_method" do + method = MethodSpecs::InheritedMethods::C.instance_method(:meow) + method.super_method.owner.should == MethodSpecs::InheritedMethods::A + end + end end |