basic.rb 13 KB

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