2014-11-18 22:19:58 +00:00
|
|
|
#!/usr/bin/ruby
|
|
|
|
|
2017-08-25 19:11:15 +00:00
|
|
|
# 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'
|
2014-12-12 23:58:26 +00:00
|
|
|
require 'google/protobuf'
|
2017-03-17 09:28:17 +00:00
|
|
|
require 'json'
|
2014-11-18 22:19:58 +00:00
|
|
|
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
|
|
|
|
optional :"a.b", :int32, 3
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
BadFieldNames = pool.lookup("BadFieldNames").msgclass
|
|
|
|
|
|
|
|
# ------------ test cases ---------------
|
|
|
|
|
|
|
|
class MessageContainerTest < Test::Unit::TestCase
|
2017-08-25 19:11:15 +00:00
|
|
|
# Required by CommonTests module to resolve proto3 proto classes used in tests.
|
|
|
|
def proto_module
|
|
|
|
::BasicTest
|
2015-11-20 20:57:28 +00:00
|
|
|
end
|
2017-08-25 19:11:15 +00:00
|
|
|
include CommonTests
|
2015-11-20 20:57:28 +00:00
|
|
|
|
2017-08-25 19:11:15 +00:00
|
|
|
def test_has_field
|
2014-11-18 22:19:58 +00:00
|
|
|
m = TestMessage.new
|
2018-10-08 20:16:56 +00:00
|
|
|
assert !m.has_optional_msg?
|
2017-08-25 19:11:15 +00:00
|
|
|
m.optional_msg = TestMessage2.new
|
2018-10-08 20:16:56 +00:00
|
|
|
assert m.has_optional_msg?
|
|
|
|
assert TestMessage.descriptor.lookup('optional_msg').has?(m)
|
2017-08-25 19:11:15 +00:00
|
|
|
|
|
|
|
m = OneofMessage.new
|
2018-10-08 20:16:56 +00:00
|
|
|
assert !m.has_my_oneof?
|
2017-08-25 19:11:15 +00:00
|
|
|
m.a = "foo"
|
2018-10-08 20:16:56 +00:00
|
|
|
assert m.has_my_oneof?
|
2017-08-25 19:11:15 +00:00
|
|
|
assert_raise NoMethodError do
|
|
|
|
m.has_a?
|
2014-11-18 22:19:58 +00:00
|
|
|
end
|
2017-08-25 19:11:15 +00:00
|
|
|
assert_raise ArgumentError do
|
|
|
|
OneofMessage.descriptor.lookup('a').has?(m)
|
2014-11-18 22:19:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
m = TestMessage.new
|
2017-08-25 19:11:15 +00:00
|
|
|
assert_raise NoMethodError do
|
|
|
|
m.has_optional_int32?
|
2014-11-18 22:19:58 +00:00
|
|
|
end
|
2017-08-25 19:11:15 +00:00
|
|
|
assert_raise ArgumentError do
|
|
|
|
TestMessage.descriptor.lookup('optional_int32').has?(m)
|
2014-11-18 22:19:58 +00:00
|
|
|
end
|
|
|
|
|
2017-08-25 19:11:15 +00:00
|
|
|
assert_raise NoMethodError do
|
|
|
|
m.has_optional_string?
|
2014-11-18 22:19:58 +00:00
|
|
|
end
|
2017-08-25 19:11:15 +00:00
|
|
|
assert_raise ArgumentError do
|
|
|
|
TestMessage.descriptor.lookup('optional_string').has?(m)
|
2014-11-18 22:19:58 +00:00
|
|
|
end
|
|
|
|
|
2017-08-25 19:11:15 +00:00
|
|
|
assert_raise NoMethodError do
|
|
|
|
m.has_optional_bool?
|
2014-11-18 22:19:58 +00:00
|
|
|
end
|
|
|
|
assert_raise ArgumentError do
|
2017-08-25 19:11:15 +00:00
|
|
|
TestMessage.descriptor.lookup('optional_bool').has?(m)
|
2014-11-18 22:19:58 +00:00
|
|
|
end
|
2017-08-25 19:11:15 +00:00
|
|
|
|
|
|
|
assert_raise NoMethodError do
|
|
|
|
m.has_repeated_msg?
|
2014-11-18 22:19:58 +00:00
|
|
|
end
|
|
|
|
assert_raise ArgumentError do
|
2017-08-25 19:11:15 +00:00
|
|
|
TestMessage.descriptor.lookup('repeated_msg').has?(m)
|
2014-11-18 22:19:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-25 19:11:15 +00:00
|
|
|
def test_set_clear_defaults
|
|
|
|
m = TestMessage.new
|
2015-05-01 18:54:29 +00:00
|
|
|
|
2017-08-25 19:11:15 +00:00
|
|
|
m.optional_int32 = -42
|
|
|
|
assert_equal -42, m.optional_int32
|
|
|
|
m.clear_optional_int32
|
|
|
|
assert_equal 0, m.optional_int32
|
2015-05-01 15:54:18 +00:00
|
|
|
|
2017-08-25 19:11:15 +00:00
|
|
|
m.optional_int32 = 50
|
|
|
|
assert_equal 50, m.optional_int32
|
|
|
|
TestMessage.descriptor.lookup('optional_int32').clear(m)
|
|
|
|
assert_equal 0, m.optional_int32
|
2015-01-06 23:44:09 +00:00
|
|
|
|
2017-08-25 19:11:15 +00:00
|
|
|
m.optional_string = "foo bar"
|
|
|
|
assert_equal "foo bar", m.optional_string
|
|
|
|
m.clear_optional_string
|
|
|
|
assert_equal "", m.optional_string
|
2015-01-06 23:44:09 +00:00
|
|
|
|
2017-08-25 19:11:15 +00:00
|
|
|
m.optional_string = "foo"
|
|
|
|
assert_equal "foo", m.optional_string
|
|
|
|
TestMessage.descriptor.lookup('optional_string').clear(m)
|
|
|
|
assert_equal "", m.optional_string
|
2015-01-06 23:44:09 +00:00
|
|
|
|
2017-08-25 19:11:15 +00:00
|
|
|
m.optional_msg = TestMessage2.new(:foo => 42)
|
|
|
|
assert_equal TestMessage2.new(:foo => 42), m.optional_msg
|
2018-10-08 20:16:56 +00:00
|
|
|
assert m.has_optional_msg?
|
2017-08-25 19:11:15 +00:00
|
|
|
m.clear_optional_msg
|
|
|
|
assert_equal nil, m.optional_msg
|
2018-10-08 20:16:56 +00:00
|
|
|
assert !m.has_optional_msg?
|
2015-01-06 23:44:09 +00:00
|
|
|
|
2017-08-25 19:11:15 +00:00
|
|
|
m.optional_msg = TestMessage2.new(:foo => 42)
|
|
|
|
assert_equal TestMessage2.new(:foo => 42), m.optional_msg
|
|
|
|
TestMessage.descriptor.lookup('optional_msg').clear(m)
|
|
|
|
assert_equal nil, m.optional_msg
|
2015-01-06 23:44:09 +00:00
|
|
|
|
2017-08-25 19:11:15 +00:00
|
|
|
m.repeated_int32.push(1)
|
|
|
|
assert_equal [1], m.repeated_int32
|
|
|
|
m.clear_repeated_int32
|
|
|
|
assert_equal [], m.repeated_int32
|
2015-01-06 23:44:09 +00:00
|
|
|
|
2017-08-25 19:11:15 +00:00
|
|
|
m.repeated_int32.push(1)
|
|
|
|
assert_equal [1], m.repeated_int32
|
|
|
|
TestMessage.descriptor.lookup('repeated_int32').clear(m)
|
|
|
|
assert_equal [], m.repeated_int32
|
2015-01-06 23:44:09 +00:00
|
|
|
|
2017-08-25 19:11:15 +00:00
|
|
|
m = OneofMessage.new
|
|
|
|
m.a = "foo"
|
|
|
|
assert_equal "foo", m.a
|
2018-10-08 20:16:56 +00:00
|
|
|
assert m.has_my_oneof?
|
2017-08-25 19:11:15 +00:00
|
|
|
m.clear_a
|
2018-10-08 20:16:56 +00:00
|
|
|
assert !m.has_my_oneof?
|
2015-01-06 23:44:09 +00:00
|
|
|
|
2017-08-25 19:11:15 +00:00
|
|
|
m.a = "foobar"
|
2018-10-08 20:16:56 +00:00
|
|
|
assert m.has_my_oneof?
|
2017-08-25 19:11:15 +00:00
|
|
|
m.clear_my_oneof
|
2018-10-08 20:16:56 +00:00
|
|
|
assert !m.has_my_oneof?
|
2015-01-06 23:44:09 +00:00
|
|
|
|
2017-08-25 19:11:15 +00:00
|
|
|
m.a = "bar"
|
|
|
|
assert_equal "bar", m.a
|
2018-10-08 20:16:56 +00:00
|
|
|
assert m.has_my_oneof?
|
2017-08-25 19:11:15 +00:00
|
|
|
OneofMessage.descriptor.lookup('a').clear(m)
|
2018-10-08 20:16:56 +00:00
|
|
|
assert !m.has_my_oneof?
|
2015-01-06 23:44:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2017-08-25 19:11:15 +00:00
|
|
|
def test_initialization_map_errors
|
|
|
|
e = assert_raise ArgumentError do
|
|
|
|
TestMessage.new(:hello => "world")
|
2015-01-06 23:44:09 +00:00
|
|
|
end
|
2017-08-25 19:11:15 +00:00
|
|
|
assert_match(/hello/, e.message)
|
2015-01-06 23:44:09 +00:00
|
|
|
|
2017-08-25 19:11:15 +00:00
|
|
|
e = assert_raise ArgumentError do
|
|
|
|
MapMessage.new(:map_string_int32 => "hello")
|
2015-01-06 23:44:09 +00:00
|
|
|
end
|
2017-08-25 19:11:15 +00:00
|
|
|
assert_equal e.message, "Expected Hash object as initializer value for map field 'map_string_int32'."
|
2015-01-06 23:44:09 +00:00
|
|
|
|
2017-08-25 19:11:15 +00:00
|
|
|
e = assert_raise ArgumentError do
|
|
|
|
TestMessage.new(:repeated_uint32 => "hello")
|
2015-01-06 23:44:09 +00:00
|
|
|
end
|
2017-08-25 19:11:15 +00:00
|
|
|
assert_equal e.message, "Expected array as initializer value for repeated field 'repeated_uint32'."
|
2015-01-06 23:44:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_map_field
|
|
|
|
m = MapMessage.new
|
|
|
|
assert m.map_string_int32 == {}
|
|
|
|
assert m.map_string_msg == {}
|
|
|
|
|
2015-01-13 21:47:58 +00:00
|
|
|
m = MapMessage.new(
|
|
|
|
:map_string_int32 => {"a" => 1, "b" => 2},
|
|
|
|
:map_string_msg => {"a" => TestMessage2.new(:foo => 1),
|
|
|
|
"b" => TestMessage2.new(:foo => 2)})
|
2015-01-06 23:44:09 +00:00
|
|
|
assert m.map_string_int32.keys.sort == ["a", "b"]
|
|
|
|
assert m.map_string_int32["a"] == 1
|
|
|
|
assert m.map_string_msg["b"].foo == 2
|
|
|
|
|
|
|
|
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) }
|
|
|
|
|
2018-06-27 03:24:24 +00:00
|
|
|
assert_raise Google::Protobuf::TypeError do
|
2015-01-06 23:44:09 +00:00
|
|
|
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)
|
2018-06-27 03:24:24 +00:00
|
|
|
assert_raise Google::Protobuf::TypeError do
|
2015-01-06 23:44:09 +00:00
|
|
|
m.map_string_int32 = Google::Protobuf::Map.new(:string, :int64)
|
|
|
|
end
|
2018-06-27 03:24:24 +00:00
|
|
|
assert_raise Google::Protobuf::TypeError do
|
2015-01-06 23:44:09 +00:00
|
|
|
m.map_string_int32 = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_raise TypeError do
|
|
|
|
m = MapMessage.new(:map_string_int32 => { 1 => "I am not a number" })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-25 19:11:15 +00:00
|
|
|
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)})
|
|
|
|
expected = "<BasicTest::MapMessage: map_string_int32: {\"b\"=>2, \"a\"=>1}, map_string_msg: {\"b\"=><BasicTest::TestMessage2: foo: 2>, \"a\"=><BasicTest::TestMessage2: foo: 1>}>"
|
|
|
|
assert_equal expected, m.inspect
|
|
|
|
end
|
|
|
|
|
2017-01-31 17:17:32 +00:00
|
|
|
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
|
|
|
|
|
2017-08-24 05:03:18 +00:00
|
|
|
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
|
|
|
|
|
2015-01-06 23:44:09 +00:00
|
|
|
def test_map_encode_decode
|
2015-01-13 21:47:58 +00:00
|
|
|
m = MapMessage.new(
|
|
|
|
:map_string_int32 => {"a" => 1, "b" => 2},
|
|
|
|
:map_string_msg => {"a" => TestMessage2.new(:foo => 1),
|
|
|
|
"b" => TestMessage2.new(:foo => 2)})
|
2015-01-06 23:44:09 +00:00
|
|
|
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
|
|
|
|
|
2019-01-08 18:53:34 +00:00
|
|
|
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
|
|
|
|
|
2015-05-03 17:55:10 +00:00
|
|
|
def test_to_h
|
2017-09-14 15:35:13 +00:00
|
|
|
m = TestMessage.new(:optional_bool => true, :optional_double => -10.100001, :optional_string => 'foo', :repeated_string => ['bar1', 'bar2'], :repeated_msg => [TestMessage2.new(:foo => 100)])
|
2015-05-03 17:55:10 +00:00
|
|
|
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_string=>"foo",
|
|
|
|
:optional_uint32=>0,
|
|
|
|
:optional_uint64=>0,
|
|
|
|
:repeated_bool=>[],
|
|
|
|
:repeated_bytes=>[],
|
|
|
|
:repeated_double=>[],
|
|
|
|
:repeated_enum=>[],
|
|
|
|
:repeated_float=>[],
|
|
|
|
:repeated_int32=>[],
|
|
|
|
:repeated_int64=>[],
|
2017-09-14 15:35:13 +00:00
|
|
|
:repeated_msg=>[{:foo => 100}],
|
2015-05-03 17:55:10 +00:00
|
|
|
:repeated_string=>["bar1", "bar2"],
|
|
|
|
:repeated_uint32=>[],
|
|
|
|
:repeated_uint64=>[]
|
|
|
|
}
|
|
|
|
assert_equal expected_result, m.to_h
|
2017-03-14 21:27:16 +00:00
|
|
|
|
|
|
|
m = MapMessage.new(
|
|
|
|
:map_string_int32 => {"a" => 1, "b" => 2},
|
|
|
|
:map_string_msg => {"a" => TestMessage2.new(:foo => 1),
|
|
|
|
"b" => TestMessage2.new(:foo => 2)})
|
|
|
|
expected_result = {
|
2017-03-15 17:35:15 +00:00
|
|
|
:map_string_int32 => {"a" => 1, "b" => 2},
|
|
|
|
:map_string_msg => {"a" => {:foo => 1}, "b" => {:foo => 2}}
|
2017-03-14 21:27:16 +00:00
|
|
|
}
|
|
|
|
assert_equal expected_result, m.to_h
|
2015-05-03 17:55:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2015-02-02 23:07:34 +00:00
|
|
|
def test_json_maps
|
2015-05-13 21:58:48 +00:00
|
|
|
# TODO: Fix JSON in JRuby version.
|
|
|
|
return if RUBY_PLATFORM == "java"
|
2015-02-02 23:07:34 +00:00
|
|
|
m = MapMessage.new(:map_string_int32 => {"a" => 1})
|
2017-03-17 09:28:17 +00:00
|
|
|
expected = {mapStringInt32: {a: 1}, mapStringMsg: {}}
|
|
|
|
expected_preserve = {map_string_int32: {a: 1}, map_string_msg: {}}
|
|
|
|
assert JSON.parse(MapMessage.encode_json(m), :symbolize_names => true) == expected
|
2016-04-15 01:33:17 +00:00
|
|
|
|
|
|
|
json = MapMessage.encode_json(m, :preserve_proto_fieldnames => true)
|
2017-03-17 09:28:17 +00:00
|
|
|
assert JSON.parse(json, :symbolize_names => true) == expected_preserve
|
2016-04-15 01:33:17 +00:00
|
|
|
|
2015-02-02 23:07:34 +00:00
|
|
|
m2 = MapMessage.decode_json(MapMessage.encode_json(m))
|
|
|
|
assert m == m2
|
|
|
|
end
|
2016-11-03 13:18:28 +00:00
|
|
|
|
2017-03-09 19:47:56 +00:00
|
|
|
def test_json_maps_emit_defaults_submsg
|
|
|
|
# TODO: Fix JSON in JRuby version.
|
|
|
|
return if RUBY_PLATFORM == "java"
|
|
|
|
m = MapMessage.new(:map_string_msg => {"a" => TestMessage2.new})
|
2017-03-17 09:28:17 +00:00
|
|
|
expected = {mapStringInt32: {}, mapStringMsg: {a: {foo: 0}}}
|
|
|
|
|
|
|
|
actual = MapMessage.encode_json(m, :emit_defaults => true)
|
|
|
|
|
|
|
|
assert JSON.parse(actual, :symbolize_names => true) == expected
|
2017-03-09 19:47:56 +00:00
|
|
|
end
|
|
|
|
|
2016-11-03 13:18:28 +00:00
|
|
|
def test_respond_to
|
2016-11-23 16:45:20 +00:00
|
|
|
# This test fails with JRuby 1.7.23, likely because of an old JRuby bug.
|
|
|
|
return if RUBY_PLATFORM == "java"
|
2016-11-03 13:18:28 +00:00
|
|
|
msg = MapMessage.new
|
|
|
|
assert msg.respond_to?(:map_string_int32)
|
2016-11-23 16:45:20 +00:00
|
|
|
assert !msg.respond_to?(:bacon)
|
2016-11-03 13:18:28 +00:00
|
|
|
end
|
2017-08-25 19:11:15 +00:00
|
|
|
|
|
|
|
def test_file_descriptor
|
|
|
|
file_descriptor = TestMessage.descriptor.file_descriptor
|
2018-10-08 20:16:56 +00:00
|
|
|
assert nil != file_descriptor
|
2017-08-25 19:11:15 +00:00
|
|
|
assert_equal "tests/basic_test.proto", file_descriptor.name
|
|
|
|
assert_equal :proto3, file_descriptor.syntax
|
|
|
|
|
|
|
|
file_descriptor = TestEnum.descriptor.file_descriptor
|
2018-10-08 20:16:56 +00:00
|
|
|
assert nil != file_descriptor
|
2017-08-25 19:11:15 +00:00
|
|
|
assert_equal "tests/basic_test.proto", file_descriptor.name
|
|
|
|
assert_equal :proto3, file_descriptor.syntax
|
|
|
|
|
|
|
|
file_descriptor = BadFieldNames.descriptor.file_descriptor
|
2018-10-08 20:16:56 +00:00
|
|
|
assert nil != file_descriptor
|
2017-08-25 19:11:15 +00:00
|
|
|
assert_equal nil, file_descriptor.name
|
|
|
|
assert_equal :proto3, file_descriptor.syntax
|
|
|
|
end
|
2014-11-18 22:19:58 +00:00
|
|
|
end
|
|
|
|
end
|