diff options
Diffstat (limited to 'spec/ruby/core/method')
-rw-r--r-- | spec/ruby/core/method/clone_spec.rb | 15 | ||||
-rw-r--r-- | spec/ruby/core/method/compose_spec.rb | 3 | ||||
-rw-r--r-- | spec/ruby/core/method/dup_spec.rb | 15 | ||||
-rw-r--r-- | spec/ruby/core/method/fixtures/classes.rb | 30 | ||||
-rw-r--r-- | spec/ruby/core/method/owner_spec.rb | 6 | ||||
-rw-r--r-- | spec/ruby/core/method/parameters_spec.rb | 63 | ||||
-rw-r--r-- | spec/ruby/core/method/private_spec.rb | 28 | ||||
-rw-r--r-- | spec/ruby/core/method/protected_spec.rb | 28 | ||||
-rw-r--r-- | spec/ruby/core/method/public_spec.rb | 28 | ||||
-rw-r--r-- | spec/ruby/core/method/shared/dup.rb | 32 | ||||
-rw-r--r-- | spec/ruby/core/method/shared/to_s.rb | 56 | ||||
-rw-r--r-- | spec/ruby/core/method/source_location_spec.rb | 9 | ||||
-rw-r--r-- | spec/ruby/core/method/super_method_spec.rb | 19 | ||||
-rw-r--r-- | spec/ruby/core/method/to_proc_spec.rb | 2 | ||||
-rw-r--r-- | spec/ruby/core/method/unbind_spec.rb | 12 |
15 files changed, 299 insertions, 47 deletions
diff --git a/spec/ruby/core/method/clone_spec.rb b/spec/ruby/core/method/clone_spec.rb index 3fe4000fb7..b0eb5751a9 100644 --- a/spec/ruby/core/method/clone_spec.rb +++ b/spec/ruby/core/method/clone_spec.rb @@ -1,14 +1,13 @@ require_relative '../../spec_helper' -require_relative 'fixtures/classes' +require_relative 'shared/dup' describe "Method#clone" do - it "returns a copy of the method" do - m1 = MethodSpecs::Methods.new.method(:foo) - m2 = m1.clone + it_behaves_like :method_dup, :clone - m1.should == m2 - m1.should_not equal(m2) - - m1.call.should == m2.call + it "preserves frozen status" do + method = Object.new.method(:method) + method.freeze + method.frozen?.should == true + method.clone.frozen?.should == true end end diff --git a/spec/ruby/core/method/compose_spec.rb b/spec/ruby/core/method/compose_spec.rb index 87cf61f7ad..7506e33ea8 100644 --- a/spec/ruby/core/method/compose_spec.rb +++ b/spec/ruby/core/method/compose_spec.rb @@ -38,8 +38,7 @@ describe "Method#<<" do double = proc { |x| x + x } (pow_2 << double).is_a?(Proc).should == true - ruby_version_is(''...'3.0') { (pow_2 << double).should.lambda? } - ruby_version_is('3.0') { (pow_2 << double).should_not.lambda? } + (pow_2 << double).should_not.lambda? end it "may accept multiple arguments" do diff --git a/spec/ruby/core/method/dup_spec.rb b/spec/ruby/core/method/dup_spec.rb new file mode 100644 index 0000000000..e3e29d8a68 --- /dev/null +++ b/spec/ruby/core/method/dup_spec.rb @@ -0,0 +1,15 @@ +require_relative '../../spec_helper' +require_relative 'shared/dup' + +describe "Method#dup" do + ruby_version_is "3.4" do + it_behaves_like :method_dup, :dup + + it "resets frozen status" do + method = Object.new.method(:method) + method.freeze + method.frozen?.should == true + method.dup.frozen?.should == false + end + end +end diff --git a/spec/ruby/core/method/fixtures/classes.rb b/spec/ruby/core/method/fixtures/classes.rb index be96f65e25..464a519aea 100644 --- a/spec/ruby/core/method/fixtures/classes.rb +++ b/spec/ruby/core/method/fixtures/classes.rb @@ -84,6 +84,12 @@ module MethodSpecs def two_req_one_opt_with_splat_and_block(a, b, c=nil, *d, &blk); end def one_req_two_opt_with_splat_and_block(a, b=nil, c=nil, *d, &blk); end + def my_public_method; end + def my_protected_method; end + def my_private_method; end + protected :my_protected_method + private :my_private_method + define_method(:zero_defined_method, Proc.new {||}) define_method(:zero_with_splat_defined_method, Proc.new {|*x|}) define_method(:one_req_defined_method, Proc.new {|x|}) @@ -213,4 +219,28 @@ module MethodSpecs n * m end end + + module InheritedMethods + module A + private + def derp(message) + 'A' + end + end + + module B + private + def derp + 'B' + super('superclass') + end + end + + class C + include A + include B + + public :derp + alias_method :meow, :derp + end + end end diff --git a/spec/ruby/core/method/owner_spec.rb b/spec/ruby/core/method/owner_spec.rb index ca5dff7295..05422f1697 100644 --- a/spec/ruby/core/method/owner_spec.rb +++ b/spec/ruby/core/method/owner_spec.rb @@ -23,4 +23,10 @@ describe "Method#owner" do @m.method(:handled_via_method_missing).owner.should == MethodSpecs::Methods end 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.new.method(:derp).owner.should == MethodSpecs::InheritedMethods::C + end + end end diff --git a/spec/ruby/core/method/parameters_spec.rb b/spec/ruby/core/method/parameters_spec.rb index e6d51d1b4d..8495aef4d2 100644 --- a/spec/ruby/core/method/parameters_spec.rb +++ b/spec/ruby/core/method/parameters_spec.rb @@ -7,6 +7,7 @@ describe "Method#parameters" do def one_keyrest(**a); end def one_keyreq(a:); end + def one_nokey(**nil); end def one_splat_one_req(*a,b); end def one_splat_two_req(*a,b,c); end @@ -15,11 +16,16 @@ describe "Method#parameters" do def one_opt_with_stabby(a=-> b { true }); end def one_unnamed_splat(*); end + def one_unnamed_keyrest(**); end def one_splat_one_block(*args, &block) local_is_not_parameter = {} end + def forward_parameters(...) end + + def underscore_parameters(_, _, _ = 1, *_, _:, _: 2, **_, &_); end + define_method(:one_optional_defined_method) {|x = 1|} end @@ -176,6 +182,11 @@ describe "Method#parameters" do m.parameters.should == [[:keyreq,:a]] end + it "returns [[:nokey]] for a method with a single **nil parameter" do + m = MethodSpecs::Methods.instance_method(:one_nokey) + m.parameters.should == [[:nokey]] + end + it "works with ->(){} as the value of an optional argument" do m = MethodSpecs::Methods.instance_method(:one_opt_with_stabby) m.parameters.should == [[:opt,:a]] @@ -223,10 +234,15 @@ describe "Method#parameters" do end ruby_version_is '3.2' do - it "adds * rest arg for \"star\" argument" do + it "adds rest arg with name * for \"star\" argument" do m = MethodSpecs::Methods.new m.method(:one_unnamed_splat).parameters.should == [[:rest, :*]] end + + it "adds keyrest arg with ** as a name for \"double star\" argument" do + m = MethodSpecs::Methods.new + m.method(:one_unnamed_keyrest).parameters.should == [[:keyrest, :**]] + end end ruby_version_is ''...'3.2' do @@ -234,6 +250,37 @@ describe "Method#parameters" do m = MethodSpecs::Methods.new m.method(:one_unnamed_splat).parameters.should == [[:rest]] end + + it "adds nameless keyrest arg for \"double star\" argument" do + m = MethodSpecs::Methods.new + m.method(:one_unnamed_keyrest).parameters.should == [[:keyrest]] + end + end + + ruby_version_is '3.1' do + it "adds block arg with name & for anonymous block argument" do + object = Object.new + + eval(<<~RUBY).should == [[:block, :&]] + def object.foo(&) + end + object.method(:foo).parameters + RUBY + end + end + + ruby_version_is ""..."3.1" do + it "returns [:rest, :*], [:block, :&] for forward parameters operator" do + m = MethodSpecs::Methods.new + m.method(:forward_parameters).parameters.should == [[:rest, :*], [:block, :&]] + end + end + + ruby_version_is "3.1" do + it "returns [:rest, :*], [:keyrest, :**], [:block, :&] for forward parameters operator" do + m = MethodSpecs::Methods.new + m.method(:forward_parameters).parameters.should == [[:rest, :*], [:keyrest, :**], [:block, :&]] + end end it "returns the args and block for a splat and block argument" do @@ -251,6 +298,20 @@ describe "Method#parameters" do m.method(:writer=).parameters.should == [[:req]] end + it "returns all parameters defined with the name _ as _" do + m = MethodSpecs::Methods.instance_method(:underscore_parameters) + m.parameters.should == [ + [:req, :_], + [:req, :_], + [:opt, :_], + [:rest, :_], + [:keyreq, :_], + [:key, :_], + [:keyrest, :_], + [:block, :_] + ] + end + it "returns [[:rest]] for core methods with variable-length argument lists" do # delete! takes rest args "foo".method(:delete!).parameters.should == [[:rest]] diff --git a/spec/ruby/core/method/private_spec.rb b/spec/ruby/core/method/private_spec.rb new file mode 100644 index 0000000000..fd550036a3 --- /dev/null +++ b/spec/ruby/core/method/private_spec.rb @@ -0,0 +1,28 @@ +require_relative '../../spec_helper' +require_relative 'fixtures/classes' + +describe "Method#private?" do + ruby_version_is "3.1"..."3.2" do + it "returns false when the method is public" do + obj = MethodSpecs::Methods.new + obj.method(:my_public_method).private?.should == false + end + + it "returns false when the method is protected" do + obj = MethodSpecs::Methods.new + obj.method(:my_protected_method).private?.should == false + end + + it "returns true when the method is private" do + obj = MethodSpecs::Methods.new + obj.method(:my_private_method).private?.should == true + end + end + + ruby_version_is "3.2" do + it "has been removed" do + obj = MethodSpecs::Methods.new + obj.method(:my_private_method).should_not.respond_to?(:private?) + end + end +end diff --git a/spec/ruby/core/method/protected_spec.rb b/spec/ruby/core/method/protected_spec.rb new file mode 100644 index 0000000000..8423e8c64c --- /dev/null +++ b/spec/ruby/core/method/protected_spec.rb @@ -0,0 +1,28 @@ +require_relative '../../spec_helper' +require_relative 'fixtures/classes' + +describe "Method#protected?" do + ruby_version_is "3.1"..."3.2" do + it "returns false when the method is public" do + obj = MethodSpecs::Methods.new + obj.method(:my_public_method).protected?.should == false + end + + it "returns true when the method is protected" do + obj = MethodSpecs::Methods.new + obj.method(:my_protected_method).protected?.should == true + end + + it "returns false when the method is private" do + obj = MethodSpecs::Methods.new + obj.method(:my_private_method).protected?.should == false + end + end + + ruby_version_is "3.2" do + it "has been removed" do + obj = MethodSpecs::Methods.new + obj.method(:my_protected_method).should_not.respond_to?(:protected?) + end + end +end diff --git a/spec/ruby/core/method/public_spec.rb b/spec/ruby/core/method/public_spec.rb new file mode 100644 index 0000000000..20d0081a27 --- /dev/null +++ b/spec/ruby/core/method/public_spec.rb @@ -0,0 +1,28 @@ +require_relative '../../spec_helper' +require_relative 'fixtures/classes' + +describe "Method#public?" do + ruby_version_is "3.1"..."3.2" do + it "returns true when the method is public" do + obj = MethodSpecs::Methods.new + obj.method(:my_public_method).public?.should == true + end + + it "returns false when the method is protected" do + obj = MethodSpecs::Methods.new + obj.method(:my_protected_method).public?.should == false + end + + it "returns false when the method is private" do + obj = MethodSpecs::Methods.new + obj.method(:my_private_method).public?.should == false + end + end + + ruby_version_is "3.2" do + it "has been removed" do + obj = MethodSpecs::Methods.new + obj.method(:my_public_method).should_not.respond_to?(:public?) + end + end +end diff --git a/spec/ruby/core/method/shared/dup.rb b/spec/ruby/core/method/shared/dup.rb new file mode 100644 index 0000000000..1a10b90689 --- /dev/null +++ b/spec/ruby/core/method/shared/dup.rb @@ -0,0 +1,32 @@ +describe :method_dup, shared: true do + it "returns a copy of self" do + a = Object.new.method(: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 = Object.new.method(: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 = Object.new.method(: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/method/shared/to_s.rb b/spec/ruby/core/method/shared/to_s.rb index 0dcae41b59..b2d27d370f 100644 --- a/spec/ruby/core/method/shared/to_s.rb +++ b/spec/ruby/core/method/shared/to_s.rb @@ -24,19 +24,17 @@ describe :method_to_s, shared: true do @string.should =~ /\#bar/ end - ruby_version_is "2.7" do - it "returns a String containing method arguments" do - obj = MethodSpecs::Methods.new - obj.method(:zero).send(@method).should.include?("()") - obj.method(:one_req).send(@method).should.include?("(a)") - obj.method(:one_req_named).send(@method).should.include?("(a:)") - obj.method(:zero_with_block).send(@method).should.include?("(&blk)") - obj.method(:one_opt).send(@method).should.include?("(a=...)") - obj.method(:one_opt_named).send(@method).should.include?("(a: ...)") - obj.method(:zero_with_splat).send(@method).should.include?("(*a)") - obj.method(:zero_with_double_splat).send(@method).should.include?("(**a)") - obj.method(:one_req_one_opt_with_splat_and_block).send(@method).should.include?("(a, b=..., *c, &blk)") - end + it "returns a String containing method arguments" do + obj = MethodSpecs::Methods.new + obj.method(:zero).send(@method).should.include?("()") + obj.method(:one_req).send(@method).should.include?("(a)") + obj.method(:one_req_named).send(@method).should.include?("(a:)") + obj.method(:zero_with_block).send(@method).should.include?("(&blk)") + obj.method(:one_opt).send(@method).should.include?("(a=...)") + obj.method(:one_opt_named).send(@method).should.include?("(a: ...)") + obj.method(:zero_with_splat).send(@method).should.include?("(*a)") + obj.method(:zero_with_double_splat).send(@method).should.include?("(**a)") + obj.method(:one_req_one_opt_with_splat_and_block).send(@method).should.include?("(a, b=..., *c, &blk)") end it "returns a String containing the Module the method is defined in" do @@ -55,20 +53,18 @@ describe :method_to_s, shared: true do MethodSpecs::A.new.method(:baz).send(@method).should.start_with? "#<Method: MethodSpecs::A#baz" end - ruby_version_is '3.0' do - it "returns a String containing the Module containing the method if object has a singleton class but method is not defined in the singleton class" do - obj = MethodSpecs::MySub.new - obj.singleton_class - @m = obj.method(:bar) - @string = @m.send(@method) - @string.should.start_with? "#<Method: MethodSpecs::MySub(MethodSpecs::MyMod)#bar" + it "returns a String containing the Module containing the method if object has a singleton class but method is not defined in the singleton class" do + obj = MethodSpecs::MySub.new + obj.singleton_class + @m = obj.method(:bar) + @string = @m.send(@method) + @string.should.start_with? "#<Method: MethodSpecs::MySub(MethodSpecs::MyMod)#bar" - c = MethodSpecs::MySub.dup - m = Module.new{def bar; end} - c.extend(m) - @string = c.method(:bar).send(@method) - @string.should.start_with? "#<Method: #<Class:#{c.inspect}>(#{m.inspect})#bar" - end + c = MethodSpecs::MySub.dup + m = Module.new{def bar; end} + c.extend(m) + @string = c.method(:bar).send(@method) + @string.should.start_with? "#<Method: #<Class:#{c.inspect}>(#{m.inspect})#bar" end it "returns a String containing the singleton class if method is defined in the singleton class" do @@ -79,11 +75,7 @@ describe :method_to_s, shared: true do @string.should.start_with? "#<Method: #<MethodSpecs::MySub:0xXXXXXX>.bar" end - ruby_version_is '2.7' do - ruby_bug '#17428', '2.7'...'3.0' do - it "shows the metaclass and the owner for a Module instance method retrieved from a class" do - String.method(:include).inspect.should.start_with?("#<Method: #<Class:String>(Module)#include") - end - end + it "shows the metaclass and the owner for a Module instance method retrieved from a class" do + String.method(:include).inspect.should.start_with?("#<Method: #<Class:String>(Module)#include") end end diff --git a/spec/ruby/core/method/source_location_spec.rb b/spec/ruby/core/method/source_location_spec.rb index 1f476aaa9b..c5b296f6e2 100644 --- a/spec/ruby/core/method/source_location_spec.rb +++ b/spec/ruby/core/method/source_location_spec.rb @@ -13,7 +13,7 @@ describe "Method#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 @@ -104,6 +104,13 @@ describe "Method#source_location" do end end + it "works for eval with a given line" do + c = Class.new do + eval('def self.m; end', nil, "foo", 100) + end + c.method(:m).source_location.should == ["foo", 100] + end + describe "for a Method generated by respond_to_missing?" do it "returns nil" do m = MethodSpecs::Methods.new diff --git a/spec/ruby/core/method/super_method_spec.rb b/spec/ruby/core/method/super_method_spec.rb index e5d8b87a06..c63a7aaa0f 100644 --- a/spec/ruby/core/method/super_method_spec.rb +++ b/spec/ruby/core/method/super_method_spec.rb @@ -42,4 +42,23 @@ describe "Method#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 + MethodSpecs::InheritedMethods::C.new.derp.should == 'BA' + end + + it "returns the expected super_method" do + method = MethodSpecs::InheritedMethods::C.new.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.new.method(:meow) + method.super_method.owner.should == MethodSpecs::InheritedMethods::A + end + end end diff --git a/spec/ruby/core/method/to_proc_spec.rb b/spec/ruby/core/method/to_proc_spec.rb index 29b7bec2b3..4993cce239 100644 --- a/spec/ruby/core/method/to_proc_spec.rb +++ b/spec/ruby/core/method/to_proc_spec.rb @@ -35,7 +35,7 @@ describe "Method#to_proc" do end it "returns a proc that can be used by define_method" do - x = 'test' + x = +'test' to_s = class << x define_method :foo, method(:to_s).to_proc to_s 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 |