* test/test_forwardable.rb: add tests for r53381.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@53382 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2015-12-30 02:26:15 +00:00
Родитель 986fb55961
Коммит d8eb5ade4f
1 изменённых файлов: 101 добавлений и 3 удалений

Просмотреть файл

@ -27,6 +27,34 @@ class TestForwardable < Test::Unit::TestCase
end
end
def test_def_instance_delegator_using_args_method_as_receiver
%i[def_delegator def_instance_delegator].each do |m|
cls = forwardable_class(
receiver_name: :args,
type: :method,
visibility: :private
) do
__send__ m, :args, :delegated1
end
assert_same RETURNED1, cls.new.delegated1
end
end
def test_def_instance_delegator_using_block_method_as_receiver
%i[def_delegator def_instance_delegator].each do |m|
cls = forwardable_class(
receiver_name: :block,
type: :method,
visibility: :private
) do
__send__ m, :block, :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
@ -38,6 +66,36 @@ class TestForwardable < Test::Unit::TestCase
end
end
def test_def_instance_delegators_using_args_method_as_receiver
%i[def_delegators def_instance_delegators].each do |m|
cls = forwardable_class(
receiver_name: :args,
type: :method,
visibility: :private
) do
__send__ m, :args, :delegated1, :delegated2
end
assert_same RETURNED1, cls.new.delegated1
assert_same RETURNED2, cls.new.delegated2
end
end
def test_def_instance_delegators_using_block_method_as_receiver
%i[def_delegators def_instance_delegators].each do |m|
cls = forwardable_class(
receiver_name: :block,
type: :method,
visibility: :private
) do
__send__ m, :block, :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
@ -56,6 +114,36 @@ class TestForwardable < Test::Unit::TestCase
end
end
def test_def_instance_delegate_using_args_method_as_receiver
%i[delegate instance_delegate].each do |m|
cls = forwardable_class(
receiver_name: :args,
type: :method,
visibility: :private
) do
__send__ m, delegated1: :args, delegated2: :args
end
assert_same RETURNED1, cls.new.delegated1
assert_same RETURNED2, cls.new.delegated2
end
end
def test_def_instance_delegate_using_block_method_as_receiver
%i[delegate instance_delegate].each do |m|
cls = forwardable_class(
receiver_name: :block,
type: :method,
visibility: :private
) do
__send__ m, delegated1: :block, delegated2: :block
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
@ -126,12 +214,22 @@ class TestForwardable < Test::Unit::TestCase
private
def forwardable_class(&block)
def forwardable_class(
receiver_name: :receiver,
type: :ivar,
visibility: :public,
&block
)
Class.new do
extend Forwardable
def initialize
@receiver = RECEIVER
define_method(:initialize) do
instance_variable_set("@#{receiver_name}", RECEIVER)
end
if type == :method
attr_reader(receiver_name)
__send__(visibility, receiver_name)
end
class_exec(&block)