diff options
Diffstat (limited to 'spec/ruby/core/method')
| -rw-r--r-- | spec/ruby/core/method/case_compare_spec.rb | 6 | ||||
| -rw-r--r-- | spec/ruby/core/method/clone_spec.rb | 15 | ||||
| -rw-r--r-- | spec/ruby/core/method/compose_spec.rb | 132 | ||||
| -rw-r--r-- | spec/ruby/core/method/curry_spec.rb | 10 | ||||
| -rw-r--r-- | spec/ruby/core/method/dup_spec.rb | 15 | ||||
| -rw-r--r-- | spec/ruby/core/method/fixtures/classes.rb | 37 | ||||
| -rw-r--r-- | spec/ruby/core/method/original_name_spec.rb | 22 | ||||
| -rw-r--r-- | spec/ruby/core/method/owner_spec.rb | 4 | ||||
| -rw-r--r-- | spec/ruby/core/method/parameters_spec.rb | 49 | ||||
| -rw-r--r-- | spec/ruby/core/method/private_spec.rb | 9 | ||||
| -rw-r--r-- | spec/ruby/core/method/protected_spec.rb | 9 | ||||
| -rw-r--r-- | spec/ruby/core/method/public_spec.rb | 9 | ||||
| -rw-r--r-- | spec/ruby/core/method/shared/call.rb | 4 | ||||
| -rw-r--r-- | spec/ruby/core/method/shared/dup.rb | 32 | ||||
| -rw-r--r-- | spec/ruby/core/method/shared/to_s.rb | 49 | ||||
| -rw-r--r-- | spec/ruby/core/method/source_location_spec.rb | 45 | ||||
| -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 | 29 |
19 files changed, 389 insertions, 108 deletions
diff --git a/spec/ruby/core/method/case_compare_spec.rb b/spec/ruby/core/method/case_compare_spec.rb index 17785b5c1d..a78953e8ad 100644 --- a/spec/ruby/core/method/case_compare_spec.rb +++ b/spec/ruby/core/method/case_compare_spec.rb @@ -2,8 +2,6 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' require_relative 'shared/call' -ruby_version_is "2.5" do - describe "Method#===" do - it_behaves_like :method_call, :=== - end +describe "Method#===" do + it_behaves_like :method_call, :=== end 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 aa0aded775..7506e33ea8 100644 --- a/spec/ruby/core/method/compose_spec.rb +++ b/spec/ruby/core/method/compose_spec.rb @@ -2,100 +2,98 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' require_relative '../proc/shared/compose' -ruby_version_is "2.6" do - describe "Method#<<" do - it "returns a Proc that is the composition of self and the passed Proc" do - succ = MethodSpecs::Composition.new.method(:succ) - upcase = proc { |s| s.upcase } +describe "Method#<<" do + it "returns a Proc that is the composition of self and the passed Proc" do + succ = MethodSpecs::Composition.new.method(:succ) + upcase = proc { |s| s.upcase } - (succ << upcase).call('Ruby').should == "RUBZ" - end + (succ << upcase).call('Ruby').should == "RUBZ" + end - it "calls passed Proc with arguments and then calls self with result" do - pow_2_proc = proc { |x| x * x } - double_proc = proc { |x| x + x } + it "calls passed Proc with arguments and then calls self with result" do + pow_2_proc = proc { |x| x * x } + double_proc = proc { |x| x + x } - pow_2_method = MethodSpecs::Composition.new.method(:pow_2) - double_method = MethodSpecs::Composition.new.method(:double) + pow_2_method = MethodSpecs::Composition.new.method(:pow_2) + double_method = MethodSpecs::Composition.new.method(:double) - (pow_2_method << double_proc).call(2).should == 16 - (double_method << pow_2_proc).call(2).should == 8 - end + (pow_2_method << double_proc).call(2).should == 16 + (double_method << pow_2_proc).call(2).should == 8 + end - it "accepts any callable object" do - inc = MethodSpecs::Composition.new.method(:inc) + it "accepts any callable object" do + inc = MethodSpecs::Composition.new.method(:inc) - double = Object.new - def double.call(n); n * 2; end + double = Object.new + def double.call(n); n * 2; end - (inc << double).call(3).should == 7 - end + (inc << double).call(3).should == 7 + end - it_behaves_like :proc_compose, :<<, -> { MethodSpecs::Composition.new.method(:upcase) } + it_behaves_like :proc_compose, :<<, -> { MethodSpecs::Composition.new.method(:upcase) } - describe "composition" do - it "is a lambda" do - pow_2 = MethodSpecs::Composition.new.method(:pow_2) - double = proc { |x| x + x } + describe "composition" do + it "is a lambda" do + pow_2 = MethodSpecs::Composition.new.method(:pow_2) + double = proc { |x| x + x } - (pow_2 << double).is_a?(Proc).should == true - (pow_2 << double).lambda?.should == true - end + (pow_2 << double).is_a?(Proc).should == true + (pow_2 << double).should_not.lambda? + end - it "may accept multiple arguments" do - inc = MethodSpecs::Composition.new.method(:inc) - mul = proc { |n, m| n * m } + it "may accept multiple arguments" do + inc = MethodSpecs::Composition.new.method(:inc) + mul = proc { |n, m| n * m } - (inc << mul).call(2, 3).should == 7 - end + (inc << mul).call(2, 3).should == 7 end end +end - describe "Method#>>" do - it "returns a Proc that is the composition of self and the passed Proc" do - upcase = proc { |s| s.upcase } - succ = MethodSpecs::Composition.new.method(:succ) +describe "Method#>>" do + it "returns a Proc that is the composition of self and the passed Proc" do + upcase = proc { |s| s.upcase } + succ = MethodSpecs::Composition.new.method(:succ) - (succ >> upcase).call('Ruby').should == "RUBZ" - end + (succ >> upcase).call('Ruby').should == "RUBZ" + end - it "calls passed Proc with arguments and then calls self with result" do - pow_2_proc = proc { |x| x * x } - double_proc = proc { |x| x + x } + it "calls passed Proc with arguments and then calls self with result" do + pow_2_proc = proc { |x| x * x } + double_proc = proc { |x| x + x } - pow_2_method = MethodSpecs::Composition.new.method(:pow_2) - double_method = MethodSpecs::Composition.new.method(:double) + pow_2_method = MethodSpecs::Composition.new.method(:pow_2) + double_method = MethodSpecs::Composition.new.method(:double) - (pow_2_method >> double_proc).call(2).should == 8 - (double_method >> pow_2_proc).call(2).should == 16 - end + (pow_2_method >> double_proc).call(2).should == 8 + (double_method >> pow_2_proc).call(2).should == 16 + end - it "accepts any callable object" do - inc = MethodSpecs::Composition.new.method(:inc) + it "accepts any callable object" do + inc = MethodSpecs::Composition.new.method(:inc) - double = Object.new - def double.call(n); n * 2; end + double = Object.new + def double.call(n); n * 2; end - (inc >> double).call(3).should == 8 - end + (inc >> double).call(3).should == 8 + end - it_behaves_like :proc_compose, :>>, -> { MethodSpecs::Composition.new.method(:upcase) } + it_behaves_like :proc_compose, :>>, -> { MethodSpecs::Composition.new.method(:upcase) } - describe "composition" do - it "is a lambda" do - pow_2 = MethodSpecs::Composition.new.method(:pow_2) - double = proc { |x| x + x } + describe "composition" do + it "is a lambda" do + pow_2 = MethodSpecs::Composition.new.method(:pow_2) + double = proc { |x| x + x } - (pow_2 >> double).is_a?(Proc).should == true - (pow_2 >> double).lambda?.should == true - end + (pow_2 >> double).is_a?(Proc).should == true + (pow_2 >> double).should.lambda? + end - it "may accept multiple arguments" do - mul = MethodSpecs::Composition.new.method(:mul) - inc = proc { |n| n + 1 } + it "may accept multiple arguments" do + mul = MethodSpecs::Composition.new.method(:mul) + inc = proc { |n| n + 1 } - (mul >> inc).call(2, 3).should == 7 - end + (mul >> inc).call(2, 3).should == 7 end end end diff --git a/spec/ruby/core/method/curry_spec.rb b/spec/ruby/core/method/curry_spec.rb index 36449f7512..219de0f6b5 100644 --- a/spec/ruby/core/method/curry_spec.rb +++ b/spec/ruby/core/method/curry_spec.rb @@ -23,14 +23,14 @@ describe "Method#curry" do end it "raises ArgumentError when the method requires less arguments than the given arity" do - lambda { @obj.method(:zero).curry(1) }.should raise_error(ArgumentError) - lambda { @obj.method(:one_req_one_opt).curry(3) }.should raise_error(ArgumentError) - lambda { @obj.method(:two_req_one_opt_with_block).curry(4) }.should raise_error(ArgumentError) + -> { @obj.method(:zero).curry(1) }.should raise_error(ArgumentError) + -> { @obj.method(:one_req_one_opt).curry(3) }.should raise_error(ArgumentError) + -> { @obj.method(:two_req_one_opt_with_block).curry(4) }.should raise_error(ArgumentError) end it "raises ArgumentError when the method requires more arguments than the given arity" do - lambda { @obj.method(:two_req_with_splat).curry(1) }.should raise_error(ArgumentError) - lambda { @obj.method(:one_req).curry(0) }.should raise_error(ArgumentError) + -> { @obj.method(:two_req_with_splat).curry(1) }.should raise_error(ArgumentError) + -> { @obj.method(:one_req).curry(0) }.should raise_error(ArgumentError) end end end 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 315672c6e0..464a519aea 100644 --- a/spec/ruby/core/method/fixtures/classes.rb +++ b/spec/ruby/core/method/fixtures/classes.rb @@ -26,6 +26,7 @@ module MethodSpecs end alias bar foo + alias baz bar def same_as_foo true @@ -49,6 +50,8 @@ module MethodSpecs def one_req(a); end def two_req(a, b); end + def one_req_named(a:); end + def zero_with_block(&blk); end def one_req_with_block(a, &blk); end def two_req_with_block(a, b, &blk); end @@ -58,6 +61,8 @@ module MethodSpecs def one_req_two_opt(a, b=nil, c=nil); end def two_req_one_opt(a, b, c=nil); end + def one_opt_named(a: nil); end + def one_opt_with_block(a=nil, &blk); end def one_req_one_opt_with_block(a, b=nil, &blk); end def one_req_two_opt_with_block(a, b=nil, c=nil, &blk); end @@ -70,6 +75,8 @@ module MethodSpecs def two_req_one_opt_with_splat(a, b, c=nil, *d); end def one_req_two_opt_with_splat(a, b=nil, c=nil, *d); end + def zero_with_double_splat(**a); end + def zero_with_splat_and_block(*a, &blk); end def one_req_with_splat_and_block(a, *b, &blk); end def two_req_with_splat_and_block(a, b, *c, &blk); end @@ -77,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|}) @@ -206,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/original_name_spec.rb b/spec/ruby/core/method/original_name_spec.rb new file mode 100644 index 0000000000..676fdaedb4 --- /dev/null +++ b/spec/ruby/core/method/original_name_spec.rb @@ -0,0 +1,22 @@ +require_relative '../../spec_helper' +require_relative 'fixtures/classes' + +describe "Method#original_name" do + it "returns the name of the method" do + "abc".method(:upcase).original_name.should == :upcase + end + + it "returns the original name when aliased" do + obj = MethodSpecs::Methods.new + obj.method(:foo).original_name.should == :foo + obj.method(:bar).original_name.should == :foo + obj.method(:bar).unbind.bind(obj).original_name.should == :foo + end + + it "returns the original name even when aliased twice" do + obj = MethodSpecs::Methods.new + obj.method(:foo).original_name.should == :foo + obj.method(:baz).original_name.should == :foo + obj.method(:baz).unbind.bind(obj).original_name.should == :foo + end +end diff --git a/spec/ruby/core/method/owner_spec.rb b/spec/ruby/core/method/owner_spec.rb index ca5dff7295..1cdc4edfa7 100644 --- a/spec/ruby/core/method/owner_spec.rb +++ b/spec/ruby/core/method/owner_spec.rb @@ -23,4 +23,8 @@ describe "Method#owner" do @m.method(:handled_via_method_missing).owner.should == MethodSpecs::Methods end end + + 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 diff --git a/spec/ruby/core/method/parameters_spec.rb b/spec/ruby/core/method/parameters_spec.rb index 1de3901040..f1c2523cf0 100644 --- a/spec/ruby/core/method/parameters_spec.rb +++ b/spec/ruby/core/method/parameters_spec.rb @@ -7,19 +7,25 @@ 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 def one_splat_one_req_with_block(*a,b,&blk); end - def one_opt_with_stabby(a=->(b){true}); end + 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]] @@ -222,9 +233,27 @@ describe "Method#parameters" do m.method(:handled_via_method_missing).parameters.should == [[:rest]] end - it "adds nameless 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 + + it "adds block arg with name & for anonymous block argument" do + object = Object.new + def object.foo(&) + end + + object.method(:foo).parameters.should == [[:block, :&]] + end + + it "returns [:rest, :*], [:keyrest, :**], [:block, :&] for forward parameters operator" do m = MethodSpecs::Methods.new - m.method(:one_unnamed_splat).parameters.should == [[:rest]] + m.method(:forward_parameters).parameters.should == [[:rest, :*], [:keyrest, :**], [:block, :&]] end it "returns the args and block for a splat and block argument" do @@ -242,6 +271,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..e708542b2e --- /dev/null +++ b/spec/ruby/core/method/private_spec.rb @@ -0,0 +1,9 @@ +require_relative '../../spec_helper' +require_relative 'fixtures/classes' + +describe "Method#private?" do + it "has been removed" do + obj = MethodSpecs::Methods.new + obj.method(:my_private_method).should_not.respond_to?(:private?) + 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..f9e422ae3d --- /dev/null +++ b/spec/ruby/core/method/protected_spec.rb @@ -0,0 +1,9 @@ +require_relative '../../spec_helper' +require_relative 'fixtures/classes' + +describe "Method#protected?" do + it "has been removed" do + obj = MethodSpecs::Methods.new + obj.method(:my_protected_method).should_not.respond_to?(:protected?) + 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..4cb23f4cf1 --- /dev/null +++ b/spec/ruby/core/method/public_spec.rb @@ -0,0 +1,9 @@ +require_relative '../../spec_helper' +require_relative 'fixtures/classes' + +describe "Method#public?" do + it "has been removed" do + obj = MethodSpecs::Methods.new + obj.method(:my_public_method).should_not.respond_to?(:public?) + end +end diff --git a/spec/ruby/core/method/shared/call.rb b/spec/ruby/core/method/shared/call.rb index f178b9da7d..f26e373695 100644 --- a/spec/ruby/core/method/shared/call.rb +++ b/spec/ruby/core/method/shared/call.rb @@ -9,10 +9,10 @@ describe :method_call, shared: true do end it "raises an ArgumentError when given incorrect number of arguments" do - lambda { + -> { MethodSpecs::Methods.new.method(:two_req).send(@method, 1, 2, 3) }.should raise_error(ArgumentError) - lambda { + -> { MethodSpecs::Methods.new.method(:two_req).send(@method, 1) }.should raise_error(ArgumentError) 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..c74847083f --- /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 373398a785..b2d27d370f 100644 --- a/spec/ruby/core/method/shared/to_s.rb +++ b/spec/ruby/core/method/shared/to_s.rb @@ -4,7 +4,7 @@ require_relative '../fixtures/classes' describe :method_to_s, shared: true do before :each do @m = MethodSpecs::MySub.new.method :bar - @string = @m.send(@method).sub(/0x\w+/, '0xXXXXXX') + @string = @m.send(@method) end it "returns a String" do @@ -24,6 +24,19 @@ describe :method_to_s, shared: true do @string.should =~ /\#bar/ 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 @string.should =~ /MethodSpecs::MyMod/ end @@ -31,4 +44,38 @@ describe :method_to_s, shared: true do it "returns a String containing the Module the method is referenced from" do @string.should =~ /MethodSpecs::MySub/ end + + it "returns a String including all details" do + @string.should.start_with? "#<Method: MethodSpecs::MySub(MethodSpecs::MyMod)#bar" + end + + it "does not show the defining module if it is the same as the receiver class" do + MethodSpecs::A.new.method(:baz).send(@method).should.start_with? "#<Method: MethodSpecs::A#baz" + end + + 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 + + it "returns a String containing the singleton class if method is defined in the singleton class" do + obj = MethodSpecs::MySub.new + def obj.bar; end + @m = obj.method(:bar) + @string = @m.send(@method).sub(/0x\h+/, '0xXXXXXX') + @string.should.start_with? "#<Method: #<MethodSpecs::MySub:0xXXXXXX>.bar" + 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 dd81b02c77..87413a2ab6 100644 --- a/spec/ruby/core/method/source_location_spec.rb +++ b/spec/ruby/core/method/source_location_spec.rb @@ -11,23 +11,23 @@ describe "Method#source_location" do end it "sets the first value to the path of the file in which the method was defined" do - file = @method.source_location.first + file = @method.source_location[0] 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 a Fixnum representing the line on which the method was defined" do - line = @method.source_location.last - line.should be_an_instance_of(Fixnum) + it "sets the last value to an Integer representing the line on which the method was defined" do + line = @method.source_location[1] + line.should be_an_instance_of(Integer) line.should == 5 end it "returns the last place the method was defined" do - MethodSpecs::SourceLocation.method(:redefined).source_location.last.should == 13 + MethodSpecs::SourceLocation.method(:redefined).source_location[1].should == 13 end it "returns the location of the original method even if it was aliased" do - MethodSpecs::SourceLocation.new.method(:aka).source_location.last.should == 17 + MethodSpecs::SourceLocation.new.method(:aka).source_location[1].should == 17 end it "works for methods defined with a block" do @@ -86,6 +86,37 @@ describe "Method#source_location" do method.source_location[1].should == line end + it "works for core methods where it returns nil or <internal:" do + loc = method(:__id__).source_location + if loc == nil + loc.should == nil + else + loc[0].should.start_with?('<internal:') + loc[1].should be_kind_of(Integer) + end + + loc = method(:tap).source_location + if loc == nil + loc.should == nil + else + loc[0].should.start_with?('<internal:') + loc[1].should be_kind_of(Integer) + end + end + + it "works for eval with a given line" do + c = Class.new do + eval('def self.m; end', nil, "foo", 100) + end + location = c.method(:m).source_location + ruby_version_is(""..."4.1") do + location.should == ["foo", 100] + end + ruby_version_is("4.1") do + location.should == ["foo", 100, 0, 100, 15] + end + 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 3d36c6d675..0b630e4d88 100644 --- a/spec/ruby/core/method/unbind_spec.rb +++ b/spec/ruby/core/method/unbind_spec.rb @@ -14,20 +14,29 @@ describe "Method#unbind" do @normal_um.should be_kind_of(UnboundMethod) end - it "returns a String containing 'UnboundMethod'" do - @string.should =~ /\bUnboundMethod\b/ - end + describe "#inspect" do + it "returns a String containing 'UnboundMethod'" do + @string.should =~ /\bUnboundMethod\b/ + end - it "returns a String containing the method name" do - @string.should =~ /\#bar/ - end + it "returns a String containing the method name" do + @string.should =~ /\#bar/ + end + + it "returns a String containing the Module the method is defined in" do + @string.should =~ /MethodSpecs::MyMod/ + end - it "returns a String containing the Module the method is defined in" do - @string.should =~ /MethodSpecs::MyMod/ + it "returns a String containing the Module the method is referenced from" do + @string.should =~ /MethodSpecs::MyMod/ + end end - it "returns a String containing the Module the method is referenced from" do - @string.should =~ /MethodSpecs::MySub/ + it "keeps the origin singleton class if there is one" do + obj = Object.new + def obj.foo + end + obj.method(:foo).unbind.inspect.should.start_with?("#<UnboundMethod: #{obj.singleton_class}#foo") end specify "rebinding UnboundMethod to Method's obj produces exactly equivalent Methods" do |
