diff options
Diffstat (limited to 'activesupport/test/core_ext/module_test.rb')
-rw-r--r-- | activesupport/test/core_ext/module_test.rb | 38 |
1 files changed, 36 insertions, 2 deletions
diff --git a/activesupport/test/core_ext/module_test.rb b/activesupport/test/core_ext/module_test.rb index a95cf1591f..f11bf3dc69 100644 --- a/activesupport/test/core_ext/module_test.rb +++ b/activesupport/test/core_ext/module_test.rb @@ -26,11 +26,20 @@ module Yz end end -Somewhere = Struct.new(:street, :city) +Somewhere = Struct.new(:street, :city) do + attr_accessor :name +end -Someone = Struct.new(:name, :place) do +class Someone < Struct.new(:name, :place) delegate :street, :city, :to_f, :to => :place + delegate :name=, :to => :place, :prefix => true delegate :upcase, :to => "place.city" + + FAILED_DELEGATE_LINE = __LINE__ + 1 + delegate :foo, :to => :place + + FAILED_DELEGATE_LINE_2 = __LINE__ + 1 + delegate :bar, :to => :place, :allow_nil => true end Invoice = Struct.new(:client) do @@ -69,6 +78,11 @@ class ModuleTest < Test::Unit::TestCase assert_equal "Chicago", @david.city end + def test_delegation_to_assignment_method + @david.place_name = "Fred" + assert_equal "Fred", @david.place.name + end + def test_delegation_down_hierarchy assert_equal "CHICAGO", @david.upcase end @@ -164,6 +178,26 @@ class ModuleTest < Test::Unit::TestCase end end + def test_delegation_exception_backtrace + someone = Someone.new("foo", "bar") + someone.foo + rescue NoMethodError => e + file_and_line = "#{__FILE__}:#{Someone::FAILED_DELEGATE_LINE}" + # We can't simply check the first line of the backtrace, because JRuby reports the call to __send__ in the backtrace. + assert e.backtrace.any?{|a| a.include?(file_and_line)}, + "[#{e.backtrace.inspect}] did not include [#{file_and_line}]" + end + + def test_delegation_exception_backtrace_with_allow_nil + someone = Someone.new("foo", "bar") + someone.bar + rescue NoMethodError => e + file_and_line = "#{__FILE__}:#{Someone::FAILED_DELEGATE_LINE_2}" + # We can't simply check the first line of the backtrace, because JRuby reports the call to __send__ in the backtrace. + assert e.backtrace.any?{|a| a.include?(file_and_line)}, + "[#{e.backtrace.inspect}] did not include [#{file_and_line}]" + end + def test_parent assert_equal Yz::Zy, Yz::Zy::Cd.parent assert_equal Yz, Yz::Zy.parent |