diff options
author | Koichi Sasada <ko1@atdot.net> | 2022-12-03 05:55:33 +0900 |
---|---|---|
committer | Koichi Sasada <ko1@atdot.net> | 2022-12-03 08:53:12 +0900 |
commit | 59e389af2893c0fcf8b2cfa008c9a16825bf56ff (patch) | |
tree | 31224e3498843912e2fc8734710e03224aaca21c /spec/ruby/core | |
parent | 7161bf34e161979b97dbc0c1f7450c195faffcfe (diff) | |
download | ruby-59e389af2893c0fcf8b2cfa008c9a16825bf56ff.tar.gz |
UnboundMethod only refer defined_class
UnboundMethod records caller's class, like `D` or `E` on the
following case:
```ruby
class C
def foo = :foo
end
class D < C
end
class E < C
end
d = D.instance_method(:foo)
e = E.instance_method(:foo)
```
But `d` and `e` only refers `C#foo` so that UnboundMethod doesn't
record `D` or `E`. This behavior changes the following methods:
* `UnboundMethod#inspect` (doesn't show caller's class)
* `UnboundMethod#==` (`d == e` for example)
fix https://bugs.ruby-lang.org/issues/18798
Diffstat (limited to 'spec/ruby/core')
-rw-r--r-- | spec/ruby/core/method/unbind_spec.rb | 12 | ||||
-rw-r--r-- | spec/ruby/core/module/instance_method_spec.rb | 8 | ||||
-rw-r--r-- | spec/ruby/core/unboundmethod/equal_value_spec.rb | 39 | ||||
-rw-r--r-- | spec/ruby/core/unboundmethod/shared/to_s.rb | 16 |
4 files changed, 58 insertions, 17 deletions
diff --git a/spec/ruby/core/method/unbind_spec.rb b/spec/ruby/core/method/unbind_spec.rb index cdd3a808f2..bdedd513ce 100644 --- a/spec/ruby/core/method/unbind_spec.rb +++ b/spec/ruby/core/method/unbind_spec.rb @@ -27,8 +27,16 @@ describe "Method#unbind" do @string.should =~ /MethodSpecs::MyMod/ end - it "returns a String containing the Module the method is referenced from" do - @string.should =~ /MethodSpecs::MySub/ + ruby_version_is ""..."3.2" do + it "returns a String containing the Module the method is referenced from" do + @string.should =~ /MethodSpecs::MySub/ + end + end + + ruby_version_is "3.2" do + it "returns a String containing the Module the method is referenced from" do + @string.should =~ /MethodSpecs::MyMod/ + end end end diff --git a/spec/ruby/core/module/instance_method_spec.rb b/spec/ruby/core/module/instance_method_spec.rb index 30e27bb180..8d006e647e 100644 --- a/spec/ruby/core/module/instance_method_spec.rb +++ b/spec/ruby/core/module/instance_method_spec.rb @@ -45,10 +45,14 @@ describe "Module#instance_method" do @parent_um.inspect.should =~ /\bModuleSpecs::InstanceMeth\b/ @child_um.inspect.should =~ /\bfoo\b/ @child_um.inspect.should =~ /\bModuleSpecs::InstanceMeth\b/ - @child_um.inspect.should =~ /\bModuleSpecs::InstanceMethChild\b/ + @mod_um.inspect.should =~ /\bbar\b/ @mod_um.inspect.should =~ /\bModuleSpecs::InstanceMethMod\b/ - @mod_um.inspect.should =~ /\bModuleSpecs::InstanceMethChild\b/ + + ruby_version_is ""..."3.2" do + @child_um.inspect.should =~ /\bModuleSpecs::InstanceMethChild\b/ + @mod_um.inspect.should =~ /\bModuleSpecs::InstanceMethChild\b/ + end end it "raises a TypeError if the given name is not a String/Symbol" do diff --git a/spec/ruby/core/unboundmethod/equal_value_spec.rb b/spec/ruby/core/unboundmethod/equal_value_spec.rb index b21677687e..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 + 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 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 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 |