1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Move spec/rubyspec to spec/ruby for consistency

* Other ruby implementations use the spec/ruby directory.
  [Misc #13792] [ruby-core:82287]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59979 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
eregon 2017-09-20 20:18:52 +00:00
parent 75bfc6440d
commit 1d15d5f080
4370 changed files with 0 additions and 0 deletions

View file

@ -0,0 +1,222 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)
describe "Method#arity" do
SpecEvaluate.desc = "for method definition"
context "returns zero" do
evaluate <<-ruby do
def m() end
ruby
method(:m).arity.should == 0
end
evaluate <<-ruby do
def n(&b) end
ruby
method(:n).arity.should == 0
end
end
context "returns positive values" do
evaluate <<-ruby do
def m(a) end
def n(a, b) end
def o(a, b, c) end
def p(a, b, c, d) end
ruby
method(:m).arity.should == 1
method(:n).arity.should == 2
method(:o).arity.should == 3
method(:p).arity.should == 4
end
evaluate <<-ruby do
def m(a:) end
def n(a:, b:) end
def o(a: 1, b:, c:, d: 2) end
ruby
method(:m).arity.should == 1
method(:n).arity.should == 1
method(:o).arity.should == 1
end
evaluate <<-ruby do
def m(a, b:) end
def n(a, b:, &l) end
ruby
method(:m).arity.should == 2
method(:n).arity.should == 2
end
evaluate <<-ruby do
def m(a, b, c:, d: 1) end
def n(a, b, c:, d: 1, **k, &l) end
ruby
method(:m).arity.should == 3
method(:n).arity.should == 3
end
end
context "returns negative values" do
evaluate <<-ruby do
def m(a=1) end
def n(a=1, b=2) end
ruby
method(:m).arity.should == -1
method(:n).arity.should == -1
end
evaluate <<-ruby do
def m(a, b=1) end
def n(a, b, c=1, d=2) end
ruby
method(:m).arity.should == -2
method(:n).arity.should == -3
end
evaluate <<-ruby do
def m(a=1, *b) end
def n(a=1, b=2, *c) end
ruby
method(:m).arity.should == -1
method(:n).arity.should == -1
end
evaluate <<-ruby do
def m(*) end
def n(*a) end
ruby
method(:m).arity.should == -1
method(:n).arity.should == -1
end
evaluate <<-ruby do
def m(a, *) end
def n(a, *b) end
def o(a, b, *c) end
def p(a, b, c, *d) end
ruby
method(:m).arity.should == -2
method(:n).arity.should == -2
method(:o).arity.should == -3
method(:p).arity.should == -4
end
evaluate <<-ruby do
def m(*a, b) end
def n(*a, b, c) end
def o(*a, b, c, d) end
ruby
method(:m).arity.should == -2
method(:n).arity.should == -3
method(:o).arity.should == -4
end
evaluate <<-ruby do
def m(a, *b, c) end
def n(a, b, *c, d, e) end
ruby
method(:m).arity.should == -3
method(:n).arity.should == -5
end
evaluate <<-ruby do
def m(a, b=1, c=2, *d, e, f) end
def n(a, b, c=1, *d, e, f, g) end
ruby
method(:m).arity.should == -4
method(:n).arity.should == -6
end
evaluate <<-ruby do
def m(a: 1) end
def n(a: 1, b: 2) end
ruby
method(:m).arity.should == -1
method(:n).arity.should == -1
end
evaluate <<-ruby do
def m(a=1, b: 2) end
def n(*a, b: 1) end
def o(a=1, b: 2) end
def p(a=1, *b, c: 2, &l) end
ruby
method(:m).arity.should == -1
method(:n).arity.should == -1
method(:o).arity.should == -1
method(:p).arity.should == -1
end
evaluate <<-ruby do
def m(**k, &l) end
def n(*a, **k) end
def o(a: 1, b: 2, **k) end
ruby
method(:m).arity.should == -1
method(:n).arity.should == -1
method(:o).arity.should == -1
end
evaluate <<-ruby do
def m(a=1, *b, c:, d: 2, **k, &l) end
ruby
method(:m).arity.should == -2
end
evaluate <<-ruby do
def m(a, b=1, *c, d, e:, f: 2, **k, &l) end
def n(a, b=1, *c, d:, e:, f: 2, **k, &l) end
def o(a=0, b=1, *c, d, e:, f: 2, **k, &l) end
def p(a=0, b=1, *c, d:, e:, f: 2, **k, &l) end
ruby
method(:m).arity.should == -4
method(:n).arity.should == -3
method(:o).arity.should == -3
method(:p).arity.should == -2
end
end
context "for a Method generated by respond_to_missing?" do
it "returns -1" do
obj = mock("method arity respond_to_missing")
obj.should_receive(:respond_to_missing?).and_return(true)
obj.method(:m).arity.should == -1
end
end
context "for a Method generated by attr_reader" do
it "return 0" do
obj = MethodSpecs::Methods.new
obj.method(:reader).arity.should == 0
end
end
context "for a Method generated by attr_writer" do
it "returns 1" do
obj = MethodSpecs::Methods.new
obj.method(:writer=).arity.should == 1
end
end
end

View file

@ -0,0 +1,7 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)
require File.expand_path('../shared/call', __FILE__)
describe "Method#call" do
it_behaves_like(:method_call, :call)
end

View file

@ -0,0 +1,14 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)
describe "Method#clone" do
it "returns a copy of the method" do
m1 = MethodSpecs::Methods.new.method(:foo)
m2 = m1.clone
m1.should == m2
m1.should_not equal(m2)
m1.call.should == m2.call
end
end

View file

@ -0,0 +1,36 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)
describe "Method#curry" do
it "returns a curried proc" do
x = Object.new
def x.foo(a,b,c); [a,b,c]; end
c = x.method(:foo).curry
c.should be_kind_of(Proc)
c.call(1).call(2, 3).should == [1,2,3]
end
describe "with optional arity argument" do
before(:each) do
@obj = MethodSpecs::Methods.new
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)
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)
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)
end
end
end

View file

@ -0,0 +1,7 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)
require File.expand_path('../shared/call', __FILE__)
describe "Method#[]" do
it_behaves_like(:method_call, :[])
end

View file

@ -0,0 +1,6 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/eql', __FILE__)
describe "Method#eql?" do
it_behaves_like(:method_equal, :eql?)
end

View file

@ -0,0 +1,6 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/eql', __FILE__)
describe "Method#==" do
it_behaves_like(:method_equal, :==)
end

View file

@ -0,0 +1,184 @@
module MethodSpecs
class SourceLocation
def self.location # This needs to be on this line
:location # for the spec to pass
end
def self.redefined
:first
end
def self.redefined
:last
end
def original
end
alias :aka :original
end
class Methods
def foo
true
end
alias bar foo
def same_as_foo
true
end
def respond_to_missing? method, bool
[:handled_via_method_missing, :also_handled].include? method
end
def method_missing(method, *arguments)
if [:handled_via_method_missing, :also_handled].include? method
arguments
else
super
end
end
attr_accessor :attr
def zero; end
def one_req(a); end
def two_req(a, b); end
def zero_with_block(&blk); end
def one_req_with_block(a, &blk); end
def two_req_with_block(a, b, &blk); end
def one_opt(a=nil); end
def one_req_one_opt(a, b=nil); end
def one_req_two_opt(a, b=nil, c=nil); end
def two_req_one_opt(a, b, c=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
def two_req_one_opt_with_block(a, b, c=nil, &blk); end
def zero_with_splat(*a); end
def one_req_with_splat(a, *b); end
def two_req_with_splat(a, b, *c); end
def one_req_one_opt_with_splat(a, b=nil, *c); end
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_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
def one_req_one_opt_with_splat_and_block(a, b=nil, *c, &blk); end
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
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|})
define_method(:two_req_defined_method, Proc.new {|x, y|})
define_method(:no_args_defined_method) {}
define_method(:two_grouped_defined_method) {|(_x1,_x2)|}
attr_reader :reader
attr_writer :writer
end
module MyMod
def bar; :bar; end
end
class MySuper
include MyMod
end
class MySub < MySuper; end
class A
def baz(a, b)
self.class
end
def overridden; end
end
class B < A
def overridden; end
end
module BetweenBAndC
def overridden; end
end
class C < B
include BetweenBAndC
def overridden; end
end
module OverrideAgain
def overridden; end
end
class D
def bar() 'done' end
end
class Eql
def same_body
1 + 1
end
alias :same_body_alias :same_body
def same_body_with_args(arg)
1 + 1
end
def different_body
1 + 2
end
def same_body_two
1 + 1
end
private
def same_body_private
1 + 1
end
end
class Eql2
def same_body
1 + 1
end
end
class ToProc
def method_called(a, b)
ScratchPad << [a, b]
end
def to_proc
method(:method_called).to_proc
end
end
class ToProcBeta
def method_called(a)
ScratchPad << a
a
end
def to_proc
method(:method_called).to_proc
end
end
end

View file

@ -0,0 +1,17 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)
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
end
# See also redmine #6048
it "returns the same value for builtin methods that are eql?" do
obj = [42]
obj.method(:to_s).hash.should == obj.method(:inspect).hash
end
end

View file

@ -0,0 +1,6 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/to_s', __FILE__)
describe "Method#inspect" do
it_behaves_like(:method_to_s, :inspect)
end

View file

@ -0,0 +1,22 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)
describe "Method#name" do
it "returns the name of the method" do
"abc".method(:upcase).name.should == :upcase
end
it "returns the name even when aliased" do
obj = MethodSpecs::Methods.new
obj.method(:foo).name.should == :foo
obj.method(:bar).name.should == :bar
obj.method(:bar).unbind.bind(obj).name.should == :bar
end
describe "for a Method generated by respond_to_missing?" do
it "returns the name passed to respond_to_missing?" do
@m = MethodSpecs::Methods.new
@m.method(:handled_via_method_missing).name.should == :handled_via_method_missing
end
end
end

View file

@ -0,0 +1,26 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)
describe "Method#owner" do
it "returns the owner of the method" do
"abc".method(:upcase).owner.should == String
end
it "returns the same owner when aliased in the same classes" do
obj = MethodSpecs::Methods.new
obj.method(:foo).owner.should == MethodSpecs::Methods
obj.method(:bar).owner.should == MethodSpecs::Methods
end
it "returns the class/module it was defined in" do
MethodSpecs::C.new.method(:baz).owner.should == MethodSpecs::A
MethodSpecs::MySuper.new.method(:bar).owner.should == MethodSpecs::MyMod
end
describe "for a Method generated by respond_to_missing?" do
it "returns the owner of the method" do
@m = MethodSpecs::Methods.new
@m.method(:handled_via_method_missing).owner.should == MethodSpecs::Methods
end
end
end

View file

@ -0,0 +1,244 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)
describe "Method#parameters" do
class MethodSpecs::Methods
def one_key(a: 1); end
def one_keyrest(**a); end
def one_keyreq(a:); 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_unnamed_splat(*); end
def one_splat_one_block(*args, &block)
local_is_not_parameter = {}
end
define_method(:one_optional_defined_method) {|x = 1|}
end
it "returns an empty Array when the method expects no arguments" do
MethodSpecs::Methods.instance_method(:zero).parameters.should == []
end
it "returns [[:req,:name]] for a method expecting one required argument called 'name'" do
MethodSpecs::Methods.instance_method(:one_req).parameters.should == [[:req,:a]]
end
it "returns [[:req,:a],[:req,:b]] for a method expecting two required arguments called 'a' and 'b''" do
m = MethodSpecs::Methods.instance_method(:two_req)
m.parameters.should == [[:req,:a], [:req,:b]]
end
it "returns [[:block,:blk]] for a method expecting one block argument called 'a'" do
m = MethodSpecs::Methods.instance_method(:zero_with_block)
m.parameters.should == [[:block,:blk]]
end
it "returns [[:req,:a],[:block,:b] for a method expecting a required argument ('a') and a block argument ('b')" do
m = MethodSpecs::Methods.instance_method(:one_req_with_block)
m.parameters.should == [[:req,:a], [:block,:blk]]
end
it "returns [[:req,:a],[:req,:b],[:block,:c] for a method expecting two required arguments ('a','b') and a block argument ('c')" do
m = MethodSpecs::Methods.instance_method(:two_req_with_block)
m.parameters.should == [[:req,:a], [:req,:b], [:block,:blk]]
end
it "returns [[:opt,:a]] for a method expecting one optional argument ('a')" do
m = MethodSpecs::Methods.instance_method(:one_opt)
m.parameters.should == [[:opt,:a]]
end
it "returns [[:req,:a],[:opt,:b]] for a method expecting one required argument ('a') and one optional argument ('b')" do
m = MethodSpecs::Methods.instance_method(:one_req_one_opt)
m.parameters.should == [[:req,:a],[:opt,:b]]
end
it "returns [[:req,:a],[:opt,:b]] for a method expecting one required argument ('a') and one optional argument ('b')" do
m = MethodSpecs::Methods.instance_method(:one_req_one_opt)
m.parameters.should == [[:req,:a],[:opt,:b]]
end
it "returns [[:req,:a],[:opt,:b],[:opt,:c]] for a method expecting one required argument ('a') and two optional arguments ('b','c')" do
m = MethodSpecs::Methods.instance_method(:one_req_two_opt)
m.parameters.should == [[:req,:a],[:opt,:b],[:opt,:c]]
end
it "returns [[:req,:a],[:req,:b],[:opt,:c]] for a method expecting two required arguments ('a','b') and one optional arguments ('c')" do
m = MethodSpecs::Methods.instance_method(:two_req_one_opt)
m.parameters.should == [[:req,:a],[:req,:b],[:opt,:c]]
end
it "returns [[:opt,:a],[:block,:b]] for a method expecting one required argument ('a') and one block argument ('b')" do
m = MethodSpecs::Methods.instance_method(:one_opt_with_block)
m.parameters.should == [[:opt,:a],[:block,:blk]]
end
it "returns [[:req,:a],[:opt,:b],[:block,:c]] for a method expecting one required argument ('a'), one optional argument ('b'), and a block ('c')" do
m = MethodSpecs::Methods.instance_method(:one_req_one_opt_with_block)
m.parameters.should == [[:req,:a],[:opt,:b],[:block,:blk]]
end
it "returns [[:req,:a],[:opt,:b],[:opt,:c],[:block,:d]] for a method expecting one required argument ('a'), two optional arguments ('b','c'), and a block ('d')" do
m = MethodSpecs::Methods.instance_method(:one_req_two_opt_with_block)
m.parameters.should == [[:req,:a],[:opt,:b],[:opt,:c],[:block,:blk]]
end
it "returns [[:rest,:a]] for a method expecting a single splat argument ('a')" do
m = MethodSpecs::Methods.instance_method(:zero_with_splat)
m.parameters.should == [[:rest,:a]]
end
it "returns [[:req,:a],[:rest,:b]] for a method expecting a splat argument ('a') and a required argument ('b')" do
m = MethodSpecs::Methods.instance_method(:one_req_with_splat)
m.parameters.should == [[:req,:a],[:rest,:b]]
end
it "returns [[:req,:a],[:req,:b],[:rest,:c]] for a method expecting two required arguments ('a','b') and a splat argument ('c')" do
m = MethodSpecs::Methods.instance_method(:two_req_with_splat)
m.parameters.should == [[:req,:a],[:req,:b],[:rest,:c]]
end
it "returns [[:req,:a],[:opt,:b],[:rest,:c]] for a method expecting a required argument ('a','b'), an optional argument ('b'), and a splat argument ('c')" do
m = MethodSpecs::Methods.instance_method(:one_req_one_opt_with_splat)
m.parameters.should == [[:req,:a],[:opt,:b],[:rest,:c]]
end
it "returns [[:req,:a],[:req,:b],[:opt,:b],[:rest,:d]] for a method expecting two required arguments ('a','b'), an optional argument ('c'), and a splat argument ('d')" do
m = MethodSpecs::Methods.instance_method(:two_req_one_opt_with_splat)
m.parameters.should == [[:req,:a],[:req,:b],[:opt,:c],[:rest,:d]]
end
it "returns [[:req,:a],[:opt,:b],[:opt,:c],[:rest,:d]] for a method expecting a required argument ('a'), two optional arguments ('b','c'), and a splat argument ('d')" do
m = MethodSpecs::Methods.instance_method(:one_req_two_opt_with_splat)
m.parameters.should == [[:req,:a],[:opt,:b],[:opt,:c],[:rest,:d]]
end
it "returns [[:rest,:a],[:block,:b]] for a method expecting a splat argument ('a') and a block argument ('b')" do
m = MethodSpecs::Methods.instance_method(:zero_with_splat_and_block)
m.parameters.should == [[:rest,:a],[:block,:blk]]
end
it "returns [[:req,:a],[:rest,:b],[:block,:c]] for a method expecting a required argument ('a'), a splat argument ('b'), and a block ('c')" do
m = MethodSpecs::Methods.instance_method(:one_req_with_splat_and_block)
m.parameters.should == [[:req,:a],[:rest,:b],[:block,:blk]]
end
it "returns [[:req,:a],[:req,:b],[:rest,:c],[:block,:d]] for a method expecting two required arguments ('a','b'), a splat argument ('c'), and a block ('d')" do
m = MethodSpecs::Methods.instance_method(:two_req_with_splat_and_block)
m.parameters.should == [[:req,:a],[:req,:b],[:rest,:c],[:block,:blk]]
end
it "returns [[:req,:a],[:opt,:b],[:rest,:c],[:block,:d]] for a method expecting a required argument ('a'), a splat argument ('c'), and a block ('d')" do
m = MethodSpecs::Methods.instance_method(:one_req_one_opt_with_splat_and_block)
m.parameters.should == [[:req,:a],[:opt,:b],[:rest,:c],[:block,:blk]]
end
it "returns [[:req,:a],[:req,:b],[:opt,:c],[:block,:d]] for a method expecting two required arguments ('a','b'), an optional argument ('c'), a splat argument ('d'), and a block ('e')" do
m = MethodSpecs::Methods.instance_method(:two_req_one_opt_with_splat_and_block)
m.parameters.should == [[:req,:a],[:req,:b],[:opt,:c],[:rest,:d],[:block,:blk]]
end
it "returns [[:rest,:a],[:req,:b]] for a method expecting a splat argument ('a') and a required argument ('b')" do
m = MethodSpecs::Methods.instance_method(:one_splat_one_req)
m.parameters.should == [[:rest,:a],[:req,:b]]
end
it "returns [[:rest,:a],[:req,:b],[:req,:c]] for a method expecting a splat argument ('a') and two required arguments ('b','c')" do
m = MethodSpecs::Methods.instance_method(:one_splat_two_req)
m.parameters.should == [[:rest,:a],[:req,:b],[:req,:c]]
end
it "returns [[:rest,:a],[:req,:b],[:block,:c]] for a method expecting a splat argument ('a'), a required argument ('b'), and a block ('c')" do
m = MethodSpecs::Methods.instance_method(:one_splat_one_req_with_block)
m.parameters.should == [[:rest,:a],[:req,:b],[:block,:blk]]
end
it "returns [[:key,:a]] for a method with a single optional keyword argument" do
m = MethodSpecs::Methods.instance_method(:one_key)
m.parameters.should == [[:key,:a]]
end
it "returns [[:keyrest,:a]] for a method with a keyword rest argument" do
m = MethodSpecs::Methods.instance_method(:one_keyrest)
m.parameters.should == [[:keyrest,:a]]
end
it "returns [[:keyreq,:a]] for a method with a single required keyword argument" do
m = MethodSpecs::Methods.instance_method(:one_keyreq)
m.parameters.should == [[:keyreq,:a]]
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]]
end
# define_method variants
it "returns [] for a define_method method with explicit no-args || specification" do
m = MethodSpecs::Methods.instance_method(:zero_defined_method)
m.parameters.should == []
end
it "returns [[:rest, :x]] for a define_method method with rest arg 'x' only" do
m = MethodSpecs::Methods.instance_method(:zero_with_splat_defined_method)
m.parameters.should == [[:rest, :x]]
end
it "returns [[:req, :x]] for a define_method method expecting one required argument 'x'" do
m = MethodSpecs::Methods.instance_method(:one_req_defined_method)
m.parameters.should == [[:req, :x]]
end
it "returns [[:req, :x], [:req, :y]] for a define_method method expecting two required arguments 'x' and 'y'" do
m = MethodSpecs::Methods.instance_method(:two_req_defined_method)
m.parameters.should == [[:req, :x], [:req, :y]]
end
it "returns [] for a define_method method with no args specification" do
m = MethodSpecs::Methods.instance_method(:no_args_defined_method)
m.parameters.should == []
end
it "returns [[:req]] for a define_method method with a grouping as its only argument" do
m = MethodSpecs::Methods.instance_method(:two_grouped_defined_method)
m.parameters.should == [[:req]]
end
it "returns [[:opt, :x]] for a define_method method with an optional argument 'x'" do
m = MethodSpecs::Methods.instance_method(:one_optional_defined_method)
m.parameters.should == [[:opt, :x]]
end
it "returns [[:rest]] for a Method generated by respond_to_missing?" do
m = MethodSpecs::Methods.new
m.method(:handled_via_method_missing).parameters.should == [[:rest]]
end
it "adds nameless rest arg for \"star\" argument" do
m = MethodSpecs::Methods.new
m.method(:one_unnamed_splat).parameters.should == [[:rest]]
end
it "returns the args and block for a splat and block argument" do
m = MethodSpecs::Methods.new
m.method(:one_splat_one_block).parameters.should == [[:rest, :args], [:block, :block]]
end
it "returns [] for a Method generated by attr_reader" do
m = MethodSpecs::Methods.new
m.method(:reader).parameters.should == []
end
it "return [[:req]] for a Method generated by attr_writer" do
m = MethodSpecs::Methods.new
m.method(:writer=).parameters.should == [[:req]]
end
end

View file

@ -0,0 +1,22 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)
describe "Method#receiver" do
it "returns the receiver of the method" do
s = "abc"
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)
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)
end
end
end

View file

@ -0,0 +1,51 @@
describe :method_call, shared: true do
it "invokes the method with the specified arguments, returning the method's return value" do
m = 12.method("+")
m.send(@method, 3).should == 15
m.send(@method, 20).should == 32
m = MethodSpecs::Methods.new.method(:attr=)
m.send(@method, 42).should == 42
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
describe "for a Method generated by respond_to_missing?" do
it "invokes method_missing with the specified arguments and returns the result" do
@m = MethodSpecs::Methods.new
meth = @m.method(:handled_via_method_missing)
meth.send(@method, :argument).should == [:argument]
end
it "invokes method_missing with the method name and the specified arguments" do
@m = MethodSpecs::Methods.new
meth = @m.method(:handled_via_method_missing)
@m.should_receive(:method_missing).with(:handled_via_method_missing, :argument)
meth.send(@method, :argument)
end
it "invokes method_missing dynamically" do
@m = MethodSpecs::Methods.new
meth = @m.method(:handled_via_method_missing)
def @m.method_missing(*); :changed; end
meth.send(@method, :argument).should == :changed
end
it "does not call the original method name even if it now exists" do
@m = MethodSpecs::Methods.new
meth = @m.method(:handled_via_method_missing)
def @m.handled_via_method_missing(*); :not_called; end
meth.send(@method, :argument).should == [:argument]
end
end
end

View file

@ -0,0 +1,94 @@
require File.expand_path('../../../../spec_helper', __FILE__)
require File.expand_path('../../fixtures/classes', __FILE__)
describe :method_equal, shared: true do
before :each do
@m = MethodSpecs::Methods.new
@m_foo = @m.method(:foo)
@m2 = MethodSpecs::Methods.new
@a = MethodSpecs::A.new
end
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
end
it "returns true on aliased methods" do
m_bar = @m.method(:bar)
m_bar.send(@method, @m_foo).should be_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
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
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
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
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
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
end
describe 'missing methods' do
it "returns true for the same method missing" do
miss1 = @m.method(:handled_via_method_missing)
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
end
it 'calls respond_to_missing? with true to include private methods' do
@m.should_receive(:respond_to_missing?).with(:some_missing_method, true).and_return(true)
@m.method(:some_missing_method)
end
end
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
end
it "returns false if the argument is not a Method object" do
String.instance_method(:size).send(@method, 7).should be_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
end
end

View file

@ -0,0 +1,34 @@
require "#{File.dirname __FILE__}/../../../spec_helper"
require "#{File.dirname __FILE__}/../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')
end
it "returns a String" do
@m.send(@method).should be_kind_of(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)
end
it "returns a String containing 'Method'" do
@string.should =~ /\bMethod\b/
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 referenced from" do
@string.should =~ /MethodSpecs::MySub/
end
end

View file

@ -0,0 +1,95 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)
describe "Method#source_location" do
before :each do
@method = MethodSpecs::SourceLocation.method(:location)
end
it "returns an Array" do
@method.source_location.should be_an_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.dirname(__FILE__) + '/fixtures/classes.rb'
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)
line.should == 5
end
it "returns the last place the method was defined" do
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.last.should == 17
end
it "works for methods defined with a block" do
line = nil
klass = Class.new do
line = __LINE__ + 1
define_method(:f) { }
end
method = klass.new.method(:f)
method.source_location[0].should =~ /#{__FILE__}/
method.source_location[1].should == line
end
it "works for methods defined with a Method" do
line = nil
klass = Class.new do
line = __LINE__ + 1
def f
end
define_method :g, new.method(:f)
end
method = klass.new.method(:g)
method.source_location[0].should =~ /#{__FILE__}/
method.source_location[1].should == line
end
it "works for methods defined with an UnboundMethod" do
line = nil
klass = Class.new do
line = __LINE__ + 1
def f
end
define_method :g, instance_method(:f)
end
method = klass.new.method(:g)
method.source_location[0].should =~ /#{__FILE__}/
method.source_location[1].should == line
end
it "works for methods whose visibility has been overridden in a subclass" do
line = nil
superclass = Class.new do
line = __LINE__ + 1
def f
end
end
subclass = Class.new(superclass) do
private :f
end
method = subclass.new.method(:f)
method.source_location[0].should =~ /#{__FILE__}/
method.source_location[1].should == line
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
end
end
end

View file

@ -0,0 +1,45 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)
describe "Method#super_method" do
it "returns the method that would be called by super in the method" do
obj = MethodSpecs::C.new
obj.extend MethodSpecs::OverrideAgain
meth = obj.method(:overridden)
s_meth = meth.super_method
s_meth.owner.should == MethodSpecs::C
s_meth.receiver.should == obj
s_meth.name.should == :overridden
ss_meth = meth.super_method.super_method
ss_meth.owner.should == MethodSpecs::BetweenBAndC
ss_meth.receiver.should == obj
ss_meth.name.should == :overridden
sss_meth = meth.super_method.super_method.super_method
sss_meth.owner.should == MethodSpecs::B
sss_meth.receiver.should == obj
sss_meth.name.should == :overridden
end
it "returns nil when there's no super method in the parent" do
method = Object.new.method(:method)
method.super_method.should == nil
end
it "returns nil when the parent's method is removed" do
klass = Class.new do
def overridden; end
end
sub = Class.new(klass) do
def overridden; end
end
object = sub.new
method = object.method(:overridden)
klass.class_eval { undef :overridden }
method.super_method.should == nil
end
end

View file

@ -0,0 +1,89 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)
describe "Method#to_proc" do
before :each do
ScratchPad.record []
@m = MethodSpecs::Methods.new
@meth = @m.method(:foo)
end
it "returns a Proc object corresponding to the method" do
@meth.to_proc.kind_of?(Proc).should == true
end
it "returns a Proc which does not depends on the value of self" do
3.instance_exec(4, &5.method(:+)).should == 9
end
it "returns a Proc object with the correct arity" do
# This may seem redundant but this bug has cropped up in jruby, mri and yarv.
# http://jira.codehaus.org/browse/JRUBY-124
[ :zero, :one_req, :two_req,
:zero_with_block, :one_req_with_block, :two_req_with_block,
:one_opt, :one_req_one_opt, :one_req_two_opt, :two_req_one_opt,
:one_opt_with_block, :one_req_one_opt_with_block, :one_req_two_opt_with_block, :two_req_one_opt_with_block,
:zero_with_splat, :one_req_with_splat, :two_req_with_splat,
:one_req_one_opt_with_splat, :one_req_two_opt_with_splat, :two_req_one_opt_with_splat,
:zero_with_splat_and_block, :one_req_with_splat_and_block, :two_req_with_splat_and_block,
:one_req_one_opt_with_splat_and_block, :one_req_two_opt_with_splat_and_block, :two_req_one_opt_with_splat_and_block
].each do |m|
@m.method(m).to_proc.arity.should == @m.method(m).arity
end
end
it "returns a proc that can be used by define_method" do
x = 'test'
to_s = class << x
define_method :foo, method(:to_s).to_proc
to_s
end
x.foo.should == to_s
end
it "returns a proc that can be yielded to" do
x = Object.new
def x.foo(*a); a; end
def x.bar; yield; end
def x.baz(*a); yield(*a); end
m = x.method :foo
x.bar(&m).should == []
x.baz(1,2,3,&m).should == [1,2,3]
end
# #5926
it "returns a proc that can receive a block" do
x = Object.new
def x.foo; yield 'bar'; end
m = x.method :foo
result = nil
m.to_proc.call {|val| result = val}
result.should == 'bar'
end
it "can be called directly and not unwrap arguments like a block" do
obj = MethodSpecs::ToProcBeta.new
obj.to_proc.call([1]).should == [1]
end
it "should correct handle arguments (unwrap)" do
obj = MethodSpecs::ToProcBeta.new
array = [[1]]
array.each(&obj)
ScratchPad.recorded.should == [[1]]
end
it "executes method with whole array (one argument)" do
obj = MethodSpecs::ToProcBeta.new
array = [[1, 2]]
array.each(&obj)
ScratchPad.recorded.should == [[1, 2]]
end
end

View file

@ -0,0 +1,6 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/to_s', __FILE__)
describe "Method#to_s" do
it_behaves_like(:method_to_s, :to_s)
end

View file

@ -0,0 +1,37 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)
describe "Method#unbind" do
before :each do
@normal = MethodSpecs::Methods.new
@normal_m = @normal.method :foo
@normal_um = @normal_m.unbind
@pop_um = MethodSpecs::MySub.new.method(:bar).unbind
@string = @pop_um.inspect.sub(/0x\w+/, '0xXXXXXX')
end
it "returns an UnboundMethod" do
@normal_um.should be_kind_of(UnboundMethod)
end
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 Module the method is defined in" do
@string.should =~ /MethodSpecs::MyMod/
end
it "returns a String containing the Module the method is referenced from" do
@string.should =~ /MethodSpecs::MySub/
end
specify "rebinding UnboundMethod to Method's obj produces exactly equivalent Methods" do
@normal_um.bind(@normal).should == @normal_m
@normal_m.should == @normal_um.bind(@normal)
end
end