1require_relative '../../spec_helper'
2require_relative 'fixtures/classes'
3
4describe "Kernel#instance_variable_get" do
5  before :each do
6    @obj = Object.new
7    @obj.instance_variable_set("@test", :test)
8  end
9
10  it "tries to convert the passed argument to a String using #to_str" do
11    obj = mock("to_str")
12    obj.should_receive(:to_str).and_return("@test")
13    @obj.instance_variable_get(obj)
14  end
15
16  it "returns the value of the passed instance variable that is referred to by the conversion result" do
17    obj = mock("to_str")
18    obj.stub!(:to_str).and_return("@test")
19    @obj.instance_variable_get(obj).should == :test
20  end
21
22  it "returns nil when the referred instance variable does not exist" do
23    @obj.instance_variable_get(:@does_not_exist).should be_nil
24  end
25
26  it "raises a TypeError when the passed argument does not respond to #to_str" do
27    lambda { @obj.instance_variable_get(Object.new) }.should raise_error(TypeError)
28  end
29
30  it "raises a TypeError when the passed argument can't be converted to a String" do
31    obj = mock("to_str")
32    obj.stub!(:to_str).and_return(123)
33    lambda { @obj.instance_variable_get(obj) }.should raise_error(TypeError)
34  end
35
36  it "raises a NameError when the conversion result does not start with an '@'" do
37    obj = mock("to_str")
38    obj.stub!(:to_str).and_return("test")
39    lambda { @obj.instance_variable_get(obj) }.should raise_error(NameError)
40  end
41
42  it "raises a NameError when passed just '@'" do
43    obj = mock("to_str")
44    obj.stub!(:to_str).and_return('@')
45    lambda { @obj.instance_variable_get(obj) }.should raise_error(NameError)
46  end
47end
48
49describe "Kernel#instance_variable_get when passed Symbol" do
50  before :each do
51    @obj = Object.new
52    @obj.instance_variable_set("@test", :test)
53  end
54
55  it "returns the value of the instance variable that is referred to by the passed Symbol" do
56    @obj.instance_variable_get(:@test).should == :test
57  end
58
59  it "raises a NameError when passed :@ as an instance variable name" do
60    lambda { @obj.instance_variable_get(:"@") }.should raise_error(NameError)
61  end
62
63  it "raises a NameError when the passed Symbol does not start with an '@'" do
64    lambda { @obj.instance_variable_get(:test) }.should raise_error(NameError)
65  end
66
67  it "raises a NameError when the passed Symbol is an invalid instance variable name" do
68    lambda { @obj.instance_variable_get(:"@0") }.should raise_error(NameError)
69  end
70end
71
72describe "Kernel#instance_variable_get when passed String" do
73  before :each do
74    @obj = Object.new
75    @obj.instance_variable_set("@test", :test)
76  end
77
78  it "returns the value of the instance variable that is referred to by the passed String" do
79    @obj.instance_variable_get("@test").should == :test
80  end
81
82  it "raises a NameError when the passed String does not start with an '@'" do
83    lambda { @obj.instance_variable_get("test") }.should raise_error(NameError)
84  end
85
86  it "raises a NameError when the passed String is an invalid instance variable name" do
87    lambda { @obj.instance_variable_get("@0") }.should raise_error(NameError)
88  end
89
90  it "raises a NameError when passed '@' as an instance variable name" do
91    lambda { @obj.instance_variable_get("@") }.should raise_error(NameError)
92  end
93end
94
95describe "Kernel#instance_variable_get when passed Fixnum" do
96  before :each do
97    @obj = Object.new
98    @obj.instance_variable_set("@test", :test)
99  end
100
101  it "raises a TypeError" do
102    lambda { @obj.instance_variable_get(10) }.should raise_error(TypeError)
103    lambda { @obj.instance_variable_get(-10) }.should raise_error(TypeError)
104  end
105end
106