ruby-changes:39878
From: hsbt <ko1@a...>
Date: Mon, 28 Sep 2015 10:31:42 +0900 (JST)
Subject: [ruby-changes:39878] hsbt:r51959 (trunk): * test/test_forwardable.rb: Write basic tests for lib/forwardable.
hsbt 2015-09-28 10:31:17 +0900 (Mon, 28 Sep 2015) New Revision: 51959 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=51959 Log: * test/test_forwardable.rb: Write basic tests for lib/forwardable. [fix GH-1035] Patch by @kachick Added files: trunk/test/test_forwardable.rb Modified files: trunk/ChangeLog Index: ChangeLog =================================================================== --- ChangeLog (revision 51958) +++ ChangeLog (revision 51959) @@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Mon Sep 28 10:31:12 2015 SHIBATA Hiroshi <hsbt@r...> + + * test/test_forwardable.rb: Write basic tests for lib/forwardable. + [fix GH-1035] Patch by @kachick + Sun Sep 27 23:32:46 2015 Nobuyoshi Nakada <nobu@r...> * class.c (rb_define_class, rb_define_class_id_under): refine Index: test/test_forwardable.rb =================================================================== --- test/test_forwardable.rb (revision 0) +++ test/test_forwardable.rb (revision 51959) @@ -0,0 +1,120 @@ https://github.com/ruby/ruby/blob/trunk/test/test_forwardable.rb#L1 +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 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/