basic.rb 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. #!/usr/bin/ruby
  2. # basic_test_pb.rb is in the same directory as this test.
  3. $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)))
  4. require 'basic_test_pb'
  5. require 'common_tests'
  6. require 'google/protobuf'
  7. require 'json'
  8. require 'test/unit'
  9. # ------------- generated code --------------
  10. module BasicTest
  11. pool = Google::Protobuf::DescriptorPool.new
  12. pool.build do
  13. add_message "BadFieldNames" do
  14. optional :dup, :int32, 1
  15. optional :class, :int32, 2
  16. end
  17. end
  18. BadFieldNames = pool.lookup("BadFieldNames").msgclass
  19. # ------------ test cases ---------------
  20. class MessageContainerTest < Test::Unit::TestCase
  21. # Required by CommonTests module to resolve proto3 proto classes used in tests.
  22. def proto_module
  23. ::BasicTest
  24. end
  25. include CommonTests
  26. def test_issue_8311_crash
  27. Google::Protobuf::DescriptorPool.generated_pool.build do
  28. add_file("inner.proto", :syntax => :proto3) do
  29. add_message "Inner" do
  30. # Removing either of these fixes the segfault.
  31. optional :foo, :string, 1
  32. optional :bar, :string, 2
  33. end
  34. end
  35. end
  36. Google::Protobuf::DescriptorPool.generated_pool.build do
  37. add_file("outer.proto", :syntax => :proto3) do
  38. add_message "Outer" do
  39. repeated :inners, :message, 1, "Inner"
  40. end
  41. end
  42. end
  43. outer = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("Outer").msgclass
  44. outer.new(
  45. inners: []
  46. )['inners'].to_s
  47. assert_raise Google::Protobuf::TypeError do
  48. outer.new(
  49. inners: [nil]
  50. ).to_s
  51. end
  52. end
  53. def test_issue_8559_crash
  54. msg = TestMessage.new
  55. msg.repeated_int32 = ::Google::Protobuf::RepeatedField.new(:int32, [1, 2, 3])
  56. # TODO: Remove the platform check once https://github.com/jruby/jruby/issues/6818 is released in JRuby 9.3.0.0
  57. GC.start(full_mark: true, immediate_sweep: true) unless RUBY_PLATFORM == "java"
  58. TestMessage.encode(msg)
  59. end
  60. def test_issue_9440
  61. msg = HelloRequest.new
  62. msg.id = 8
  63. assert_equal 8, msg.id
  64. msg.version = '1'
  65. assert_equal 8, msg.id
  66. end
  67. def test_has_field
  68. m = TestSingularFields.new
  69. assert !m.has_singular_msg?
  70. m.singular_msg = TestMessage2.new
  71. assert m.has_singular_msg?
  72. assert TestSingularFields.descriptor.lookup('singular_msg').has?(m)
  73. m = OneofMessage.new
  74. assert !m.has_my_oneof?
  75. m.a = "foo"
  76. assert m.has_my_oneof?
  77. assert_raise NoMethodError do
  78. m.has_a?
  79. end
  80. assert_true OneofMessage.descriptor.lookup('a').has?(m)
  81. m = TestSingularFields.new
  82. assert_raise NoMethodError do
  83. m.has_singular_int32?
  84. end
  85. assert_raise ArgumentError do
  86. TestSingularFields.descriptor.lookup('singular_int32').has?(m)
  87. end
  88. assert_raise NoMethodError do
  89. m.has_singular_string?
  90. end
  91. assert_raise ArgumentError do
  92. TestSingularFields.descriptor.lookup('singular_string').has?(m)
  93. end
  94. assert_raise NoMethodError do
  95. m.has_singular_bool?
  96. end
  97. assert_raise ArgumentError do
  98. TestSingularFields.descriptor.lookup('singular_bool').has?(m)
  99. end
  100. m = TestMessage.new
  101. assert_raise NoMethodError do
  102. m.has_repeated_msg?
  103. end
  104. assert_raise ArgumentError do
  105. TestMessage.descriptor.lookup('repeated_msg').has?(m)
  106. end
  107. end
  108. def test_no_presence
  109. m = TestSingularFields.new
  110. # Explicitly setting to zero does not cause anything to be serialized.
  111. m.singular_int32 = 0
  112. assert_equal "", TestSingularFields.encode(m)
  113. # Explicitly setting to a non-zero value *does* cause serialization.
  114. m.singular_int32 = 1
  115. assert_not_equal "", TestSingularFields.encode(m)
  116. m.singular_int32 = 0
  117. assert_equal "", TestSingularFields.encode(m)
  118. end
  119. def test_set_clear_defaults
  120. m = TestSingularFields.new
  121. m.singular_int32 = -42
  122. assert_equal -42, m.singular_int32
  123. m.clear_singular_int32
  124. assert_equal 0, m.singular_int32
  125. m.singular_int32 = 50
  126. assert_equal 50, m.singular_int32
  127. TestSingularFields.descriptor.lookup('singular_int32').clear(m)
  128. assert_equal 0, m.singular_int32
  129. m.singular_string = "foo bar"
  130. assert_equal "foo bar", m.singular_string
  131. m.clear_singular_string
  132. assert_equal "", m.singular_string
  133. m.singular_string = "foo"
  134. assert_equal "foo", m.singular_string
  135. TestSingularFields.descriptor.lookup('singular_string').clear(m)
  136. assert_equal "", m.singular_string
  137. m.singular_msg = TestMessage2.new(:foo => 42)
  138. assert_equal TestMessage2.new(:foo => 42), m.singular_msg
  139. assert m.has_singular_msg?
  140. m.clear_singular_msg
  141. assert_equal nil, m.singular_msg
  142. assert !m.has_singular_msg?
  143. m.singular_msg = TestMessage2.new(:foo => 42)
  144. assert_equal TestMessage2.new(:foo => 42), m.singular_msg
  145. TestSingularFields.descriptor.lookup('singular_msg').clear(m)
  146. assert_equal nil, m.singular_msg
  147. end
  148. def test_import_proto2
  149. m = TestMessage.new
  150. assert !m.has_optional_proto2_submessage?
  151. m.optional_proto2_submessage = ::FooBar::Proto2::TestImportedMessage.new
  152. assert m.has_optional_proto2_submessage?
  153. assert TestMessage.descriptor.lookup('optional_proto2_submessage').has?(m)
  154. m.clear_optional_proto2_submessage
  155. assert !m.has_optional_proto2_submessage?
  156. end
  157. def test_clear_repeated_fields
  158. m = TestMessage.new
  159. m.repeated_int32.push(1)
  160. assert_equal [1], m.repeated_int32
  161. m.clear_repeated_int32
  162. assert_equal [], m.repeated_int32
  163. m.repeated_int32.push(1)
  164. assert_equal [1], m.repeated_int32
  165. TestMessage.descriptor.lookup('repeated_int32').clear(m)
  166. assert_equal [], m.repeated_int32
  167. m = OneofMessage.new
  168. m.a = "foo"
  169. assert_equal "foo", m.a
  170. assert m.has_my_oneof?
  171. assert_equal :a, m.my_oneof
  172. m.clear_a
  173. assert !m.has_my_oneof?
  174. m.a = "foobar"
  175. assert m.has_my_oneof?
  176. m.clear_my_oneof
  177. assert !m.has_my_oneof?
  178. m.a = "bar"
  179. assert_equal "bar", m.a
  180. assert m.has_my_oneof?
  181. OneofMessage.descriptor.lookup('a').clear(m)
  182. assert !m.has_my_oneof?
  183. end
  184. def test_initialization_map_errors
  185. e = assert_raise ArgumentError do
  186. TestMessage.new(:hello => "world")
  187. end
  188. assert_match(/hello/, e.message)
  189. e = assert_raise ArgumentError do
  190. MapMessage.new(:map_string_int32 => "hello")
  191. end
  192. assert_equal e.message, "Expected Hash object as initializer value for map field 'map_string_int32' (given String)."
  193. e = assert_raise ArgumentError do
  194. TestMessage.new(:repeated_uint32 => "hello")
  195. end
  196. assert_equal e.message, "Expected array as initializer value for repeated field 'repeated_uint32' (given String)."
  197. end
  198. def test_map_field
  199. m = MapMessage.new
  200. assert m.map_string_int32 == {}
  201. assert m.map_string_msg == {}
  202. m = MapMessage.new(
  203. :map_string_int32 => {"a" => 1, "b" => 2},
  204. :map_string_msg => {"a" => TestMessage2.new(:foo => 1),
  205. "b" => TestMessage2.new(:foo => 2)},
  206. :map_string_enum => {"a" => :A, "b" => :B})
  207. assert m.map_string_int32.keys.sort == ["a", "b"]
  208. assert m.map_string_int32["a"] == 1
  209. assert m.map_string_msg["b"].foo == 2
  210. assert m.map_string_enum["a"] == :A
  211. m.map_string_int32["c"] = 3
  212. assert m.map_string_int32["c"] == 3
  213. m.map_string_msg["c"] = TestMessage2.new(:foo => 3)
  214. assert m.map_string_msg["c"] == TestMessage2.new(:foo => 3)
  215. m.map_string_msg.delete("b")
  216. m.map_string_msg.delete("c")
  217. assert m.map_string_msg == { "a" => TestMessage2.new(:foo => 1) }
  218. assert_raise Google::Protobuf::TypeError do
  219. m.map_string_msg["e"] = TestMessage.new # wrong value type
  220. end
  221. # ensure nothing was added by the above
  222. assert m.map_string_msg == { "a" => TestMessage2.new(:foo => 1) }
  223. m.map_string_int32 = Google::Protobuf::Map.new(:string, :int32)
  224. assert_raise Google::Protobuf::TypeError do
  225. m.map_string_int32 = Google::Protobuf::Map.new(:string, :int64)
  226. end
  227. assert_raise Google::Protobuf::TypeError do
  228. m.map_string_int32 = {}
  229. end
  230. assert_raise Google::Protobuf::TypeError do
  231. m = MapMessage.new(:map_string_int32 => { 1 => "I am not a number" })
  232. end
  233. end
  234. def test_map_field_with_symbol
  235. m = MapMessage.new
  236. assert m.map_string_int32 == {}
  237. assert m.map_string_msg == {}
  238. m = MapMessage.new(
  239. :map_string_int32 => {a: 1, "b" => 2},
  240. :map_string_msg => {a: TestMessage2.new(:foo => 1),
  241. b: TestMessage2.new(:foo => 10)})
  242. assert_equal 1, m.map_string_int32[:a]
  243. assert_equal 2, m.map_string_int32[:b]
  244. assert_equal 10, m.map_string_msg[:b].foo
  245. end
  246. def test_map_inspect
  247. m = MapMessage.new(
  248. :map_string_int32 => {"a" => 1, "b" => 2},
  249. :map_string_msg => {"a" => TestMessage2.new(:foo => 1),
  250. "b" => TestMessage2.new(:foo => 2)},
  251. :map_string_enum => {"a" => :A, "b" => :B})
  252. # JRuby doesn't keep consistent ordering so check for either version
  253. 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}>"
  254. 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}>"
  255. inspect_result = m.inspect
  256. assert expected_a == inspect_result || expected_b == inspect_result, "Incorrect inspect result: #{inspect_result}"
  257. end
  258. def test_map_corruption
  259. # This pattern led to a crash in a previous version of upb/protobuf.
  260. m = MapMessage.new(map_string_int32: { "aaa" => 1 })
  261. m.map_string_int32['podid'] = 2
  262. m.map_string_int32['aaa'] = 3
  263. end
  264. def test_map_wrappers
  265. run_asserts = ->(m) {
  266. assert_equal 2.0, m.map_double[0].value
  267. assert_equal 4.0, m.map_float[0].value
  268. assert_equal 3, m.map_int32[0].value
  269. assert_equal 4, m.map_int64[0].value
  270. assert_equal 5, m.map_uint32[0].value
  271. assert_equal 6, m.map_uint64[0].value
  272. assert_equal true, m.map_bool[0].value
  273. assert_equal 'str', m.map_string[0].value
  274. assert_equal 'fun', m.map_bytes[0].value
  275. }
  276. m = proto_module::Wrapper.new(
  277. map_double: {0 => Google::Protobuf::DoubleValue.new(value: 2.0)},
  278. map_float: {0 => Google::Protobuf::FloatValue.new(value: 4.0)},
  279. map_int32: {0 => Google::Protobuf::Int32Value.new(value: 3)},
  280. map_int64: {0 => Google::Protobuf::Int64Value.new(value: 4)},
  281. map_uint32: {0 => Google::Protobuf::UInt32Value.new(value: 5)},
  282. map_uint64: {0 => Google::Protobuf::UInt64Value.new(value: 6)},
  283. map_bool: {0 => Google::Protobuf::BoolValue.new(value: true)},
  284. map_string: {0 => Google::Protobuf::StringValue.new(value: 'str')},
  285. map_bytes: {0 => Google::Protobuf::BytesValue.new(value: 'fun')},
  286. )
  287. run_asserts.call(m)
  288. serialized = proto_module::Wrapper::encode(m)
  289. m2 = proto_module::Wrapper::decode(serialized)
  290. run_asserts.call(m2)
  291. # Test the case where we are serializing directly from the parsed form
  292. # (before anything lazy is materialized).
  293. m3 = proto_module::Wrapper::decode(serialized)
  294. serialized2 = proto_module::Wrapper::encode(m3)
  295. m4 = proto_module::Wrapper::decode(serialized2)
  296. run_asserts.call(m4)
  297. # Test that the lazy form compares equal to the expanded form.
  298. m5 = proto_module::Wrapper::decode(serialized2)
  299. assert_equal m5, m
  300. end
  301. def test_map_wrappers_with_default_values
  302. run_asserts = ->(m) {
  303. assert_equal 0.0, m.map_double[0].value
  304. assert_equal 0.0, m.map_float[0].value
  305. assert_equal 0, m.map_int32[0].value
  306. assert_equal 0, m.map_int64[0].value
  307. assert_equal 0, m.map_uint32[0].value
  308. assert_equal 0, m.map_uint64[0].value
  309. assert_equal false, m.map_bool[0].value
  310. assert_equal '', m.map_string[0].value
  311. assert_equal '', m.map_bytes[0].value
  312. }
  313. m = proto_module::Wrapper.new(
  314. map_double: {0 => Google::Protobuf::DoubleValue.new(value: 0.0)},
  315. map_float: {0 => Google::Protobuf::FloatValue.new(value: 0.0)},
  316. map_int32: {0 => Google::Protobuf::Int32Value.new(value: 0)},
  317. map_int64: {0 => Google::Protobuf::Int64Value.new(value: 0)},
  318. map_uint32: {0 => Google::Protobuf::UInt32Value.new(value: 0)},
  319. map_uint64: {0 => Google::Protobuf::UInt64Value.new(value: 0)},
  320. map_bool: {0 => Google::Protobuf::BoolValue.new(value: false)},
  321. map_string: {0 => Google::Protobuf::StringValue.new(value: '')},
  322. map_bytes: {0 => Google::Protobuf::BytesValue.new(value: '')},
  323. )
  324. run_asserts.call(m)
  325. serialized = proto_module::Wrapper::encode(m)
  326. m2 = proto_module::Wrapper::decode(serialized)
  327. run_asserts.call(m2)
  328. # Test the case where we are serializing directly from the parsed form
  329. # (before anything lazy is materialized).
  330. m3 = proto_module::Wrapper::decode(serialized)
  331. serialized2 = proto_module::Wrapper::encode(m3)
  332. m4 = proto_module::Wrapper::decode(serialized2)
  333. run_asserts.call(m4)
  334. # Test that the lazy form compares equal to the expanded form.
  335. m5 = proto_module::Wrapper::decode(serialized2)
  336. assert_equal m5, m
  337. end
  338. def test_map_wrappers_with_no_value
  339. run_asserts = ->(m) {
  340. assert_equal 0.0, m.map_double[0].value
  341. assert_equal 0.0, m.map_float[0].value
  342. assert_equal 0, m.map_int32[0].value
  343. assert_equal 0, m.map_int64[0].value
  344. assert_equal 0, m.map_uint32[0].value
  345. assert_equal 0, m.map_uint64[0].value
  346. assert_equal false, m.map_bool[0].value
  347. assert_equal '', m.map_string[0].value
  348. assert_equal '', m.map_bytes[0].value
  349. }
  350. m = proto_module::Wrapper.new(
  351. map_double: {0 => Google::Protobuf::DoubleValue.new()},
  352. map_float: {0 => Google::Protobuf::FloatValue.new()},
  353. map_int32: {0 => Google::Protobuf::Int32Value.new()},
  354. map_int64: {0 => Google::Protobuf::Int64Value.new()},
  355. map_uint32: {0 => Google::Protobuf::UInt32Value.new()},
  356. map_uint64: {0 => Google::Protobuf::UInt64Value.new()},
  357. map_bool: {0 => Google::Protobuf::BoolValue.new()},
  358. map_string: {0 => Google::Protobuf::StringValue.new()},
  359. map_bytes: {0 => Google::Protobuf::BytesValue.new()},
  360. )
  361. run_asserts.call(m)
  362. serialized = proto_module::Wrapper::encode(m)
  363. m2 = proto_module::Wrapper::decode(serialized)
  364. run_asserts.call(m2)
  365. # Test the case where we are serializing directly from the parsed form
  366. # (before anything lazy is materialized).
  367. m3 = proto_module::Wrapper::decode(serialized)
  368. serialized2 = proto_module::Wrapper::encode(m3)
  369. m4 = proto_module::Wrapper::decode(serialized2)
  370. run_asserts.call(m4)
  371. end
  372. def test_concurrent_decoding
  373. o = Outer.new
  374. o.items[0] = Inner.new
  375. raw = Outer.encode(o)
  376. thds = 2.times.map do
  377. Thread.new do
  378. 100000.times do
  379. assert_equal o, Outer.decode(raw)
  380. end
  381. end
  382. end
  383. thds.map(&:join)
  384. end
  385. def test_map_encode_decode
  386. m = MapMessage.new(
  387. :map_string_int32 => {"a" => 1, "b" => 2},
  388. :map_string_msg => {"a" => TestMessage2.new(:foo => 1),
  389. "b" => TestMessage2.new(:foo => 2)},
  390. :map_string_enum => {"a" => :A, "b" => :B})
  391. m2 = MapMessage.decode(MapMessage.encode(m))
  392. assert m == m2
  393. m3 = MapMessageWireEquiv.decode(MapMessage.encode(m))
  394. assert m3.map_string_int32.length == 2
  395. kv = {}
  396. m3.map_string_int32.map { |msg| kv[msg.key] = msg.value }
  397. assert kv == {"a" => 1, "b" => 2}
  398. kv = {}
  399. m3.map_string_msg.map { |msg| kv[msg.key] = msg.value }
  400. assert kv == {"a" => TestMessage2.new(:foo => 1),
  401. "b" => TestMessage2.new(:foo => 2)}
  402. end
  403. def test_protobuf_decode_json_ignore_unknown_fields
  404. m = TestMessage.decode_json({
  405. optional_string: "foo",
  406. not_in_message: "some_value"
  407. }.to_json, { ignore_unknown_fields: true })
  408. assert_equal m.optional_string, "foo"
  409. e = assert_raise Google::Protobuf::ParseError do
  410. TestMessage.decode_json({ not_in_message: "some_value" }.to_json)
  411. end
  412. assert_match(/No such field: not_in_message/, e.message)
  413. end
  414. #def test_json_quoted_string
  415. # m = TestMessage.decode_json(%q(
  416. # "optionalInt64": "1",,
  417. # }))
  418. # puts(m)
  419. # assert_equal 1, m.optional_int32
  420. #end
  421. def test_to_h
  422. m = TestMessage.new(:optional_bool => true, :optional_double => -10.100001, :optional_string => 'foo', :repeated_string => ['bar1', 'bar2'], :repeated_msg => [TestMessage2.new(:foo => 100)])
  423. expected_result = {
  424. :optional_bool=>true,
  425. :optional_bytes=>"",
  426. :optional_double=>-10.100001,
  427. :optional_enum=>:Default,
  428. :optional_float=>0.0,
  429. :optional_int32=>0,
  430. :optional_int64=>0,
  431. :optional_msg=>nil,
  432. :optional_msg2=>nil,
  433. :optional_proto2_submessage=>nil,
  434. :optional_string=>"foo",
  435. :optional_uint32=>0,
  436. :optional_uint64=>0,
  437. :repeated_bool=>[],
  438. :repeated_bytes=>[],
  439. :repeated_double=>[],
  440. :repeated_enum=>[],
  441. :repeated_float=>[],
  442. :repeated_int32=>[],
  443. :repeated_int64=>[],
  444. :repeated_msg=>[{:foo => 100}],
  445. :repeated_string=>["bar1", "bar2"],
  446. :repeated_uint32=>[],
  447. :repeated_uint64=>[]
  448. }
  449. assert_equal expected_result, m.to_h
  450. m = MapMessage.new(
  451. :map_string_int32 => {"a" => 1, "b" => 2},
  452. :map_string_msg => {"a" => TestMessage2.new(:foo => 1),
  453. "b" => TestMessage2.new(:foo => 2)},
  454. :map_string_enum => {"a" => :A, "b" => :B})
  455. expected_result = {
  456. :map_string_int32 => {"a" => 1, "b" => 2},
  457. :map_string_msg => {"a" => {:foo => 1}, "b" => {:foo => 2}},
  458. :map_string_enum => {"a" => :A, "b" => :B}
  459. }
  460. assert_equal expected_result, m.to_h
  461. end
  462. def test_json_maps
  463. # TODO: Fix JSON in JRuby version.
  464. return if RUBY_PLATFORM == "java"
  465. m = MapMessage.new(:map_string_int32 => {"a" => 1})
  466. expected = {mapStringInt32: {a: 1}, mapStringMsg: {}, mapStringEnum: {}}
  467. expected_preserve = {map_string_int32: {a: 1}, map_string_msg: {}, map_string_enum: {}}
  468. assert_equal JSON.parse(MapMessage.encode_json(m, :emit_defaults=>true), :symbolize_names => true), expected
  469. json = MapMessage.encode_json(m, :preserve_proto_fieldnames => true, :emit_defaults=>true)
  470. assert_equal JSON.parse(json, :symbolize_names => true), expected_preserve
  471. m2 = MapMessage.decode_json(MapMessage.encode_json(m))
  472. assert_equal m, m2
  473. end
  474. def test_json_maps_emit_defaults_submsg
  475. # TODO: Fix JSON in JRuby version.
  476. return if RUBY_PLATFORM == "java"
  477. m = MapMessage.new(:map_string_msg => {"a" => TestMessage2.new(foo: 0)})
  478. expected = {mapStringInt32: {}, mapStringMsg: {a: {foo: 0}}, mapStringEnum: {}}
  479. actual = MapMessage.encode_json(m, :emit_defaults => true)
  480. assert_equal JSON.parse(actual, :symbolize_names => true), expected
  481. end
  482. def test_json_emit_defaults_submsg
  483. # TODO: Fix JSON in JRuby version.
  484. return if RUBY_PLATFORM == "java"
  485. m = TestSingularFields.new(singular_msg: proto_module::TestMessage2.new)
  486. expected = {
  487. singularInt32: 0,
  488. singularInt64: "0",
  489. singularUint32: 0,
  490. singularUint64: "0",
  491. singularBool: false,
  492. singularFloat: 0,
  493. singularDouble: 0,
  494. singularString: "",
  495. singularBytes: "",
  496. singularMsg: {},
  497. singularEnum: "Default",
  498. }
  499. actual = proto_module::TestMessage.encode_json(m, :emit_defaults => true)
  500. assert_equal expected, JSON.parse(actual, :symbolize_names => true)
  501. end
  502. def test_respond_to
  503. # This test fails with JRuby 1.7.23, likely because of an old JRuby bug.
  504. return if RUBY_PLATFORM == "java"
  505. msg = MapMessage.new
  506. assert msg.respond_to?(:map_string_int32)
  507. assert !msg.respond_to?(:bacon)
  508. end
  509. def test_file_descriptor
  510. file_descriptor = TestMessage.descriptor.file_descriptor
  511. assert nil != file_descriptor
  512. assert_equal "tests/basic_test.proto", file_descriptor.name
  513. assert_equal :proto3, file_descriptor.syntax
  514. file_descriptor = TestEnum.descriptor.file_descriptor
  515. assert nil != file_descriptor
  516. assert_equal "tests/basic_test.proto", file_descriptor.name
  517. assert_equal :proto3, file_descriptor.syntax
  518. end
  519. # Ruby 2.5 changed to raise FrozenError instead of RuntimeError
  520. FrozenErrorType = Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.5') ? RuntimeError : FrozenError
  521. def test_map_freeze
  522. m = proto_module::MapMessage.new
  523. m.map_string_int32['a'] = 5
  524. m.map_string_msg['b'] = proto_module::TestMessage2.new
  525. m.map_string_int32.freeze
  526. m.map_string_msg.freeze
  527. assert m.map_string_int32.frozen?
  528. assert m.map_string_msg.frozen?
  529. assert_raise(FrozenErrorType) { m.map_string_int32['foo'] = 1 }
  530. assert_raise(FrozenErrorType) { m.map_string_msg['bar'] = proto_module::TestMessage2.new }
  531. assert_raise(FrozenErrorType) { m.map_string_int32.delete('a') }
  532. assert_raise(FrozenErrorType) { m.map_string_int32.clear }
  533. end
  534. def test_map_length
  535. m = proto_module::MapMessage.new
  536. assert_equal 0, m.map_string_int32.length
  537. assert_equal 0, m.map_string_msg.length
  538. assert_equal 0, m.map_string_int32.size
  539. assert_equal 0, m.map_string_msg.size
  540. m.map_string_int32['a'] = 1
  541. m.map_string_int32['b'] = 2
  542. m.map_string_msg['a'] = proto_module::TestMessage2.new
  543. assert_equal 2, m.map_string_int32.length
  544. assert_equal 1, m.map_string_msg.length
  545. assert_equal 2, m.map_string_int32.size
  546. assert_equal 1, m.map_string_msg.size
  547. end
  548. end
  549. end