basic.rb 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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_inspect
  172. m = MapMessage.new(
  173. :map_string_int32 => {"a" => 1, "b" => 2},
  174. :map_string_msg => {"a" => TestMessage2.new(:foo => 1),
  175. "b" => TestMessage2.new(:foo => 2)},
  176. :map_string_enum => {"a" => :A, "b" => :B})
  177. 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}>"
  178. assert_equal expected, m.inspect
  179. end
  180. def test_map_corruption
  181. # This pattern led to a crash in a previous version of upb/protobuf.
  182. m = MapMessage.new(map_string_int32: { "aaa" => 1 })
  183. m.map_string_int32['podid'] = 2
  184. m.map_string_int32['aaa'] = 3
  185. end
  186. def test_concurrent_decoding
  187. o = Outer.new
  188. o.items[0] = Inner.new
  189. raw = Outer.encode(o)
  190. thds = 2.times.map do
  191. Thread.new do
  192. 100000.times do
  193. assert_equal o, Outer.decode(raw)
  194. end
  195. end
  196. end
  197. thds.map(&:join)
  198. end
  199. def test_map_encode_decode
  200. m = MapMessage.new(
  201. :map_string_int32 => {"a" => 1, "b" => 2},
  202. :map_string_msg => {"a" => TestMessage2.new(:foo => 1),
  203. "b" => TestMessage2.new(:foo => 2)},
  204. :map_string_enum => {"a" => :A, "b" => :B})
  205. m2 = MapMessage.decode(MapMessage.encode(m))
  206. assert m == m2
  207. m3 = MapMessageWireEquiv.decode(MapMessage.encode(m))
  208. assert m3.map_string_int32.length == 2
  209. kv = {}
  210. m3.map_string_int32.map { |msg| kv[msg.key] = msg.value }
  211. assert kv == {"a" => 1, "b" => 2}
  212. kv = {}
  213. m3.map_string_msg.map { |msg| kv[msg.key] = msg.value }
  214. assert kv == {"a" => TestMessage2.new(:foo => 1),
  215. "b" => TestMessage2.new(:foo => 2)}
  216. end
  217. def test_protobuf_decode_json_ignore_unknown_fields
  218. m = TestMessage.decode_json({
  219. optional_string: "foo",
  220. not_in_message: "some_value"
  221. }.to_json, { ignore_unknown_fields: true })
  222. assert_equal m.optional_string, "foo"
  223. e = assert_raise Google::Protobuf::ParseError do
  224. TestMessage.decode_json({ not_in_message: "some_value" }.to_json)
  225. end
  226. assert_match(/No such field: not_in_message/, e.message)
  227. end
  228. def test_to_h
  229. m = TestMessage.new(:optional_bool => true, :optional_double => -10.100001, :optional_string => 'foo', :repeated_string => ['bar1', 'bar2'], :repeated_msg => [TestMessage2.new(:foo => 100)])
  230. expected_result = {
  231. :optional_bool=>true,
  232. :optional_bytes=>"",
  233. :optional_double=>-10.100001,
  234. :optional_enum=>:Default,
  235. :optional_float=>0.0,
  236. :optional_int32=>0,
  237. :optional_int64=>0,
  238. :optional_msg=>nil,
  239. :optional_string=>"foo",
  240. :optional_uint32=>0,
  241. :optional_uint64=>0,
  242. :repeated_bool=>[],
  243. :repeated_bytes=>[],
  244. :repeated_double=>[],
  245. :repeated_enum=>[],
  246. :repeated_float=>[],
  247. :repeated_int32=>[],
  248. :repeated_int64=>[],
  249. :repeated_msg=>[{:foo => 100}],
  250. :repeated_string=>["bar1", "bar2"],
  251. :repeated_uint32=>[],
  252. :repeated_uint64=>[]
  253. }
  254. assert_equal expected_result, m.to_h
  255. m = MapMessage.new(
  256. :map_string_int32 => {"a" => 1, "b" => 2},
  257. :map_string_msg => {"a" => TestMessage2.new(:foo => 1),
  258. "b" => TestMessage2.new(:foo => 2)},
  259. :map_string_enum => {"a" => :A, "b" => :B})
  260. expected_result = {
  261. :map_string_int32 => {"a" => 1, "b" => 2},
  262. :map_string_msg => {"a" => {:foo => 1}, "b" => {:foo => 2}},
  263. :map_string_enum => {"a" => :A, "b" => :B}
  264. }
  265. assert_equal expected_result, m.to_h
  266. end
  267. def test_json_maps
  268. # TODO: Fix JSON in JRuby version.
  269. return if RUBY_PLATFORM == "java"
  270. m = MapMessage.new(:map_string_int32 => {"a" => 1})
  271. expected = {mapStringInt32: {a: 1}, mapStringMsg: {}, mapStringEnum: {}}
  272. expected_preserve = {map_string_int32: {a: 1}, map_string_msg: {}, map_string_enum: {}}
  273. assert_equal JSON.parse(MapMessage.encode_json(m), :symbolize_names => true), expected
  274. json = MapMessage.encode_json(m, :preserve_proto_fieldnames => true)
  275. assert_equal JSON.parse(json, :symbolize_names => true), expected_preserve
  276. m2 = MapMessage.decode_json(MapMessage.encode_json(m))
  277. assert_equal m, m2
  278. end
  279. def test_json_maps_emit_defaults_submsg
  280. # TODO: Fix JSON in JRuby version.
  281. return if RUBY_PLATFORM == "java"
  282. m = MapMessage.new(:map_string_msg => {"a" => TestMessage2.new})
  283. expected = {mapStringInt32: {}, mapStringMsg: {a: {foo: 0}}, mapStringEnum: {}}
  284. actual = MapMessage.encode_json(m, :emit_defaults => true)
  285. assert_equal JSON.parse(actual, :symbolize_names => true), expected
  286. end
  287. def test_respond_to
  288. # This test fails with JRuby 1.7.23, likely because of an old JRuby bug.
  289. return if RUBY_PLATFORM == "java"
  290. msg = MapMessage.new
  291. assert msg.respond_to?(:map_string_int32)
  292. assert !msg.respond_to?(:bacon)
  293. end
  294. def test_file_descriptor
  295. file_descriptor = TestMessage.descriptor.file_descriptor
  296. assert nil != file_descriptor
  297. assert_equal "tests/basic_test.proto", file_descriptor.name
  298. assert_equal :proto3, file_descriptor.syntax
  299. file_descriptor = TestEnum.descriptor.file_descriptor
  300. assert nil != file_descriptor
  301. assert_equal "tests/basic_test.proto", file_descriptor.name
  302. assert_equal :proto3, file_descriptor.syntax
  303. file_descriptor = BadFieldNames.descriptor.file_descriptor
  304. assert nil != file_descriptor
  305. assert_equal nil, file_descriptor.name
  306. assert_equal :proto3, file_descriptor.syntax
  307. end
  308. # Ruby 2.5 changed to raise FrozenError instead of RuntimeError
  309. FrozenErrorType = Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.5') ? RuntimeError : FrozenError
  310. def test_map_freeze
  311. m = proto_module::MapMessage.new
  312. m.map_string_int32['a'] = 5
  313. m.map_string_msg['b'] = proto_module::TestMessage2.new
  314. m.map_string_int32.freeze
  315. m.map_string_msg.freeze
  316. assert m.map_string_int32.frozen?
  317. assert m.map_string_msg.frozen?
  318. assert_raise(FrozenErrorType) { m.map_string_int32['foo'] = 1 }
  319. assert_raise(FrozenErrorType) { m.map_string_msg['bar'] = proto_module::TestMessage2.new }
  320. assert_raise(FrozenErrorType) { m.map_string_int32.delete('a') }
  321. assert_raise(FrozenErrorType) { m.map_string_int32.clear }
  322. end
  323. end
  324. end