Add dependencies locally

This commit is contained in:
Ahrimdon
2024-03-07 05:13:50 -05:00
parent e4a5cd056e
commit 9a56906be7
9538 changed files with 3064916 additions and 107 deletions

739
deps/protobuf/ruby/tests/basic.rb vendored Normal file
View File

@ -0,0 +1,739 @@
#!/usr/bin/ruby
# basic_test_pb.rb is in the same directory as this test.
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)))
require 'basic_test_pb'
require 'common_tests'
require 'google/protobuf'
require 'json'
require 'test/unit'
# ------------- generated code --------------
module BasicTest
pool = Google::Protobuf::DescriptorPool.new
pool.build do
add_message "BadFieldNames" do
optional :dup, :int32, 1
optional :class, :int32, 2
end
end
BadFieldNames = pool.lookup("BadFieldNames").msgclass
# ------------ test cases ---------------
class MessageContainerTest < Test::Unit::TestCase
# Required by CommonTests module to resolve proto3 proto classes used in tests.
def proto_module
::BasicTest
end
include CommonTests
def test_issue_8311_crash
Google::Protobuf::DescriptorPool.generated_pool.build do
add_file("inner.proto", :syntax => :proto3) do
add_message "Inner" do
# Removing either of these fixes the segfault.
optional :foo, :string, 1
optional :bar, :string, 2
end
end
end
Google::Protobuf::DescriptorPool.generated_pool.build do
add_file("outer.proto", :syntax => :proto3) do
add_message "Outer" do
repeated :inners, :message, 1, "Inner"
end
end
end
outer = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("Outer").msgclass
outer.new(
inners: []
)['inners'].to_s
assert_raise Google::Protobuf::TypeError do
outer.new(
inners: [nil]
).to_s
end
end
def test_issue_8559_crash
msg = TestMessage.new
msg.repeated_int32 = ::Google::Protobuf::RepeatedField.new(:int32, [1, 2, 3])
# https://github.com/jruby/jruby/issues/6818 was fixed in JRuby 9.3.0.0
if cruby_or_jruby_9_3_or_higher?
GC.start(full_mark: true, immediate_sweep: true)
end
TestMessage.encode(msg)
end
def test_issue_9440
msg = HelloRequest.new
msg.id = 8
assert_equal 8, msg.id
msg.version = '1'
assert_equal 8, msg.id
end
def test_issue_9507
pool = Google::Protobuf::DescriptorPool.new
pool.build do
add_message "NpeMessage" do
optional :type, :enum, 1, "TestEnum"
optional :other, :string, 2
end
add_enum "TestEnum" do
value :Something, 0
end
end
msgclass = pool.lookup("NpeMessage").msgclass
m = msgclass.new(
other: "foo" # must be set, but can be blank
)
begin
encoded = msgclass.encode(m)
rescue java.lang.NullPointerException
flunk "NPE rescued"
end
decoded = msgclass.decode(encoded)
decoded.inspect
decoded.to_proto
end
def test_has_field
m = TestSingularFields.new
assert !m.has_singular_msg?
m.singular_msg = TestMessage2.new
assert m.has_singular_msg?
assert TestSingularFields.descriptor.lookup('singular_msg').has?(m)
m = OneofMessage.new
assert !m.has_my_oneof?
m.a = "foo"
assert m.has_my_oneof?
assert_raise NoMethodError do
m.has_a?
end
assert_true OneofMessage.descriptor.lookup('a').has?(m)
m = TestSingularFields.new
assert_raise NoMethodError do
m.has_singular_int32?
end
assert_raise ArgumentError do
TestSingularFields.descriptor.lookup('singular_int32').has?(m)
end
assert_raise NoMethodError do
m.has_singular_string?
end
assert_raise ArgumentError do
TestSingularFields.descriptor.lookup('singular_string').has?(m)
end
assert_raise NoMethodError do
m.has_singular_bool?
end
assert_raise ArgumentError do
TestSingularFields.descriptor.lookup('singular_bool').has?(m)
end
m = TestMessage.new
assert_raise NoMethodError do
m.has_repeated_msg?
end
assert_raise ArgumentError do
TestMessage.descriptor.lookup('repeated_msg').has?(m)
end
end
def test_no_presence
m = TestSingularFields.new
# Explicitly setting to zero does not cause anything to be serialized.
m.singular_int32 = 0
assert_equal "", TestSingularFields.encode(m)
# Explicitly setting to a non-zero value *does* cause serialization.
m.singular_int32 = 1
assert_not_equal "", TestSingularFields.encode(m)
m.singular_int32 = 0
assert_equal "", TestSingularFields.encode(m)
end
def test_set_clear_defaults
m = TestSingularFields.new
m.singular_int32 = -42
assert_equal( -42, m.singular_int32 )
m.clear_singular_int32
assert_equal 0, m.singular_int32
m.singular_int32 = 50
assert_equal 50, m.singular_int32
TestSingularFields.descriptor.lookup('singular_int32').clear(m)
assert_equal 0, m.singular_int32
m.singular_string = "foo bar"
assert_equal "foo bar", m.singular_string
m.clear_singular_string
assert_equal "", m.singular_string
m.singular_string = "foo"
assert_equal "foo", m.singular_string
TestSingularFields.descriptor.lookup('singular_string').clear(m)
assert_equal "", m.singular_string
m.singular_msg = TestMessage2.new(:foo => 42)
assert_equal TestMessage2.new(:foo => 42), m.singular_msg
assert m.has_singular_msg?
m.clear_singular_msg
assert_equal nil, m.singular_msg
assert !m.has_singular_msg?
m.singular_msg = TestMessage2.new(:foo => 42)
assert_equal TestMessage2.new(:foo => 42), m.singular_msg
TestSingularFields.descriptor.lookup('singular_msg').clear(m)
assert_equal nil, m.singular_msg
end
def test_import_proto2
m = TestMessage.new
assert !m.has_optional_proto2_submessage?
m.optional_proto2_submessage = ::FooBar::Proto2::TestImportedMessage.new
assert m.has_optional_proto2_submessage?
assert TestMessage.descriptor.lookup('optional_proto2_submessage').has?(m)
m.clear_optional_proto2_submessage
assert !m.has_optional_proto2_submessage?
end
def test_clear_repeated_fields
m = TestMessage.new
m.repeated_int32.push(1)
assert_equal [1], m.repeated_int32
m.clear_repeated_int32
assert_equal [], m.repeated_int32
m.repeated_int32.push(1)
assert_equal [1], m.repeated_int32
TestMessage.descriptor.lookup('repeated_int32').clear(m)
assert_equal [], m.repeated_int32
m = OneofMessage.new
m.a = "foo"
assert_equal "foo", m.a
assert m.has_my_oneof?
assert_equal :a, m.my_oneof
m.clear_a
assert !m.has_my_oneof?
m.a = "foobar"
assert m.has_my_oneof?
m.clear_my_oneof
assert !m.has_my_oneof?
m.a = "bar"
assert_equal "bar", m.a
assert m.has_my_oneof?
OneofMessage.descriptor.lookup('a').clear(m)
assert !m.has_my_oneof?
end
def test_initialization_map_errors
e = assert_raise ArgumentError do
TestMessage.new(:hello => "world")
end
assert_match(/hello/, e.message)
e = assert_raise ArgumentError do
MapMessage.new(:map_string_int32 => "hello")
end
assert_equal e.message, "Expected Hash object as initializer value for map field 'map_string_int32' (given String)."
e = assert_raise ArgumentError do
TestMessage.new(:repeated_uint32 => "hello")
end
assert_equal e.message, "Expected array as initializer value for repeated field 'repeated_uint32' (given String)."
end
def test_map_field
m = MapMessage.new
assert m.map_string_int32 == {}
assert m.map_string_msg == {}
m = MapMessage.new(
:map_string_int32 => {"a" => 1, "b" => 2},
:map_string_msg => {"a" => TestMessage2.new(:foo => 1),
"b" => TestMessage2.new(:foo => 2)},
:map_string_enum => {"a" => :A, "b" => :B})
assert m.map_string_int32.keys.sort == ["a", "b"]
assert m.map_string_int32["a"] == 1
assert m.map_string_msg["b"].foo == 2
assert m.map_string_enum["a"] == :A
m.map_string_int32["c"] = 3
assert m.map_string_int32["c"] == 3
m.map_string_msg["c"] = TestMessage2.new(:foo => 3)
assert m.map_string_msg["c"] == TestMessage2.new(:foo => 3)
m.map_string_msg.delete("b")
m.map_string_msg.delete("c")
assert m.map_string_msg == { "a" => TestMessage2.new(:foo => 1) }
assert_raise Google::Protobuf::TypeError do
m.map_string_msg["e"] = TestMessage.new # wrong value type
end
# ensure nothing was added by the above
assert m.map_string_msg == { "a" => TestMessage2.new(:foo => 1) }
m.map_string_int32 = Google::Protobuf::Map.new(:string, :int32)
assert_raise Google::Protobuf::TypeError do
m.map_string_int32 = Google::Protobuf::Map.new(:string, :int64)
end
assert_raise Google::Protobuf::TypeError do
m.map_string_int32 = {}
end
assert_raise Google::Protobuf::TypeError do
m = MapMessage.new(:map_string_int32 => { 1 => "I am not a number" })
end
end
def test_map_field_with_symbol
m = MapMessage.new
assert m.map_string_int32 == {}
assert m.map_string_msg == {}
m = MapMessage.new(
:map_string_int32 => {a: 1, "b" => 2},
:map_string_msg => {a: TestMessage2.new(:foo => 1),
b: TestMessage2.new(:foo => 10)})
assert_equal 1, m.map_string_int32[:a]
assert_equal 2, m.map_string_int32[:b]
assert_equal 10, m.map_string_msg[:b].foo
end
def test_map_inspect
m = MapMessage.new(
:map_string_int32 => {"a" => 1, "b" => 2},
:map_string_msg => {"a" => TestMessage2.new(:foo => 1),
"b" => TestMessage2.new(:foo => 2)},
:map_string_enum => {"a" => :A, "b" => :B})
# JRuby doesn't keep consistent ordering so check for either version
expected_a = "<BasicTest::MapMessage: map_string_int32: {\"b\"=>2, \"a\"=>1}, map_string_msg: {\"b\"=><BasicTest::TestMessage2: foo: 2>, \"a\"=><BasicTest::TestMessage2: foo: 1>}, map_string_enum: {\"b\"=>:B, \"a\"=>:A}>"
expected_b = "<BasicTest::MapMessage: map_string_int32: {\"a\"=>1, \"b\"=>2}, map_string_msg: {\"a\"=><BasicTest::TestMessage2: foo: 1>, \"b\"=><BasicTest::TestMessage2: foo: 2>}, map_string_enum: {\"a\"=>:A, \"b\"=>:B}>"
inspect_result = m.inspect
assert expected_a == inspect_result || expected_b == inspect_result, "Incorrect inspect result: #{inspect_result}"
end
def test_map_corruption
# This pattern led to a crash in a previous version of upb/protobuf.
m = MapMessage.new(map_string_int32: { "aaa" => 1 })
m.map_string_int32['podid'] = 2
m.map_string_int32['aaa'] = 3
end
def test_map_wrappers
run_asserts = ->(m) {
assert_equal 2.0, m.map_double[0].value
assert_equal 4.0, m.map_float[0].value
assert_equal 3, m.map_int32[0].value
assert_equal 4, m.map_int64[0].value
assert_equal 5, m.map_uint32[0].value
assert_equal 6, m.map_uint64[0].value
assert_equal true, m.map_bool[0].value
assert_equal 'str', m.map_string[0].value
assert_equal 'fun', m.map_bytes[0].value
}
m = proto_module::Wrapper.new(
map_double: {0 => Google::Protobuf::DoubleValue.new(value: 2.0)},
map_float: {0 => Google::Protobuf::FloatValue.new(value: 4.0)},
map_int32: {0 => Google::Protobuf::Int32Value.new(value: 3)},
map_int64: {0 => Google::Protobuf::Int64Value.new(value: 4)},
map_uint32: {0 => Google::Protobuf::UInt32Value.new(value: 5)},
map_uint64: {0 => Google::Protobuf::UInt64Value.new(value: 6)},
map_bool: {0 => Google::Protobuf::BoolValue.new(value: true)},
map_string: {0 => Google::Protobuf::StringValue.new(value: 'str')},
map_bytes: {0 => Google::Protobuf::BytesValue.new(value: 'fun')},
)
run_asserts.call(m)
serialized = proto_module::Wrapper::encode(m)
m2 = proto_module::Wrapper::decode(serialized)
run_asserts.call(m2)
# Test the case where we are serializing directly from the parsed form
# (before anything lazy is materialized).
m3 = proto_module::Wrapper::decode(serialized)
serialized2 = proto_module::Wrapper::encode(m3)
m4 = proto_module::Wrapper::decode(serialized2)
run_asserts.call(m4)
# Test that the lazy form compares equal to the expanded form.
m5 = proto_module::Wrapper::decode(serialized2)
assert_equal m5, m
end
def test_map_wrappers_with_default_values
run_asserts = ->(m) {
assert_equal 0.0, m.map_double[0].value
assert_equal 0.0, m.map_float[0].value
assert_equal 0, m.map_int32[0].value
assert_equal 0, m.map_int64[0].value
assert_equal 0, m.map_uint32[0].value
assert_equal 0, m.map_uint64[0].value
assert_equal false, m.map_bool[0].value
assert_equal '', m.map_string[0].value
assert_equal '', m.map_bytes[0].value
}
m = proto_module::Wrapper.new(
map_double: {0 => Google::Protobuf::DoubleValue.new(value: 0.0)},
map_float: {0 => Google::Protobuf::FloatValue.new(value: 0.0)},
map_int32: {0 => Google::Protobuf::Int32Value.new(value: 0)},
map_int64: {0 => Google::Protobuf::Int64Value.new(value: 0)},
map_uint32: {0 => Google::Protobuf::UInt32Value.new(value: 0)},
map_uint64: {0 => Google::Protobuf::UInt64Value.new(value: 0)},
map_bool: {0 => Google::Protobuf::BoolValue.new(value: false)},
map_string: {0 => Google::Protobuf::StringValue.new(value: '')},
map_bytes: {0 => Google::Protobuf::BytesValue.new(value: '')},
)
run_asserts.call(m)
serialized = proto_module::Wrapper::encode(m)
m2 = proto_module::Wrapper::decode(serialized)
run_asserts.call(m2)
# Test the case where we are serializing directly from the parsed form
# (before anything lazy is materialized).
m3 = proto_module::Wrapper::decode(serialized)
serialized2 = proto_module::Wrapper::encode(m3)
m4 = proto_module::Wrapper::decode(serialized2)
run_asserts.call(m4)
# Test that the lazy form compares equal to the expanded form.
m5 = proto_module::Wrapper::decode(serialized2)
assert_equal m5, m
end
def test_map_wrappers_with_no_value
run_asserts = ->(m) {
assert_equal 0.0, m.map_double[0].value
assert_equal 0.0, m.map_float[0].value
assert_equal 0, m.map_int32[0].value
assert_equal 0, m.map_int64[0].value
assert_equal 0, m.map_uint32[0].value
assert_equal 0, m.map_uint64[0].value
assert_equal false, m.map_bool[0].value
assert_equal '', m.map_string[0].value
assert_equal '', m.map_bytes[0].value
}
m = proto_module::Wrapper.new(
map_double: {0 => Google::Protobuf::DoubleValue.new()},
map_float: {0 => Google::Protobuf::FloatValue.new()},
map_int32: {0 => Google::Protobuf::Int32Value.new()},
map_int64: {0 => Google::Protobuf::Int64Value.new()},
map_uint32: {0 => Google::Protobuf::UInt32Value.new()},
map_uint64: {0 => Google::Protobuf::UInt64Value.new()},
map_bool: {0 => Google::Protobuf::BoolValue.new()},
map_string: {0 => Google::Protobuf::StringValue.new()},
map_bytes: {0 => Google::Protobuf::BytesValue.new()},
)
run_asserts.call(m)
serialized = proto_module::Wrapper::encode(m)
m2 = proto_module::Wrapper::decode(serialized)
run_asserts.call(m2)
# Test the case where we are serializing directly from the parsed form
# (before anything lazy is materialized).
m3 = proto_module::Wrapper::decode(serialized)
serialized2 = proto_module::Wrapper::encode(m3)
m4 = proto_module::Wrapper::decode(serialized2)
run_asserts.call(m4)
end
def test_concurrent_decoding
o = Outer.new
o.items[0] = Inner.new
raw = Outer.encode(o)
thds = 2.times.map do
Thread.new do
100000.times do
assert_equal o, Outer.decode(raw)
end
end
end
thds.map(&:join)
end
def test_map_encode_decode
m = MapMessage.new(
:map_string_int32 => {"a" => 1, "b" => 2},
:map_string_msg => {"a" => TestMessage2.new(:foo => 1),
"b" => TestMessage2.new(:foo => 2)},
:map_string_enum => {"a" => :A, "b" => :B})
m2 = MapMessage.decode(MapMessage.encode(m))
assert m == m2
m3 = MapMessageWireEquiv.decode(MapMessage.encode(m))
assert m3.map_string_int32.length == 2
kv = {}
m3.map_string_int32.map { |msg| kv[msg.key] = msg.value }
assert kv == {"a" => 1, "b" => 2}
kv = {}
m3.map_string_msg.map { |msg| kv[msg.key] = msg.value }
assert kv == {"a" => TestMessage2.new(:foo => 1),
"b" => TestMessage2.new(:foo => 2)}
end
def test_protobuf_decode_json_ignore_unknown_fields
m = TestMessage.decode_json({
optional_string: "foo",
not_in_message: "some_value"
}.to_json, { ignore_unknown_fields: true })
assert_equal m.optional_string, "foo"
e = assert_raise Google::Protobuf::ParseError do
TestMessage.decode_json({ not_in_message: "some_value" }.to_json)
end
assert_match(/No such field: not_in_message/, e.message)
end
#def test_json_quoted_string
# m = TestMessage.decode_json(%q(
# "optionalInt64": "1",,
# }))
# puts(m)
# assert_equal 1, m.optional_int32
#end
def test_to_h
m = TestMessage.new(:optional_bool => true, :optional_double => -10.100001, :optional_string => 'foo', :repeated_string => ['bar1', 'bar2'], :repeated_msg => [TestMessage2.new(:foo => 100)])
expected_result = {
:optional_bool=>true,
:optional_bytes=>"",
:optional_double=>-10.100001,
:optional_enum=>:Default,
:optional_float=>0.0,
:optional_int32=>0,
:optional_int64=>0,
:optional_msg=>nil,
:optional_msg2=>nil,
:optional_proto2_submessage=>nil,
:optional_string=>"foo",
:optional_uint32=>0,
:optional_uint64=>0,
:repeated_bool=>[],
:repeated_bytes=>[],
:repeated_double=>[],
:repeated_enum=>[],
:repeated_float=>[],
:repeated_int32=>[],
:repeated_int64=>[],
:repeated_msg=>[{:foo => 100}],
:repeated_string=>["bar1", "bar2"],
:repeated_uint32=>[],
:repeated_uint64=>[]
}
assert_equal expected_result, m.to_h
m = MapMessage.new(
:map_string_int32 => {"a" => 1, "b" => 2},
:map_string_msg => {"a" => TestMessage2.new(:foo => 1),
"b" => TestMessage2.new(:foo => 2)},
:map_string_enum => {"a" => :A, "b" => :B})
expected_result = {
:map_string_int32 => {"a" => 1, "b" => 2},
:map_string_msg => {"a" => {:foo => 1}, "b" => {:foo => 2}},
:map_string_enum => {"a" => :A, "b" => :B}
}
assert_equal expected_result, m.to_h
end
def test_json_maps
m = MapMessage.new(:map_string_int32 => {"a" => 1})
expected = {mapStringInt32: {a: 1}, mapStringMsg: {}, mapStringEnum: {}}
expected_preserve = {map_string_int32: {a: 1}, map_string_msg: {}, map_string_enum: {}}
assert_equal JSON.parse(MapMessage.encode_json(m, :emit_defaults=>true), :symbolize_names => true), expected
json = MapMessage.encode_json(m, :preserve_proto_fieldnames => true, :emit_defaults=>true)
assert_equal JSON.parse(json, :symbolize_names => true), expected_preserve
m2 = MapMessage.decode_json(MapMessage.encode_json(m))
assert_equal m, m2
end
def test_json_maps_emit_defaults_submsg
m = MapMessage.new(:map_string_msg => {"a" => TestMessage2.new(foo: 0)})
expected = {mapStringInt32: {}, mapStringMsg: {a: {foo: 0}}, mapStringEnum: {}}
actual = MapMessage.encode_json(m, :emit_defaults => true)
assert_equal JSON.parse(actual, :symbolize_names => true), expected
end
def test_json_emit_defaults_submsg
m = TestSingularFields.new(singular_msg: proto_module::TestMessage2.new)
expected = {
singularInt32: 0,
singularInt64: "0",
singularUint32: 0,
singularUint64: "0",
singularBool: false,
singularFloat: 0,
singularDouble: 0,
singularString: "",
singularBytes: "",
singularMsg: {},
singularEnum: "Default",
}
actual = proto_module::TestMessage.encode_json(m, :emit_defaults => true)
assert_equal expected, JSON.parse(actual, :symbolize_names => true)
end
def test_respond_to
msg = MapMessage.new
assert msg.respond_to?(:map_string_int32)
assert !msg.respond_to?(:bacon)
end
def test_file_descriptor
file_descriptor = TestMessage.descriptor.file_descriptor
assert nil != file_descriptor
assert_equal "tests/basic_test.proto", file_descriptor.name
assert_equal :proto3, file_descriptor.syntax
file_descriptor = TestEnum.descriptor.file_descriptor
assert nil != file_descriptor
assert_equal "tests/basic_test.proto", file_descriptor.name
assert_equal :proto3, file_descriptor.syntax
end
# Ruby 2.5 changed to raise FrozenError instead of RuntimeError
FrozenErrorType = Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.5') ? RuntimeError : FrozenError
def test_map_freeze
m = proto_module::MapMessage.new
m.map_string_int32['a'] = 5
m.map_string_msg['b'] = proto_module::TestMessage2.new
m.map_string_int32.freeze
m.map_string_msg.freeze
assert m.map_string_int32.frozen?
assert m.map_string_msg.frozen?
assert_raise(FrozenErrorType) { m.map_string_int32['foo'] = 1 }
assert_raise(FrozenErrorType) { m.map_string_msg['bar'] = proto_module::TestMessage2.new }
assert_raise(FrozenErrorType) { m.map_string_int32.delete('a') }
assert_raise(FrozenErrorType) { m.map_string_int32.clear }
end
def test_map_length
m = proto_module::MapMessage.new
assert_equal 0, m.map_string_int32.length
assert_equal 0, m.map_string_msg.length
assert_equal 0, m.map_string_int32.size
assert_equal 0, m.map_string_msg.size
m.map_string_int32['a'] = 1
m.map_string_int32['b'] = 2
m.map_string_msg['a'] = proto_module::TestMessage2.new
assert_equal 2, m.map_string_int32.length
assert_equal 1, m.map_string_msg.length
assert_equal 2, m.map_string_int32.size
assert_equal 1, m.map_string_msg.size
end
def test_string_with_singleton_class_enabled
str = 'foobar'
# NOTE: Accessing a singleton class of an object changes its low level class representation
# as far as the C API's CLASS_OF() method concerned, exposing the issue
str.singleton_class
m = proto_module::TestMessage.new(
optional_string: str,
optional_bytes: str
)
assert_equal str, m.optional_string
assert_equal str, m.optional_bytes
end
def test_utf8
m = proto_module::TestMessage.new(
optional_string: "µpb",
)
m2 = proto_module::TestMessage.decode(proto_module::TestMessage.encode(m))
assert_equal m2, m
end
def test_map_fields_respond_to? # regression test for issue 9202
msg = proto_module::MapMessage.new
assert msg.respond_to?(:map_string_int32=)
msg.map_string_int32 = Google::Protobuf::Map.new(:string, :int32)
assert msg.respond_to?(:map_string_int32)
assert_equal( Google::Protobuf::Map.new(:string, :int32), msg.map_string_int32 )
assert msg.respond_to?(:clear_map_string_int32)
msg.clear_map_string_int32
assert !msg.respond_to?(:has_map_string_int32?)
assert_raise NoMethodError do
msg.has_map_string_int32?
end
assert !msg.respond_to?(:map_string_int32_as_value)
assert_raise NoMethodError do
msg.map_string_int32_as_value
end
assert !msg.respond_to?(:map_string_int32_as_value=)
assert_raise NoMethodError do
msg.map_string_int32_as_value = :boom
end
end
end
def test_oneof_fields_respond_to? # regression test for issue 9202
msg = proto_module::OneofMessage.new
# `has_` prefix + "?" suffix actions should only work for oneofs fields.
assert msg.has_my_oneof?
assert msg.respond_to? :has_my_oneof?
assert !msg.respond_to?( :has_a? )
assert_raise NoMethodError do
msg.has_a?
end
assert !msg.respond_to?( :has_b? )
assert_raise NoMethodError do
msg.has_b?
end
assert !msg.respond_to?( :has_c? )
assert_raise NoMethodError do
msg.has_c?
end
assert !msg.respond_to?( :has_d? )
assert_raise NoMethodError do
msg.has_d?
end
end
end

274
deps/protobuf/ruby/tests/basic_proto2.rb vendored Normal file
View File

@ -0,0 +1,274 @@
#!/usr/bin/ruby
# basic_test_pb.rb is in the same directory as this test.
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)))
require 'basic_test_proto2_pb'
require 'common_tests'
require 'google/protobuf'
require 'json'
require 'test/unit'
# ------------- generated code --------------
module BasicTestProto2
pool = Google::Protobuf::DescriptorPool.new
pool.build do
add_file "test_proto2.proto", syntax: :proto2 do
add_message "BadFieldNames" do
optional :dup, :int32, 1
optional :class, :int32, 2
end
end
end
BadFieldNames = pool.lookup("BadFieldNames").msgclass
# ------------ test cases ---------------
class MessageContainerTest < Test::Unit::TestCase
# Required by CommonTests module to resolve proto2 proto classes used in tests.
def proto_module
::BasicTestProto2
end
include CommonTests
def test_has_field
m = TestMessage.new
assert !m.has_optional_int32?
assert !TestMessage.descriptor.lookup('optional_int32').has?(m)
assert !m.has_optional_int64?
assert !TestMessage.descriptor.lookup('optional_int64').has?(m)
assert !m.has_optional_uint32?
assert !TestMessage.descriptor.lookup('optional_uint32').has?(m)
assert !m.has_optional_uint64?
assert !TestMessage.descriptor.lookup('optional_uint64').has?(m)
assert !m.has_optional_bool?
assert !TestMessage.descriptor.lookup('optional_bool').has?(m)
assert !m.has_optional_float?
assert !TestMessage.descriptor.lookup('optional_float').has?(m)
assert !m.has_optional_double?
assert !TestMessage.descriptor.lookup('optional_double').has?(m)
assert !m.has_optional_string?
assert !TestMessage.descriptor.lookup('optional_string').has?(m)
assert !m.has_optional_bytes?
assert !TestMessage.descriptor.lookup('optional_bytes').has?(m)
assert !m.has_optional_enum?
assert !TestMessage.descriptor.lookup('optional_enum').has?(m)
m = TestMessage.new(:optional_int32 => nil)
assert !m.has_optional_int32?
assert_raise NoMethodError do
m.has_repeated_msg?
end
assert_raise ArgumentError do
TestMessage.descriptor.lookup('repeated_msg').has?(m)
end
m.optional_msg = TestMessage2.new
assert m.has_optional_msg?
assert TestMessage.descriptor.lookup('optional_msg').has?(m)
m = OneofMessage.new
assert !m.has_my_oneof?
m.a = "foo"
assert m.has_my_oneof?
assert_equal :a, m.my_oneof
assert m.has_a?
assert OneofMessage.descriptor.lookup('a').has?(m)
assert_equal "foo", m.a
assert !m.has_b?
assert !OneofMessage.descriptor.lookup('b').has?(m)
assert !m.has_c?
assert !OneofMessage.descriptor.lookup('c').has?(m)
assert !m.has_d?
assert !OneofMessage.descriptor.lookup('d').has?(m)
m = OneofMessage.new
m.b = 100
assert m.has_b?
assert_equal 100, m.b
assert m.has_my_oneof?
assert !m.has_a?
assert !m.has_c?
assert !m.has_d?
m = OneofMessage.new
m.c = TestMessage2.new
assert m.has_c?
assert_equal TestMessage2.new, m.c
assert m.has_my_oneof?
assert !m.has_a?
assert !m.has_b?
assert !m.has_d?
m = OneofMessage.new
m.d = :A
assert m.has_d?
assert_equal :A, m.d
assert m.has_my_oneof?
assert !m.has_a?
assert !m.has_b?
assert !m.has_c?
end
def test_defined_defaults
m = TestMessageDefaults.new
assert_equal 1, m.optional_int32
assert_equal 2, m.optional_int64
assert_equal 3, m.optional_uint32
assert_equal 4, m.optional_uint64
assert_equal true, m.optional_bool
assert_equal 6.0, m.optional_float
assert_equal 7.0, m.optional_double
assert_equal "Default Str", m.optional_string
assert_equal "\xCF\xA5s\xBD\xBA\xE6fubar".force_encoding("ASCII-8BIT"), m.optional_bytes
assert_equal :B2, m.optional_enum
assert !m.has_optional_int32?
assert !m.has_optional_int64?
assert !m.has_optional_uint32?
assert !m.has_optional_uint64?
assert !m.has_optional_bool?
assert !m.has_optional_float?
assert !m.has_optional_double?
assert !m.has_optional_string?
assert !m.has_optional_bytes?
assert !m.has_optional_enum?
end
def test_set_clear_defaults
m = TestMessageDefaults.new
m.optional_int32 = -42
assert_equal( -42, m.optional_int32 )
assert m.has_optional_int32?
m.clear_optional_int32
assert_equal 1, m.optional_int32
assert !m.has_optional_int32?
m.optional_string = "foo bar"
assert_equal "foo bar", m.optional_string
assert m.has_optional_string?
m.clear_optional_string
assert_equal "Default Str", m.optional_string
assert !m.has_optional_string?
m.optional_msg = TestMessage2.new(:foo => 42)
assert_equal TestMessage2.new(:foo => 42), m.optional_msg
assert m.has_optional_msg?
m.clear_optional_msg
assert_equal nil, m.optional_msg
assert !m.has_optional_msg?
m.optional_msg = TestMessage2.new(:foo => 42)
assert_equal TestMessage2.new(:foo => 42), m.optional_msg
assert TestMessageDefaults.descriptor.lookup('optional_msg').has?(m)
TestMessageDefaults.descriptor.lookup('optional_msg').clear(m)
assert_equal nil, m.optional_msg
assert !TestMessageDefaults.descriptor.lookup('optional_msg').has?(m)
m = TestMessage.new
m.repeated_int32.push(1)
assert_equal [1], m.repeated_int32
m.clear_repeated_int32
assert_equal [], m.repeated_int32
m = OneofMessage.new
m.a = "foo"
assert_equal "foo", m.a
assert m.has_a?
m.clear_a
assert !m.has_a?
m = OneofMessage.new
m.a = "foobar"
assert m.has_my_oneof?
m.clear_my_oneof
assert !m.has_my_oneof?
m = OneofMessage.new
m.a = "bar"
assert_equal "bar", m.a
assert m.has_my_oneof?
OneofMessage.descriptor.lookup('a').clear(m)
assert !m.has_my_oneof?
end
def test_assign_nil
m = TestMessageDefaults.new
m.optional_msg = TestMessage2.new(:foo => 42)
assert_equal TestMessage2.new(:foo => 42), m.optional_msg
assert m.has_optional_msg?
m.optional_msg = nil
assert_equal nil, m.optional_msg
assert !m.has_optional_msg?
end
def test_initialization_map_errors
e = assert_raise ArgumentError do
TestMessage.new(:hello => "world")
end
assert_match(/hello/, e.message)
e = assert_raise ArgumentError do
TestMessage.new(:repeated_uint32 => "hello")
end
assert_equal e.message, "Expected array as initializer value for repeated field 'repeated_uint32' (given String)."
end
def test_to_h
m = TestMessage.new(:optional_bool => true, :optional_double => -10.100001, :optional_string => 'foo', :repeated_string => ['bar1', 'bar2'])
expected_result = {
:optional_bool=>true,
:optional_double=>-10.100001,
:optional_string=>"foo",
:repeated_string=>["bar1", "bar2"],
}
assert_equal expected_result, m.to_h
m = OneofMessage.new(:a => "foo")
expected_result = {:a => "foo"}
assert_equal expected_result, m.to_h
end
def test_respond_to
# This test fails with JRuby 1.7.23, likely because of an old JRuby bug.
return if RUBY_PLATFORM == "java"
msg = TestMessage.new
assert !msg.respond_to?(:bacon)
end
def test_file_descriptor
file_descriptor = TestMessage.descriptor.file_descriptor
assert nil != file_descriptor
assert_equal "tests/basic_test_proto2.proto", file_descriptor.name
assert_equal :proto2, file_descriptor.syntax
file_descriptor = TestEnum.descriptor.file_descriptor
assert nil != file_descriptor
assert_equal "tests/basic_test_proto2.proto", file_descriptor.name
assert_equal :proto2, file_descriptor.syntax
end
def test_oneof_fields_respond_to? # regression test for issue 9202
msg = proto_module::OneofMessage.new(a: "foo")
# `has_` prefix + "?" suffix actions should only work for oneofs fields.
assert msg.respond_to? :has_my_oneof?
assert msg.has_my_oneof?
assert msg.respond_to? :has_a?
assert msg.has_a?
assert msg.respond_to? :has_b?
assert !msg.has_b?
assert msg.respond_to? :has_c?
assert !msg.has_c?
assert msg.respond_to? :has_d?
assert !msg.has_d?
end
end
end

View File

@ -0,0 +1,252 @@
syntax = "proto3";
package basic_test;
import "google/protobuf/wrappers.proto";
import "google/protobuf/timestamp.proto";
import "google/protobuf/duration.proto";
import "google/protobuf/struct.proto";
import "test_import_proto2.proto";
message Foo {
Bar bar = 1;
repeated Baz baz = 2;
}
message Bar {
string msg = 1;
}
message Baz {
string msg = 1;
}
message TestMessage {
optional int32 optional_int32 = 1;
optional int64 optional_int64 = 2;
optional uint32 optional_uint32 = 3;
optional uint64 optional_uint64 = 4;
optional bool optional_bool = 5;
optional float optional_float = 6;
optional double optional_double = 7;
optional string optional_string = 8;
optional bytes optional_bytes = 9;
optional TestMessage2 optional_msg = 10;
optional TestEnum optional_enum = 11;
optional foo_bar.proto2.TestImportedMessage optional_proto2_submessage = 24;
repeated int32 repeated_int32 = 12;
repeated int64 repeated_int64 = 13;
repeated uint32 repeated_uint32 = 14;
repeated uint64 repeated_uint64 = 15;
repeated bool repeated_bool = 16;
repeated float repeated_float = 17;
repeated double repeated_double = 18;
repeated string repeated_string = 19;
repeated bytes repeated_bytes = 20;
repeated TestMessage2 repeated_msg = 21;
repeated TestEnum repeated_enum = 22;
optional TestSingularFields optional_msg2 = 23;
}
message TestSingularFields {
int32 singular_int32 = 1;
int64 singular_int64 = 2;
uint32 singular_uint32 = 3;
uint64 singular_uint64 = 4;
bool singular_bool = 5;
float singular_float = 6;
double singular_double = 7;
string singular_string = 8;
bytes singular_bytes = 9;
TestMessage2 singular_msg = 10;
TestEnum singular_enum = 11;
}
message TestMessage2 {
optional int32 foo = 1;
}
enum TestEnum {
Default = 0;
A = 1;
B = 2;
C = 3;
}
message TestEmbeddedMessageParent {
TestEmbeddedMessageChild child_msg = 1;
int32 number = 2;
repeated TestEmbeddedMessageChild repeated_msg = 3;
repeated int32 repeated_number = 4;
}
message TestEmbeddedMessageChild {
TestMessage sub_child = 1;
}
message Recursive1 {
Recursive2 foo = 1;
}
message Recursive2 {
Recursive1 foo = 1;
}
message MapMessage {
map<string, int32> map_string_int32 = 1;
map<string, TestMessage2> map_string_msg = 2;
map<string, TestEnum> map_string_enum = 3;
}
message MapMessageWireEquiv {
repeated MapMessageWireEquiv_entry1 map_string_int32 = 1;
repeated MapMessageWireEquiv_entry2 map_string_msg = 2;
}
message MapMessageWireEquiv_entry1 {
string key = 1;
int32 value = 2;
}
message MapMessageWireEquiv_entry2 {
string key = 1;
TestMessage2 value = 2;
}
message OneofMessage {
oneof my_oneof {
string a = 1;
int32 b = 2;
TestMessage2 c = 3;
TestEnum d = 4;
}
}
message Outer {
map<int32, Inner> items = 1;
}
message Inner {
}
message Wrapper {
google.protobuf.DoubleValue double = 1;
google.protobuf.FloatValue float = 2;
google.protobuf.Int32Value int32 = 3;
google.protobuf.Int64Value int64 = 4;
google.protobuf.UInt32Value uint32 = 5;
google.protobuf.UInt64Value uint64 = 6;
google.protobuf.BoolValue bool = 7;
google.protobuf.StringValue string = 8;
google.protobuf.BytesValue bytes = 9;
string real_string = 100;
oneof a_oneof {
string string_in_oneof = 10;
}
// Repeated wrappers don't make sense, but we still need to make sure they
// work and don't crash.
repeated google.protobuf.DoubleValue repeated_double = 11;
repeated google.protobuf.FloatValue repeated_float = 12;
repeated google.protobuf.Int32Value repeated_int32 = 13;
repeated google.protobuf.Int64Value repeated_int64 = 14;
repeated google.protobuf.UInt32Value repeated_uint32 = 15;
repeated google.protobuf.UInt64Value repeated_uint64 = 16;
repeated google.protobuf.BoolValue repeated_bool = 17;
repeated google.protobuf.StringValue repeated_string = 18;
repeated google.protobuf.BytesValue repeated_bytes = 19;
// Wrappers as map keys don't make sense, but we still need to make sure they
// work and don't crash.
map<int32, google.protobuf.DoubleValue> map_double = 21;
map<int32, google.protobuf.FloatValue> map_float = 22;
map<int32, google.protobuf.Int32Value> map_int32 = 23;
map<int32, google.protobuf.Int64Value> map_int64 = 24;
map<int32, google.protobuf.UInt32Value> map_uint32 = 25;
map<int32, google.protobuf.UInt64Value> map_uint64 = 26;
map<int32, google.protobuf.BoolValue> map_bool = 27;
map<int32, google.protobuf.StringValue> map_string = 28;
map<int32, google.protobuf.BytesValue> map_bytes = 29;
// Wrappers in oneofs don't make sense, but we still need to make sure they
// work and don't crash.
oneof wrapper_oneof {
google.protobuf.DoubleValue oneof_double = 31;
google.protobuf.FloatValue oneof_float = 32;
google.protobuf.Int32Value oneof_int32 = 33;
google.protobuf.Int64Value oneof_int64 = 34;
google.protobuf.UInt32Value oneof_uint32 = 35;
google.protobuf.UInt64Value oneof_uint64 = 36;
google.protobuf.BoolValue oneof_bool = 37;
google.protobuf.StringValue oneof_string = 38;
google.protobuf.BytesValue oneof_bytes = 39;
string oneof_plain_string = 101;
}
}
message TimeMessage {
google.protobuf.Timestamp timestamp = 1;
google.protobuf.Duration duration = 2;
}
message Enumer {
TestEnum optional_enum = 1;
repeated TestEnum repeated_enum = 2;
string a_const = 3;
oneof a_oneof {
string str = 10;
TestEnum const = 11;
}
}
message MyRepeatedStruct {
repeated MyStruct structs = 1;
}
message MyStruct {
string string = 1;
google.protobuf.Struct struct = 2;
}
message WithJsonName {
optional int32 foo_bar = 1 [json_name="jsonFooBar"];
repeated WithJsonName baz = 2 [json_name="jsonBaz"];
}
message HelloRequest {
optional uint32 id = 1;
optional uint32 random_name_a0 = 2;
optional uint32 random_name_a1 = 3;
optional uint32 random_name_a2 = 4;
optional uint32 random_name_a3 = 5;
optional uint32 random_name_a4 = 6;
optional uint32 random_name_a5 = 7;
optional uint32 random_name_a6 = 8;
optional uint32 random_name_a7 = 9;
optional uint32 random_name_a8 = 10;
optional uint32 random_name_a9 = 11;
optional uint32 random_name_b0 = 12;
optional uint32 random_name_b1 = 13;
optional uint32 random_name_b2 = 14;
optional uint32 random_name_b3 = 15;
optional uint32 random_name_b4 = 16;
optional uint32 random_name_b5 = 17;
optional uint32 random_name_b6 = 18;
optional uint32 random_name_b7 = 19;
optional uint32 random_name_b8 = 20;
optional uint32 random_name_b9 = 21;
optional uint32 random_name_c0 = 22;
optional uint32 random_name_c1 = 23;
optional uint32 random_name_c2 = 24;
optional uint32 random_name_c3 = 25;
optional uint32 random_name_c4 = 26;
optional uint32 random_name_c5 = 27;
optional uint32 random_name_c6 = 28;
optional uint32 random_name_c7 = 29;
optional uint32 random_name_c8 = 30;
optional uint32 random_name_c9 = 31;
optional string version = 32;
}

View File

@ -0,0 +1,189 @@
syntax = "proto2";
package basic_test_proto2;
import "google/protobuf/wrappers.proto";
import "google/protobuf/timestamp.proto";
import "google/protobuf/duration.proto";
import "google/protobuf/struct.proto";
message Foo {
optional Bar bar = 1;
repeated Baz baz = 2;
}
message Bar {
optional string msg = 1;
}
message Baz {
optional string msg = 1;
}
message TestMessage {
optional int32 optional_int32 = 1;
optional int64 optional_int64 = 2;
optional uint32 optional_uint32 = 3;
optional uint64 optional_uint64 = 4;
optional bool optional_bool = 5;
optional float optional_float = 6;
optional double optional_double = 7;
optional string optional_string = 8;
optional bytes optional_bytes = 9;
optional TestMessage2 optional_msg = 10;
optional TestEnum optional_enum = 11;
repeated int32 repeated_int32 = 12;
repeated int64 repeated_int64 = 13;
repeated uint32 repeated_uint32 = 14;
repeated uint64 repeated_uint64 = 15;
repeated bool repeated_bool = 16;
repeated float repeated_float = 17;
repeated double repeated_double = 18;
repeated string repeated_string = 19;
repeated bytes repeated_bytes = 20;
repeated TestMessage2 repeated_msg = 21;
repeated TestEnum repeated_enum = 22;
}
message TestMessage2 {
optional int32 foo = 1;
}
message TestMessageDefaults {
optional int32 optional_int32 = 1 [default = 1];
optional int64 optional_int64 = 2 [default = 2];
optional uint32 optional_uint32 = 3 [default = 3];
optional uint64 optional_uint64 = 4 [default = 4];
optional bool optional_bool = 5 [default = true];
optional float optional_float = 6 [default = 6];
optional double optional_double = 7 [default = 7];
optional string optional_string = 8 [default = "Default Str"];
optional bytes optional_bytes = 9 [default = "\xCF\xA5s\xBD\xBA\xE6fubar"];
optional TestMessage2 optional_msg = 10;
optional TestNonZeroEnum optional_enum = 11 [default = B2];
}
enum TestEnum {
Default = 0;
A = 1;
B = 2;
C = 3;
}
enum TestNonZeroEnum {
A2 = 1;
B2 = 2;
C2 = 3;
}
message TestEmbeddedMessageParent {
optional TestEmbeddedMessageChild child_msg = 1;
optional int32 number = 2;
repeated TestEmbeddedMessageChild repeated_msg = 3;
repeated int32 repeated_number = 4;
}
message TestEmbeddedMessageChild {
optional TestMessage sub_child = 1;
}
message Recursive1 {
optional Recursive2 foo = 1;
}
message Recursive2 {
optional Recursive1 foo = 1;
}
message MapMessageWireEquiv {
repeated MapMessageWireEquiv_entry1 map_string_int32 = 1;
repeated MapMessageWireEquiv_entry2 map_string_msg = 2;
}
message MapMessageWireEquiv_entry1 {
optional string key = 1;
optional int32 value = 2;
}
message MapMessageWireEquiv_entry2 {
optional string key = 1;
optional TestMessage2 value = 2;
}
message OneofMessage {
oneof my_oneof {
string a = 1;
int32 b = 2;
TestMessage2 c = 3;
TestEnum d = 4;
}
}
message Wrapper {
optional google.protobuf.DoubleValue double = 1;
optional google.protobuf.FloatValue float = 2;
optional google.protobuf.Int32Value int32 = 3;
optional google.protobuf.Int64Value int64 = 4;
optional google.protobuf.UInt32Value uint32 = 5;
optional google.protobuf.UInt64Value uint64 = 6;
optional google.protobuf.BoolValue bool = 7;
optional google.protobuf.StringValue string = 8;
optional google.protobuf.BytesValue bytes = 9;
optional string real_string = 100;
oneof a_oneof {
string string_in_oneof = 10;
}
// Repeated wrappers don't really make sense, but we still need to make sure
// they work and don't crash.
repeated google.protobuf.DoubleValue repeated_double = 11;
repeated google.protobuf.FloatValue repeated_float = 12;
repeated google.protobuf.Int32Value repeated_int32 = 13;
repeated google.protobuf.Int64Value repeated_int64 = 14;
repeated google.protobuf.UInt32Value repeated_uint32 = 15;
repeated google.protobuf.UInt64Value repeated_uint64 = 16;
repeated google.protobuf.BoolValue repeated_bool = 17;
repeated google.protobuf.StringValue repeated_string = 18;
repeated google.protobuf.BytesValue repeated_bytes = 19;
// Wrappers in oneofs don't make sense, but we still need to make sure they
// work and don't crash.
oneof wrapper_oneof {
google.protobuf.DoubleValue oneof_double = 31;
google.protobuf.FloatValue oneof_float = 32;
google.protobuf.Int32Value oneof_int32 = 33;
google.protobuf.Int64Value oneof_int64 = 34;
google.protobuf.UInt32Value oneof_uint32 = 35;
google.protobuf.UInt64Value oneof_uint64 = 36;
google.protobuf.BoolValue oneof_bool = 37;
google.protobuf.StringValue oneof_string = 38;
google.protobuf.BytesValue oneof_bytes = 39;
string oneof_plain_string = 101;
}
}
message TimeMessage {
optional google.protobuf.Timestamp timestamp = 1;
optional google.protobuf.Duration duration = 2;
}
message Enumer {
optional TestEnum optional_enum = 11;
repeated TestEnum repeated_enum = 22;
optional string a_const = 3;
oneof a_oneof {
string str = 100;
TestEnum const = 101;
}
}
message MyRepeatedStruct {
repeated MyStruct structs = 1;
}
message MyStruct {
optional string string = 1;
optional google.protobuf.Struct struct = 2;
}

1983
deps/protobuf/ruby/tests/common_tests.rb vendored Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,155 @@
#!/usr/bin/ruby
# generated_code.rb is in the same directory as this test.
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)))
require 'generated_code_pb'
require 'google/protobuf/well_known_types'
require 'test/unit'
def hex2bin(s)
s.scan(/../).map { |x| x.hex.chr }.join
end
class EncodeDecodeTest < Test::Unit::TestCase
def test_discard_unknown
# Test discard unknown in message.
unknown_msg = A::B::C::TestUnknown.new(:unknown_field => 1)
from = A::B::C::TestUnknown.encode(unknown_msg)
m = A::B::C::TestMessage.decode(from)
Google::Protobuf.discard_unknown(m)
to = A::B::C::TestMessage.encode(m)
assert_equal '', to
# Test discard unknown for singular message field.
unknown_msg = A::B::C::TestUnknown.new(
:optional_unknown =>
A::B::C::TestUnknown.new(:unknown_field => 1))
from = A::B::C::TestUnknown.encode(unknown_msg)
m = A::B::C::TestMessage.decode(from)
Google::Protobuf.discard_unknown(m)
to = A::B::C::TestMessage.encode(m.optional_msg)
assert_equal '', to
# Test discard unknown for repeated message field.
unknown_msg = A::B::C::TestUnknown.new(
:repeated_unknown =>
[A::B::C::TestUnknown.new(:unknown_field => 1)])
from = A::B::C::TestUnknown.encode(unknown_msg)
m = A::B::C::TestMessage.decode(from)
Google::Protobuf.discard_unknown(m)
to = A::B::C::TestMessage.encode(m.repeated_msg[0])
assert_equal '', to
# Test discard unknown for map value message field.
unknown_msg = A::B::C::TestUnknown.new(
:map_unknown =>
{"" => A::B::C::TestUnknown.new(:unknown_field => 1)})
from = A::B::C::TestUnknown.encode(unknown_msg)
m = A::B::C::TestMessage.decode(from)
Google::Protobuf.discard_unknown(m)
to = A::B::C::TestMessage.encode(m.map_string_msg[''])
assert_equal '', to
# Test discard unknown for oneof message field.
unknown_msg = A::B::C::TestUnknown.new(
:oneof_unknown =>
A::B::C::TestUnknown.new(:unknown_field => 1))
from = A::B::C::TestUnknown.encode(unknown_msg)
m = A::B::C::TestMessage.decode(from)
Google::Protobuf.discard_unknown(m)
to = A::B::C::TestMessage.encode(m.oneof_msg)
assert_equal '', to
end
def test_encode_json
msg = A::B::C::TestMessage.new({ optional_int32: 22 })
json = msg.to_json
to = A::B::C::TestMessage.decode_json(json)
assert_equal to.optional_int32, 22
msg = A::B::C::TestMessage.new({ optional_int32: 22 })
json = msg.to_json({ preserve_proto_fieldnames: true })
assert_match 'optional_int32', json
to = A::B::C::TestMessage.decode_json(json)
assert_equal 22, to.optional_int32
msg = A::B::C::TestMessage.new({ optional_int32: 22 })
json = A::B::C::TestMessage.encode_json(
msg,
{ preserve_proto_fieldnames: true, emit_defaults: true }
)
assert_match 'optional_int32', json
end
def test_encode_wrong_msg
assert_raise ::ArgumentError do
m = A::B::C::TestMessage.new(
:optional_int32 => 1,
)
Google::Protobuf::Any.encode(m)
end
end
def test_json_name
msg = A::B::C::TestJsonName.new(:value => 42)
json = msg.to_json
assert_match json, "{\"CustomJsonName\":42}"
end
def test_decode_depth_limit
msg = A::B::C::TestMessage.new(
optional_msg: A::B::C::TestMessage.new(
optional_msg: A::B::C::TestMessage.new(
optional_msg: A::B::C::TestMessage.new(
optional_msg: A::B::C::TestMessage.new(
optional_msg: A::B::C::TestMessage.new(
)
)
)
)
)
)
msg_encoded = A::B::C::TestMessage.encode(msg)
msg_out = A::B::C::TestMessage.decode(msg_encoded)
assert_match msg.to_json, msg_out.to_json
assert_raise Google::Protobuf::ParseError do
A::B::C::TestMessage.decode(msg_encoded, { recursion_limit: 4 })
end
msg_out = A::B::C::TestMessage.decode(msg_encoded, { recursion_limit: 5 })
assert_match msg.to_json, msg_out.to_json
end
def test_encode_depth_limit
msg = A::B::C::TestMessage.new(
optional_msg: A::B::C::TestMessage.new(
optional_msg: A::B::C::TestMessage.new(
optional_msg: A::B::C::TestMessage.new(
optional_msg: A::B::C::TestMessage.new(
optional_msg: A::B::C::TestMessage.new(
)
)
)
)
)
)
msg_encoded = A::B::C::TestMessage.encode(msg)
msg_out = A::B::C::TestMessage.decode(msg_encoded)
assert_match msg.to_json, msg_out.to_json
assert_raise RuntimeError do
A::B::C::TestMessage.encode(msg, { recursion_limit: 5 })
end
msg_encoded = A::B::C::TestMessage.encode(msg, { recursion_limit: 6 })
msg_out = A::B::C::TestMessage.decode(msg_encoded)
assert_match msg.to_json, msg_out.to_json
end
end

109
deps/protobuf/ruby/tests/gc_test.rb vendored Normal file
View File

@ -0,0 +1,109 @@
#!/usr/bin/ruby
#
# generated_code.rb is in the same directory as this test.
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)))
old_gc = GC.stress
# Ruby 2.7.0 - 2.7.1 has a GC bug in its parser, so turn off stress for now
# See https://bugs.ruby-lang.org/issues/16807
GC.stress = 0x01 | 0x04 unless RUBY_VERSION.match?(/^2\.7\./)
require 'generated_code_pb'
require 'generated_code_proto2_pb'
GC.stress = old_gc
require 'test/unit'
class GCTest < Test::Unit::TestCase
def get_msg_proto3
A::B::C::TestMessage.new(
:optional_int32 => 1,
:optional_int64 => 1,
:optional_uint32 => 1,
:optional_uint64 => 1,
:optional_bool => true,
:optional_double => 1.0,
:optional_float => 1.0,
:optional_string => "a",
:optional_bytes => "b",
:optional_enum => A::B::C::TestEnum::A,
:optional_msg => A::B::C::TestMessage.new(),
:repeated_int32 => [1],
:repeated_int64 => [1],
:repeated_uint32 => [1],
:repeated_uint64 => [1],
:repeated_bool => [true],
:repeated_double => [1.0],
:repeated_float => [1.0],
:repeated_string => ["a"],
:repeated_bytes => ["b"],
:repeated_enum => [A::B::C::TestEnum::A],
:repeated_msg => [A::B::C::TestMessage.new()],
:map_int32_string => {1 => "a"},
:map_int64_string => {1 => "a"},
:map_uint32_string => {1 => "a"},
:map_uint64_string => {1 => "a"},
:map_bool_string => {true => "a"},
:map_string_string => {"a" => "a"},
:map_string_msg => {"a" => A::B::C::TestMessage.new()},
:map_string_int32 => {"a" => 1},
:map_string_bool => {"a" => true},
)
end
def get_msg_proto2
A::B::Proto2::TestMessage.new(
:optional_int32 => 1,
:optional_int64 => 1,
:optional_uint32 => 1,
:optional_uint64 => 1,
:optional_bool => true,
:optional_double => 1.0,
:optional_float => 1.0,
:optional_string => "a",
:optional_bytes => "b",
:optional_enum => A::B::Proto2::TestEnum::A,
:optional_msg => A::B::Proto2::TestMessage.new(),
:repeated_int32 => [1],
:repeated_int64 => [1],
:repeated_uint32 => [1],
:repeated_uint64 => [1],
:repeated_bool => [true],
:repeated_double => [1.0],
:repeated_float => [1.0],
:repeated_string => ["a"],
:repeated_bytes => ["b"],
:repeated_enum => [A::B::Proto2::TestEnum::A],
:repeated_msg => [A::B::Proto2::TestMessage.new()],
:required_int32 => 1,
:required_int64 => 1,
:required_uint32 => 1,
:required_uint64 => 1,
:required_bool => true,
:required_double => 1.0,
:required_float => 1.0,
:required_string => "a",
:required_bytes => "b",
:required_enum => A::B::Proto2::TestEnum::A,
:required_msg => A::B::Proto2::TestMessage.new(),
)
end
def test_generated_msg
old_gc = GC.stress
GC.stress = 0x01 | 0x04
from = get_msg_proto3
data = A::B::C::TestMessage.encode(from)
to = A::B::C::TestMessage.decode(data)
# This doesn't work for proto2 on JRuby because there is a nested required message.
# A::B::Proto2::TestMessage has :required_msg which is of type:
# A::B::Proto2::TestMessage so there is no way to generate a valid
# message that doesn't exceed the depth limit
if !defined? JRUBY_VERSION
from = get_msg_proto2
data = A::B::Proto2::TestMessage.encode(from)
to = A::B::Proto2::TestMessage.decode(data)
end
GC.stress = old_gc
puts "passed"
end
end

View File

@ -0,0 +1,89 @@
syntax = "proto3";
package a.b.c;
message TestMessage {
int32 optional_int32 = 1;
int64 optional_int64 = 2;
uint32 optional_uint32 = 3;
uint64 optional_uint64 = 4;
bool optional_bool = 5;
double optional_double = 6;
float optional_float = 7;
string optional_string = 8;
bytes optional_bytes = 9;
TestEnum optional_enum = 10;
TestMessage optional_msg = 11;
repeated int32 repeated_int32 = 21;
repeated int64 repeated_int64 = 22;
repeated uint32 repeated_uint32 = 23;
repeated uint64 repeated_uint64 = 24;
repeated bool repeated_bool = 25;
repeated double repeated_double = 26;
repeated float repeated_float = 27;
repeated string repeated_string = 28;
repeated bytes repeated_bytes = 29;
repeated TestEnum repeated_enum = 30;
repeated TestMessage repeated_msg = 31;
oneof my_oneof {
int32 oneof_int32 = 41;
int64 oneof_int64 = 42;
uint32 oneof_uint32 = 43;
uint64 oneof_uint64 = 44;
bool oneof_bool = 45;
double oneof_double = 46;
float oneof_float = 47;
string oneof_string = 48;
bytes oneof_bytes = 49;
TestEnum oneof_enum = 50;
TestMessage oneof_msg = 51;
}
map<int32, string> map_int32_string = 61;
map<int64, string> map_int64_string = 62;
map<uint32, string> map_uint32_string = 63;
map<uint64, string> map_uint64_string = 64;
map<bool, string> map_bool_string = 65;
map<string, string> map_string_string = 66;
map<string, TestMessage> map_string_msg = 67;
map<string, TestEnum> map_string_enum = 68;
map<string, int32> map_string_int32 = 69;
map<string, bool> map_string_bool = 70;
message NestedMessage {
int32 foo = 1;
}
NestedMessage nested_message = 80;
// Reserved for non-existing field test.
// int32 non_exist = 89;
}
enum TestEnum {
Default = 0;
A = 1;
B = 2;
C = 3;
}
message testLowercaseNested {
message lowercase{}
}
message TestUnknown {
TestUnknown optional_unknown = 11;
repeated TestUnknown repeated_unknown = 31;
oneof my_oneof {
TestUnknown oneof_unknown = 51;
}
map<string, TestUnknown> map_unknown = 67;
int32 unknown_field = 89;
}
message TestJsonName {
int32 value = 1 [json_name = "CustomJsonName"];
}

View File

@ -0,0 +1,80 @@
syntax = "proto2";
package a.b.proto2;
message TestMessage {
optional int32 optional_int32 = 1;
optional int64 optional_int64 = 2;
optional uint32 optional_uint32 = 3;
optional uint64 optional_uint64 = 4;
optional bool optional_bool = 5;
optional double optional_double = 6;
optional float optional_float = 7;
optional string optional_string = 8;
optional bytes optional_bytes = 9;
optional TestEnum optional_enum = 10;
optional TestMessage optional_msg = 11;
repeated int32 repeated_int32 = 21;
repeated int64 repeated_int64 = 22;
repeated uint32 repeated_uint32 = 23;
repeated uint64 repeated_uint64 = 24;
repeated bool repeated_bool = 25;
repeated double repeated_double = 26;
repeated float repeated_float = 27;
repeated string repeated_string = 28;
repeated bytes repeated_bytes = 29;
repeated TestEnum repeated_enum = 30;
repeated TestMessage repeated_msg = 31;
required int32 required_int32 = 41;
required int64 required_int64 = 42;
required uint32 required_uint32 = 43;
required uint64 required_uint64 = 44;
required bool required_bool = 45;
required double required_double = 46;
required float required_float = 47;
required string required_string = 48;
required bytes required_bytes = 49;
required TestEnum required_enum = 50;
required TestMessage required_msg = 51;
oneof my_oneof {
int32 oneof_int32 = 61;
int64 oneof_int64 = 62;
uint32 oneof_uint32 = 63;
uint64 oneof_uint64 = 64;
bool oneof_bool = 65;
double oneof_double = 66;
float oneof_float = 67;
string oneof_string = 68;
bytes oneof_bytes = 69;
TestEnum oneof_enum = 70;
TestMessage oneof_msg = 71;
}
message NestedMessage {
optional int32 foo = 1;
}
optional NestedMessage nested_message = 80;
// Reserved for non-existing field test.
// int32 non_exist = 89;
}
enum TestEnum {
Default = 0;
A = 1;
B = 2;
C = 3;
}
message TestUnknown {
optional TestUnknown optional_unknown = 11;
repeated TestUnknown repeated_unknown = 31;
oneof my_oneof {
TestUnknown oneof_unknown = 51;
}
optional int32 unknown_field = 89;
}

View File

@ -0,0 +1,21 @@
#!/usr/bin/ruby
# generated_code.rb is in the same directory as this test.
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)))
require 'generated_code_proto2_pb'
require 'test_import_proto2_pb'
require 'test_ruby_package_proto2_pb'
require 'test/unit'
class GeneratedCodeProto2Test < Test::Unit::TestCase
def test_generated_msg
# just test that we can instantiate the message. The purpose of this test
# is to ensure that the output of the code generator is valid Ruby and
# successfully creates message definitions and classes, not to test every
# aspect of the extension (basic.rb is for that).
A::B::Proto2::TestMessage.new
FooBar::Proto2::TestImportedMessage.new
A::B::Proto2::TestRubyPackageMessage.new
end
end

View File

@ -0,0 +1,23 @@
#!/usr/bin/ruby
# generated_code.rb is in the same directory as this test.
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)))
require 'generated_code_pb'
require 'test_import_pb'
require 'test_ruby_package_pb'
require 'test/unit'
class GeneratedCodeTest < Test::Unit::TestCase
def test_generated_msg
# just test that we can instantiate the message. The purpose of this test
# is to ensure that the output of the code generator is valid Ruby and
# successfully creates message definitions and classes, not to test every
# aspect of the extension (basic.rb is for that).
A::B::C::TestMessage.new
A::B::C::TestMessage::NestedMessage.new
A::B::C::TestLowercaseNested::Lowercase.new
FooBar::TestImportedMessage.new
A::B::TestRubyPackageMessage.new
end
end

View File

@ -0,0 +1,19 @@
syntax = "proto3";
message Function {
string name = 1;
repeated Function.Parameter parameters = 2;
string return_type = 3;
message Parameter {
string name = 1;
Function.Parameter.Value value = 2;
message Value {
oneof type {
string string = 1;
int64 integer = 2;
}
}
}
}

View File

@ -0,0 +1,20 @@
#!/usr/bin/ruby
# multi_level_nesting_test_pb.rb is in the same directory as this test.
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)))
require 'test/unit'
require 'multi_level_nesting_test_pb'
#
# Provide tests for having messages nested 3 levels deep
#
class MultiLevelNestingTest < Test::Unit::TestCase
def test_levels_exist
assert ::Google::Protobuf::DescriptorPool.generated_pool.lookup("Function").msgclass
assert ::Google::Protobuf::DescriptorPool.generated_pool.lookup("Function.Parameter").msgclass
assert ::Google::Protobuf::DescriptorPool.generated_pool.lookup("Function.Parameter.Value").msgclass
end
end

View File

@ -0,0 +1,653 @@
#!/usr/bin/ruby
require 'google/protobuf'
require 'test/unit'
class RepeatedFieldTest < Test::Unit::TestCase
def test_acts_like_enumerator
m = TestMessage.new
(Enumerable.instance_methods - TestMessage.new.repeated_string.methods).each do |method_name|
assert m.repeated_string.respond_to?(method_name) == true, "does not respond to #{method_name}"
end
end
def test_acts_like_an_array
m = TestMessage.new
arr_methods = ([].methods - TestMessage.new.repeated_string.methods)
# jRuby additions to the Array class that we can ignore
arr_methods -= [ :indices, :iter_for_each, :iter_for_each_index,
:iter_for_each_with_index, :dimensions, :copy_data, :copy_data_simple,
:nitems, :iter_for_reverse_each, :indexes, :append, :prepend]
arr_methods -= [:union, :difference, :filter!]
arr_methods -= [:intersection, :deconstruct] # ruby 2.7 methods we can ignore
arr_methods -= [:intersect?] # ruby 3.1 methods we can ignore
arr_methods.each do |method_name|
assert m.repeated_string.respond_to?(method_name) == true, "does not respond to #{method_name}"
end
end
def test_first
m = TestMessage.new
repeated_field_names(TestMessage).each do |field_name|
assert_nil m.send(field_name).first
assert_equal [], m.send(field_name).first(0)
assert_equal [], m.send(field_name).first(1)
end
fill_test_msg(m)
assert_equal -10, m.repeated_int32.first
assert_equal -1_000_000, m.repeated_int64.first
assert_equal 10, m.repeated_uint32.first
assert_equal 1_000_000, m.repeated_uint64.first
assert_equal true, m.repeated_bool.first
assert_equal -1.01, m.repeated_float.first.round(2)
assert_equal -1.0000000000001, m.repeated_double.first
assert_equal 'foo', m.repeated_string.first
assert_equal "bar".encode!('ASCII-8BIT'), m.repeated_bytes.first
assert_equal TestMessage2.new(:foo => 1), m.repeated_msg.first
assert_equal :A, m.repeated_enum.first
assert_equal [], m.repeated_int32.first(0)
assert_equal [-10], m.repeated_int32.first(1)
assert_equal [-10, -11], m.repeated_int32.first(2)
assert_equal [-10, -11], m.repeated_int32.first(3)
end
def test_last
m = TestMessage.new
repeated_field_names(TestMessage).each do |field_name|
assert_nil m.send(field_name).first
end
fill_test_msg(m)
assert_equal -11, m.repeated_int32.last
assert_equal -1_000_001, m.repeated_int64.last
assert_equal 11, m.repeated_uint32.last
assert_equal 1_000_001, m.repeated_uint64.last
assert_equal false, m.repeated_bool.last
assert_equal -1.02, m.repeated_float.last.round(2)
assert_equal -1.0000000000002, m.repeated_double.last
assert_equal 'bar', m.repeated_string.last
assert_equal "foo".encode!('ASCII-8BIT'), m.repeated_bytes.last
assert_equal TestMessage2.new(:foo => 2), m.repeated_msg.last
assert_equal :B, m.repeated_enum.last
end
def test_pop
m = TestMessage.new
repeated_field_names(TestMessage).each do |field_name|
assert_nil m.send(field_name).pop
end
fill_test_msg(m)
assert_equal -11, m.repeated_int32.pop
assert_equal -10, m.repeated_int32.pop
assert_equal -1_000_001, m.repeated_int64.pop
assert_equal -1_000_000, m.repeated_int64.pop
assert_equal 11, m.repeated_uint32.pop
assert_equal 10, m.repeated_uint32.pop
assert_equal 1_000_001, m.repeated_uint64.pop
assert_equal 1_000_000, m.repeated_uint64.pop
assert_equal false, m.repeated_bool.pop
assert_equal true, m.repeated_bool.pop
assert_equal -1.02, m.repeated_float.pop.round(2)
assert_equal -1.01, m.repeated_float.pop.round(2)
assert_equal -1.0000000000002, m.repeated_double.pop
assert_equal -1.0000000000001, m.repeated_double.pop
assert_equal 'bar', m.repeated_string.pop
assert_equal 'foo', m.repeated_string.pop
assert_equal "foo".encode!('ASCII-8BIT'), m.repeated_bytes.pop
assert_equal "bar".encode!('ASCII-8BIT'), m.repeated_bytes.pop
assert_equal TestMessage2.new(:foo => 2), m.repeated_msg.pop
assert_equal TestMessage2.new(:foo => 1), m.repeated_msg.pop
assert_equal :B, m.repeated_enum.pop
assert_equal :A, m.repeated_enum.pop
repeated_field_names(TestMessage).each do |field_name|
assert_nil m.send(field_name).pop
end
fill_test_msg(m)
assert_equal ['bar', 'foo'], m.repeated_string.pop(2)
assert_nil m.repeated_string.pop
end
def test_each
m = TestMessage.new
5.times{|i| m.repeated_string << 'string' }
count = 0
m.repeated_string.each do |val|
assert_equal 'string', val
count += 1
end
assert_equal 5, count
result = m.repeated_string.each{|val| val + '_junk'}
assert_equal ['string'] * 5, result
end
def test_empty?
m = TestMessage.new
assert_equal true, m.repeated_string.empty?
m.repeated_string << 'foo'
assert_equal false, m.repeated_string.empty?
m.repeated_string << 'bar'
assert_equal false, m.repeated_string.empty?
end
def test_reassign
m = TestMessage.new
m.repeated_msg = Google::Protobuf::RepeatedField.new(:message, TestMessage2, [TestMessage2.new(:foo => 1)])
assert_equal m.repeated_msg.first, TestMessage2.new(:foo => 1)
end
def test_array_accessor
m = TestMessage.new
reference_arr = %w(foo bar baz)
m.repeated_string += reference_arr.clone
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr[1]
end
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr[-2]
end
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr[20]
end
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr[1, 2]
end
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr[0..2]
end
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr[-1, 1]
end
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr[10, 12]
end
end
def test_array_settor
m = TestMessage.new
reference_arr = %w(foo bar baz)
m.repeated_string += reference_arr.clone
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr[1] = 'junk'
end
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr[-2] = 'snappy'
end
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr[3] = ''
end
# slight deviation; we are strongly typed, and nil is not allowed
# for string types;
m.repeated_string[5] = 'spacious'
assert_equal ["foo", "snappy", "baz", "", "", "spacious"], m.repeated_string
#make sure it sests the default types for other fields besides strings
%w(repeated_int32 repeated_int64 repeated_uint32 repeated_uint64).each do |field_name|
m.send(field_name)[3] = 10
assert_equal [0,0,0,10], m.send(field_name)
end
m.repeated_float[3] = 10.1
#wonky mri float handling
assert_equal [0,0,0], m.repeated_float.to_a[0..2]
assert_equal 10.1, m.repeated_float[3].round(1)
m.repeated_double[3] = 10.1
assert_equal [0,0,0,10.1], m.repeated_double
m.repeated_bool[3] = true
assert_equal [false, false, false, true], m.repeated_bool
m.repeated_bytes[3] = "bar".encode!('ASCII-8BIT')
assert_equal ['', '', '', "bar".encode!('ASCII-8BIT')], m.repeated_bytes
m.repeated_msg[3] = TestMessage2.new(:foo => 1)
assert_equal [nil, nil, nil, TestMessage2.new(:foo => 1)], m.repeated_msg
m.repeated_enum[3] = :A
assert_equal [:Default, :Default, :Default, :A], m.repeated_enum
# check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
# arr[20] = 'spacious'
# end
# TODO: accessor doesn't allow other ruby-like methods
# check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
# arr[1, 2] = 'fizz'
# end
# check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
# arr[0..2] = 'buzz'
# end
end
def test_push
m = TestMessage.new
reference_arr = %w[foo bar baz]
m.repeated_string += reference_arr.clone
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr.push('fizz')
end
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr << 'fizz'
end
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr.push('fizz', 'buzz')
end
end
def test_clear
m = TestMessage.new
reference_arr = %w(foo bar baz)
m.repeated_string += reference_arr.clone
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr.clear
end
end
def test_concat
m = TestMessage.new
reference_arr = %w(foo bar baz)
m.repeated_string += reference_arr.clone
m.repeated_string.concat(['fizz', 'buzz'])
assert_equal %w(foo bar baz fizz buzz), m.repeated_string
#TODO: concat should return the orig array
# check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
# arr.concat(['fizz', 'buzz'])
# end
end
def test_equal
m = TestMessage.new
reference_arr = %w(foo bar baz)
m.repeated_string += reference_arr.clone
assert_equal reference_arr, m.repeated_string
reference_arr << 'fizz'
assert_not_equal reference_arr, m.repeated_string
m.repeated_string << 'fizz'
assert_equal reference_arr, m.repeated_string
end
def test_hash
# just a sanity check
m = TestMessage.new
reference_arr = %w(foo bar baz)
m.repeated_string += reference_arr.clone
assert m.repeated_string.hash.is_a?(Integer)
hash = m.repeated_string.hash
assert_equal hash, m.repeated_string.hash
m.repeated_string << 'j'
assert_not_equal hash, m.repeated_string.hash
end
def test_plus
m = TestMessage.new
reference_arr = %w(foo bar baz)
m.repeated_string += reference_arr.clone
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr + ['fizz', 'buzz']
end
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr += ['fizz', 'buzz']
end
end
def test_replace
m = TestMessage.new
reference_arr = %w(foo bar baz)
m.repeated_string += reference_arr.clone
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr.replace(['fizz', 'buzz'])
end
end
def test_to_a
m = TestMessage.new
reference_arr = %w(foo bar baz)
m.repeated_string += reference_arr.clone
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr.to_a
end
end
def test_to_ary
m = TestMessage.new
reference_arr = %w(foo bar baz)
m.repeated_string += reference_arr.clone
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr.to_ary
end
end
# emulate Array behavior
##########################
def test_collect!
m = TestMessage.new
reference_arr = %w(foo bar baz)
m.repeated_string += reference_arr.clone
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr.collect!{|x| x + "!" }
end
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr.collect!.with_index{|x, i| x[0...i] }
end
end
def test_delete
m = TestMessage.new
reference_arr = %w(foo bar baz)
m.repeated_string += reference_arr.clone
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr.delete('bar')
end
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr.delete('nope')
end
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr.delete('nope'){'within'}
end
end
def test_delete_at
m = TestMessage.new
reference_arr = %w(foo bar baz)
m.repeated_string += reference_arr.clone
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr.delete_at(2)
end
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr.delete_at(10)
end
end
def test_delete_if
m = TestMessage.new
reference_arr = %w(foo bar baz)
m.repeated_string += reference_arr.clone
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr.delete_if { |v| v == "bar" }
end
end
def test_fill
m = TestMessage.new
reference_arr = %w(foo bar baz)
m.repeated_string += reference_arr.clone
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr.fill("x")
end
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr.fill("z", 2, 2)
end
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr.fill("y", 0..1)
end
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr.fill { |i| (i*i).to_s }
end
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr.fill(-2) { |i| (i*i*i).to_s }
end
end
def test_flatten!
m = TestMessage.new
reference_arr = %w(foo bar baz)
m.repeated_string += reference_arr.clone
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr.flatten!
end
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr.flatten!(1)
end
end
def test_insert
m = TestMessage.new
reference_arr = %w(foo bar baz)
m.repeated_string += reference_arr.clone
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr.insert(2, 'fizz')
end
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr.insert(3, 'fizz', 'buzz', 'bazz')
end
end
def test_inspect
m = TestMessage.new
assert_equal '[]', m.repeated_string.inspect
m.repeated_string << 'foo'
assert_equal m.repeated_string.to_a.inspect, m.repeated_string.inspect
m.repeated_string << 'bar'
assert_equal m.repeated_string.to_a.inspect, m.repeated_string.inspect
end
def test_reverse!
m = TestMessage.new
reference_arr = %w(foo bar baz)
m.repeated_string += reference_arr.clone
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr.reverse!
end
end
def test_rotate!
m = TestMessage.new
reference_arr = %w(foo bar baz)
m.repeated_string += reference_arr.clone
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr.rotate!
end
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr.rotate!(2)
end
end
def test_select!
m = TestMessage.new
reference_arr = %w(foo bar baz)
m.repeated_string += reference_arr.clone
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr.select! { |v| v =~ /[aeiou]/ }
end
end
def test_shift
m = TestMessage.new
reference_arr = %w(foo bar baz)
m.repeated_string += reference_arr.clone
# should return an element
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr.shift
end
# should return an array
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr.shift(2)
end
# should return nil
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr.shift
end
end
def test_shuffle!
m = TestMessage.new
m.repeated_string += %w(foo bar baz)
orig_repeated_string = m.repeated_string.clone
result = m.repeated_string.shuffle!
assert_equal m.repeated_string, result
# NOTE: sometimes it doesn't change the order...
# assert_not_equal m.repeated_string.to_a, orig_repeated_string.to_a
end
def test_slice!
m = TestMessage.new
reference_arr = %w(foo bar baz bar fizz buzz)
m.repeated_string += reference_arr.clone
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr.slice!(2)
end
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr.slice!(1,2)
end
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr.slice!(0..1)
end
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr.slice!(10)
end
end
def test_sort!
m = TestMessage.new
reference_arr = %w(foo bar baz)
m.repeated_string += reference_arr.clone
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr.sort!
end
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr.sort! { |x,y| y <=> x }
end
end
def test_sort_by!
m = TestMessage.new
reference_arr = %w(foo bar baz)
m.repeated_string += reference_arr.clone
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr.sort_by!
end
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr.sort_by!(&:hash)
end
end
def test_uniq!
m = TestMessage.new
reference_arr = %w(foo bar baz)
m.repeated_string += reference_arr.clone
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr.uniq!
end
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr.uniq!{|s| s[0] }
end
end
def test_unshift
m = TestMessage.new
reference_arr = %w(foo bar baz)
m.repeated_string += reference_arr.clone
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr.unshift('1')
end
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr.unshift('a', 'b')
end
check_self_modifying_method(m.repeated_string, reference_arr) do |arr|
arr.unshift('')
end
end
##### HELPER METHODS
def check_self_modifying_method(repeated_field, ref_array)
expected_result = yield(ref_array)
actual_result = yield(repeated_field)
if expected_result.is_a?(Enumerator)
assert_equal expected_result.to_a, actual_result.to_a
else
assert_equal expected_result, actual_result
end
assert_equal ref_array, repeated_field
end
def repeated_field_names(klass)
klass.descriptor.find_all{|f| f.label == :repeated}.map(&:name)
end
def fill_test_msg(test_msg)
test_msg.repeated_int32 += [-10, -11]
test_msg.repeated_int64 += [-1_000_000, -1_000_001]
test_msg.repeated_uint32 += [10, 11]
test_msg.repeated_uint64 += [1_000_000, 1_000_001]
test_msg.repeated_bool += [true, false]
test_msg.repeated_float += [-1.01, -1.02]
test_msg.repeated_double += [-1.0000000000001, -1.0000000000002]
test_msg.repeated_string += %w(foo bar)
test_msg.repeated_bytes += ["bar".encode!('ASCII-8BIT'), "foo".encode!('ASCII-8BIT')]
test_msg.repeated_msg << TestMessage2.new(:foo => 1)
test_msg.repeated_msg << TestMessage2.new(:foo => 2)
test_msg.repeated_enum << :A
test_msg.repeated_enum << :B
end
pool = Google::Protobuf::DescriptorPool.new
pool.build do
add_message "TestMessage" do
optional :optional_int32, :int32, 1
optional :optional_int64, :int64, 2
optional :optional_uint32, :uint32, 3
optional :optional_uint64, :uint64, 4
optional :optional_bool, :bool, 5
optional :optional_float, :float, 6
optional :optional_double, :double, 7
optional :optional_string, :string, 8
optional :optional_bytes, :bytes, 9
optional :optional_msg, :message, 10, "TestMessage2"
optional :optional_enum, :enum, 11, "TestEnum"
repeated :repeated_int32, :int32, 12
repeated :repeated_int64, :int64, 13
repeated :repeated_uint32, :uint32, 14
repeated :repeated_uint64, :uint64, 15
repeated :repeated_bool, :bool, 16
repeated :repeated_float, :float, 17
repeated :repeated_double, :double, 18
repeated :repeated_string, :string, 19
repeated :repeated_bytes, :bytes, 20
repeated :repeated_msg, :message, 21, "TestMessage2"
repeated :repeated_enum, :enum, 22, "TestEnum"
end
add_message "TestMessage2" do
optional :foo, :int32, 1
end
add_enum "TestEnum" do
value :Default, 0
value :A, 1
value :B, 2
value :C, 3
end
end
TestMessage = pool.lookup("TestMessage").msgclass
TestMessage2 = pool.lookup("TestMessage2").msgclass
TestEnum = pool.lookup("TestEnum").enummodule
end

38
deps/protobuf/ruby/tests/stress.rb vendored Normal file
View File

@ -0,0 +1,38 @@
#!/usr/bin/ruby
require 'google/protobuf'
require 'test/unit'
module StressTest
pool = Google::Protobuf::DescriptorPool.new
pool.build do
add_message "TestMessage" do
optional :a, :int32, 1
repeated :b, :message, 2, "M"
end
add_message "M" do
optional :foo, :string, 1
end
end
TestMessage = pool.lookup("TestMessage").msgclass
M = pool.lookup("M").msgclass
class StressTest < Test::Unit::TestCase
def get_msg
TestMessage.new(:a => 1000,
:b => [M.new(:foo => "hello"),
M.new(:foo => "world")])
end
def test_stress
m = get_msg
data = TestMessage.encode(m)
100_000.times do
mnew = TestMessage.decode(data)
mnew = mnew.dup
assert_equal m.inspect, mnew.inspect
assert TestMessage.encode(mnew) == data
end
end
end
end

View File

@ -0,0 +1,5 @@
syntax = "proto3";
package foo_bar;
message TestImportedMessage {}

View File

@ -0,0 +1,5 @@
syntax = "proto2";
package foo_bar.proto2;
message TestImportedMessage {}

View File

@ -0,0 +1,7 @@
syntax = "proto3";
package foo_bar;
option ruby_package = "A::B";
message TestRubyPackageMessage {}

View File

@ -0,0 +1,7 @@
syntax = "proto2";
package foo_bar_proto2;
option ruby_package = "A::B::Proto2";
message TestRubyPackageMessage {}

176
deps/protobuf/ruby/tests/type_errors.rb vendored Normal file
View File

@ -0,0 +1,176 @@
#!/usr/bin/ruby
# generated_code.rb is in the same directory as this test.
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)))
require 'test/unit'
require 'google/protobuf/well_known_types'
require 'generated_code_pb'
class TestTypeErrors < Test::Unit::TestCase
# Ruby 2.4 unified Fixnum with Integer
IntegerType = Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.4') ? Fixnum : Integer
def test_bad_string
check_error Google::Protobuf::TypeError,
"Invalid argument for string field 'optional_string' (given #{IntegerType.name})." do
A::B::C::TestMessage.new(optional_string: 4)
end
check_error Google::Protobuf::TypeError,
"Invalid argument for string field 'oneof_string' (given #{IntegerType.name})." do
A::B::C::TestMessage.new(oneof_string: 4)
end
check_error ArgumentError,
"Expected array as initializer value for repeated field 'repeated_string' (given String)." do
A::B::C::TestMessage.new(repeated_string: '4')
end
end
def test_bad_float
check_error Google::Protobuf::TypeError,
"Expected number type for float field 'optional_float' (given TrueClass)." do
A::B::C::TestMessage.new(optional_float: true)
end
check_error Google::Protobuf::TypeError,
"Expected number type for float field 'oneof_float' (given TrueClass)." do
A::B::C::TestMessage.new(oneof_float: true)
end
check_error ArgumentError,
"Expected array as initializer value for repeated field 'repeated_float' (given String)." do
A::B::C::TestMessage.new(repeated_float: 'true')
end
end
def test_bad_double
check_error Google::Protobuf::TypeError,
"Expected number type for double field 'optional_double' (given Symbol)." do
A::B::C::TestMessage.new(optional_double: :double)
end
check_error Google::Protobuf::TypeError,
"Expected number type for double field 'oneof_double' (given Symbol)." do
A::B::C::TestMessage.new(oneof_double: :double)
end
check_error ArgumentError,
"Expected array as initializer value for repeated field 'repeated_double' (given FalseClass)." do
A::B::C::TestMessage.new(repeated_double: false)
end
end
def test_bad_bool
check_error Google::Protobuf::TypeError,
"Invalid argument for boolean field 'optional_bool' (given Float)." do
A::B::C::TestMessage.new(optional_bool: 4.4)
end
check_error Google::Protobuf::TypeError,
"Invalid argument for boolean field 'oneof_bool' (given Float)." do
A::B::C::TestMessage.new(oneof_bool: 4.4)
end
check_error ArgumentError,
"Expected array as initializer value for repeated field 'repeated_bool' (given String)." do
A::B::C::TestMessage.new(repeated_bool: 'hi')
end
end
def test_bad_int
check_error Google::Protobuf::TypeError,
"Expected number type for integral field 'optional_int32' (given String)." do
A::B::C::TestMessage.new(optional_int32: 'hi')
end
check_error RangeError,
"Non-integral floating point value assigned to integer field 'optional_int64' (given Float)." do
A::B::C::TestMessage.new(optional_int64: 2.4)
end
check_error Google::Protobuf::TypeError,
"Expected number type for integral field 'optional_uint32' (given Symbol)." do
A::B::C::TestMessage.new(optional_uint32: :thing)
end
check_error Google::Protobuf::TypeError,
"Expected number type for integral field 'optional_uint64' (given FalseClass)." do
A::B::C::TestMessage.new(optional_uint64: false)
end
check_error Google::Protobuf::TypeError,
"Expected number type for integral field 'oneof_int32' (given Symbol)." do
A::B::C::TestMessage.new(oneof_int32: :hi)
end
check_error RangeError,
"Non-integral floating point value assigned to integer field 'oneof_int64' (given Float)." do
A::B::C::TestMessage.new(oneof_int64: 2.4)
end
check_error Google::Protobuf::TypeError,
"Expected number type for integral field 'oneof_uint32' (given String)." do
A::B::C::TestMessage.new(oneof_uint32: 'x')
end
check_error RangeError,
"Non-integral floating point value assigned to integer field 'oneof_uint64' (given Float)." do
A::B::C::TestMessage.new(oneof_uint64: 1.1)
end
check_error ArgumentError,
"Expected array as initializer value for repeated field 'repeated_int32' (given Symbol)." do
A::B::C::TestMessage.new(repeated_int32: :hi)
end
check_error ArgumentError,
"Expected array as initializer value for repeated field 'repeated_int64' (given Float)." do
A::B::C::TestMessage.new(repeated_int64: 2.4)
end
check_error ArgumentError,
"Expected array as initializer value for repeated field 'repeated_uint32' (given String)." do
A::B::C::TestMessage.new(repeated_uint32: 'x')
end
check_error ArgumentError,
"Expected array as initializer value for repeated field 'repeated_uint64' (given Float)." do
A::B::C::TestMessage.new(repeated_uint64: 1.1)
end
end
def test_bad_enum
check_error RangeError,
"Unknown symbol value for enum field 'optional_enum'." do
A::B::C::TestMessage.new(optional_enum: 'enum')
end
check_error RangeError,
"Unknown symbol value for enum field 'oneof_enum'." do
A::B::C::TestMessage.new(oneof_enum: '')
end
check_error ArgumentError,
"Expected array as initializer value for repeated field 'repeated_enum' (given String)." do
A::B::C::TestMessage.new(repeated_enum: '')
end
end
def test_bad_bytes
check_error Google::Protobuf::TypeError,
"Invalid argument for bytes field 'optional_bytes' (given Float)." do
A::B::C::TestMessage.new(optional_bytes: 22.22)
end
check_error Google::Protobuf::TypeError,
"Invalid argument for bytes field 'oneof_bytes' (given Symbol)." do
A::B::C::TestMessage.new(oneof_bytes: :T22)
end
check_error ArgumentError,
"Expected array as initializer value for repeated field 'repeated_bytes' (given Symbol)." do
A::B::C::TestMessage.new(repeated_bytes: :T22)
end
end
def test_bad_msg
check_error Google::Protobuf::TypeError,
"Invalid type #{IntegerType.name} to assign to submessage field 'optional_msg'." do
A::B::C::TestMessage.new(optional_msg: 2)
end
check_error Google::Protobuf::TypeError,
"Invalid type String to assign to submessage field 'oneof_msg'." do
A::B::C::TestMessage.new(oneof_msg: '2')
end
check_error ArgumentError,
"Expected array as initializer value for repeated field 'repeated_msg' (given String)." do
A::B::C::TestMessage.new(repeated_msg: '2')
end
end
def check_error(type, message)
err = assert_raises type do
yield
end
assert_equal message, err.message
end
end

View File

@ -0,0 +1,247 @@
#!/usr/bin/ruby
require 'test/unit'
require 'google/protobuf/well_known_types'
class TestWellKnownTypes < Test::Unit::TestCase
def test_timestamp
ts = Google::Protobuf::Timestamp.new
assert_equal Time.at(0), ts.to_time
ts.seconds = 12345
assert_equal Time.at(12345), ts.to_time
assert_equal 12345, ts.to_i
# millisecond accuracy
time = Time.at(123456, 654321)
resp = ts.from_time(time)
assert_equal 123456, ts.seconds
assert_equal 654321000, ts.nanos
assert_equal time, ts.to_time
assert_equal resp, ts
# nanosecond accuracy
time = Time.at(123456, Rational(654321321, 1000))
resp = ts.from_time(time)
assert_equal 123456, ts.seconds
assert_equal 654321321, ts.nanos
assert_equal time, ts.to_time
assert_equal resp, ts
# Class based initialisation using from_time
time = Time.at(123456, Rational(654321321, 1000))
ts = Google::Protobuf::Timestamp.from_time(time)
assert_equal 123456, ts.seconds
assert_equal 654321321, ts.nanos
assert_equal time, ts.to_time
# Instance method returns the same value as class method
assert_equal Google::Protobuf::Timestamp.new.from_time(time),
Google::Protobuf::Timestamp.from_time(time)
end
def test_duration
duration = Google::Protobuf::Duration.new(seconds: 123, nanos: 456)
assert_equal 123.000000456, duration.to_f
end
def test_struct
struct = Google::Protobuf::Struct.new
substruct = {
"subkey" => 999,
"subkey2" => false
}
sublist = ["abc", 123, {"deepkey" => "deepval"}]
struct["number"] = 12345
struct["boolean-true"] = true
struct["boolean-false"] = false
struct["null"] = nil
struct["string"] = "abcdef"
struct["substruct"] = substruct
struct["sublist"] = sublist
assert_equal 12345, struct["number"]
assert_equal true, struct["boolean-true"]
assert_equal false, struct["boolean-false"]
assert_equal nil, struct["null"]
assert_equal "abcdef", struct["string"]
assert_equal(Google::Protobuf::Struct.from_hash(substruct),
struct["substruct"])
assert_equal(Google::Protobuf::ListValue.from_a(sublist),
struct["sublist"])
assert_equal true, struct.has_key?("null")
assert_equal false, struct.has_key?("missing_key")
should_equal = {
"number" => 12345,
"boolean-true" => true,
"boolean-false" => false,
"null" => nil,
"string" => "abcdef",
"substruct" => {
"subkey" => 999,
"subkey2" => false
},
"sublist" => ["abc", 123, {"deepkey" => "deepval"}]
}
list = struct["sublist"]
list.is_a?(Google::Protobuf::ListValue)
assert_equal "abc", list[0]
assert_equal 123, list[1]
assert_equal({"deepkey" => "deepval"}, list[2].to_h)
# to_h returns a fully-flattened Ruby structure (Hash and Array).
assert_equal(should_equal, struct.to_h)
# Test that we can safely access a missing key
assert_equal(nil, struct["missing_key"])
# Test that we can assign Struct and ListValue directly.
struct["substruct"] = Google::Protobuf::Struct.from_hash(substruct)
struct["sublist"] = Google::Protobuf::ListValue.from_a(sublist)
assert_equal(should_equal, struct.to_h)
struct["sublist"] << nil
should_equal["sublist"] << nil
assert_equal(should_equal, struct.to_h)
assert_equal(should_equal["sublist"].length, struct["sublist"].length)
assert_raise Google::Protobuf::UnexpectedStructType do
struct[123] = 5
end
assert_raise Google::Protobuf::UnexpectedStructType do
struct[5] = Time.new
end
assert_raise Google::Protobuf::UnexpectedStructType do
struct[5] = [Time.new]
end
assert_raise Google::Protobuf::UnexpectedStructType do
struct[5] = {123 => 456}
end
assert_raise Google::Protobuf::UnexpectedStructType do
struct = Google::Protobuf::Struct.new
struct.fields["foo"] = Google::Protobuf::Value.new
# Tries to return a Ruby value for a Value class whose type
# hasn't been filled in.
struct["foo"]
end
end
def test_any
ts = Google::Protobuf::Timestamp.new(seconds: 12345, nanos: 6789)
any = Google::Protobuf::Any.new
any.pack(ts)
assert any.is(Google::Protobuf::Timestamp)
assert_equal ts, any.unpack(Google::Protobuf::Timestamp)
any = Google::Protobuf::Any.pack(ts)
assert any.is(Google::Protobuf::Timestamp)
assert_equal ts, any.unpack(Google::Protobuf::Timestamp)
end
def test_struct_init
s = Google::Protobuf::Struct.new(fields: {'a' => Google::Protobuf::Value.new({number_value: 4.4})})
assert_equal 4.4, s['a']
s = Google::Protobuf::Struct.new(fields: {'a' => {number_value: 2.2}})
assert_equal 2.2, s['a']
s = Google::Protobuf::Struct.new(fields: {a: {number_value: 1.1}})
assert_equal 1.1, s[:a]
end
def test_struct_nested_init
s = Google::Protobuf::Struct.new(
fields: {
'a' => {string_value: 'A'},
'b' => {struct_value: {
fields: {
'x' => {list_value: {values: [{number_value: 1.0}, {string_value: "ok"}]}},
'y' => {bool_value: true}}}
},
'c' => {struct_value: {}}
}
)
assert_equal 'A', s['a']
assert_equal 'A', s[:a]
expected_b_x = [Google::Protobuf::Value.new(number_value: 1.0), Google::Protobuf::Value.new(string_value: "ok")]
assert_equal expected_b_x, s['b']['x'].values
assert_equal expected_b_x, s[:b][:x].values
assert_equal expected_b_x, s['b'][:x].values
assert_equal expected_b_x, s[:b]['x'].values
assert_equal true, s['b']['y']
assert_equal true, s[:b][:y]
assert_equal true, s[:b]['y']
assert_equal true, s['b'][:y]
assert_equal Google::Protobuf::Struct.new, s['c']
assert_equal Google::Protobuf::Struct.new, s[:c]
s = Google::Protobuf::Struct.new(
fields: {
a: {string_value: 'Eh'},
b: {struct_value: {
fields: {
y: {bool_value: false}}}
}
}
)
assert_equal 'Eh', s['a']
assert_equal 'Eh', s[:a]
assert_equal false, s['b']['y']
assert_equal false, s[:b][:y]
assert_equal false, s['b'][:y]
assert_equal false, s[:b]['y']
end
def test_b8325
value_field = Google::Protobuf::ListValue.descriptor.lookup("values")
proto = Google::Protobuf::ListValue.new(
values: [Google::Protobuf::Value.new(string_value: "Hello")]
)
assert_equal '[<Google::Protobuf::Value: string_value: "Hello">]', value_field.get(proto).inspect
end
def test_from_ruby
pb = Google::Protobuf::Value.from_ruby(nil)
assert_equal pb.null_value, :NULL_VALUE
pb = Google::Protobuf::Value.from_ruby(1.23)
assert_equal pb.number_value, 1.23
pb = Google::Protobuf::Value.from_ruby('1.23')
assert_equal pb.string_value, '1.23'
pb = Google::Protobuf::Value.from_ruby(true)
assert_equal pb.bool_value, true
pb = Google::Protobuf::Value.from_ruby(false)
assert_equal pb.bool_value, false
pb = Google::Protobuf::Value.from_ruby(Google::Protobuf::Struct.from_hash({ 'a' => 1, 'b' => '2', 'c' => [1, 2, 3], 'd' => nil, 'e' => true }))
assert_equal pb.struct_value, Google::Protobuf::Struct.from_hash({ 'a' => 1, 'b' => '2', 'c' => [1, 2, 3], 'd' => nil, 'e' => true })
pb = Google::Protobuf::Value.from_ruby({ 'a' => 1, 'b' => '2', 'c' => [1, 2, 3], 'd' => nil, 'e' => true })
assert_equal pb.struct_value, Google::Protobuf::Struct.from_hash({ 'a' => 1, 'b' => '2', 'c' => [1, 2, 3], 'd' => nil, 'e' => true })
pb = Google::Protobuf::Value.from_ruby(Google::Protobuf::ListValue.from_a([1, 2, 3]))
assert_equal pb.list_value, Google::Protobuf::ListValue.from_a([1, 2, 3])
pb = Google::Protobuf::Value.from_ruby([1, 2, 3])
assert_equal pb.list_value, Google::Protobuf::ListValue.from_a([1, 2, 3])
end
end