basic.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. 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. m.map_string_int32["c"] = 3
  147. assert m.map_string_int32["c"] == 3
  148. m.map_string_msg["c"] = TestMessage2.new(:foo => 3)
  149. assert m.map_string_msg["c"] == TestMessage2.new(:foo => 3)
  150. m.map_string_msg.delete("b")
  151. m.map_string_msg.delete("c")
  152. assert m.map_string_msg == { "a" => TestMessage2.new(:foo => 1) }
  153. assert_raise Google::Protobuf::TypeError do
  154. m.map_string_msg["e"] = TestMessage.new # wrong value type
  155. end
  156. # ensure nothing was added by the above
  157. assert m.map_string_msg == { "a" => TestMessage2.new(:foo => 1) }
  158. m.map_string_int32 = Google::Protobuf::Map.new(:string, :int32)
  159. assert_raise Google::Protobuf::TypeError do
  160. m.map_string_int32 = Google::Protobuf::Map.new(:string, :int64)
  161. end
  162. assert_raise Google::Protobuf::TypeError do
  163. m.map_string_int32 = {}
  164. end
  165. assert_raise TypeError do
  166. m = MapMessage.new(:map_string_int32 => { 1 => "I am not a number" })
  167. end
  168. end
  169. def test_map_inspect
  170. m = MapMessage.new(
  171. :map_string_int32 => {"a" => 1, "b" => 2},
  172. :map_string_msg => {"a" => TestMessage2.new(:foo => 1),
  173. "b" => TestMessage2.new(:foo => 2)})
  174. expected = "<BasicTest::MapMessage: map_string_int32: {\"b\"=>2, \"a\"=>1}, map_string_msg: {\"b\"=><BasicTest::TestMessage2: foo: 2>, \"a\"=><BasicTest::TestMessage2: foo: 1>}>"
  175. assert_equal expected, m.inspect
  176. end
  177. def test_map_corruption
  178. # This pattern led to a crash in a previous version of upb/protobuf.
  179. m = MapMessage.new(map_string_int32: { "aaa" => 1 })
  180. m.map_string_int32['podid'] = 2
  181. m.map_string_int32['aaa'] = 3
  182. end
  183. def test_concurrent_decoding
  184. o = Outer.new
  185. o.items[0] = Inner.new
  186. raw = Outer.encode(o)
  187. thds = 2.times.map do
  188. Thread.new do
  189. 100000.times do
  190. assert_equal o, Outer.decode(raw)
  191. end
  192. end
  193. end
  194. thds.map(&:join)
  195. end
  196. def test_map_encode_decode
  197. m = MapMessage.new(
  198. :map_string_int32 => {"a" => 1, "b" => 2},
  199. :map_string_msg => {"a" => TestMessage2.new(:foo => 1),
  200. "b" => TestMessage2.new(:foo => 2)})
  201. m2 = MapMessage.decode(MapMessage.encode(m))
  202. assert m == m2
  203. m3 = MapMessageWireEquiv.decode(MapMessage.encode(m))
  204. assert m3.map_string_int32.length == 2
  205. kv = {}
  206. m3.map_string_int32.map { |msg| kv[msg.key] = msg.value }
  207. assert kv == {"a" => 1, "b" => 2}
  208. kv = {}
  209. m3.map_string_msg.map { |msg| kv[msg.key] = msg.value }
  210. assert kv == {"a" => TestMessage2.new(:foo => 1),
  211. "b" => TestMessage2.new(:foo => 2)}
  212. end
  213. def test_protobuf_decode_json_ignore_unknown_fields
  214. m = TestMessage.decode_json({
  215. optional_string: "foo",
  216. not_in_message: "some_value"
  217. }.to_json, { ignore_unknown_fields: true })
  218. assert_equal m.optional_string, "foo"
  219. e = assert_raise Google::Protobuf::ParseError do
  220. TestMessage.decode_json({ not_in_message: "some_value" }.to_json)
  221. end
  222. assert_match(/No such field: not_in_message/, e.message)
  223. end
  224. def test_to_h
  225. m = TestMessage.new(:optional_bool => true, :optional_double => -10.100001, :optional_string => 'foo', :repeated_string => ['bar1', 'bar2'], :repeated_msg => [TestMessage2.new(:foo => 100)])
  226. expected_result = {
  227. :optional_bool=>true,
  228. :optional_bytes=>"",
  229. :optional_double=>-10.100001,
  230. :optional_enum=>:Default,
  231. :optional_float=>0.0,
  232. :optional_int32=>0,
  233. :optional_int64=>0,
  234. :optional_msg=>nil,
  235. :optional_string=>"foo",
  236. :optional_uint32=>0,
  237. :optional_uint64=>0,
  238. :repeated_bool=>[],
  239. :repeated_bytes=>[],
  240. :repeated_double=>[],
  241. :repeated_enum=>[],
  242. :repeated_float=>[],
  243. :repeated_int32=>[],
  244. :repeated_int64=>[],
  245. :repeated_msg=>[{:foo => 100}],
  246. :repeated_string=>["bar1", "bar2"],
  247. :repeated_uint32=>[],
  248. :repeated_uint64=>[]
  249. }
  250. assert_equal expected_result, m.to_h
  251. m = MapMessage.new(
  252. :map_string_int32 => {"a" => 1, "b" => 2},
  253. :map_string_msg => {"a" => TestMessage2.new(:foo => 1),
  254. "b" => TestMessage2.new(:foo => 2)})
  255. expected_result = {
  256. :map_string_int32 => {"a" => 1, "b" => 2},
  257. :map_string_msg => {"a" => {:foo => 1}, "b" => {:foo => 2}}
  258. }
  259. assert_equal expected_result, m.to_h
  260. end
  261. def test_json_maps
  262. # TODO: Fix JSON in JRuby version.
  263. return if RUBY_PLATFORM == "java"
  264. m = MapMessage.new(:map_string_int32 => {"a" => 1})
  265. expected = {mapStringInt32: {a: 1}, mapStringMsg: {}}
  266. expected_preserve = {map_string_int32: {a: 1}, map_string_msg: {}}
  267. assert JSON.parse(MapMessage.encode_json(m), :symbolize_names => true) == expected
  268. json = MapMessage.encode_json(m, :preserve_proto_fieldnames => true)
  269. assert JSON.parse(json, :symbolize_names => true) == expected_preserve
  270. m2 = MapMessage.decode_json(MapMessage.encode_json(m))
  271. assert m == m2
  272. end
  273. def test_json_maps_emit_defaults_submsg
  274. # TODO: Fix JSON in JRuby version.
  275. return if RUBY_PLATFORM == "java"
  276. m = MapMessage.new(:map_string_msg => {"a" => TestMessage2.new})
  277. expected = {mapStringInt32: {}, mapStringMsg: {a: {foo: 0}}}
  278. actual = MapMessage.encode_json(m, :emit_defaults => true)
  279. assert JSON.parse(actual, :symbolize_names => true) == expected
  280. end
  281. def test_respond_to
  282. # This test fails with JRuby 1.7.23, likely because of an old JRuby bug.
  283. return if RUBY_PLATFORM == "java"
  284. msg = MapMessage.new
  285. assert msg.respond_to?(:map_string_int32)
  286. assert !msg.respond_to?(:bacon)
  287. end
  288. def test_file_descriptor
  289. file_descriptor = TestMessage.descriptor.file_descriptor
  290. assert nil != file_descriptor
  291. assert_equal "tests/basic_test.proto", file_descriptor.name
  292. assert_equal :proto3, file_descriptor.syntax
  293. file_descriptor = TestEnum.descriptor.file_descriptor
  294. assert nil != file_descriptor
  295. assert_equal "tests/basic_test.proto", file_descriptor.name
  296. assert_equal :proto3, file_descriptor.syntax
  297. file_descriptor = BadFieldNames.descriptor.file_descriptor
  298. assert nil != file_descriptor
  299. assert_equal nil, file_descriptor.name
  300. assert_equal :proto3, file_descriptor.syntax
  301. end
  302. end
  303. end