basic.rb 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. optional :"a.b", :int32, 3
  17. end
  18. end
  19. BadFieldNames = pool.lookup("BadFieldNames").msgclass
  20. # ------------ test cases ---------------
  21. class MessageContainerTest < Test::Unit::TestCase
  22. # Required by CommonTests module to resolve proto3 proto classes used in tests.
  23. def proto_module
  24. ::BasicTest
  25. end
  26. include CommonTests
  27. def test_has_field
  28. m = TestMessage.new
  29. assert !m.has_optional_msg?
  30. m.optional_msg = TestMessage2.new
  31. assert m.has_optional_msg?
  32. assert TestMessage.descriptor.lookup('optional_msg').has?(m)
  33. m = OneofMessage.new
  34. assert !m.has_my_oneof?
  35. m.a = "foo"
  36. assert m.has_my_oneof?
  37. assert_raise NoMethodError do
  38. m.has_a?
  39. end
  40. assert_raise ArgumentError do
  41. OneofMessage.descriptor.lookup('a').has?(m)
  42. end
  43. m = TestMessage.new
  44. assert_raise NoMethodError do
  45. m.has_optional_int32?
  46. end
  47. assert_raise ArgumentError do
  48. TestMessage.descriptor.lookup('optional_int32').has?(m)
  49. end
  50. assert_raise NoMethodError do
  51. m.has_optional_string?
  52. end
  53. assert_raise ArgumentError do
  54. TestMessage.descriptor.lookup('optional_string').has?(m)
  55. end
  56. assert_raise NoMethodError do
  57. m.has_optional_bool?
  58. end
  59. assert_raise ArgumentError do
  60. TestMessage.descriptor.lookup('optional_bool').has?(m)
  61. end
  62. assert_raise NoMethodError do
  63. m.has_repeated_msg?
  64. end
  65. assert_raise ArgumentError do
  66. TestMessage.descriptor.lookup('repeated_msg').has?(m)
  67. end
  68. end
  69. def test_set_clear_defaults
  70. m = TestMessage.new
  71. m.optional_int32 = -42
  72. assert_equal -42, m.optional_int32
  73. m.clear_optional_int32
  74. assert_equal 0, m.optional_int32
  75. m.optional_int32 = 50
  76. assert_equal 50, m.optional_int32
  77. TestMessage.descriptor.lookup('optional_int32').clear(m)
  78. assert_equal 0, m.optional_int32
  79. m.optional_string = "foo bar"
  80. assert_equal "foo bar", m.optional_string
  81. m.clear_optional_string
  82. assert_equal "", m.optional_string
  83. m.optional_string = "foo"
  84. assert_equal "foo", m.optional_string
  85. TestMessage.descriptor.lookup('optional_string').clear(m)
  86. assert_equal "", m.optional_string
  87. m.optional_msg = TestMessage2.new(:foo => 42)
  88. assert_equal TestMessage2.new(:foo => 42), m.optional_msg
  89. assert m.has_optional_msg?
  90. m.clear_optional_msg
  91. assert_equal nil, m.optional_msg
  92. assert !m.has_optional_msg?
  93. m.optional_msg = TestMessage2.new(:foo => 42)
  94. assert_equal TestMessage2.new(:foo => 42), m.optional_msg
  95. TestMessage.descriptor.lookup('optional_msg').clear(m)
  96. assert_equal nil, m.optional_msg
  97. m.repeated_int32.push(1)
  98. assert_equal [1], m.repeated_int32
  99. m.clear_repeated_int32
  100. assert_equal [], m.repeated_int32
  101. m.repeated_int32.push(1)
  102. assert_equal [1], m.repeated_int32
  103. TestMessage.descriptor.lookup('repeated_int32').clear(m)
  104. assert_equal [], m.repeated_int32
  105. m = OneofMessage.new
  106. m.a = "foo"
  107. assert_equal "foo", m.a
  108. assert m.has_my_oneof?
  109. m.clear_a
  110. assert !m.has_my_oneof?
  111. m.a = "foobar"
  112. assert m.has_my_oneof?
  113. m.clear_my_oneof
  114. assert !m.has_my_oneof?
  115. m.a = "bar"
  116. assert_equal "bar", m.a
  117. assert m.has_my_oneof?
  118. OneofMessage.descriptor.lookup('a').clear(m)
  119. assert !m.has_my_oneof?
  120. end
  121. def test_initialization_map_errors
  122. e = assert_raise ArgumentError do
  123. TestMessage.new(:hello => "world")
  124. end
  125. assert_match(/hello/, e.message)
  126. e = assert_raise ArgumentError do
  127. MapMessage.new(:map_string_int32 => "hello")
  128. end
  129. assert_equal e.message, "Expected Hash object as initializer value for map field 'map_string_int32' (given String)."
  130. e = assert_raise ArgumentError do
  131. TestMessage.new(:repeated_uint32 => "hello")
  132. end
  133. assert_equal e.message, "Expected array as initializer value for repeated field 'repeated_uint32' (given String)."
  134. end
  135. def test_map_field
  136. m = MapMessage.new
  137. assert m.map_string_int32 == {}
  138. assert m.map_string_msg == {}
  139. m = MapMessage.new(
  140. :map_string_int32 => {"a" => 1, "b" => 2},
  141. :map_string_msg => {"a" => TestMessage2.new(:foo => 1),
  142. "b" => TestMessage2.new(:foo => 2)},
  143. :map_string_enum => {"a" => :A, "b" => :B})
  144. assert m.map_string_int32.keys.sort == ["a", "b"]
  145. assert m.map_string_int32["a"] == 1
  146. assert m.map_string_msg["b"].foo == 2
  147. assert m.map_string_enum["a"] == :A
  148. m.map_string_int32["c"] = 3
  149. assert m.map_string_int32["c"] == 3
  150. m.map_string_msg["c"] = TestMessage2.new(:foo => 3)
  151. assert m.map_string_msg["c"] == TestMessage2.new(:foo => 3)
  152. m.map_string_msg.delete("b")
  153. m.map_string_msg.delete("c")
  154. assert m.map_string_msg == { "a" => TestMessage2.new(:foo => 1) }
  155. assert_raise Google::Protobuf::TypeError do
  156. m.map_string_msg["e"] = TestMessage.new # wrong value type
  157. end
  158. # ensure nothing was added by the above
  159. assert m.map_string_msg == { "a" => TestMessage2.new(:foo => 1) }
  160. m.map_string_int32 = Google::Protobuf::Map.new(:string, :int32)
  161. assert_raise Google::Protobuf::TypeError do
  162. m.map_string_int32 = Google::Protobuf::Map.new(:string, :int64)
  163. end
  164. assert_raise Google::Protobuf::TypeError do
  165. m.map_string_int32 = {}
  166. end
  167. assert_raise TypeError do
  168. m = MapMessage.new(:map_string_int32 => { 1 => "I am not a number" })
  169. end
  170. end
  171. def test_map_field_with_symbol
  172. m = MapMessage.new
  173. assert m.map_string_int32 == {}
  174. assert m.map_string_msg == {}
  175. m = MapMessage.new(
  176. :map_string_int32 => {a: 1, "b" => 2},
  177. :map_string_msg => {a: TestMessage2.new(:foo => 1),
  178. b: TestMessage2.new(:foo => 10)})
  179. assert_equal 1, m.map_string_int32[:a]
  180. assert_equal 2, m.map_string_int32[:b]
  181. assert_equal 10, m.map_string_msg[:b].foo
  182. end
  183. def test_map_inspect
  184. m = MapMessage.new(
  185. :map_string_int32 => {"a" => 1, "b" => 2},
  186. :map_string_msg => {"a" => TestMessage2.new(:foo => 1),
  187. "b" => TestMessage2.new(:foo => 2)},
  188. :map_string_enum => {"a" => :A, "b" => :B})
  189. expected = "<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}>"
  190. assert_equal expected, m.inspect
  191. end
  192. def test_map_corruption
  193. # This pattern led to a crash in a previous version of upb/protobuf.
  194. m = MapMessage.new(map_string_int32: { "aaa" => 1 })
  195. m.map_string_int32['podid'] = 2
  196. m.map_string_int32['aaa'] = 3
  197. end
  198. def test_concurrent_decoding
  199. o = Outer.new
  200. o.items[0] = Inner.new
  201. raw = Outer.encode(o)
  202. thds = 2.times.map do
  203. Thread.new do
  204. 100000.times do
  205. assert_equal o, Outer.decode(raw)
  206. end
  207. end
  208. end
  209. thds.map(&:join)
  210. end
  211. def test_map_encode_decode
  212. m = MapMessage.new(
  213. :map_string_int32 => {"a" => 1, "b" => 2},
  214. :map_string_msg => {"a" => TestMessage2.new(:foo => 1),
  215. "b" => TestMessage2.new(:foo => 2)},
  216. :map_string_enum => {"a" => :A, "b" => :B})
  217. m2 = MapMessage.decode(MapMessage.encode(m))
  218. assert m == m2
  219. m3 = MapMessageWireEquiv.decode(MapMessage.encode(m))
  220. assert m3.map_string_int32.length == 2
  221. kv = {}
  222. m3.map_string_int32.map { |msg| kv[msg.key] = msg.value }
  223. assert kv == {"a" => 1, "b" => 2}
  224. kv = {}
  225. m3.map_string_msg.map { |msg| kv[msg.key] = msg.value }
  226. assert kv == {"a" => TestMessage2.new(:foo => 1),
  227. "b" => TestMessage2.new(:foo => 2)}
  228. end
  229. def test_protobuf_decode_json_ignore_unknown_fields
  230. m = TestMessage.decode_json({
  231. optional_string: "foo",
  232. not_in_message: "some_value"
  233. }.to_json, { ignore_unknown_fields: true })
  234. assert_equal m.optional_string, "foo"
  235. e = assert_raise Google::Protobuf::ParseError do
  236. TestMessage.decode_json({ not_in_message: "some_value" }.to_json)
  237. end
  238. assert_match(/No such field: not_in_message/, e.message)
  239. end
  240. def test_to_h
  241. m = TestMessage.new(:optional_bool => true, :optional_double => -10.100001, :optional_string => 'foo', :repeated_string => ['bar1', 'bar2'], :repeated_msg => [TestMessage2.new(:foo => 100)])
  242. expected_result = {
  243. :optional_bool=>true,
  244. :optional_bytes=>"",
  245. :optional_double=>-10.100001,
  246. :optional_enum=>:Default,
  247. :optional_float=>0.0,
  248. :optional_int32=>0,
  249. :optional_int64=>0,
  250. :optional_msg=>nil,
  251. :optional_string=>"foo",
  252. :optional_uint32=>0,
  253. :optional_uint64=>0,
  254. :repeated_bool=>[],
  255. :repeated_bytes=>[],
  256. :repeated_double=>[],
  257. :repeated_enum=>[],
  258. :repeated_float=>[],
  259. :repeated_int32=>[],
  260. :repeated_int64=>[],
  261. :repeated_msg=>[{:foo => 100}],
  262. :repeated_string=>["bar1", "bar2"],
  263. :repeated_uint32=>[],
  264. :repeated_uint64=>[]
  265. }
  266. assert_equal expected_result, m.to_h
  267. m = MapMessage.new(
  268. :map_string_int32 => {"a" => 1, "b" => 2},
  269. :map_string_msg => {"a" => TestMessage2.new(:foo => 1),
  270. "b" => TestMessage2.new(:foo => 2)},
  271. :map_string_enum => {"a" => :A, "b" => :B})
  272. expected_result = {
  273. :map_string_int32 => {"a" => 1, "b" => 2},
  274. :map_string_msg => {"a" => {:foo => 1}, "b" => {:foo => 2}},
  275. :map_string_enum => {"a" => :A, "b" => :B}
  276. }
  277. assert_equal expected_result, m.to_h
  278. end
  279. def test_json_maps
  280. # TODO: Fix JSON in JRuby version.
  281. return if RUBY_PLATFORM == "java"
  282. m = MapMessage.new(:map_string_int32 => {"a" => 1})
  283. expected = {mapStringInt32: {a: 1}, mapStringMsg: {}, mapStringEnum: {}}
  284. expected_preserve = {map_string_int32: {a: 1}, map_string_msg: {}, map_string_enum: {}}
  285. assert_equal JSON.parse(MapMessage.encode_json(m), :symbolize_names => true), expected
  286. json = MapMessage.encode_json(m, :preserve_proto_fieldnames => true)
  287. assert_equal JSON.parse(json, :symbolize_names => true), expected_preserve
  288. m2 = MapMessage.decode_json(MapMessage.encode_json(m))
  289. assert_equal m, m2
  290. end
  291. def test_json_maps_emit_defaults_submsg
  292. # TODO: Fix JSON in JRuby version.
  293. return if RUBY_PLATFORM == "java"
  294. m = MapMessage.new(:map_string_msg => {"a" => TestMessage2.new})
  295. expected = {mapStringInt32: {}, mapStringMsg: {a: {foo: 0}}, mapStringEnum: {}}
  296. actual = MapMessage.encode_json(m, :emit_defaults => true)
  297. assert_equal JSON.parse(actual, :symbolize_names => true), expected
  298. end
  299. def test_respond_to
  300. # This test fails with JRuby 1.7.23, likely because of an old JRuby bug.
  301. return if RUBY_PLATFORM == "java"
  302. msg = MapMessage.new
  303. assert msg.respond_to?(:map_string_int32)
  304. assert !msg.respond_to?(:bacon)
  305. end
  306. def test_file_descriptor
  307. file_descriptor = TestMessage.descriptor.file_descriptor
  308. assert nil != file_descriptor
  309. assert_equal "tests/basic_test.proto", file_descriptor.name
  310. assert_equal :proto3, file_descriptor.syntax
  311. file_descriptor = TestEnum.descriptor.file_descriptor
  312. assert nil != file_descriptor
  313. assert_equal "tests/basic_test.proto", file_descriptor.name
  314. assert_equal :proto3, file_descriptor.syntax
  315. file_descriptor = BadFieldNames.descriptor.file_descriptor
  316. assert nil != file_descriptor
  317. assert_equal nil, file_descriptor.name
  318. assert_equal :proto3, file_descriptor.syntax
  319. end
  320. # Ruby 2.5 changed to raise FrozenError instead of RuntimeError
  321. FrozenErrorType = Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.5') ? RuntimeError : FrozenError
  322. def test_map_freeze
  323. m = proto_module::MapMessage.new
  324. m.map_string_int32['a'] = 5
  325. m.map_string_msg['b'] = proto_module::TestMessage2.new
  326. m.map_string_int32.freeze
  327. m.map_string_msg.freeze
  328. assert m.map_string_int32.frozen?
  329. assert m.map_string_msg.frozen?
  330. assert_raise(FrozenErrorType) { m.map_string_int32['foo'] = 1 }
  331. assert_raise(FrozenErrorType) { m.map_string_msg['bar'] = proto_module::TestMessage2.new }
  332. assert_raise(FrozenErrorType) { m.map_string_int32.delete('a') }
  333. assert_raise(FrozenErrorType) { m.map_string_int32.clear }
  334. end
  335. end
  336. end