ostruct_square_bracket_equals.patch

Test case for []= and [], and fix for []= bug - Caius Durling, 12/21/2010 08:40 AM

Download (977 Bytes)

View differences:

lib/ostruct.rb
89 89
  def method_missing(mid, *args) # :nodoc:
90 90
    mname = mid.id2name
91 91
    len = args.length
92
    if mname.chomp!('=')
92
    if mname.chomp!('=') && mname != "[]"
93 93
      if len != 1
94 94
        raise ArgumentError, "wrong number of arguments (#{len} for 1)", caller(1)
95 95
      end
test/ostruct/test_ostruct.rb
63 63
    assert_raise(TypeError) {o.a = 'z'}
64 64
    assert_equal('a', o.a)
65 65
  end
66

  
67
  def test_method_missing_handles_square_bracket_equals
68
    o = OpenStruct.new
69
    assert_raise(NoMethodError) { o[:foo] = :bar }
70
  end
71

  
72
  def test_method_missing_handles_square_brackets
73
    o = OpenStruct.new
74
    assert_raise(NoMethodError) { o[:foo] }
75
  end
76

  
66 77
end