diff options
Diffstat (limited to 'spec/ruby/core/method')
22 files changed, 474 insertions, 138 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..79c5d7c662 100644 --- a/spec/ruby/core/method/curry_spec.rb +++ b/spec/ruby/core/method/curry_spec.rb @@ -7,7 +7,7 @@ describe "Method#curry" do def x.foo(a,b,c); [a,b,c]; end c = x.method(:foo).curry - c.should be_kind_of(Proc) + c.should.is_a?(Proc) c.call(1).call(2, 3).should == [1,2,3] end @@ -17,20 +17,20 @@ describe "Method#curry" do end it "returns a curried proc when given correct arity" do - @obj.method(:one_req).curry(1).should be_kind_of(Proc) - @obj.method(:zero_with_splat).curry(100).should be_kind_of(Proc) - @obj.method(:two_req_with_splat).curry(2).should be_kind_of(Proc) + @obj.method(:one_req).curry(1).should.is_a?(Proc) + @obj.method(:zero_with_splat).curry(100).should.is_a?(Proc) + @obj.method(:two_req_with_splat).curry(2).should.is_a?(Proc) 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(ArgumentError) + -> { @obj.method(:one_req_one_opt).curry(3) }.should.raise(ArgumentError) + -> { @obj.method(:two_req_one_opt_with_block).curry(4) }.should.raise(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(ArgumentError) + -> { @obj.method(:one_req).curry(0) }.should.raise(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..41904df1d1 100644 --- a/spec/ruby/core/method/fixtures/classes.rb +++ b/spec/ruby/core/method/fixtures/classes.rb @@ -26,6 +26,8 @@ module MethodSpecs end alias bar foo + alias baz bar + alias qux baz def same_as_foo true @@ -49,6 +51,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 +62,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 +76,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 +85,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 +220,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/hash_spec.rb b/spec/ruby/core/method/hash_spec.rb index b6807ca1e2..d6c8440acc 100644 --- a/spec/ruby/core/method/hash_spec.rb +++ b/spec/ruby/core/method/hash_spec.rb @@ -2,8 +2,6 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' describe "Method#hash" do - it "needs to be reviewed for spec completeness" - it "returns the same value for user methods that are eql?" do obj = MethodSpecs::Methods.new obj.method(:foo).hash.should == obj.method(:bar).hash 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..8fec0e7c33 --- /dev/null +++ b/spec/ruby/core/method/original_name_spec.rb @@ -0,0 +1,43 @@ +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 + + it "returns the original name even when aliased thrice" do + obj = MethodSpecs::Methods.new + obj.method(:qux).original_name.should == :foo + obj.method(:qux).unbind.bind(obj).original_name.should == :foo + end + + it "returns the source UnboundMethod's name (not the name given to define_method)" do + klass = Class.new { define_method(:my_inspect, ::Kernel.instance_method(:inspect)) } + klass.new.method(:my_inspect).original_name.should == :inspect + end + + it "preserves the source method's name through define_method and alias" do + source = Class.new { def my_method; end } + klass = Class.new(source) do + define_method(:renamed, source.instance_method(:my_method)) + alias aliased renamed + end + klass.new.method(:renamed).original_name.should == :my_method + klass.new.method(:aliased).original_name.should == :my_method + 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 d750f4c221..fd88e8dcb8 100644 --- a/spec/ruby/core/method/parameters_spec.rb +++ b/spec/ruby/core/method/parameters_spec.rb @@ -7,19 +7,31 @@ 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 + ruby_version_is "4.1" do + eval <<-RUBY + def one_noblock(&nil); end + RUBY + end + + def forward_parameters(...) end + + def underscore_parameters(_, _, _ = 1, *_, _:, _: 2, **_, &_); end + define_method(:one_optional_defined_method) {|x = 1|} end @@ -176,6 +188,18 @@ 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 + + ruby_version_is "4.1" do + it "returns [[:noblock]] for a method with a single &nil parameter" do + m = MethodSpecs::Methods.instance_method(:one_noblock) + m.parameters.should == [[:noblock]] + end + 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 +246,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]] + 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(:forward_parameters).parameters.should == [[:rest, :*], [:keyrest, :**], [:block, :&]] end it "returns the args and block for a splat and block argument" do @@ -241,4 +283,35 @@ describe "Method#parameters" do m = MethodSpecs::Methods.new 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]] + end + + it "returns [[:rest]] or [[:opt]] for core methods with optional arguments" do + # pop takes 1 optional argument + [ + [[:rest]], + [[:opt]] + ].should.include?([].method(:pop).parameters) + end + + it "returns [[:req]] for each parameter for core methods with fixed-length argument lists" do + "foo".method(:+).parameters.should == [[:req]] + end end 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/receiver_spec.rb b/spec/ruby/core/method/receiver_spec.rb index 2b2e11dd2e..315a08d288 100644 --- a/spec/ruby/core/method/receiver_spec.rb +++ b/spec/ruby/core/method/receiver_spec.rb @@ -4,19 +4,19 @@ require_relative 'fixtures/classes' describe "Method#receiver" do it "returns the receiver of the method" do s = "abc" - s.method(:upcase).receiver.should equal(s) + s.method(:upcase).receiver.should.equal?(s) end it "returns the right receiver even when aliased" do obj = MethodSpecs::Methods.new - obj.method(:foo).receiver.should equal(obj) - obj.method(:bar).receiver.should equal(obj) + obj.method(:foo).receiver.should.equal?(obj) + obj.method(:bar).receiver.should.equal?(obj) end describe "for a Method generated by respond_to_missing?" do it "returns the receiver of the method" do m = MethodSpecs::Methods.new - m.method(:handled_via_method_missing).receiver.should equal(m) + m.method(:handled_via_method_missing).receiver.should.equal?(m) end end end diff --git a/spec/ruby/core/method/shared/call.rb b/spec/ruby/core/method/shared/call.rb index f178b9da7d..41ee2b06cb 100644 --- a/spec/ruby/core/method/shared/call.rb +++ b/spec/ruby/core/method/shared/call.rb @@ -9,12 +9,12 @@ 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 { + }.should.raise(ArgumentError) + -> { MethodSpecs::Methods.new.method(:two_req).send(@method, 1) - }.should raise_error(ArgumentError) + }.should.raise(ArgumentError) end describe "for a Method generated by respond_to_missing?" do diff --git a/spec/ruby/core/method/shared/dup.rb b/spec/ruby/core/method/shared/dup.rb new file mode 100644 index 0000000000..eee790890a --- /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/eql.rb b/spec/ruby/core/method/shared/eql.rb index 5c720cbac1..3c340202b7 100644 --- a/spec/ruby/core/method/shared/eql.rb +++ b/spec/ruby/core/method/shared/eql.rb @@ -12,54 +12,54 @@ describe :method_equal, shared: true do it "returns true if methods are the same" do m2 = @m.method(:foo) - @m_foo.send(@method, @m_foo).should be_true - @m_foo.send(@method, m2).should be_true + @m_foo.send(@method, @m_foo).should == true + @m_foo.send(@method, m2).should == true end it "returns true on aliased methods" do m_bar = @m.method(:bar) - m_bar.send(@method, @m_foo).should be_true + m_bar.send(@method, @m_foo).should == true end it "returns true if the two core methods are aliases" do s = "hello" a = s.method(:size) b = s.method(:length) - a.send(@method, b).should be_true + a.send(@method, b).should == true end it "returns false on a method which is neither aliased nor the same method" do m2 = @m.method(:zero) - @m_foo.send(@method, m2).should be_false + @m_foo.send(@method, m2).should == false end it "returns false for a method which is not bound to the same object" do m2_foo = @m2.method(:foo) a_baz = @a.method(:baz) - @m_foo.send(@method, m2_foo).should be_false - @m_foo.send(@method, a_baz).should be_false + @m_foo.send(@method, m2_foo).should == false + @m_foo.send(@method, a_baz).should == false end it "returns false if the two methods are bound to the same object but were defined independently" do m2 = @m.method(:same_as_foo) - @m_foo.send(@method, m2).should be_false + @m_foo.send(@method, m2).should == false end it "returns true if a method was defined using the other one" do MethodSpecs::Methods.send :define_method, :defined_foo, MethodSpecs::Methods.instance_method(:foo) m2 = @m.method(:defined_foo) - @m_foo.send(@method, m2).should be_true + @m_foo.send(@method, m2).should == true end it "returns false if comparing a method defined via define_method and def" do defn = @m.method(:zero) defined = @m.method(:zero_defined_method) - defn.send(@method, defined).should be_false - defined.send(@method, defn).should be_false + defn.send(@method, defined).should == false + defined.send(@method, defn).should == false end describe 'missing methods' do @@ -68,8 +68,8 @@ describe :method_equal, shared: true do miss1bis = @m.method(:handled_via_method_missing) miss2 = @m.method(:also_handled) - miss1.send(@method, miss1bis).should be_true - miss1.send(@method, miss2).should be_false + miss1.send(@method, miss1bis).should == true + miss1.send(@method, miss2).should == false end it 'calls respond_to_missing? with true to include private methods' do @@ -81,14 +81,14 @@ describe :method_equal, shared: true do it "returns false if the two methods are bound to different objects, have the same names, and identical bodies" do a = MethodSpecs::Eql.instance_method(:same_body) b = MethodSpecs::Eql2.instance_method(:same_body) - a.send(@method, b).should be_false + a.send(@method, b).should == false end it "returns false if the argument is not a Method object" do - String.instance_method(:size).send(@method, 7).should be_false + String.instance_method(:size).send(@method, 7).should == false end it "returns false if the argument is an unbound version of self" do - method(:load).send(@method, method(:load).unbind).should be_false + method(:load).send(@method, method(:load).unbind).should == false end end diff --git a/spec/ruby/core/method/shared/to_s.rb b/spec/ruby/core/method/shared/to_s.rb index 373398a785..bfb58e6896 100644 --- a/spec/ruby/core/method/shared/to_s.rb +++ b/spec/ruby/core/method/shared/to_s.rb @@ -4,16 +4,16 @@ 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 - @m.send(@method).should be_kind_of(String) + @m.send(@method).should.is_a?(String) end it "returns a String for methods defined with attr_accessor" do m = MethodSpecs::Methods.new.method :attr - m.send(@method).should be_kind_of(String) + m.send(@method).should.is_a?(String) end it "returns a String containing 'Method'" 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..22fcb98c74 100644 --- a/spec/ruby/core/method/source_location_spec.rb +++ b/spec/ruby/core/method/source_location_spec.rb @@ -7,18 +7,18 @@ describe "Method#source_location" do end it "returns an Array" do - @method.source_location.should be_an_instance_of(Array) + @method.source_location.should.instance_of?(Array) 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.should be_an_instance_of(String) - file.should == File.realpath('../fixtures/classes.rb', __FILE__) + file.should.instance_of?(String) + 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 + it "sets the last value to an Integer representing the line on which the method was defined" do line = @method.source_location.last - line.should be_an_instance_of(Fixnum) + line.should.instance_of?(Integer) line.should == 5 end @@ -86,10 +86,35 @@ 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.is_a?(Integer) + end + + loc = method(:tap).source_location + if loc == nil + loc.should == nil + else + loc[0].should.start_with?('<internal:') + loc[1].should.is_a?(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 + 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 - m.method(:handled_via_method_missing).source_location.should be_nil + m.method(:handled_via_method_missing).source_location.should == nil end end end 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 92bd64aaf1..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 @@ -90,4 +90,15 @@ describe "Method#to_proc" do array.each(&obj) ScratchPad.recorded.should == [[1, 2]] end + + it "returns a proc that properly invokes module methods with super" do + m1 = Module.new { def foo(ary); ary << :m1; end; } + m2 = Module.new { def foo(ary = []); super(ary); ary << :m2; end; } + c2 = Class.new do + include m1 + include m2 + end + + c2.new.method(:foo).to_proc.call.should == %i[m1 m2] + end end diff --git a/spec/ruby/core/method/unbind_spec.rb b/spec/ruby/core/method/unbind_spec.rb index 3d36c6d675..994c3a8879 100644 --- a/spec/ruby/core/method/unbind_spec.rb +++ b/spec/ruby/core/method/unbind_spec.rb @@ -11,23 +11,32 @@ describe "Method#unbind" do end it "returns an UnboundMethod" do - @normal_um.should be_kind_of(UnboundMethod) + @normal_um.should.is_a?(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 |
