summaryrefslogtreecommitdiff
path: root/spec/ruby/core/module/attr_reader_spec.rb
blob: 65cafdba9f9d058ba9fa588aed66a200cdf32d24 (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 "Module#attr_reader" do
  it "creates a getter for each given attribute name" do
    c = Class.new do
      attr_reader :a, "b"

      def initialize
        @a = "test"
        @b = "test2"
      end
    end

    o = c.new
    %w{a b}.each do |x|
      o.respond_to?(x).should == true
      o.respond_to?("#{x}=").should == false
    end

    o.a.should == "test"
    o.b.should == "test2"
    o.send(:a).should == "test"
    o.send(:b).should == "test2"
  end

  it "not allows for adding an attr_reader to an immediate" do
    class TrueClass
      attr_reader :spec_attr_reader
    end

    lambda { true.instance_variable_set("@spec_attr_reader", "a") }.should raise_error(RuntimeError)
  end

  it "converts non string/symbol/fixnum names to strings using to_str" do
    (o = mock('test')).should_receive(:to_str).any_number_of_times.and_return("test")
    c = Class.new do
      attr_reader o
    end

    c.new.respond_to?("test").should == true
    c.new.respond_to?("test=").should == false
  end

  it "raises a TypeError when the given names can't be converted to strings using to_str" do
    o = mock('o')
    lambda { Class.new { attr_reader o } }.should raise_error(TypeError)
    (o = mock('123')).should_receive(:to_str).and_return(123)
    lambda { Class.new { attr_reader o } }.should raise_error(TypeError)
  end

  it "applies current visibility to methods created" do
    c = Class.new do
      protected
      attr_reader :foo
    end

    lambda { c.new.foo }.should raise_error(NoMethodError)
  end

  ruby_version_is ''...'2.5' do
    it "is a private method" do
      Module.should have_private_instance_method(:attr_reader, false)
    end
  end
  ruby_version_is '2.5' do
    it "is a public method" do
      Module.should have_public_instance_method(:attr_reader, false)
    end
  end
end