Skip to content
This repository
Browse code

Fix the line number in the backtrace when Module#delegate raises

  • Loading branch information...
commit 63d100ea35a7fabea25c37f654177c3828fc1dcb 1 parent 7b56fb0
Jon Leighton authored
2  activesupport/lib/active_support/core_ext/module/delegation.rb
@@ -133,7 +133,7 @@ def delegate(*methods)
133 133
           %(raise "#{self}##{method_prefix}#{method} delegated to #{to}.#{method}, but #{to} is nil: \#{self.inspect}")
134 134
         end
135 135
 
136  
-      module_eval(<<-EOS, file, line - 5)
  136
+      module_eval(<<-EOS, file, line - 1)
137 137
         def #{method_prefix}#{method}(*args, &block)        # def customer_name(*args, &block)
138 138
           #{to}.#{call}*args, &block)                       #   client.name(*args, &block)
139 139
         rescue NoMethodError                                # rescue NoMethodError
14  activesupport/test/core_ext/module_test.rb
@@ -38,9 +38,12 @@ def private_method
38 38
   end
39 39
 end
40 40
 
41  
-Someone   = Struct.new(:name, :place) do
  41
+class Someone < Struct.new(:name, :place)
42 42
   delegate :street, :city, :to_f, :protected_method, :private_method, :to => :place
43 43
   delegate :upcase, :to => "place.city"
  44
+
  45
+  FAILED_DELEGATE_LINE = __LINE__ + 1
  46
+  delegate :foo, :to => :place
44 47
 end
45 48
 
46 49
 Invoice   = Struct.new(:client) do
@@ -182,6 +185,15 @@ class << self
182 185
     end
183 186
   end
184 187
 
  188
+  def test_delegation_exception_backtrace
  189
+    someone = Someone.new("foo", "bar")
  190
+    someone.foo
  191
+  rescue NoMethodError => e
  192
+    file_and_line = "#{__FILE__}:#{Someone::FAILED_DELEGATE_LINE}"
  193
+    assert e.backtrace.first.include?(file_and_line),
  194
+           "[#{e.backtrace.first}] did not include [#{file_and_line}]"
  195
+  end
  196
+
185 197
   def test_parent
186 198
     assert_equal Yz::Zy, Yz::Zy::Cd.parent
187 199
     assert_equal Yz, Yz::Zy.parent

0 notes on commit 63d100e

Please sign in to comment.
Something went wrong with that request. Please try again.