File: instance_variable_get_spec.rb

package info (click to toggle)
jruby 1.7.26-1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 84,572 kB
  • sloc: ruby: 669,910; java: 253,056; xml: 35,152; ansic: 9,187; yacc: 7,267; cpp: 5,244; sh: 1,036; makefile: 345; jsp: 48; tcl: 40
file content (128 lines) | stat: -rw-r--r-- 4,423 bytes parent folder | download
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)

describe "Kernel#instance_variable_get" do
  before(:each) do
    @obj = Object.new
    @obj.instance_variable_set("@test", :test)
  end

  it "tries to convert the passed argument to a String using #to_str" do
    obj = mock("to_str")
    obj.should_receive(:to_str).and_return("@test")
    @obj.instance_variable_get(obj)
  end

  it "returns the value of the passed instance variable that is referred to by the conversion result" do
    obj = mock("to_str")
    obj.stub!(:to_str).and_return("@test")
    @obj.instance_variable_get(obj).should == :test
  end

  it "returns nil when the referred instance variable does not exist" do
    @obj.instance_variable_get(:@does_not_exist).should be_nil
  end

  it "raises a TypeError when the passed argument does not respond to #to_str" do
    lambda { @obj.instance_variable_get(Object.new) }.should raise_error(TypeError)
  end

  it "raises a TypeError when the passed argument can't be converted to a String" do
    obj = mock("to_str")
    obj.stub!(:to_str).and_return(123)
    lambda { @obj.instance_variable_get(obj) }.should raise_error(TypeError)
  end

  it "raises a NameError when the conversion result does not start with an '@'" do
    obj = mock("to_str")
    obj.stub!(:to_str).and_return("test")
    lambda { @obj.instance_variable_get(obj) }.should raise_error(NameError)
  end

  it "returns nil when passed just '@'" do
    obj = mock("to_str")
    obj.stub!(:to_str).and_return('@')
    @obj.instance_variable_get(obj).should be_nil
  end
end

describe "Kernel#instance_variable_get when passed Symbol" do
  before(:each) do
    @obj = Object.new
    @obj.instance_variable_set("@test", :test)
  end

  it "returns the value of the instance variable that is referred to by the passed Symbol" do
    @obj.instance_variable_get(:@test).should == :test
  end

  it "raises a NameError when the passed Symbol does not start with an '@'" do
    lambda { @obj.instance_variable_get(:test) }.should raise_error(NameError)
  end

  it "returns nil when passed just '@'" do
    @obj.instance_variable_get(:"@").should be_nil
  end
end

describe "Kernel#instance_variable_get when passed String" do
  before(:each) do
    @obj = Object.new
    @obj.instance_variable_set("@test", :test)
  end

  it "returns the value of the instance variable that is referred to by the passed String" do
    @obj.instance_variable_get("@test").should == :test
  end

  it "raises a NameError when the passed String does not start with an '@'" do
    lambda { @obj.instance_variable_get("test") }.should raise_error(NameError)
  end

  it "returns nil when passed just '@'" do
    @obj.instance_variable_get("@").should be_nil
  end
end

describe "Kernel#instance_variable_get when passed Fixnum" do
  before(:each) do
    @obj = Object.new
    @obj.instance_variable_set("@test", :test)
  end

  ruby_version_is "" ... "1.9" do
    deviates_on :rubinius do
      it "always raises an ArgumentError" do
        lambda { @obj.instance_variable_get(0) }.should raise_error(ArgumentError)
        lambda { @obj.instance_variable_get(10) }.should raise_error(ArgumentError)
        lambda { @obj.instance_variable_get(100) }.should raise_error(ArgumentError)
        lambda { @obj.instance_variable_get(-100) }.should raise_error(ArgumentError)
      end
    end

    not_compliant_on :rubinius do
      it "tries to convert the passed Integer to a Symbol and returns the instance variable that is referred by the Symbol" do
        @obj.instance_variable_get(:@test.to_i).should == :test
      end

      it "outputs a warning" do
        lambda { @obj.instance_variable_get(:@test.to_i) }.should complain(/#{"do not use Fixnums as Symbols"}/)
      end

      it "raises an ArgumentError when the passed Fixnum can't be converted to a Symbol" do
        lambda { @obj.instance_variable_get(-10) }.should raise_error(ArgumentError)
      end

      it "raises a NameError when the Symbol does not start with an '@'" do
        lambda { @obj.instance_variable_get(:test.to_i) }.should raise_error(NameError)
      end
    end
  end

  ruby_version_is "1.9" do
    it "raises a TypeError" do
      lambda { @obj.instance_variable_get(10) }.should raise_error(TypeError)
      lambda { @obj.instance_variable_get(-10) }.should raise_error(TypeError)
    end
  end
end