Skip to content

Commit 1b95566

Browse files
committed
Move Method to rely less on shared examples
1 parent f25d4b4 commit 1b95566

8 files changed

Lines changed: 151 additions & 161 deletions

File tree

core/method/call_spec.rb

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,54 @@
11
require_relative '../../spec_helper'
22
require_relative 'fixtures/classes'
3-
require_relative 'shared/call'
43

54
describe "Method#call" do
6-
it_behaves_like :method_call, :call
5+
it "invokes the method with the specified arguments, returning the method's return value" do
6+
m = 12.method("+")
7+
m.call(3).should == 15
8+
m.call(20).should == 32
9+
10+
m = MethodSpecs::Methods.new.method(:attr=)
11+
m.call(42).should == 42
12+
end
13+
14+
it "raises an ArgumentError when given incorrect number of arguments" do
15+
-> {
16+
MethodSpecs::Methods.new.method(:two_req).call(1, 2, 3)
17+
}.should.raise(ArgumentError)
18+
-> {
19+
MethodSpecs::Methods.new.method(:two_req).call(1)
20+
}.should.raise(ArgumentError)
21+
end
22+
23+
describe "for a Method generated by respond_to_missing?" do
24+
it "invokes method_missing with the specified arguments and returns the result" do
25+
@m = MethodSpecs::Methods.new
26+
meth = @m.method(:handled_via_method_missing)
27+
meth.call(:argument).should == [:argument]
28+
end
29+
30+
it "invokes method_missing with the method name and the specified arguments" do
31+
@m = MethodSpecs::Methods.new
32+
meth = @m.method(:handled_via_method_missing)
33+
34+
@m.should_receive(:method_missing).with(:handled_via_method_missing, :argument)
35+
meth.call(:argument)
36+
end
37+
38+
it "invokes method_missing dynamically" do
39+
@m = MethodSpecs::Methods.new
40+
meth = @m.method(:handled_via_method_missing)
41+
42+
def @m.method_missing(*); :changed; end
43+
meth.call(:argument).should == :changed
44+
end
45+
46+
it "does not call the original method name even if it now exists" do
47+
@m = MethodSpecs::Methods.new
48+
meth = @m.method(:handled_via_method_missing)
49+
50+
def @m.handled_via_method_missing(*); :not_called; end
51+
meth.call(:argument).should == [:argument]
52+
end
53+
end
754
end

core/method/case_compare_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require_relative '../../spec_helper'
2-
require_relative 'fixtures/classes'
3-
require_relative 'shared/call'
42

53
describe "Method#===" do
6-
it_behaves_like :method_call, :===
4+
it "is an alias of Method#call" do
5+
Method.instance_method(:===).should == Method.instance_method(:call)
6+
end
77
end
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require_relative '../../spec_helper'
2-
require_relative 'fixtures/classes'
3-
require_relative 'shared/call'
42

53
describe "Method#[]" do
6-
it_behaves_like :method_call, :[]
4+
it "is an alias of Method#call" do
5+
Method.instance_method(:[]).should == Method.instance_method(:call)
6+
end
77
end

core/method/eql_spec.rb

Lines changed: 90 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,94 @@
11
require_relative '../../spec_helper'
2-
require_relative 'shared/eql'
2+
require_relative 'fixtures/classes'
33

44
describe "Method#eql?" do
5-
it_behaves_like :method_equal, :eql?
5+
before :each do
6+
@m = MethodSpecs::Methods.new
7+
@m_foo = @m.method(:foo)
8+
@m2 = MethodSpecs::Methods.new
9+
@a = MethodSpecs::A.new
10+
end
11+
12+
it "returns true if methods are the same" do
13+
m2 = @m.method(:foo)
14+
15+
@m_foo.eql?(@m_foo).should == true
16+
@m_foo.eql?(m2).should == true
17+
end
18+
19+
it "returns true on aliased methods" do
20+
m_bar = @m.method(:bar)
21+
22+
m_bar.eql?(@m_foo).should == true
23+
end
24+
25+
it "returns true if the two core methods are aliases" do
26+
s = "hello"
27+
a = s.method(:size)
28+
b = s.method(:length)
29+
a.eql?(b).should == true
30+
end
31+
32+
it "returns false on a method which is neither aliased nor the same method" do
33+
m2 = @m.method(:zero)
34+
35+
@m_foo.eql?(m2).should == false
36+
end
37+
38+
it "returns false for a method which is not bound to the same object" do
39+
m2_foo = @m2.method(:foo)
40+
a_baz = @a.method(:baz)
41+
42+
@m_foo.eql?(m2_foo).should == false
43+
@m_foo.eql?(a_baz).should == false
44+
end
45+
46+
it "returns false if the two methods are bound to the same object but were defined independently" do
47+
m2 = @m.method(:same_as_foo)
48+
@m_foo.eql?(m2).should == false
49+
end
50+
51+
it "returns true if a method was defined using the other one" do
52+
MethodSpecs::Methods.send :define_method, :defined_foo, MethodSpecs::Methods.instance_method(:foo)
53+
m2 = @m.method(:defined_foo)
54+
@m_foo.eql?(m2).should == true
55+
end
56+
57+
it "returns false if comparing a method defined via define_method and def" do
58+
defn = @m.method(:zero)
59+
defined = @m.method(:zero_defined_method)
60+
61+
defn.eql?(defined).should == false
62+
defined.eql?(defn).should == false
63+
end
64+
65+
describe 'missing methods' do
66+
it "returns true for the same method missing" do
67+
miss1 = @m.method(:handled_via_method_missing)
68+
miss1bis = @m.method(:handled_via_method_missing)
69+
miss2 = @m.method(:also_handled)
70+
71+
miss1.eql?(miss1bis).should == true
72+
miss1.eql?(miss2).should == false
73+
end
74+
75+
it 'calls respond_to_missing? with true to include private methods' do
76+
@m.should_receive(:respond_to_missing?).with(:some_missing_method, true).and_return(true)
77+
@m.method(:some_missing_method)
78+
end
79+
end
80+
81+
it "returns false if the two methods are bound to different objects, have the same names, and identical bodies" do
82+
a = MethodSpecs::Eql.instance_method(:same_body)
83+
b = MethodSpecs::Eql2.instance_method(:same_body)
84+
a.eql?(b).should == false
85+
end
86+
87+
it "returns false if the argument is not a Method object" do
88+
String.instance_method(:size).eql?(7).should == false
89+
end
90+
91+
it "returns false if the argument is an unbound version of self" do
92+
method(:load).eql?(method(:load).unbind).should == false
93+
end
694
end

core/method/equal_value_spec.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require_relative '../../spec_helper'
2-
require_relative 'shared/eql'
32

43
describe "Method#==" do
5-
it_behaves_like :method_equal, :==
4+
it "is an alias of Method#eql?" do
5+
Method.instance_method(:==).should == Method.instance_method(:eql?)
6+
end
67
end

core/method/shared/call.rb

Lines changed: 0 additions & 51 deletions
This file was deleted.

core/method/shared/eql.rb

Lines changed: 0 additions & 94 deletions
This file was deleted.

core/method/to_s_spec.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
require_relative '../../spec_helper'
2-
require_relative 'shared/to_s'
3-
require_relative 'shared/aliased_inspect'
42

53
describe "Method#to_s" do
6-
it_behaves_like :method_to_s, :to_s
7-
it_behaves_like :method_to_s_aliased, :to_s, -> meth { meth }
4+
it "is an alias of Method#inspect" do
5+
Method.instance_method(:to_s).should == Method.instance_method(:inspect)
6+
end
87
end

0 commit comments

Comments
 (0)