diff options
author | Chris Fallin <cfallin@c1f.net> | 2015-05-02 19:00:44 -0700 |
---|---|---|
committer | Chris Fallin <cfallin@c1f.net> | 2015-05-02 19:00:44 -0700 |
commit | dcf12136835af5239f0877aa455eabbd7b88b61e (patch) | |
tree | c40fc1f7c0c249c5d66fc6429222414cb5fcdf94 /ruby/tests/basic.rb | |
parent | 4b2a6328bd2fe6a1d58bbe536c512c566a2673d3 (diff) | |
parent | d55733c76ee1db702529f38f602548ffe48a4ab1 (diff) | |
download | protobuf-dcf12136835af5239f0877aa455eabbd7b88b61e.tar.gz protobuf-dcf12136835af5239f0877aa455eabbd7b88b61e.tar.bz2 protobuf-dcf12136835af5239f0877aa455eabbd7b88b61e.zip |
Merge pull request #332 from skippy/nil-out-of-bounds
ruby: return nil for out of bounds
Diffstat (limited to 'ruby/tests/basic.rb')
-rw-r--r-- | ruby/tests/basic.rb | 43 |
1 files changed, 39 insertions, 4 deletions
diff --git a/ruby/tests/basic.rb b/ruby/tests/basic.rb index 307374e3..141ce7c5 100644 --- a/ruby/tests/basic.rb +++ b/ruby/tests/basic.rb @@ -314,6 +314,17 @@ module BasicTest assert l4 == [0, 0, 0, 0, 0, 42, 100, 101, 102] end + def test_parent_rptfield + #make sure we set the RepeatedField and can add to it + m = TestMessage.new + assert m.repeated_string == [] + m.repeated_string << 'ok' + m.repeated_string.push('ok2') + assert m.repeated_string == ['ok', 'ok2'] + m.repeated_string += ['ok3'] + assert m.repeated_string == ['ok', 'ok2', 'ok3'] + end + def test_rptfield_msg l = Google::Protobuf::RepeatedField.new(:message, TestMessage) l.push TestMessage.new @@ -383,10 +394,31 @@ module BasicTest length_methods.each do |lm| assert l.send(lm) == 0 end + # out of bounds returns a nil + assert l[0] == nil + assert l[1] == nil + assert l[-1] == nil l.push 4 length_methods.each do |lm| - assert l.send(lm) == 1 + assert l.send(lm) == 1 + end + assert l[0] == 4 + assert l[1] == nil + assert l[-1] == 4 + assert l[-2] == nil + + l.push 2 + length_methods.each do |lm| + assert l.send(lm) == 2 end + assert l[0] == 4 + assert l[1] == 2 + assert l[2] == nil + assert l[-1] == 2 + assert l[-2] == 4 + assert l[-3] == nil + + #adding out of scope will backfill with empty objects end def test_map_basic @@ -724,9 +756,12 @@ module BasicTest m = TestMessage.new m.optional_string = "hello" m.optional_int32 = 42 - m.repeated_msg.push TestMessage2.new(:foo => 100) - m.repeated_msg.push TestMessage2.new(:foo => 200) - + tm1 = TestMessage2.new(:foo => 100) + tm2 = TestMessage2.new(:foo => 200) + m.repeated_msg.push tm1 + assert m.repeated_msg[-1] == tm1 + m.repeated_msg.push tm2 + assert m.repeated_msg[-1] == tm2 m2 = m.dup assert m == m2 m.optional_int32 += 1 |