File: attr_writer_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 (82 lines) | stat: -rw-r--r-- 2,439 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
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)

describe "Module#attr_writer" do
  it "creates a setter for each given attribute name" do
    c = Class.new do
      attr_writer :test1, "test2"
    end
    o = c.new

    o.respond_to?("test1").should == false
    o.respond_to?("test2").should == false

    o.respond_to?("test1=").should == true
    o.test1 = "test_1"
    o.instance_variable_get(:@test1).should == "test_1"

    o.respond_to?("test2=").should == true
    o.test2 = "test_2"
    o.instance_variable_get(:@test2).should == "test_2"
    o.send(:test1=,"test_1 updated")
    o.instance_variable_get(:@test1).should == "test_1 updated"
    o.send(:test2=,"test_2 updated")
    o.instance_variable_get(:@test2).should == "test_2 updated"
  end

  it "allows for adding an attr_writer to an immediate" do
    class TrueClass
      attr_writer :spec_attr_writer
    end

    true.spec_attr_writer = "a"
    true.instance_variable_get("@spec_attr_writer").should == "a"
  end

  ruby_version_is ""..."1.9" do
    not_compliant_on :rubinius do
      it "creates a setter for an attribute name given as a Fixnum" do
        c = Class.new do
          attr_writer :test1.to_i
        end

        o = c.new
        o.respond_to?("test1").should == false
        o.respond_to?("test1=").should == true

        o.test1 = "test_1"
        o.instance_variable_get(:@test1).should == "test_1"
      end
    end
  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_writer o
    end

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

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

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

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

  it "is a private method" do
    lambda { Class.new.attr_writer(:foo) }.should raise_error(NoMethodError)
  end
end