diff options
Diffstat (limited to 'test/ruby/test_eval.rb')
-rw-r--r-- | test/ruby/test_eval.rb | 77 |
1 files changed, 75 insertions, 2 deletions
diff --git a/test/ruby/test_eval.rb b/test/ruby/test_eval.rb index bf551c6845..082d1dc03c 100644 --- a/test/ruby/test_eval.rb +++ b/test/ruby/test_eval.rb @@ -219,6 +219,12 @@ class TestEval < Test::Unit::TestCase end end + def test_instance_exec_cvar + [Object.new, [], 7, :sym, true, false, nil].each do |obj| + assert_equal(13, obj.instance_exec{@@cvar}) + end + end + def test_instance_eval_method bug2788 = '[ruby-core:28324]' [Object.new, [], nil, true, false].each do |o| @@ -253,6 +259,70 @@ class TestEval < Test::Unit::TestCase assert_equal(2, bar) end + def test_instance_exec_block_basic + forall_TYPE do |o| + assert_equal nil, o.instance_exec { nil } + assert_equal true, o.instance_exec { true } + assert_equal false, o.instance_exec { false } + assert_equal o, o.instance_exec { self } + assert_equal 1, o.instance_exec { 1 } + assert_equal :sym, o.instance_exec { :sym } + + assert_equal 11, o.instance_exec { 11 } + assert_equal 12, o.instance_exec { @ivar } unless o.frozen? + assert_equal 13, o.instance_exec { @@cvar } + assert_equal 14, o.instance_exec { $gvar__eval } + assert_equal 15, o.instance_exec { Const } + assert_equal 16, o.instance_exec { 7 + 9 } + assert_equal 17, o.instance_exec { 17.to_i } + assert_equal "18", o.instance_exec { "18" } + assert_equal "19", o.instance_exec { "1#{9}" } + + 1.times { + assert_equal 12, o.instance_exec { @ivar } unless o.frozen? + assert_equal 13, o.instance_exec { @@cvar } + assert_equal 14, o.instance_exec { $gvar__eval } + assert_equal 15, o.instance_exec { Const } + } + end + end + + def test_instance_exec_method_definition + klass = Class.new + o = klass.new + + o.instance_exec do + def foo + :foo_result + end + end + + assert_respond_to o, :foo + refute_respond_to klass, :foo + refute_respond_to klass.new, :foo + + assert_equal :foo_result, o.foo + end + + def test_instance_exec_eval_method_definition + klass = Class.new + o = klass.new + + o.instance_exec do + eval %{ + def foo + :foo_result + end + } + end + + assert_respond_to o, :foo + refute_respond_to klass, :foo + refute_respond_to klass.new, :foo + + assert_equal :foo_result, o.foo + end + # # From ruby/test/ruby/test_eval.rb # @@ -418,6 +488,9 @@ class TestEval < Test::Unit::TestCase end end assert_equal(feature6609, feature6609_method) + ensure + Object.undef_method(:feature6609_block) rescue nil + Object.undef_method(:feature6609_method) rescue nil end def test_eval_using_integer_as_binding @@ -474,8 +547,8 @@ class TestEval < Test::Unit::TestCase end def test_eval_location_binding - assert_equal(['(eval)', 1], eval("[__FILE__, __LINE__]", nil)) - assert_equal(['(eval)', 1], eval("[__FILE__, __LINE__]", binding)) + assert_equal(["(eval at #{__FILE__}:#{__LINE__})", 1], eval("[__FILE__, __LINE__]", nil)) + assert_equal(["(eval at #{__FILE__}:#{__LINE__})", 1], eval("[__FILE__, __LINE__]", binding)) assert_equal(['foo', 1], eval("[__FILE__, __LINE__]", nil, 'foo')) assert_equal(['foo', 1], eval("[__FILE__, __LINE__]", binding, 'foo')) assert_equal(['foo', 2], eval("[__FILE__, __LINE__]", nil, 'foo', 2)) |