google-protobuf 3.17.0 → 3.23.3

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