In Ruby repeated fields, each_index actually iterates over the index, not the values

pull/11767/head
Charles OuGuo 2 years ago
parent a138e54582
commit 874916c21d
  1. 14
      ruby/compatibility_tests/v3.0.0/tests/repeated_field_test.rb
  2. 3
      ruby/lib/google/protobuf/repeated_field.rb
  3. 14
      ruby/tests/repeated_field_test.rb

@ -119,6 +119,20 @@ class RepeatedFieldTest < Test::Unit::TestCase
assert_equal ['string'] * 5, result assert_equal ['string'] * 5, result
end end
def test_each_index
m = TestMessage.new
5.times{|i| m.repeated_string << 'string' }
expected = 0
m.repeated_string.each_index do |idx|
assert_equal expected, idx
expected += 1
assert_equal 'string', m.repeated_string[idx]
end
assert_equal 5, expected
end
def test_empty? def test_empty?
m = TestMessage.new m = TestMessage.new

@ -117,7 +117,6 @@ module Google
end end
# array aliases into enumerable # array aliases into enumerable
alias_method :each_index, :each_with_index
alias_method :slice, :[] alias_method :slice, :[]
alias_method :values_at, :select alias_method :values_at, :select
alias_method :map, :collect alias_method :map, :collect
@ -168,7 +167,7 @@ module Google
end end
%w(collect! compact! delete_if fill flatten! insert reverse! %w(collect! compact! delete_if each_index fill flatten! insert reverse!
rotate! select! shuffle! sort! sort_by! uniq!).each do |method_name| rotate! select! shuffle! sort! sort_by! uniq!).each do |method_name|
define_array_wrapper_with_result_method(method_name) define_array_wrapper_with_result_method(method_name)
end end

@ -141,6 +141,20 @@ class RepeatedFieldTest < Test::Unit::TestCase
end end
def test_each_index
m = TestMessage.new
5.times{|i| m.repeated_string << 'string' }
expected = 0
m.repeated_string.each_index do |idx|
assert_equal expected, idx
expected += 1
assert_equal 'string', m.repeated_string[idx]
end
assert_equal 5, expected
end
def test_empty? def test_empty?
m = TestMessage.new m = TestMessage.new
assert_equal true, m.repeated_string.empty? assert_equal true, m.repeated_string.empty?

Loading…
Cancel
Save