| 1 |
assert("Kernel.#binding") do |
| 2 |
assert_kind_of Binding, binding |
| 3 |
end |
| 4 |
|
| 5 |
assert("Binding#local_variables") do |
| 6 |
block = Proc.new do |a| |
| 7 |
b = 1 |
| 8 |
binding |
| 9 |
end |
| 10 |
assert_equal [:a, :b, :block], block.call(0).local_variables.sort |
| 11 |
end |
| 12 |
|
| 13 |
assert("Binding#local_variable_set") do |
| 14 |
bind = binding |
| 15 |
1.times { |
| 16 |
assert_equal(9, bind.local_variable_set(:x, 9)) |
| 17 |
assert_raise(NameError) { x } |
| 18 |
assert_equal([:bind, :x], bind.local_variables.sort) |
| 19 |
} |
| 20 |
end |
| 21 |
|
| 22 |
assert("Binding#local_variable_get") do |
| 23 |
bind = binding |
| 24 |
x = 1 |
| 25 |
1.times { |
| 26 |
y = 2 |
| 27 |
assert_equal(1, bind.local_variable_get(:x)) |
| 28 |
x = 10 |
| 29 |
assert_equal(10, bind.local_variable_get(:x)) |
| 30 |
assert_raise(NameError) { bind.local_variable_get(:y) } |
| 31 |
assert_equal([:bind, :x], bind.local_variables.sort) |
| 32 |
} |
| 33 |
end |
| 34 |
|
| 35 |
assert("Binding#source_location") do |
| 36 |
skip unless -> {}.source_location |
| 37 |
|
| 38 |
bind, source_location = binding, [__FILE__, __LINE__] |
| 39 |
assert_equal source_location, bind.source_location |
| 40 |
end |
| 41 |
|
| 42 |
assert("Binding#dup") do |
| 43 |
x = 5 |
| 44 |
bind1 = binding |
| 45 |
bind1.local_variable_set(:y, 10) |
| 46 |
bind2 = bind1.dup |
| 47 |
assert_equal 5, bind2.local_variable_get(:x) |
| 48 |
assert_equal 10, bind2.local_variable_get(:y) |
| 49 |
x = 50 |
| 50 |
assert_equal 50, bind1.local_variable_get(:x) |
| 51 |
assert_equal 50, bind2.local_variable_get(:x) |
| 52 |
bind1.local_variable_set(:y, 20) |
| 53 |
assert_equal 20, bind1.local_variable_get(:y) |
| 54 |
assert_equal 20, bind2.local_variable_get(:y) |
| 55 |
bind1.local_variable_set(:z, 30) |
| 56 |
assert_raise(NameError) { bind2.local_variable_get(:z) } |
| 57 |
bind2.local_variable_set(:z, 40) |
| 58 |
assert_equal 30, bind1.local_variable_get(:z) |
| 59 |
assert_equal 40, bind2.local_variable_get(:z) |
| 60 |
end |
| 61 |
|
| 62 |
assert "Kernel#binding and .eval from C" do |
| 63 |
assert_raise(RuntimeError) { binding_in_c } |
| 64 |
end |