2003-09-04 12:18:59 -04:00
|
|
|
require 'test/unit'
|
|
|
|
|
|
|
|
class TestEval < Test::Unit::TestCase
|
|
|
|
# eval with binding
|
|
|
|
def test_ev
|
|
|
|
local1 = "local1"
|
|
|
|
lambda {
|
|
|
|
local2 = "local2"
|
|
|
|
return binding
|
|
|
|
}.call
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_eval
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_nil(eval(""))
|
2003-09-04 12:18:59 -04:00
|
|
|
$bad=false
|
|
|
|
eval 'while false; $bad = true; print "foo\n" end'
|
|
|
|
assert(!$bad)
|
2003-09-05 11:15:43 -04:00
|
|
|
|
2003-09-04 12:18:59 -04:00
|
|
|
assert(eval('TRUE'))
|
|
|
|
assert(eval('true'))
|
|
|
|
assert(!eval('NIL'))
|
|
|
|
assert(!eval('nil'))
|
|
|
|
assert(!eval('FALSE'))
|
|
|
|
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"))
|
2003-09-04 12:18:59 -04:00
|
|
|
i = 5
|
|
|
|
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
|
|
|
|
2003-09-04 12:18:59 -04:00
|
|
|
$x = test_ev
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_equal("local1", eval("local1", $x)) # normal local var
|
|
|
|
assert_equal("local2", eval("local2", $x)) # nested local var
|
2003-09-04 12:18:59 -04:00
|
|
|
$bad = true
|
|
|
|
begin
|
|
|
|
p eval("local1")
|
|
|
|
rescue NameError # must raise error
|
|
|
|
$bad = false
|
|
|
|
end
|
|
|
|
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
|
|
|
|
self.class.class_eval %q(
|
|
|
|
module EvTest
|
|
|
|
EVTEST1 = 25
|
|
|
|
evtest2 = 125
|
|
|
|
$x = binding
|
|
|
|
end
|
|
|
|
)
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_equal(25, eval("EVTEST1", $x)) # constant in module
|
|
|
|
assert_equal(125, eval("evtest2", $x)) # local var in module
|
2003-09-04 12:18:59 -04:00
|
|
|
$bad = true
|
|
|
|
begin
|
|
|
|
eval("EVTEST1")
|
|
|
|
rescue NameError # must raise error
|
|
|
|
$bad = false
|
|
|
|
end
|
|
|
|
assert(!$bad)
|
2003-09-05 11:15:43 -04:00
|
|
|
|
2003-09-04 12:18:59 -04:00
|
|
|
x = proc{}
|
|
|
|
eval "i4 = 1", x
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_equal(1, eval("i4", x))
|
2003-09-04 12:18:59 -04:00
|
|
|
x = proc{proc{}}.call
|
|
|
|
eval "i4 = 22", x
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_equal(22, eval("i4", x))
|
2003-09-04 12:18:59 -04:00
|
|
|
$x = []
|
|
|
|
x = proc{proc{}}.call
|
|
|
|
eval "(0..9).each{|i5| $x[i5] = proc{i5*2}}", x
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_equal(8, $x[4].call)
|
|
|
|
|
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))
|
2003-09-04 12:18:59 -04:00
|
|
|
$x = []
|
|
|
|
x = proc{binding}.call
|
|
|
|
eval "(0..9).each{|i5| $x[i5] = proc{i5*2}}", x
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_equal(8, $x[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
|
|
|
|
proc{foo11=22}.call
|
|
|
|
proc{foo22=55}.call
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_equal(eval("foo11"), eval("foo11", p))
|
|
|
|
assert_equal(1, eval("foo11"))
|
|
|
|
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
|
|
|
|
2003-09-04 12:18:59 -04:00
|
|
|
p1 = proc{i7 = 0; proc{i7}}.call
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_equal(0, p1.call)
|
2003-09-04 12:18:59 -04:00
|
|
|
eval "i7=5", p1
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_equal(5, p1.call)
|
2003-09-04 12:18:59 -04:00
|
|
|
assert(!defined?(i7))
|
2003-09-05 11:15:43 -04:00
|
|
|
|
2003-09-04 12:18:59 -04:00
|
|
|
p1 = proc{i7 = 0; proc{i7}}.call
|
|
|
|
i7 = nil
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_equal(0, p1.call)
|
2003-09-04 12:18:59 -04:00
|
|
|
eval "i7=1", p1
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_equal(1, p1.call)
|
2003-09-04 12:18:59 -04:00
|
|
|
eval "i7=5", p1
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_equal(5, p1.call)
|
|
|
|
assert_nil(i7)
|
2003-09-04 12:18:59 -04:00
|
|
|
end
|
|
|
|
end
|