From 7342e78ea6650a6a58f8ad448df01d448383496d Mon Sep 17 00:00:00 2001 From: nobu Date: Fri, 6 Jun 2014 07:58:35 +0000 Subject: [PATCH] test_assignment.rb: assignment to private attribute * test/ruby/test_assignment.rb (test_assign_private_self): test for r3509, assignment to private attribute is allowed iff its receiver is literal `self`. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@46364 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- test/ruby/test_assignment.rb | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/test/ruby/test_assignment.rb b/test/ruby/test_assignment.rb index ebde099586..604464bbd3 100644 --- a/test/ruby/test_assignment.rb +++ b/test/ruby/test_assignment.rb @@ -101,6 +101,29 @@ class TestAssignment < Test::Unit::TestCase assert_equal([1, 2, 3, [1, 2, 3]], a[:x], bug2050) end + def test_assign_private_self + o = Object.new + class << o + private + def foo=(a); 42; end + def []=(i, a); 42; end + end + + assert_raise(NoMethodError) { + o.instance_eval {o.foo = 1} + } + assert_nothing_raised(NoMethodError) { + assert_equal(1, o.instance_eval {self.foo = 1}) + } + + assert_raise(NoMethodError) { + o.instance_eval {o[0] = 1} + } + assert_nothing_raised(NoMethodError) { + assert_equal(1, o.instance_eval {self[0] = 1}) + } + end + def test_yield def f; yield(nil); end; f {|a| assert_nil(a)}; undef f def f; yield(1); end; f {|a| assert_equal(1, a)}; undef f