2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2003-09-04 12:18:59 -04:00
|
|
|
require 'test/unit'
|
|
|
|
|
|
|
|
class TestEval < Test::Unit::TestCase
|
2006-12-31 10:02:22 -05:00
|
|
|
|
|
|
|
@ivar = 12
|
|
|
|
@@cvar = 13
|
|
|
|
$gvar__eval = 14
|
|
|
|
Const = 15
|
|
|
|
|
2008-12-25 00:40:41 -05:00
|
|
|
def ruby(*args)
|
|
|
|
args = ['-e', '$>.write($<.read)'] if args.empty?
|
|
|
|
ruby = EnvUtil.rubybin
|
|
|
|
f = IO.popen([ruby] + args, 'r+')
|
|
|
|
yield(f)
|
|
|
|
ensure
|
|
|
|
f.close unless !f || f.closed?
|
|
|
|
end
|
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
def test_eval_basic
|
|
|
|
assert_equal nil, eval("nil")
|
|
|
|
assert_equal true, eval("true")
|
|
|
|
assert_equal false, eval("false")
|
|
|
|
assert_equal self, eval("self")
|
|
|
|
assert_equal 1, eval("1")
|
|
|
|
assert_equal :sym, eval(":sym")
|
|
|
|
|
|
|
|
assert_equal 11, eval("11")
|
|
|
|
@ivar = 12
|
|
|
|
assert_equal 12, eval("@ivar")
|
|
|
|
assert_equal 13, eval("@@cvar")
|
|
|
|
assert_equal 14, eval("$gvar__eval")
|
|
|
|
assert_equal 15, eval("Const")
|
|
|
|
|
|
|
|
assert_equal 16, eval("7 + 9")
|
|
|
|
assert_equal 17, eval("17.to_i")
|
|
|
|
assert_equal "18", eval(%q("18"))
|
|
|
|
assert_equal "19", eval(%q("1#{9}"))
|
|
|
|
|
|
|
|
1.times {
|
|
|
|
assert_equal 12, eval("@ivar")
|
|
|
|
assert_equal 13, eval("@@cvar")
|
|
|
|
assert_equal 14, eval("$gvar__eval")
|
|
|
|
assert_equal 15, eval("Const")
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_eval_binding_basic
|
|
|
|
assert_equal nil, eval("nil", binding())
|
|
|
|
assert_equal true, eval("true", binding())
|
|
|
|
assert_equal false, eval("false", binding())
|
|
|
|
assert_equal self, eval("self", binding())
|
|
|
|
assert_equal 1, eval("1", binding())
|
|
|
|
assert_equal :sym, eval(":sym", binding())
|
|
|
|
|
|
|
|
assert_equal 11, eval("11", binding())
|
|
|
|
@ivar = 12
|
|
|
|
assert_equal 12, eval("@ivar", binding())
|
|
|
|
assert_equal 13, eval("@@cvar", binding())
|
|
|
|
assert_equal 14, eval("$gvar__eval", binding())
|
|
|
|
assert_equal 15, eval("Const", binding())
|
|
|
|
|
|
|
|
assert_equal 16, eval("7 + 9", binding())
|
|
|
|
assert_equal 17, eval("17.to_i", binding())
|
|
|
|
assert_equal "18", eval(%q("18"), binding())
|
|
|
|
assert_equal "19", eval(%q("1#{9}"), binding())
|
|
|
|
|
|
|
|
1.times {
|
|
|
|
assert_equal 12, eval("@ivar")
|
|
|
|
assert_equal 13, eval("@@cvar")
|
|
|
|
assert_equal 14, eval("$gvar__eval")
|
|
|
|
assert_equal 15, eval("Const")
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_module_eval_string_basic
|
|
|
|
c = self.class
|
|
|
|
assert_equal nil, c.module_eval("nil")
|
|
|
|
assert_equal true, c.module_eval("true")
|
|
|
|
assert_equal false, c.module_eval("false")
|
|
|
|
assert_equal c, c.module_eval("self")
|
|
|
|
assert_equal :sym, c.module_eval(":sym")
|
|
|
|
assert_equal 11, c.module_eval("11")
|
|
|
|
@ivar = 12
|
|
|
|
assert_equal 12, c.module_eval("@ivar")
|
|
|
|
assert_equal 13, c.module_eval("@@cvar")
|
|
|
|
assert_equal 14, c.module_eval("$gvar__eval")
|
|
|
|
assert_equal 15, c.module_eval("Const")
|
|
|
|
assert_equal 16, c.module_eval("7 + 9")
|
|
|
|
assert_equal 17, c.module_eval("17.to_i")
|
|
|
|
assert_equal "18", c.module_eval(%q("18"))
|
|
|
|
assert_equal "19", c.module_eval(%q("1#{9}"))
|
|
|
|
|
|
|
|
@ivar = 12
|
|
|
|
1.times {
|
|
|
|
assert_equal 12, c.module_eval("@ivar")
|
|
|
|
assert_equal 13, c.module_eval("@@cvar")
|
|
|
|
assert_equal 14, c.module_eval("$gvar__eval")
|
|
|
|
assert_equal 15, c.module_eval("Const")
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_module_eval_block_basic
|
|
|
|
c = self.class
|
|
|
|
assert_equal nil, c.module_eval { nil }
|
|
|
|
assert_equal true, c.module_eval { true }
|
|
|
|
assert_equal false, c.module_eval { false }
|
|
|
|
assert_equal c, c.module_eval { self }
|
|
|
|
assert_equal :sym, c.module_eval { :sym }
|
|
|
|
assert_equal 11, c.module_eval { 11 }
|
|
|
|
@ivar = 12
|
|
|
|
assert_equal 12, c.module_eval { @ivar }
|
|
|
|
assert_equal 13, c.module_eval { @@cvar }
|
|
|
|
assert_equal 14, c.module_eval { $gvar__eval }
|
|
|
|
assert_equal 15, c.module_eval { Const }
|
|
|
|
assert_equal 16, c.module_eval { 7 + 9 }
|
|
|
|
assert_equal 17, c.module_eval { "17".to_i }
|
|
|
|
assert_equal "18", c.module_eval { "18" }
|
|
|
|
assert_equal "19", c.module_eval { "1#{9}" }
|
|
|
|
|
|
|
|
@ivar = 12
|
|
|
|
1.times {
|
|
|
|
assert_equal 12, c.module_eval { @ivar }
|
|
|
|
assert_equal 13, c.module_eval { @@cvar }
|
|
|
|
assert_equal 14, c.module_eval { $gvar__eval }
|
|
|
|
assert_equal 15, c.module_eval { Const }
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2015-08-04 02:35:31 -04:00
|
|
|
def test_module_eval_block_symbol
|
|
|
|
assert_equal "Math", Math.module_eval(&:to_s)
|
|
|
|
end
|
|
|
|
|
2012-07-19 02:05:58 -04:00
|
|
|
def forall_TYPE
|
2013-09-19 01:30:02 -04:00
|
|
|
objects = [Object.new, [], nil, true, false] # TODO: check
|
2006-12-31 10:02:22 -05:00
|
|
|
objects.each do |obj|
|
2014-09-11 01:37:32 -04:00
|
|
|
obj.instance_variable_set :@ivar, 12 unless obj.frozen?
|
2012-07-19 02:05:58 -04:00
|
|
|
yield obj
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_instance_eval_string_basic
|
2012-07-19 02:05:58 -04:00
|
|
|
forall_TYPE do |o|
|
2012-07-19 02:06:03 -04:00
|
|
|
assert_equal nil, o.instance_eval("nil")
|
|
|
|
assert_equal true, o.instance_eval("true")
|
|
|
|
assert_equal false, o.instance_eval("false")
|
|
|
|
assert_equal o, o.instance_eval("self")
|
|
|
|
assert_equal 1, o.instance_eval("1")
|
|
|
|
assert_equal :sym, o.instance_eval(":sym")
|
|
|
|
|
|
|
|
assert_equal 11, o.instance_eval("11")
|
2014-09-11 01:37:32 -04:00
|
|
|
assert_equal 12, o.instance_eval("@ivar") unless o.frozen?
|
2012-07-19 02:06:03 -04:00
|
|
|
assert_equal 13, o.instance_eval("@@cvar")
|
|
|
|
assert_equal 14, o.instance_eval("$gvar__eval")
|
|
|
|
assert_equal 15, o.instance_eval("Const")
|
|
|
|
assert_equal 16, o.instance_eval("7 + 9")
|
|
|
|
assert_equal 17, o.instance_eval("17.to_i")
|
|
|
|
assert_equal "18", o.instance_eval(%q("18"))
|
|
|
|
assert_equal "19", o.instance_eval(%q("1#{9}"))
|
|
|
|
|
|
|
|
1.times {
|
2014-09-11 01:37:32 -04:00
|
|
|
assert_equal 12, o.instance_eval("@ivar") unless o.frozen?
|
2012-07-19 02:06:03 -04:00
|
|
|
assert_equal 13, o.instance_eval("@@cvar")
|
|
|
|
assert_equal 14, o.instance_eval("$gvar__eval")
|
|
|
|
assert_equal 15, o.instance_eval("Const")
|
|
|
|
}
|
2012-07-19 02:05:58 -04:00
|
|
|
end
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_instance_eval_block_basic
|
2012-07-19 02:05:58 -04:00
|
|
|
forall_TYPE do |o|
|
2012-07-19 02:06:03 -04:00
|
|
|
assert_equal nil, o.instance_eval { nil }
|
|
|
|
assert_equal true, o.instance_eval { true }
|
|
|
|
assert_equal false, o.instance_eval { false }
|
|
|
|
assert_equal o, o.instance_eval { self }
|
|
|
|
assert_equal 1, o.instance_eval { 1 }
|
|
|
|
assert_equal :sym, o.instance_eval { :sym }
|
|
|
|
|
|
|
|
assert_equal 11, o.instance_eval { 11 }
|
2014-09-11 01:37:32 -04:00
|
|
|
assert_equal 12, o.instance_eval { @ivar } unless o.frozen?
|
2012-07-19 02:06:03 -04:00
|
|
|
assert_equal 13, o.instance_eval { @@cvar }
|
|
|
|
assert_equal 14, o.instance_eval { $gvar__eval }
|
|
|
|
assert_equal 15, o.instance_eval { Const }
|
|
|
|
assert_equal 16, o.instance_eval { 7 + 9 }
|
|
|
|
assert_equal 17, o.instance_eval { 17.to_i }
|
|
|
|
assert_equal "18", o.instance_eval { "18" }
|
|
|
|
assert_equal "19", o.instance_eval { "1#{9}" }
|
|
|
|
|
|
|
|
1.times {
|
2014-09-11 01:37:32 -04:00
|
|
|
assert_equal 12, o.instance_eval { @ivar } unless o.frozen?
|
2012-07-19 02:06:03 -04:00
|
|
|
assert_equal 13, o.instance_eval { @@cvar }
|
|
|
|
assert_equal 14, o.instance_eval { $gvar__eval }
|
|
|
|
assert_equal 15, o.instance_eval { Const }
|
|
|
|
}
|
2012-07-19 02:05:58 -04:00
|
|
|
end
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
2014-12-02 13:22:15 -05:00
|
|
|
def test_instance_eval_block_self
|
|
|
|
# instance_eval(&block)'s self must not be sticky (jruby/jruby#2060)
|
|
|
|
pr = proc { self }
|
|
|
|
assert_equal self, pr.call
|
|
|
|
o = Object.new
|
|
|
|
assert_equal o, o.instance_eval(&pr)
|
|
|
|
assert_equal self, pr.call
|
|
|
|
end
|
|
|
|
|
2015-08-04 02:35:31 -04:00
|
|
|
def test_instance_eval_block_symbol
|
|
|
|
forall_TYPE do |o|
|
|
|
|
assert_equal o.to_s, o.instance_eval(&:to_s)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
def test_instance_eval_cvar
|
2007-12-21 12:22:34 -05:00
|
|
|
[Object.new, [], 7, :sym, true, false, nil].each do |obj|
|
2008-05-19 14:47:56 -04:00
|
|
|
assert_equal(13, obj.instance_eval("@@cvar"))
|
|
|
|
assert_equal(13, obj.instance_eval{@@cvar})
|
|
|
|
# assert_raise(NameError){obj.instance_eval("@@cvar")}
|
|
|
|
# assert_raise(NameError){obj.instance_eval{@@cvar}}
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-08-06 11:31:13 -04:00
|
|
|
def test_instance_eval_method
|
|
|
|
bug2788 = '[ruby-core:28324]'
|
|
|
|
[Object.new, [], nil, true, false].each do |o|
|
|
|
|
assert_nothing_raised(TypeError, "#{bug2788} (#{o.inspect})") do
|
|
|
|
o.instance_eval {
|
|
|
|
def defd_using_instance_eval() :ok end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
assert_equal(:ok, o.defd_using_instance_eval)
|
|
|
|
class << o
|
|
|
|
remove_method :defd_using_instance_eval
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-30 17:27:27 -04:00
|
|
|
def test_instance_eval_on_argf_singleton_class
|
|
|
|
bug8188 = '[ruby-core:53839] [Bug #8188]'
|
|
|
|
assert_warning('', bug8188) do
|
|
|
|
ARGF.singleton_class.instance_eval{}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-08-08 09:51:32 -04:00
|
|
|
class Foo
|
|
|
|
Bar = 2
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_instance_eval_const
|
|
|
|
bar = nil
|
|
|
|
assert_nothing_raised(NameError) do
|
|
|
|
bar = Foo.new.instance_eval("Bar")
|
|
|
|
end
|
|
|
|
assert_equal(2, bar)
|
|
|
|
end
|
|
|
|
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2006-12-31 10:02:22 -05:00
|
|
|
# From ruby/test/ruby/test_eval.rb
|
|
|
|
#
|
|
|
|
|
2014-03-04 09:00:48 -05:00
|
|
|
def make_test_binding
|
2016-02-19 02:48:02 -05:00
|
|
|
local1 = local1 = "local1"
|
2003-09-04 12:18:59 -04:00
|
|
|
lambda {
|
2016-02-19 02:48:02 -05:00
|
|
|
local2 = local2 = "local2"
|
2003-09-04 12:18:59 -04:00
|
|
|
return binding
|
|
|
|
}.call
|
|
|
|
end
|
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
def test_eval_orig
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_nil(eval(""))
|
2012-08-08 09:46:09 -04:00
|
|
|
bad=false
|
|
|
|
eval 'while false; bad = true; print "foo\n" end'
|
|
|
|
assert(!bad)
|
2003-09-05 11:15:43 -04:00
|
|
|
|
2016-08-07 05:08:29 -04:00
|
|
|
assert(eval('Object'))
|
2003-09-04 12:18:59 -04:00
|
|
|
assert(eval('true'))
|
|
|
|
assert(!eval('nil'))
|
|
|
|
assert(!eval('false'))
|
2003-09-05 11:15:43 -04:00
|
|
|
|
2003-09-04 12:18:59 -04:00
|
|
|
$foo = 'assert(true)'
|
|
|
|
begin
|
|
|
|
eval $foo
|
|
|
|
rescue
|
|
|
|
assert(false)
|
|
|
|
end
|
2003-09-05 11:15:43 -04:00
|
|
|
|
|
|
|
assert_equal('assert(true)', eval("$foo"))
|
|
|
|
assert_equal(true, eval("true"))
|
2016-02-19 02:48:02 -05:00
|
|
|
i = i = 5
|
2003-09-04 12:18:59 -04:00
|
|
|
assert(eval("i == 5"))
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_equal(5, eval("i"))
|
2003-09-04 12:18:59 -04:00
|
|
|
assert(eval("defined? i"))
|
2003-09-05 11:15:43 -04:00
|
|
|
|
2014-03-04 09:00:48 -05:00
|
|
|
x = make_test_binding
|
2012-08-08 09:46:09 -04:00
|
|
|
assert_equal("local1", eval("local1", x)) # normal local var
|
|
|
|
assert_equal("local2", eval("local2", x)) # nested local var
|
|
|
|
bad = true
|
2003-09-04 12:18:59 -04:00
|
|
|
begin
|
|
|
|
p eval("local1")
|
|
|
|
rescue NameError # must raise error
|
2012-08-08 09:46:09 -04:00
|
|
|
bad = false
|
2003-09-04 12:18:59 -04:00
|
|
|
end
|
2012-08-08 09:46:09 -04:00
|
|
|
assert(!bad)
|
2003-09-05 11:15:43 -04:00
|
|
|
|
2003-09-04 12:18:59 -04:00
|
|
|
# !! use class_eval to avoid nested definition
|
2012-08-08 09:46:09 -04:00
|
|
|
x = self.class.class_eval %q(
|
2003-09-04 12:18:59 -04:00
|
|
|
module EvTest
|
|
|
|
EVTEST1 = 25
|
|
|
|
evtest2 = 125
|
2016-02-19 02:48:02 -05:00
|
|
|
evtest2 = evtest2
|
2012-08-08 09:46:09 -04:00
|
|
|
binding
|
2003-09-04 12:18:59 -04:00
|
|
|
end
|
|
|
|
)
|
2012-08-08 09:46:09 -04:00
|
|
|
assert_equal(25, eval("EVTEST1", x)) # constant in module
|
|
|
|
assert_equal(125, eval("evtest2", x)) # local var in module
|
|
|
|
bad = true
|
2003-09-04 12:18:59 -04:00
|
|
|
begin
|
|
|
|
eval("EVTEST1")
|
|
|
|
rescue NameError # must raise error
|
2012-08-08 09:46:09 -04:00
|
|
|
bad = false
|
2003-09-04 12:18:59 -04:00
|
|
|
end
|
2012-08-08 09:46:09 -04:00
|
|
|
assert(!bad)
|
2003-09-05 11:15:43 -04:00
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
if false
|
|
|
|
# Ruby 2.0 doesn't see Proc as Binding
|
|
|
|
x = proc{}
|
|
|
|
eval "i4 = 1", x
|
|
|
|
assert_equal(1, eval("i4", x))
|
|
|
|
x = proc{proc{}}.call
|
|
|
|
eval "i4 = 22", x
|
|
|
|
assert_equal(22, eval("i4", x))
|
2012-08-08 09:46:09 -04:00
|
|
|
t = []
|
2006-12-31 10:02:22 -05:00
|
|
|
x = proc{proc{}}.call
|
2012-08-08 09:46:09 -04:00
|
|
|
eval "(0..9).each{|i5| t[i5] = proc{i5*2}}", x
|
|
|
|
assert_equal(8, t[4].call)
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
2003-09-05 11:15:43 -04:00
|
|
|
|
2003-09-04 12:18:59 -04:00
|
|
|
x = binding
|
|
|
|
eval "i = 1", x
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_equal(1, eval("i", x))
|
2003-09-04 12:18:59 -04:00
|
|
|
x = proc{binding}.call
|
|
|
|
eval "i = 22", x
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_equal(22, eval("i", x))
|
2012-08-08 09:46:09 -04:00
|
|
|
t = []
|
2003-09-04 12:18:59 -04:00
|
|
|
x = proc{binding}.call
|
2012-08-08 09:46:09 -04:00
|
|
|
eval "(0..9).each{|i5| t[i5] = proc{i5*2}}", x
|
|
|
|
assert_equal(8, t[4].call)
|
2003-09-04 12:18:59 -04:00
|
|
|
x = proc{binding}.call
|
|
|
|
eval "for i6 in 1..1; j6=i6; end", x
|
|
|
|
assert(eval("defined? i6", x))
|
|
|
|
assert(eval("defined? j6", x))
|
2003-09-05 11:15:43 -04:00
|
|
|
|
2003-09-04 12:18:59 -04:00
|
|
|
proc {
|
|
|
|
p = binding
|
|
|
|
eval "foo11 = 1", p
|
|
|
|
foo22 = 5
|
2016-02-19 02:48:02 -05:00
|
|
|
proc{foo11=22;foo11}.call
|
2003-09-04 12:18:59 -04:00
|
|
|
proc{foo22=55}.call
|
2006-12-31 10:02:22 -05:00
|
|
|
# assert_equal(eval("foo11"), eval("foo11", p))
|
|
|
|
# assert_equal(1, eval("foo11"))
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_equal(eval("foo22"), eval("foo22", p))
|
|
|
|
assert_equal(55, eval("foo22"))
|
2003-09-04 12:18:59 -04:00
|
|
|
}.call
|
2003-09-05 11:15:43 -04:00
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
if false
|
|
|
|
# Ruby 2.0 doesn't see Proc as Binding
|
|
|
|
p1 = proc{i7 = 0; proc{i7}}.call
|
|
|
|
assert_equal(0, p1.call)
|
|
|
|
eval "i7=5", p1
|
|
|
|
assert_equal(5, p1.call)
|
|
|
|
assert(!defined?(i7))
|
|
|
|
end
|
2003-09-05 11:15:43 -04:00
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
if false
|
|
|
|
# Ruby 2.0 doesn't see Proc as Binding
|
|
|
|
p1 = proc{i7 = 0; proc{i7}}.call
|
|
|
|
i7 = nil
|
|
|
|
assert_equal(0, p1.call)
|
|
|
|
eval "i7=1", p1
|
|
|
|
assert_equal(1, p1.call)
|
|
|
|
eval "i7=5", p1
|
|
|
|
assert_equal(5, p1.call)
|
|
|
|
assert_nil(i7)
|
|
|
|
end
|
2003-09-04 12:18:59 -04:00
|
|
|
end
|
2004-09-07 01:32:26 -04:00
|
|
|
|
2007-11-18 02:18:56 -05:00
|
|
|
def test_nil_instance_eval_cvar
|
2004-09-07 01:32:26 -04:00
|
|
|
def nil.test_binding
|
|
|
|
binding
|
|
|
|
end
|
|
|
|
bb = eval("nil.instance_eval \"binding\"", nil.test_binding)
|
2007-11-18 02:18:56 -05:00
|
|
|
assert_raise(NameError, "[ruby-dev:24103]") { eval("@@a", bb) }
|
2004-09-07 01:32:26 -04:00
|
|
|
class << nil
|
|
|
|
remove_method :test_binding
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-11-18 02:18:56 -05:00
|
|
|
def test_fixnum_instance_eval_cvar
|
|
|
|
assert_raise(NameError, "[ruby-dev:24213]") { 1.instance_eval "@@a" }
|
2004-09-07 01:32:26 -04:00
|
|
|
end
|
|
|
|
|
2007-11-18 02:18:56 -05:00
|
|
|
def test_cvar_scope_with_instance_eval
|
2008-05-19 14:47:56 -04:00
|
|
|
# TODO: check
|
Use Integer instead of Fixnum and Bignum.
* object.c, numeric.c, enum.c, ext/-test-/bignum/mul.c,
lib/rexml/quickpath.rb, lib/rexml/text.rb, lib/rexml/xpath_parser.rb,
lib/rubygems/specification.rb, lib/uri/generic.rb,
bootstraptest/test_eval.rb, basictest/test.rb,
test/-ext-/bignum/test_big2str.rb, test/-ext-/bignum/test_div.rb,
test/-ext-/bignum/test_mul.rb, test/-ext-/bignum/test_str2big.rb,
test/csv/test_data_converters.rb, test/date/test_date.rb,
test/json/test_json_generate.rb, test/minitest/test_minitest_mock.rb,
test/openssl/test_cipher.rb, test/rexml/test_jaxen.rb,
test/ruby/test_array.rb, test/ruby/test_basicinstructions.rb,
test/ruby/test_bignum.rb, test/ruby/test_case.rb,
test/ruby/test_class.rb, test/ruby/test_complex.rb,
test/ruby/test_enum.rb, test/ruby/test_eval.rb,
test/ruby/test_iseq.rb, test/ruby/test_literal.rb,
test/ruby/test_math.rb, test/ruby/test_module.rb,
test/ruby/test_numeric.rb, test/ruby/test_range.rb,
test/ruby/test_rational.rb, test/ruby/test_refinement.rb,
test/ruby/test_rubyvm.rb, test/ruby/test_struct.rb,
test/ruby/test_variable.rb, test/rubygems/test_gem_specification.rb,
test/thread/test_queue.rb: Use Integer instead of Fixnum and Bignum.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55029 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-05-17 09:15:57 -04:00
|
|
|
Integer.class_eval "@@test_cvar_scope_with_instance_eval = 1" # depends on [ruby-dev:24229]
|
2004-09-07 01:32:26 -04:00
|
|
|
@@test_cvar_scope_with_instance_eval = 4
|
2008-05-19 14:47:56 -04:00
|
|
|
assert_equal(4, 1.instance_eval("@@test_cvar_scope_with_instance_eval"), "[ruby-dev:24223]")
|
Use Integer instead of Fixnum and Bignum.
* object.c, numeric.c, enum.c, ext/-test-/bignum/mul.c,
lib/rexml/quickpath.rb, lib/rexml/text.rb, lib/rexml/xpath_parser.rb,
lib/rubygems/specification.rb, lib/uri/generic.rb,
bootstraptest/test_eval.rb, basictest/test.rb,
test/-ext-/bignum/test_big2str.rb, test/-ext-/bignum/test_div.rb,
test/-ext-/bignum/test_mul.rb, test/-ext-/bignum/test_str2big.rb,
test/csv/test_data_converters.rb, test/date/test_date.rb,
test/json/test_json_generate.rb, test/minitest/test_minitest_mock.rb,
test/openssl/test_cipher.rb, test/rexml/test_jaxen.rb,
test/ruby/test_array.rb, test/ruby/test_basicinstructions.rb,
test/ruby/test_bignum.rb, test/ruby/test_case.rb,
test/ruby/test_class.rb, test/ruby/test_complex.rb,
test/ruby/test_enum.rb, test/ruby/test_eval.rb,
test/ruby/test_iseq.rb, test/ruby/test_literal.rb,
test/ruby/test_math.rb, test/ruby/test_module.rb,
test/ruby/test_numeric.rb, test/ruby/test_range.rb,
test/ruby/test_rational.rb, test/ruby/test_refinement.rb,
test/ruby/test_rubyvm.rb, test/ruby/test_struct.rb,
test/ruby/test_variable.rb, test/rubygems/test_gem_specification.rb,
test/thread/test_queue.rb: Use Integer instead of Fixnum and Bignum.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55029 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-05-17 09:15:57 -04:00
|
|
|
Integer.__send__(:remove_class_variable, :@@test_cvar_scope_with_instance_eval)
|
2004-09-07 01:32:26 -04:00
|
|
|
end
|
|
|
|
|
2007-11-18 02:18:56 -05:00
|
|
|
def test_eval_and_define_method
|
|
|
|
assert_nothing_raised("[ruby-dev:24228]") {
|
2004-09-07 01:32:26 -04:00
|
|
|
def temporally_method_for_test_eval_and_define_method(&block)
|
|
|
|
lambda {
|
2007-11-04 15:36:20 -05:00
|
|
|
class << Object.new; self end.send(:define_method, :zzz, &block)
|
2004-09-07 01:32:26 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
v = eval("temporally_method_for_test_eval_and_define_method {}")
|
|
|
|
{}[0] = {}
|
|
|
|
v.call
|
|
|
|
}
|
|
|
|
end
|
2006-12-31 10:02:22 -05:00
|
|
|
|
2008-05-16 22:44:38 -04:00
|
|
|
def test_define_method_block
|
|
|
|
cc = Class.new do
|
|
|
|
define_method(:foo) {|&block|
|
|
|
|
block.call if block
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
c = cc.new
|
|
|
|
x = "ng"
|
|
|
|
c.foo() {x = "ok"}
|
|
|
|
assert_equal("ok", x)
|
|
|
|
end
|
|
|
|
|
2012-11-01 19:24:33 -04:00
|
|
|
def test_define_method_toplevel
|
|
|
|
feature6609 = '[ruby-core:45715]'
|
|
|
|
main = eval("self", TOPLEVEL_BINDING)
|
|
|
|
assert_nothing_raised(NoMethodError, feature6609) do
|
|
|
|
main.instance_eval do
|
|
|
|
define_method("feature6609_block") {feature6609}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
assert_equal(feature6609, feature6609_block)
|
|
|
|
|
|
|
|
assert_nothing_raised(NoMethodError, feature6609) do
|
|
|
|
main.instance_eval do
|
|
|
|
define_method("feature6609_method", Object.instance_method(:feature6609_block))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
assert_equal(feature6609, feature6609_method)
|
|
|
|
end
|
|
|
|
|
2008-05-14 08:52:17 -04:00
|
|
|
def test_eval_using_integer_as_binding
|
|
|
|
assert_raise(TypeError) { eval("", 1) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_eval_raise
|
|
|
|
assert_raise(RuntimeError) { eval("raise ''") }
|
|
|
|
end
|
|
|
|
|
2008-12-25 00:40:41 -05:00
|
|
|
def test_eval_with_toplevel_binding # [ruby-dev:37142]
|
|
|
|
ruby("-e", "x = 0; eval('p x', TOPLEVEL_BINDING)") do |f|
|
|
|
|
f.close_write
|
|
|
|
assert_equal("0", f.read.chomp)
|
|
|
|
end
|
|
|
|
end
|
2009-08-17 01:35:10 -04:00
|
|
|
|
|
|
|
def test_eval_ascii_incompatible
|
|
|
|
assert_raise(ArgumentError) {eval("__ENCODING__".encode("utf-16be"))}
|
|
|
|
assert_raise(ArgumentError) {eval("__ENCODING__".encode("utf-16le"))}
|
|
|
|
assert_raise(ArgumentError) {eval("__ENCODING__".encode("utf-32be"))}
|
|
|
|
assert_raise(ArgumentError) {eval("__ENCODING__".encode("utf-32le"))}
|
|
|
|
end
|
2010-09-22 20:01:40 -04:00
|
|
|
|
|
|
|
def test_instance_eval_method_proc
|
|
|
|
bug3860 = Class.new do
|
|
|
|
def initialize(a);
|
|
|
|
@a=a
|
|
|
|
end
|
|
|
|
def get(*args)
|
|
|
|
@a
|
|
|
|
end
|
|
|
|
end
|
|
|
|
foo = bug3860.new 1
|
|
|
|
foo_pr = foo.method(:get).to_proc
|
|
|
|
result = foo.instance_eval(&foo_pr)
|
|
|
|
assert_equal(1, result, 'Bug #3786, Bug #3860, [ruby-core:32501]')
|
|
|
|
end
|
2013-07-29 04:00:34 -04:00
|
|
|
|
|
|
|
def test_file_encoding
|
|
|
|
fname = "\u{3042}".encode("euc-jp")
|
|
|
|
assert_equal(fname, eval("__FILE__", nil, fname, 1))
|
|
|
|
end
|
2013-11-27 00:39:38 -05:00
|
|
|
|
|
|
|
def test_eval_location_fstring
|
|
|
|
o = Object.new
|
|
|
|
o.instance_eval "def foo() end", "generated code"
|
|
|
|
o.instance_eval "def bar() end", "generated code"
|
|
|
|
|
|
|
|
a, b = o.method(:foo).source_location[0],
|
|
|
|
o.method(:bar).source_location[0]
|
|
|
|
|
2013-11-27 01:03:37 -05:00
|
|
|
assert_same a, b
|
2013-11-27 00:39:38 -05:00
|
|
|
end
|
2014-10-18 07:46:31 -04:00
|
|
|
|
2016-11-14 00:17:55 -05:00
|
|
|
def test_fstring_instance_eval
|
|
|
|
bug = "[ruby-core:78116] [Bug #12930]".freeze
|
|
|
|
assert_same bug, (bug.instance_eval {self})
|
|
|
|
assert_raise(RuntimeError) {
|
|
|
|
bug.instance_eval {@ivar = true}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2014-10-18 07:46:31 -04:00
|
|
|
def test_gced_binding_block
|
|
|
|
assert_normal_exit %q{
|
|
|
|
def m
|
|
|
|
binding
|
|
|
|
end
|
|
|
|
GC.stress = true
|
|
|
|
b = nil
|
|
|
|
tap do
|
|
|
|
b = m {}
|
|
|
|
end
|
|
|
|
0.times.to_a
|
|
|
|
b.eval('yield')
|
|
|
|
}, '[Bug #10368]'
|
|
|
|
end
|
2017-06-27 02:57:34 -04:00
|
|
|
|
|
|
|
def test_return_in_eval_proc
|
|
|
|
x = proc {eval("return :ng")}
|
|
|
|
assert_raise(LocalJumpError) {x.call}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_return_in_eval_lambda
|
|
|
|
x = lambda {eval("return :ok")}
|
|
|
|
assert_equal(:ok, x.call)
|
|
|
|
end
|
2003-09-04 12:18:59 -04:00
|
|
|
end
|