aboutsummaryrefslogtreecommitdiff
path: root/ruby/tests/basic.rb
diff options
context:
space:
mode:
authorAdam Greene <adam.greene@gmail.com>2015-05-01 11:54:29 -0700
committerAdam Greene <adam.greene@gmail.com>2015-05-01 22:50:57 -0700
commitd55733c76ee1db702529f38f602548ffe48a4ab1 (patch)
treec40fc1f7c0c249c5d66fc6429222414cb5fcdf94 /ruby/tests/basic.rb
parentc70b6058eaae4fa5b1af577c548e6809a53dfd98 (diff)
downloadprotobuf-d55733c76ee1db702529f38f602548ffe48a4ab1.tar.gz
protobuf-d55733c76ee1db702529f38f602548ffe48a4ab1.tar.bz2
protobuf-d55733c76ee1db702529f38f602548ffe48a4ab1.zip
return nil if array index indicie is out of bounds
ruby arrays don't throw an exception; they return nil. Lets do the same! this fix also includes the ability to use negative array indicies
Diffstat (limited to 'ruby/tests/basic.rb')
-rw-r--r--ruby/tests/basic.rb43
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