summaryrefslogtreecommitdiff
path: root/spec/ruby/core/binding/local_variable_set_spec.rb
blob: 067dcbd03edb0fea3557da8ffeb80c0a57f462dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)

describe "Binding#local_variable_set" do
  it "adds nonexistent variables to the binding's eval scope" do
    obj = BindingSpecs::Demo.new(1)
    bind = obj.get_empty_binding
    bind.eval('local_variables').should == []
    bind.local_variable_set :foo, 1
    bind.eval('local_variables').should == [:foo]
    bind.eval('foo').should == 1
  end

  it 'sets a new local variable' do
    bind = binding

    bind.local_variable_set(:number, 10)
    bind.local_variable_get(:number).should == 10
  end

  it 'sets a local variable using a String as the variable name' do
    bind = binding

    bind.local_variable_set('number', 10)
    bind.local_variable_get('number').should == 10
  end

  it 'sets a local variable using an object responding to #to_str as the variable name' do
    bind = binding
    name = mock(:obj)
    name.stub!(:to_str).and_return('number')

    bind.local_variable_set(name, 10)
    bind.local_variable_get(name).should == 10
  end

  it 'scopes new local variables to the receiving Binding' do
    bind = binding
    bind.local_variable_set(:number, 10)

    lambda { number }.should raise_error(NameError)
  end

  it 'overwrites an existing local variable defined before a Binding' do
    number = 10
    bind = binding

    bind.local_variable_set(:number, 20)
    number.should == 20
  end

  it 'overwrites a local variable defined using eval()' do
    bind = binding
    bind.eval('number = 10')

    bind.local_variable_set(:number, 20)
    bind.local_variable_get(:number).should == 20
  end

  it "raises a NameError on global access" do
    bind = binding
    lambda { bind.local_variable_set(:$0, "") }.should raise_error(NameError)
  end

  it "raises a NameError on special variable access" do
    bind = binding
    lambda { bind.local_variable_set(:$~, "") }.should raise_error(NameError)
    lambda { bind.local_variable_set(:$_, "") }.should raise_error(NameError)
  end

end