google-protobuf 3.7.0 → 3.18.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


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

Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/ext/google/protobuf_c/convert.c +348 -0
  3. data/ext/google/protobuf_c/convert.h +72 -0
  4. data/ext/google/protobuf_c/defs.c +611 -1584
  5. data/ext/google/protobuf_c/defs.h +107 -0
  6. data/ext/google/protobuf_c/extconf.rb +4 -7
  7. data/ext/google/protobuf_c/map.c +312 -470
  8. data/ext/google/protobuf_c/map.h +67 -0
  9. data/ext/google/protobuf_c/message.c +941 -346
  10. data/ext/google/protobuf_c/message.h +101 -0
  11. data/ext/google/protobuf_c/protobuf.c +400 -51
  12. data/ext/google/protobuf_c/protobuf.h +47 -543
  13. data/ext/google/protobuf_c/repeated_field.c +313 -308
  14. data/ext/google/protobuf_c/repeated_field.h +63 -0
  15. data/ext/google/protobuf_c/ruby-upb.c +9171 -0
  16. data/ext/google/protobuf_c/ruby-upb.h +4704 -0
  17. data/lib/google/protobuf/any_pb.rb +1 -1
  18. data/lib/google/protobuf/api_pb.rb +5 -5
  19. data/lib/google/protobuf/descriptor_dsl.rb +458 -0
  20. data/lib/google/protobuf/descriptor_pb.rb +266 -0
  21. data/lib/google/protobuf/duration_pb.rb +1 -1
  22. data/lib/google/protobuf/empty_pb.rb +1 -1
  23. data/lib/google/protobuf/field_mask_pb.rb +1 -1
  24. data/lib/google/protobuf/source_context_pb.rb +1 -1
  25. data/lib/google/protobuf/struct_pb.rb +4 -4
  26. data/lib/google/protobuf/timestamp_pb.rb +1 -1
  27. data/lib/google/protobuf/type_pb.rb +10 -10
  28. data/lib/google/protobuf/well_known_types.rb +8 -2
  29. data/lib/google/protobuf/wrappers_pb.rb +9 -9
  30. data/lib/google/protobuf.rb +2 -0
  31. data/tests/basic.rb +322 -72
  32. data/tests/generated_code_test.rb +0 -0
  33. data/tests/stress.rb +0 -0
  34. metadata +28 -16
  35. data/ext/google/protobuf_c/encode_decode.c +0 -1574
  36. data/ext/google/protobuf_c/storage.c +0 -1019
  37. data/ext/google/protobuf_c/upb.c +0 -17318
  38. data/ext/google/protobuf_c/upb.h +0 -9755
data/tests/basic.rb CHANGED
@@ -17,7 +17,6 @@ module BasicTest
17
17
  add_message "BadFieldNames" do
18
18
  optional :dup, :int32, 1
19
19
  optional :class, :int32, 2
20
- optional :"a.b", :int32, 3
21
20
  end
22
21
  end
23
22
 
@@ -32,12 +31,51 @@ module BasicTest
32
31
  end
33
32
  include CommonTests
34
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
+
35
73
  def test_has_field
36
- m = TestMessage.new
37
- assert !m.has_optional_msg?
38
- m.optional_msg = TestMessage2.new
39
- assert m.has_optional_msg?
40
- assert TestMessage.descriptor.lookup('optional_msg').has?(m)
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)
41
79
 
42
80
  m = OneofMessage.new
43
81
  assert !m.has_my_oneof?
@@ -46,32 +84,31 @@ module BasicTest
46
84
  assert_raise NoMethodError do
47
85
  m.has_a?
48
86
  end
49
- assert_raise ArgumentError do
50
- OneofMessage.descriptor.lookup('a').has?(m)
51
- end
87
+ assert_true OneofMessage.descriptor.lookup('a').has?(m)
52
88
 
53
- m = TestMessage.new
89
+ m = TestSingularFields.new
54
90
  assert_raise NoMethodError do
55
- m.has_optional_int32?
91
+ m.has_singular_int32?
56
92
  end
57
93
  assert_raise ArgumentError do
58
- TestMessage.descriptor.lookup('optional_int32').has?(m)
94
+ TestSingularFields.descriptor.lookup('singular_int32').has?(m)
59
95
  end
60
96
 
61
97
  assert_raise NoMethodError do
62
- m.has_optional_string?
98
+ m.has_singular_string?
63
99
  end
64
100
  assert_raise ArgumentError do
65
- TestMessage.descriptor.lookup('optional_string').has?(m)
101
+ TestSingularFields.descriptor.lookup('singular_string').has?(m)
66
102
  end
67
103
 
68
104
  assert_raise NoMethodError do
69
- m.has_optional_bool?
105
+ m.has_singular_bool?
70
106
  end
71
107
  assert_raise ArgumentError do
72
- TestMessage.descriptor.lookup('optional_bool').has?(m)
108
+ TestSingularFields.descriptor.lookup('singular_bool').has?(m)
73
109
  end
74
110
 
111
+ m = TestMessage.new
75
112
  assert_raise NoMethodError do
76
113
  m.has_repeated_msg?
77
114
  end
@@ -80,40 +117,59 @@ module BasicTest
80
117
  end
81
118
  end
82
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
+
83
135
  def test_set_clear_defaults
84
- m = TestMessage.new
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
85
170
 
86
- m.optional_int32 = -42
87
- assert_equal -42, m.optional_int32
88
- m.clear_optional_int32
89
- assert_equal 0, m.optional_int32
90
-
91
- m.optional_int32 = 50
92
- assert_equal 50, m.optional_int32
93
- TestMessage.descriptor.lookup('optional_int32').clear(m)
94
- assert_equal 0, m.optional_int32
95
-
96
- m.optional_string = "foo bar"
97
- assert_equal "foo bar", m.optional_string
98
- m.clear_optional_string
99
- assert_equal "", m.optional_string
100
-
101
- m.optional_string = "foo"
102
- assert_equal "foo", m.optional_string
103
- TestMessage.descriptor.lookup('optional_string').clear(m)
104
- assert_equal "", m.optional_string
105
-
106
- m.optional_msg = TestMessage2.new(:foo => 42)
107
- assert_equal TestMessage2.new(:foo => 42), m.optional_msg
108
- assert m.has_optional_msg?
109
- m.clear_optional_msg
110
- assert_equal nil, m.optional_msg
111
- assert !m.has_optional_msg?
112
-
113
- m.optional_msg = TestMessage2.new(:foo => 42)
114
- assert_equal TestMessage2.new(:foo => 42), m.optional_msg
115
- TestMessage.descriptor.lookup('optional_msg').clear(m)
116
- assert_equal nil, m.optional_msg
171
+ def test_clear_repeated_fields
172
+ m = TestMessage.new
117
173
 
118
174
  m.repeated_int32.push(1)
119
175
  assert_equal [1], m.repeated_int32
@@ -129,6 +185,7 @@ module BasicTest
129
185
  m.a = "foo"
130
186
  assert_equal "foo", m.a
131
187
  assert m.has_my_oneof?
188
+ assert_equal :a, m.my_oneof
132
189
  m.clear_a
133
190
  assert !m.has_my_oneof?
134
191
 
@@ -144,7 +201,6 @@ module BasicTest
144
201
  assert !m.has_my_oneof?
145
202
  end
146
203
 
147
-
148
204
  def test_initialization_map_errors
149
205
  e = assert_raise ArgumentError do
150
206
  TestMessage.new(:hello => "world")
@@ -154,12 +210,12 @@ module BasicTest
154
210
  e = assert_raise ArgumentError do
155
211
  MapMessage.new(:map_string_int32 => "hello")
156
212
  end
157
- assert_equal e.message, "Expected Hash object as initializer value for map field 'map_string_int32'."
213
+ assert_equal e.message, "Expected Hash object as initializer value for map field 'map_string_int32' (given String)."
158
214
 
159
215
  e = assert_raise ArgumentError do
160
216
  TestMessage.new(:repeated_uint32 => "hello")
161
217
  end
162
- assert_equal e.message, "Expected array as initializer value for repeated field 'repeated_uint32'."
218
+ assert_equal e.message, "Expected array as initializer value for repeated field 'repeated_uint32' (given String)."
163
219
  end
164
220
 
165
221
  def test_map_field
@@ -170,10 +226,12 @@ module BasicTest
170
226
  m = MapMessage.new(
171
227
  :map_string_int32 => {"a" => 1, "b" => 2},
172
228
  :map_string_msg => {"a" => TestMessage2.new(:foo => 1),
173
- "b" => TestMessage2.new(:foo => 2)})
229
+ "b" => TestMessage2.new(:foo => 2)},
230
+ :map_string_enum => {"a" => :A, "b" => :B})
174
231
  assert m.map_string_int32.keys.sort == ["a", "b"]
175
232
  assert m.map_string_int32["a"] == 1
176
233
  assert m.map_string_msg["b"].foo == 2
234
+ assert m.map_string_enum["a"] == :A
177
235
 
178
236
  m.map_string_int32["c"] = 3
179
237
  assert m.map_string_int32["c"] == 3
@@ -197,18 +255,37 @@ module BasicTest
197
255
  m.map_string_int32 = {}
198
256
  end
199
257
 
200
- assert_raise TypeError do
258
+ assert_raise Google::Protobuf::TypeError do
201
259
  m = MapMessage.new(:map_string_int32 => { 1 => "I am not a number" })
202
260
  end
203
261
  end
204
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
+
205
277
  def test_map_inspect
206
278
  m = MapMessage.new(
207
279
  :map_string_int32 => {"a" => 1, "b" => 2},
208
280
  :map_string_msg => {"a" => TestMessage2.new(:foo => 1),
209
- "b" => TestMessage2.new(:foo => 2)})
210
- expected = "<BasicTest::MapMessage: map_string_int32: {\"b\"=>2, \"a\"=>1}, map_string_msg: {\"b\"=><BasicTest::TestMessage2: foo: 2>, \"a\"=><BasicTest::TestMessage2: foo: 1>}>"
211
- assert_equal expected, m.inspect
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}"
212
289
  end
213
290
 
214
291
  def test_map_corruption
@@ -218,6 +295,128 @@ module BasicTest
218
295
  m.map_string_int32['aaa'] = 3
219
296
  end
220
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
+
221
420
  def test_concurrent_decoding
222
421
  o = Outer.new
223
422
  o.items[0] = Inner.new
@@ -237,7 +436,8 @@ module BasicTest
237
436
  m = MapMessage.new(
238
437
  :map_string_int32 => {"a" => 1, "b" => 2},
239
438
  :map_string_msg => {"a" => TestMessage2.new(:foo => 1),
240
- "b" => TestMessage2.new(:foo => 2)})
439
+ "b" => TestMessage2.new(:foo => 2)},
440
+ :map_string_enum => {"a" => :A, "b" => :B})
241
441
  m2 = MapMessage.decode(MapMessage.encode(m))
242
442
  assert m == m2
243
443
 
@@ -267,6 +467,14 @@ module BasicTest
267
467
  assert_match(/No such field: not_in_message/, e.message)
268
468
  end
269
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
+
270
478
  def test_to_h
271
479
  m = TestMessage.new(:optional_bool => true, :optional_double => -10.100001, :optional_string => 'foo', :repeated_string => ['bar1', 'bar2'], :repeated_msg => [TestMessage2.new(:foo => 100)])
272
480
  expected_result = {
@@ -278,6 +486,7 @@ module BasicTest
278
486
  :optional_int32=>0,
279
487
  :optional_int64=>0,
280
488
  :optional_msg=>nil,
489
+ :optional_msg2=>nil,
281
490
  :optional_string=>"foo",
282
491
  :optional_uint32=>0,
283
492
  :optional_uint64=>0,
@@ -298,10 +507,12 @@ module BasicTest
298
507
  m = MapMessage.new(
299
508
  :map_string_int32 => {"a" => 1, "b" => 2},
300
509
  :map_string_msg => {"a" => TestMessage2.new(:foo => 1),
301
- "b" => TestMessage2.new(:foo => 2)})
510
+ "b" => TestMessage2.new(:foo => 2)},
511
+ :map_string_enum => {"a" => :A, "b" => :B})
302
512
  expected_result = {
303
513
  :map_string_int32 => {"a" => 1, "b" => 2},
304
- :map_string_msg => {"a" => {:foo => 1}, "b" => {:foo => 2}}
514
+ :map_string_msg => {"a" => {:foo => 1}, "b" => {:foo => 2}},
515
+ :map_string_enum => {"a" => :A, "b" => :B}
305
516
  }
306
517
  assert_equal expected_result, m.to_h
307
518
  end
@@ -311,26 +522,50 @@ module BasicTest
311
522
  # TODO: Fix JSON in JRuby version.
312
523
  return if RUBY_PLATFORM == "java"
313
524
  m = MapMessage.new(:map_string_int32 => {"a" => 1})
314
- expected = {mapStringInt32: {a: 1}, mapStringMsg: {}}
315
- expected_preserve = {map_string_int32: {a: 1}, map_string_msg: {}}
316
- assert JSON.parse(MapMessage.encode_json(m), :symbolize_names => true) == expected
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
317
528
 
318
- json = MapMessage.encode_json(m, :preserve_proto_fieldnames => true)
319
- assert JSON.parse(json, :symbolize_names => true) == expected_preserve
529
+ json = MapMessage.encode_json(m, :preserve_proto_fieldnames => true, :emit_defaults=>true)
530
+ assert_equal JSON.parse(json, :symbolize_names => true), expected_preserve
320
531
 
321
532
  m2 = MapMessage.decode_json(MapMessage.encode_json(m))
322
- assert m == m2
533
+ assert_equal m, m2
323
534
  end
324
535
 
325
536
  def test_json_maps_emit_defaults_submsg
326
537
  # TODO: Fix JSON in JRuby version.
327
538
  return if RUBY_PLATFORM == "java"
328
- m = MapMessage.new(:map_string_msg => {"a" => TestMessage2.new})
329
- expected = {mapStringInt32: {}, mapStringMsg: {a: {foo: 0}}}
539
+ m = MapMessage.new(:map_string_msg => {"a" => TestMessage2.new(foo: 0)})
540
+ expected = {mapStringInt32: {}, mapStringMsg: {a: {foo: 0}}, mapStringEnum: {}}
330
541
 
331
542
  actual = MapMessage.encode_json(m, :emit_defaults => true)
332
543
 
333
- assert JSON.parse(actual, :symbolize_names => true) == expected
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)
334
569
  end
335
570
 
336
571
  def test_respond_to
@@ -351,11 +586,26 @@ module BasicTest
351
586
  assert nil != file_descriptor
352
587
  assert_equal "tests/basic_test.proto", file_descriptor.name
353
588
  assert_equal :proto3, file_descriptor.syntax
589
+ end
354
590
 
355
- file_descriptor = BadFieldNames.descriptor.file_descriptor
356
- assert nil != file_descriptor
357
- assert_equal nil, file_descriptor.name
358
- assert_equal :proto3, file_descriptor.syntax
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 }
359
609
  end
360
610
  end
361
611
  end
File without changes
data/tests/stress.rb CHANGED
File without changes
metadata CHANGED
@@ -1,43 +1,49 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google-protobuf
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.7.0
4
+ version: 3.18.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Protobuf Authors
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-03-01 00:00:00.000000000 Z
11
+ date: 2021-09-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake-compiler-dock
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 0.6.0
19
+ version: 1.1.0
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '2.0'
20
23
  type: :development
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
24
- - - "~>"
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: 1.1.0
30
+ - - "<"
25
31
  - !ruby/object:Gem::Version
26
- version: 0.6.0
32
+ version: '2.0'
27
33
  - !ruby/object:Gem::Dependency
28
34
  name: rake-compiler
29
35
  requirement: !ruby/object:Gem::Requirement
30
36
  requirements:
31
37
  - - "~>"
32
38
  - !ruby/object:Gem::Version
33
- version: 0.9.5
39
+ version: 1.1.0
34
40
  type: :development
35
41
  prerelease: false
36
42
  version_requirements: !ruby/object:Gem::Requirement
37
43
  requirements:
38
44
  - - "~>"
39
45
  - !ruby/object:Gem::Version
40
- version: 0.9.5
46
+ version: 1.1.0
41
47
  - !ruby/object:Gem::Dependency
42
48
  name: test-unit
43
49
  requirement: !ruby/object:Gem::Requirement
@@ -79,21 +85,27 @@ extensions:
79
85
  - ext/google/protobuf_c/extconf.rb
80
86
  extra_rdoc_files: []
81
87
  files:
88
+ - ext/google/protobuf_c/convert.c
89
+ - ext/google/protobuf_c/convert.h
82
90
  - ext/google/protobuf_c/defs.c
83
- - ext/google/protobuf_c/encode_decode.c
91
+ - ext/google/protobuf_c/defs.h
84
92
  - ext/google/protobuf_c/extconf.rb
85
93
  - ext/google/protobuf_c/map.c
94
+ - ext/google/protobuf_c/map.h
86
95
  - ext/google/protobuf_c/message.c
96
+ - ext/google/protobuf_c/message.h
87
97
  - ext/google/protobuf_c/protobuf.c
88
98
  - ext/google/protobuf_c/protobuf.h
89
99
  - ext/google/protobuf_c/repeated_field.c
90
- - ext/google/protobuf_c/storage.c
91
- - ext/google/protobuf_c/upb.c
92
- - ext/google/protobuf_c/upb.h
100
+ - ext/google/protobuf_c/repeated_field.h
101
+ - ext/google/protobuf_c/ruby-upb.c
102
+ - ext/google/protobuf_c/ruby-upb.h
93
103
  - ext/google/protobuf_c/wrap_memcpy.c
94
104
  - lib/google/protobuf.rb
95
105
  - lib/google/protobuf/any_pb.rb
96
106
  - lib/google/protobuf/api_pb.rb
107
+ - lib/google/protobuf/descriptor_dsl.rb
108
+ - lib/google/protobuf/descriptor_pb.rb
97
109
  - lib/google/protobuf/duration_pb.rb
98
110
  - lib/google/protobuf/empty_pb.rb
99
111
  - lib/google/protobuf/field_mask_pb.rb
@@ -111,7 +123,8 @@ files:
111
123
  homepage: https://developers.google.com/protocol-buffers
112
124
  licenses:
113
125
  - BSD-3-Clause
114
- metadata: {}
126
+ metadata:
127
+ source_code_uri: https://github.com/protocolbuffers/protobuf/tree/v3.18.0/ruby
115
128
  post_install_message:
116
129
  rdoc_options: []
117
130
  require_paths:
@@ -120,15 +133,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
120
133
  requirements:
121
134
  - - ">="
122
135
  - !ruby/object:Gem::Version
123
- version: '0'
136
+ version: '2.3'
124
137
  required_rubygems_version: !ruby/object:Gem::Requirement
125
138
  requirements:
126
139
  - - ">="
127
140
  - !ruby/object:Gem::Version
128
141
  version: '0'
129
142
  requirements: []
130
- rubyforge_project:
131
- rubygems_version: 2.7.8
143
+ rubygems_version: 3.2.27
132
144
  signing_key:
133
145
  specification_version: 4
134
146
  summary: Protocol Buffers