aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test/core_ext/module_test.rb
diff options
context:
space:
mode:
authorSergio Gil <sgilperez@gmail.com>2008-12-08 00:53:38 +0100
committerPratik Naik <pratiknaik@gmail.com>2008-12-21 23:24:06 +0000
commite8de7a67a5ef063164da022845a7cae1753da80e (patch)
tree6b9f49120dd9f95b4b5800454520bf1cc4dcf793 /activesupport/test/core_ext/module_test.rb
parentf7bd0beb67c5d9d50e37aa596605b91e61197fbe (diff)
downloadrails-e8de7a67a5ef063164da022845a7cae1753da80e.tar.gz
rails-e8de7a67a5ef063164da022845a7cae1753da80e.tar.bz2
rails-e8de7a67a5ef063164da022845a7cae1753da80e.zip
Add :allow_nil option to delegate [#1127 state:resolved]
Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
Diffstat (limited to 'activesupport/test/core_ext/module_test.rb')
-rw-r--r--activesupport/test/core_ext/module_test.rb27
1 files changed, 27 insertions, 0 deletions
diff --git a/activesupport/test/core_ext/module_test.rb b/activesupport/test/core_ext/module_test.rb
index 886f692499..a5d98507ba 100644
--- a/activesupport/test/core_ext/module_test.rb
+++ b/activesupport/test/core_ext/module_test.rb
@@ -41,6 +41,10 @@ Invoice = Struct.new(:client) do
delegate :street, :city, :name, :to => :client, :prefix => :customer
end
+Project = Struct.new(:description, :person) do
+ delegate :name, :to => :person, :allow_nil => true
+end
+
class Name
delegate :upcase, :to => :@full_name
@@ -117,6 +121,29 @@ class ModuleTest < Test::Unit::TestCase
end
end
+ def test_delegation_with_allow_nil
+ rails = Project.new("Rails", Someone.new("David"))
+ assert_equal rails.name, "David"
+ end
+
+ def test_delegation_with_allow_nil_and_nil_value
+ rails = Project.new("Rails")
+ assert_nil rails.name
+ end
+
+ def test_delegation_with_allow_nil_and_nil_value_and_prefix
+ Project.class_eval do
+ delegate :name, :to => :person, :allow_nil => true, :prefix => true
+ end
+ rails = Project.new("Rails")
+ assert_nil rails.person_name
+ end
+
+ def test_delegation_without_allow_nil_and_nil_value
+ david = Someone.new("David")
+ assert_raises(NoMethodError) { david.street }
+ end
+
def test_parent
assert_equal Yz::Zy, Yz::Zy::Cd.parent
assert_equal Yz, Yz::Zy.parent