method_info 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.6
1
+ 0.1.7
@@ -115,7 +115,10 @@ module MethodInfo
115
115
 
116
116
  # Returns the class or module where method is defined
117
117
  def method_owner(method_symbol)
118
- method = @object.method(method_symbol)
118
+ # Under normal circumstances just calling @object.method(method_symbol) would work,
119
+ # but this will go wrong if the object has redefined the method method.
120
+ method = Object.instance_method(:method).bind(@object).call(method_symbol)
121
+
119
122
  method.owner
120
123
  rescue NameError
121
124
  poor_mans_method_owner(method, method_symbol.to_s)
@@ -13,50 +13,60 @@ module MethodInfo
13
13
  end
14
14
 
15
15
  describe "method_owner" do
16
- it "gets the method from the object" do
17
- obj = Object.new
18
- obj.should_receive(:method).with(:to_s).and_return(mock('method', :owner => nil))
19
- ams = AncestorMethodStructure.new(obj,
16
+ it "gets the owner of the method and returns it" do
17
+ ams = AncestorMethodStructure.new(5,
20
18
  :ancestors_to_show => [],
21
19
  :ancestors_to_exclude => [])
22
- ams.send(:method_owner, :to_s)
20
+ ams.send(:method_owner, :ceil).should == Integer
23
21
  end
24
22
 
25
- describe "for ruby >= 1.8.7" do
26
- it "gets the owner of the method and returns it" do
27
- obj = mock('object')
28
- mock_method = mock('method')
29
- obj.stub!(:method).and_return(mock_method)
30
- mock_method.should_receive(:owner).and_return :foo
31
- ams = AncestorMethodStructure.new(obj,
32
- :ancestors_to_show => [],
33
- :ancestors_to_exclude => [])
34
- ams.send(:method_owner, :to_s).should == :foo
23
+ it "can still return the owner of the method if the object has redefined :method" do
24
+ class MethodRedefinedTestClass
25
+ def method
26
+ raise "I do not want to call this method"
27
+ end
28
+
29
+ def foo
30
+ :foo
31
+ end
35
32
  end
33
+ obj = MethodRedefinedTestClass.new
34
+
35
+ ams = AncestorMethodStructure.new(obj,
36
+ :ancestors_to_show => [],
37
+ :ancestors_to_exclude => [])
38
+ ams.send(:method_owner, :foo).should == MethodRedefinedTestClass
36
39
  end
37
40
 
38
41
  describe "for ruby 1.8.6" do
39
42
  it "should use the poor_mans_method_owner" do
40
- obj = mock('object')
41
43
  mock_method = mock('method', :to_s => 'mock_method_name')
42
- obj.stub!(:method).and_return(mock_method)
44
+ mock_method.stub!(:bind).and_return mock_method
45
+ mock_method.stub!(:call).and_return mock_method
46
+
43
47
  mock_method.stub!(:owner).and_raise NameError
44
- ams = AncestorMethodStructure.new(obj,
48
+
49
+ Object.stub!(:instance_method).with(:method).and_return mock_method
50
+ ams = AncestorMethodStructure.new(5,
45
51
  :ancestors_to_show => [],
46
52
  :ancestors_to_exclude => [])
47
- ams.should_receive(:poor_mans_method_owner).with(mock_method, "to_i")
48
- ams.send(:method_owner, :to_i)
53
+ ams.should_receive(:poor_mans_method_owner).with(mock_method, "dup")
54
+ ams.send(:method_owner, :dup)
49
55
  end
50
56
 
51
57
  it "raises an error if an error is raised that is not a NameError" do
52
- obj = mock('object')
53
58
  mock_method = mock('method', :to_s => 'mock_method_name')
54
- obj.stub!(:method).and_return(mock_method)
55
- mock_method.stub!(:owner).and_raise ArgumentError
56
- ams = AncestorMethodStructure.new(obj,
59
+ mock_method.stub!(:bind).and_return mock_method
60
+ mock_method.stub!(:call).and_return mock_method
61
+
62
+ mock_method.stub!(:owner).and_raise StandardError
63
+
64
+ Object.stub!(:instance_method).with(:method).and_return mock_method
65
+ ams = AncestorMethodStructure.new(5,
57
66
  :ancestors_to_show => [],
58
67
  :ancestors_to_exclude => [])
59
- lambda { ams.send(:method_owner, :to_i) }.should raise_error(ArgumentError)
68
+
69
+ lambda { ams.send(:method_owner, :to_i) }.should raise_error(StandardError)
60
70
  end
61
71
 
62
72
  describe "poor_mans_method_owner" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: method_info
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom ten Thij