From 07c03bc30984a496558d9e830bc4fb2f8cfb1854 Mon Sep 17 00:00:00 2001 From: Koichi Sasada Date: Fri, 23 Oct 2020 13:27:21 +0900 Subject: check isolated Proc more strictly Isolated Proc prohibit to access outer local variables, but it was violated by binding and so on, so they should be error. --- test/ruby/test_proc.rb | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) (limited to 'test') diff --git a/test/ruby/test_proc.rb b/test/ruby/test_proc.rb index 0cc5d488c2..c1f17a003a 100644 --- a/test/ruby/test_proc.rb +++ b/test/ruby/test_proc.rb @@ -1538,6 +1538,57 @@ class TestProc < Test::Unit::TestCase assert_equal(42, Module.new { extend self def m1(&b) b end; def m2(); m1 { next 42 } end }.m2.call) end + + def test_isolate + assert_raise_with_message ArgumentError, /\(a\)/ do + a = :a + Proc.new{p a}.isolate.call + end + + assert_raise_with_message ArgumentError, /\(a\)/ do + a = :a + 1.times{ + Proc.new{p a}.isolate.call + } + end + + assert_raise_with_message ArgumentError, /yield/ do + Proc.new{yield}.isolate.call + end + + # binding + + :a.tap{|a| + :b.tap{|b| + Proc.new{ + :c.tap{|c| + assert_equal :c, eval('c') + + assert_raise_with_message SyntaxError, /\`a\'/ do + eval('p a') + end + + assert_raise_with_message SyntaxError, /\`b\'/ do + eval('p b') + end + + assert_raise_with_message SyntaxError, /can not yield from isolated Proc/ do + eval('p yield') + end + + assert_equal :c, binding.local_variable_get(:c) + + assert_raise_with_message NameError, /local variable \`a\' is not defined/ do + binding.local_variable_get(:a) + end + + assert_equal [:c], local_variables + assert_equal [:c], binding.local_variables + } + }.isolate.call + } + } + end if proc{}.respond_to? :isolate end class TestProcKeywords < Test::Unit::TestCase -- cgit v1.2.3