google-protobuf 3.17.2 → 3.23.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of google-protobuf might be problematic. Click here for more details.

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