summaryrefslogtreecommitdiff
path: root/spec/ruby/core/method
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/core/method')
-rw-r--r--spec/ruby/core/method/curry_spec.rb18
-rw-r--r--spec/ruby/core/method/fixtures/classes.rb1
-rw-r--r--spec/ruby/core/method/original_name_spec.rb21
-rw-r--r--spec/ruby/core/method/owner_spec.rb6
-rw-r--r--spec/ruby/core/method/parameters_spec.rb41
-rw-r--r--spec/ruby/core/method/private_spec.rb25
-rw-r--r--spec/ruby/core/method/protected_spec.rb25
-rw-r--r--spec/ruby/core/method/public_spec.rb25
-rw-r--r--spec/ruby/core/method/receiver_spec.rb8
-rw-r--r--spec/ruby/core/method/shared/call.rb4
-rw-r--r--spec/ruby/core/method/shared/dup.rb2
-rw-r--r--spec/ruby/core/method/shared/eql.rb32
-rw-r--r--spec/ruby/core/method/shared/to_s.rb4
-rw-r--r--spec/ruby/core/method/source_location_spec.rb28
-rw-r--r--spec/ruby/core/method/unbind_spec.rb14
15 files changed, 101 insertions, 153 deletions
diff --git a/spec/ruby/core/method/curry_spec.rb b/spec/ruby/core/method/curry_spec.rb
index 219de0f6b5..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
- -> { @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)
+ -> { @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
- -> { @obj.method(:two_req_with_splat).curry(1) }.should raise_error(ArgumentError)
- -> { @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/fixtures/classes.rb b/spec/ruby/core/method/fixtures/classes.rb
index 464a519aea..41904df1d1 100644
--- a/spec/ruby/core/method/fixtures/classes.rb
+++ b/spec/ruby/core/method/fixtures/classes.rb
@@ -27,6 +27,7 @@ module MethodSpecs
alias bar foo
alias baz bar
+ alias qux baz
def same_as_foo
true
diff --git a/spec/ruby/core/method/original_name_spec.rb b/spec/ruby/core/method/original_name_spec.rb
index 676fdaedb4..8fec0e7c33 100644
--- a/spec/ruby/core/method/original_name_spec.rb
+++ b/spec/ruby/core/method/original_name_spec.rb
@@ -19,4 +19,25 @@ describe "Method#original_name" do
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 05422f1697..1cdc4edfa7 100644
--- a/spec/ruby/core/method/owner_spec.rb
+++ b/spec/ruby/core/method/owner_spec.rb
@@ -24,9 +24,7 @@ describe "Method#owner" do
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
+ 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 0178a61de6..fd88e8dcb8 100644
--- a/spec/ruby/core/method/parameters_spec.rb
+++ b/spec/ruby/core/method/parameters_spec.rb
@@ -22,6 +22,12 @@ describe "Method#parameters" do
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
@@ -187,6 +193,13 @@ describe "Method#parameters" do
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]]
@@ -233,28 +246,14 @@ describe "Method#parameters" do
m.method(:handled_via_method_missing).parameters.should == [[:rest]]
end
- ruby_version_is '3.2' 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 rest arg with name * for \"star\" argument" do
+ m = MethodSpecs::Methods.new
+ m.method(:one_unnamed_splat).parameters.should == [[:rest, :*]]
end
- ruby_version_is ''...'3.2' do
- it "adds nameless rest arg for \"star\" argument" 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
+ 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
@@ -309,7 +308,7 @@ describe "Method#parameters" do
[
[[:rest]],
[[:opt]]
- ].should include([].method(:pop).parameters)
+ ].should.include?([].method(:pop).parameters)
end
it "returns [[:req]] for each parameter for core methods with fixed-length argument lists" do
diff --git a/spec/ruby/core/method/private_spec.rb b/spec/ruby/core/method/private_spec.rb
index 9b67a77243..e708542b2e 100644
--- a/spec/ruby/core/method/private_spec.rb
+++ b/spec/ruby/core/method/private_spec.rb
@@ -2,27 +2,8 @@ require_relative '../../spec_helper'
require_relative 'fixtures/classes'
describe "Method#private?" do
- ruby_version_is ""..."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
+ 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
index 28c60c7536..f9e422ae3d 100644
--- a/spec/ruby/core/method/protected_spec.rb
+++ b/spec/ruby/core/method/protected_spec.rb
@@ -2,27 +2,8 @@ require_relative '../../spec_helper'
require_relative 'fixtures/classes'
describe "Method#protected?" do
- ruby_version_is ""..."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
+ 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
index 4844f4b90b..4cb23f4cf1 100644
--- a/spec/ruby/core/method/public_spec.rb
+++ b/spec/ruby/core/method/public_spec.rb
@@ -2,27 +2,8 @@ require_relative '../../spec_helper'
require_relative 'fixtures/classes'
describe "Method#public?" do
- ruby_version_is ""..."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
+ 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 f26e373695..41ee2b06cb 100644
--- a/spec/ruby/core/method/shared/call.rb
+++ b/spec/ruby/core/method/shared/call.rb
@@ -11,10 +11,10 @@ describe :method_call, shared: true do
it "raises an ArgumentError when given incorrect number of arguments" do
-> {
MethodSpecs::Methods.new.method(:two_req).send(@method, 1, 2, 3)
- }.should raise_error(ArgumentError)
+ }.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
index c74847083f..eee790890a 100644
--- a/spec/ruby/core/method/shared/dup.rb
+++ b/spec/ruby/core/method/shared/dup.rb
@@ -4,7 +4,7 @@ describe :method_dup, shared: true do
b = a.send(@method)
a.should == b
- a.should_not equal(b)
+ a.should_not.equal?(b)
end
ruby_version_is "3.4" do
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 b2d27d370f..bfb58e6896 100644
--- a/spec/ruby/core/method/shared/to_s.rb
+++ b/spec/ruby/core/method/shared/to_s.rb
@@ -8,12 +8,12 @@ describe :method_to_s, shared: true do
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
diff --git a/spec/ruby/core/method/source_location_spec.rb b/spec/ruby/core/method/source_location_spec.rb
index 23d956ebec..22fcb98c74 100644
--- a/spec/ruby/core/method/source_location_spec.rb
+++ b/spec/ruby/core/method/source_location_spec.rb
@@ -7,27 +7,27 @@ 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[0]
- file.should be_an_instance_of(String)
+ file = @method.source_location.first
+ file.should.instance_of?(String)
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
- line = @method.source_location[1]
- line.should be_an_instance_of(Integer)
+ line = @method.source_location.last
+ line.should.instance_of?(Integer)
line.should == 5
end
it "returns the last place the method was defined" do
- MethodSpecs::SourceLocation.method(:redefined).source_location[1].should == 13
+ MethodSpecs::SourceLocation.method(:redefined).source_location.last.should == 13
end
it "returns the location of the original method even if it was aliased" do
- MethodSpecs::SourceLocation.new.method(:aka).source_location[1].should == 17
+ MethodSpecs::SourceLocation.new.method(:aka).source_location.last.should == 17
end
it "works for methods defined with a block" do
@@ -92,7 +92,7 @@ describe "Method#source_location" do
loc.should == nil
else
loc[0].should.start_with?('<internal:')
- loc[1].should be_kind_of(Integer)
+ loc[1].should.is_a?(Integer)
end
loc = method(:tap).source_location
@@ -100,7 +100,7 @@ describe "Method#source_location" do
loc.should == nil
else
loc[0].should.start_with?('<internal:')
- loc[1].should be_kind_of(Integer)
+ loc[1].should.is_a?(Integer)
end
end
@@ -108,19 +108,13 @@ describe "Method#source_location" do
c = Class.new do
eval('def self.m; end', nil, "foo", 100)
end
- location = c.method(:m).source_location
- ruby_version_is(""..."3.5") do
- location.should == ["foo", 100]
- end
- ruby_version_is("3.5") do
- location.should == ["foo", 100, 0, 100, 15]
- 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/unbind_spec.rb b/spec/ruby/core/method/unbind_spec.rb
index bdedd513ce..994c3a8879 100644
--- a/spec/ruby/core/method/unbind_spec.rb
+++ b/spec/ruby/core/method/unbind_spec.rb
@@ -11,7 +11,7 @@ describe "Method#unbind" do
end
it "returns an UnboundMethod" do
- @normal_um.should be_kind_of(UnboundMethod)
+ @normal_um.should.is_a?(UnboundMethod)
end
describe "#inspect" do
@@ -27,16 +27,8 @@ describe "Method#unbind" do
@string.should =~ /MethodSpecs::MyMod/
end
- 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
+ it "returns a String containing the Module the method is referenced from" do
+ @string.should =~ /MethodSpecs::MyMod/
end
end