| 1 |
assert("Binding#eval") do |
| 2 |
b = nil |
| 3 |
1.times { x, y, z = 1, 2, 3; [x,y,z]; b = binding } |
| 4 |
assert_equal([1, 2, 3], b.eval("[x, y, z]")) |
| 5 |
here = self |
| 6 |
assert_equal(here, b.eval("self")) |
| 7 |
end |
| 8 |
|
| 9 |
assert("Binding#local_variables") do |
| 10 |
block = Proc.new do |a| |
| 11 |
b = 1 |
| 12 |
binding |
| 13 |
end |
| 14 |
bind = block.call(0) |
| 15 |
assert_equal [:a, :b, :bind, :block], bind.local_variables.sort |
| 16 |
bind.eval("x = 2") |
| 17 |
assert_equal [:a, :b, :bind, :block, :x], bind.local_variables.sort |
| 18 |
end |
| 19 |
|
| 20 |
assert("Binding#local_variable_set") do |
| 21 |
bind = binding |
| 22 |
1.times { |
| 23 |
assert_equal(9, bind.local_variable_set(:x, 9)) |
| 24 |
assert_equal(9, bind.eval("x")) |
| 25 |
assert_equal([:bind, :x], bind.eval("local_variables.sort")) |
| 26 |
} |
| 27 |
end |
| 28 |
|
| 29 |
assert("Binding#local_variable_get") do |
| 30 |
bind = binding |
| 31 |
x = 1 |
| 32 |
1.times { |
| 33 |
y = 2 |
| 34 |
assert_equal(1, bind.local_variable_get(:x)) |
| 35 |
x = 10 |
| 36 |
assert_equal(10, bind.local_variable_get(:x)) |
| 37 |
assert_raise(NameError) { bind.local_variable_get(:y) } |
| 38 |
bind.eval("z = 3") |
| 39 |
assert_equal(3, bind.local_variable_get(:z)) |
| 40 |
bind.eval("y = 5") |
| 41 |
assert_equal(5, bind.local_variable_get(:y)) |
| 42 |
assert_equal(2, y) |
| 43 |
} |
| 44 |
end |
| 45 |
|
| 46 |
assert "Binding#eval with Binding.new via UnboundMethod" do |
| 47 |
assert_raise(NoMethodError) { Class.instance_method(:new).bind_call(Binding) } |
| 48 |
end |
| 49 |
|
| 50 |
assert "Binding#eval with Binding.new via Method" do |
| 51 |
# The following test is OK if SIGSEGV does not occur |
| 52 |
cx = Class.new(Binding) |
| 53 |
cx.define_singleton_method(:allocate, &Object.method(:allocate)) |
| 54 |
Class.instance_method(:new).bind_call(cx).eval("") |
| 55 |
|
| 56 |
assert_true true |
| 57 |
end |
| 58 |
|
| 59 |
assert "access local variables into procs" do |
| 60 |
bx = binding |
| 61 |
block = bx.eval("a = 1; proc { a }") |
| 62 |
bx.eval("a = 2") |
| 63 |
assert_equal 2, block.call |
| 64 |
end |