summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorhsbt <hsbt@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-09-28 01:31:17 +0000
committerhsbt <hsbt@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-09-28 01:31:17 +0000
commitd92470bbc2a1b054fe351585f374157e470a7bb6 (patch)
treeac6e2e1e2dc5de6594b3e0f852b35143d89a7031 /test
parenteb47de300519d6680dd1f1180a427a7509f97b8f (diff)
downloadruby-d92470bbc2a1b054fe351585f374157e470a7bb6.tar.gz
* test/test_forwardable.rb: Write basic tests for lib/forwardable.
[fix GH-1035] Patch by @kachick git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51959 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test')
-rw-r--r--test/test_forwardable.rb120
1 files changed, 120 insertions, 0 deletions
diff --git a/test/test_forwardable.rb b/test/test_forwardable.rb
new file mode 100644
index 0000000000..c04c78577b
--- /dev/null
+++ b/test/test_forwardable.rb
@@ -0,0 +1,120 @@
+require 'test/unit'
+require 'forwardable'
+
+class TestForwardable < Test::Unit::TestCase
+ RECEIVER = BasicObject.new
+ RETURNED1 = BasicObject.new
+ RETURNED2 = BasicObject.new
+
+ class << RECEIVER
+ def delegated1
+ RETURNED1
+ end
+
+ def delegated2
+ RETURNED2
+ end
+ end
+
+ def test_def_instance_delegator
+ %i[def_delegator def_instance_delegator].each do |m|
+ cls = forwardable_class do
+ __send__ m, :@receiver, :delegated1
+ end
+
+ assert_same RETURNED1, cls.new.delegated1
+ end
+ end
+
+ def test_def_instance_delegators
+ %i[def_delegators def_instance_delegators].each do |m|
+ cls = forwardable_class do
+ __send__ m, :@receiver, :delegated1, :delegated2
+ end
+
+ assert_same RETURNED1, cls.new.delegated1
+ assert_same RETURNED2, cls.new.delegated2
+ end
+ end
+
+ def test_instance_delegate
+ %i[delegate instance_delegate].each do |m|
+ cls = forwardable_class do
+ __send__ m, delegated1: :@receiver, delegated2: :@receiver
+ end
+
+ assert_same RETURNED1, cls.new.delegated1
+ assert_same RETURNED2, cls.new.delegated2
+
+ cls = forwardable_class do
+ __send__ m, %i[delegated1 delegated2] => :@receiver
+ end
+
+ assert_same RETURNED1, cls.new.delegated1
+ assert_same RETURNED2, cls.new.delegated2
+ end
+ end
+
+ def test_def_single_delegator
+ %i[def_delegator def_single_delegator].each do |m|
+ cls = single_forwardable_class do
+ __send__ m, :@receiver, :delegated1
+ end
+
+ assert_same RETURNED1, cls.delegated1
+ end
+ end
+
+ def test_def_single_delegators
+ %i[def_delegators def_single_delegators].each do |m|
+ cls = single_forwardable_class do
+ __send__ m, :@receiver, :delegated1, :delegated2
+ end
+
+ assert_same RETURNED1, cls.delegated1
+ assert_same RETURNED2, cls.delegated2
+ end
+ end
+
+ def test_single_delegate
+ %i[delegate single_delegate].each do |m|
+ cls = single_forwardable_class do
+ __send__ m, delegated1: :@receiver, delegated2: :@receiver
+ end
+
+ assert_same RETURNED1, cls.delegated1
+ assert_same RETURNED2, cls.delegated2
+
+ cls = single_forwardable_class do
+ __send__ m, %i[delegated1 delegated2] => :@receiver
+ end
+
+ assert_same RETURNED1, cls.delegated1
+ assert_same RETURNED2, cls.delegated2
+ end
+ end
+
+ private
+
+ def forwardable_class(&block)
+ Class.new do
+ extend Forwardable
+
+ def initialize
+ @receiver = RECEIVER
+ end
+
+ class_exec(&block)
+ end
+ end
+
+ def single_forwardable_class(&block)
+ Class.new do
+ extend SingleForwardable
+
+ @receiver = RECEIVER
+
+ class_exec(&block)
+ end
+ end
+end