google-protobuf 3.7.0 → 3.19.0
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.
- checksums.yaml +4 -4
- data/ext/google/protobuf_c/convert.c +348 -0
- data/ext/google/protobuf_c/convert.h +72 -0
- data/ext/google/protobuf_c/defs.c +611 -1584
- data/ext/google/protobuf_c/defs.h +107 -0
- data/ext/google/protobuf_c/extconf.rb +4 -7
- data/ext/google/protobuf_c/map.c +315 -470
- data/ext/google/protobuf_c/map.h +67 -0
- data/ext/google/protobuf_c/message.c +941 -346
- data/ext/google/protobuf_c/message.h +101 -0
- data/ext/google/protobuf_c/protobuf.c +400 -51
- data/ext/google/protobuf_c/protobuf.h +47 -543
- data/ext/google/protobuf_c/repeated_field.c +313 -308
- data/ext/google/protobuf_c/repeated_field.h +63 -0
- data/ext/google/protobuf_c/ruby-upb.c +9171 -0
- data/ext/google/protobuf_c/ruby-upb.h +4704 -0
- data/lib/google/protobuf/any_pb.rb +1 -1
- data/lib/google/protobuf/api_pb.rb +4 -3
- data/lib/google/protobuf/descriptor_dsl.rb +458 -0
- data/lib/google/protobuf/descriptor_pb.rb +268 -0
- data/lib/google/protobuf/duration_pb.rb +1 -1
- data/lib/google/protobuf/empty_pb.rb +1 -1
- data/lib/google/protobuf/field_mask_pb.rb +1 -1
- data/lib/google/protobuf/source_context_pb.rb +1 -1
- data/lib/google/protobuf/struct_pb.rb +4 -4
- data/lib/google/protobuf/timestamp_pb.rb +1 -1
- data/lib/google/protobuf/type_pb.rb +9 -8
- data/lib/google/protobuf/well_known_types.rb +13 -2
- data/lib/google/protobuf/wrappers_pb.rb +9 -9
- data/lib/google/protobuf.rb +2 -0
- data/tests/basic.rb +350 -71
- data/tests/generated_code_test.rb +0 -0
- data/tests/stress.rb +1 -1
- metadata +28 -30
- data/ext/google/protobuf_c/encode_decode.c +0 -1574
- data/ext/google/protobuf_c/storage.c +0 -1019
- data/ext/google/protobuf_c/upb.c +0 -17318
- 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,52 @@ 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
|
+
# TODO: Remove the platform check once https://github.com/jruby/jruby/issues/6818 is released in JRuby 9.3.0.0
|
70
|
+
GC.start(full_mark: true, immediate_sweep: true) unless RUBY_PLATFORM == "java"
|
71
|
+
TestMessage.encode(msg)
|
72
|
+
end
|
73
|
+
|
35
74
|
def test_has_field
|
36
|
-
m =
|
37
|
-
assert !m.
|
38
|
-
m.
|
39
|
-
assert m.
|
40
|
-
assert
|
75
|
+
m = TestSingularFields.new
|
76
|
+
assert !m.has_singular_msg?
|
77
|
+
m.singular_msg = TestMessage2.new
|
78
|
+
assert m.has_singular_msg?
|
79
|
+
assert TestSingularFields.descriptor.lookup('singular_msg').has?(m)
|
41
80
|
|
42
81
|
m = OneofMessage.new
|
43
82
|
assert !m.has_my_oneof?
|
@@ -46,32 +85,31 @@ module BasicTest
|
|
46
85
|
assert_raise NoMethodError do
|
47
86
|
m.has_a?
|
48
87
|
end
|
49
|
-
|
50
|
-
OneofMessage.descriptor.lookup('a').has?(m)
|
51
|
-
end
|
88
|
+
assert_true OneofMessage.descriptor.lookup('a').has?(m)
|
52
89
|
|
53
|
-
m =
|
90
|
+
m = TestSingularFields.new
|
54
91
|
assert_raise NoMethodError do
|
55
|
-
m.
|
92
|
+
m.has_singular_int32?
|
56
93
|
end
|
57
94
|
assert_raise ArgumentError do
|
58
|
-
|
95
|
+
TestSingularFields.descriptor.lookup('singular_int32').has?(m)
|
59
96
|
end
|
60
97
|
|
61
98
|
assert_raise NoMethodError do
|
62
|
-
m.
|
99
|
+
m.has_singular_string?
|
63
100
|
end
|
64
101
|
assert_raise ArgumentError do
|
65
|
-
|
102
|
+
TestSingularFields.descriptor.lookup('singular_string').has?(m)
|
66
103
|
end
|
67
104
|
|
68
105
|
assert_raise NoMethodError do
|
69
|
-
m.
|
106
|
+
m.has_singular_bool?
|
70
107
|
end
|
71
108
|
assert_raise ArgumentError do
|
72
|
-
|
109
|
+
TestSingularFields.descriptor.lookup('singular_bool').has?(m)
|
73
110
|
end
|
74
111
|
|
112
|
+
m = TestMessage.new
|
75
113
|
assert_raise NoMethodError do
|
76
114
|
m.has_repeated_msg?
|
77
115
|
end
|
@@ -80,40 +118,70 @@ module BasicTest
|
|
80
118
|
end
|
81
119
|
end
|
82
120
|
|
121
|
+
def test_no_presence
|
122
|
+
m = TestSingularFields.new
|
123
|
+
|
124
|
+
# Explicitly setting to zero does not cause anything to be serialized.
|
125
|
+
m.singular_int32 = 0
|
126
|
+
assert_equal "", TestSingularFields.encode(m)
|
127
|
+
|
128
|
+
# Explicitly setting to a non-zero value *does* cause serialization.
|
129
|
+
m.singular_int32 = 1
|
130
|
+
assert_not_equal "", TestSingularFields.encode(m)
|
131
|
+
|
132
|
+
m.singular_int32 = 0
|
133
|
+
assert_equal "", TestSingularFields.encode(m)
|
134
|
+
end
|
135
|
+
|
83
136
|
def test_set_clear_defaults
|
137
|
+
m = TestSingularFields.new
|
138
|
+
|
139
|
+
m.singular_int32 = -42
|
140
|
+
assert_equal -42, m.singular_int32
|
141
|
+
m.clear_singular_int32
|
142
|
+
assert_equal 0, m.singular_int32
|
143
|
+
|
144
|
+
m.singular_int32 = 50
|
145
|
+
assert_equal 50, m.singular_int32
|
146
|
+
TestSingularFields.descriptor.lookup('singular_int32').clear(m)
|
147
|
+
assert_equal 0, m.singular_int32
|
148
|
+
|
149
|
+
m.singular_string = "foo bar"
|
150
|
+
assert_equal "foo bar", m.singular_string
|
151
|
+
m.clear_singular_string
|
152
|
+
assert_equal "", m.singular_string
|
153
|
+
|
154
|
+
m.singular_string = "foo"
|
155
|
+
assert_equal "foo", m.singular_string
|
156
|
+
TestSingularFields.descriptor.lookup('singular_string').clear(m)
|
157
|
+
assert_equal "", m.singular_string
|
158
|
+
|
159
|
+
m.singular_msg = TestMessage2.new(:foo => 42)
|
160
|
+
assert_equal TestMessage2.new(:foo => 42), m.singular_msg
|
161
|
+
assert m.has_singular_msg?
|
162
|
+
m.clear_singular_msg
|
163
|
+
assert_equal nil, m.singular_msg
|
164
|
+
assert !m.has_singular_msg?
|
165
|
+
|
166
|
+
m.singular_msg = TestMessage2.new(:foo => 42)
|
167
|
+
assert_equal TestMessage2.new(:foo => 42), m.singular_msg
|
168
|
+
TestSingularFields.descriptor.lookup('singular_msg').clear(m)
|
169
|
+
assert_equal nil, m.singular_msg
|
170
|
+
end
|
171
|
+
|
172
|
+
def test_import_proto2
|
84
173
|
m = TestMessage.new
|
174
|
+
assert !m.has_optional_proto2_submessage?
|
175
|
+
m.optional_proto2_submessage = ::FooBar::Proto2::TestImportedMessage.new
|
176
|
+
assert m.has_optional_proto2_submessage?
|
177
|
+
assert TestMessage.descriptor.lookup('optional_proto2_submessage').has?(m)
|
178
|
+
|
179
|
+
m.clear_optional_proto2_submessage
|
180
|
+
assert !m.has_optional_proto2_submessage?
|
181
|
+
end
|
85
182
|
|
86
|
-
|
87
|
-
|
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
|
183
|
+
def test_clear_repeated_fields
|
184
|
+
m = TestMessage.new
|
117
185
|
|
118
186
|
m.repeated_int32.push(1)
|
119
187
|
assert_equal [1], m.repeated_int32
|
@@ -129,6 +197,7 @@ module BasicTest
|
|
129
197
|
m.a = "foo"
|
130
198
|
assert_equal "foo", m.a
|
131
199
|
assert m.has_my_oneof?
|
200
|
+
assert_equal :a, m.my_oneof
|
132
201
|
m.clear_a
|
133
202
|
assert !m.has_my_oneof?
|
134
203
|
|
@@ -144,7 +213,6 @@ module BasicTest
|
|
144
213
|
assert !m.has_my_oneof?
|
145
214
|
end
|
146
215
|
|
147
|
-
|
148
216
|
def test_initialization_map_errors
|
149
217
|
e = assert_raise ArgumentError do
|
150
218
|
TestMessage.new(:hello => "world")
|
@@ -154,12 +222,12 @@ module BasicTest
|
|
154
222
|
e = assert_raise ArgumentError do
|
155
223
|
MapMessage.new(:map_string_int32 => "hello")
|
156
224
|
end
|
157
|
-
assert_equal e.message, "Expected Hash object as initializer value for map field 'map_string_int32'."
|
225
|
+
assert_equal e.message, "Expected Hash object as initializer value for map field 'map_string_int32' (given String)."
|
158
226
|
|
159
227
|
e = assert_raise ArgumentError do
|
160
228
|
TestMessage.new(:repeated_uint32 => "hello")
|
161
229
|
end
|
162
|
-
assert_equal e.message, "Expected array as initializer value for repeated field 'repeated_uint32'."
|
230
|
+
assert_equal e.message, "Expected array as initializer value for repeated field 'repeated_uint32' (given String)."
|
163
231
|
end
|
164
232
|
|
165
233
|
def test_map_field
|
@@ -170,10 +238,12 @@ module BasicTest
|
|
170
238
|
m = MapMessage.new(
|
171
239
|
:map_string_int32 => {"a" => 1, "b" => 2},
|
172
240
|
:map_string_msg => {"a" => TestMessage2.new(:foo => 1),
|
173
|
-
"b" => TestMessage2.new(:foo => 2)}
|
241
|
+
"b" => TestMessage2.new(:foo => 2)},
|
242
|
+
:map_string_enum => {"a" => :A, "b" => :B})
|
174
243
|
assert m.map_string_int32.keys.sort == ["a", "b"]
|
175
244
|
assert m.map_string_int32["a"] == 1
|
176
245
|
assert m.map_string_msg["b"].foo == 2
|
246
|
+
assert m.map_string_enum["a"] == :A
|
177
247
|
|
178
248
|
m.map_string_int32["c"] = 3
|
179
249
|
assert m.map_string_int32["c"] == 3
|
@@ -197,18 +267,37 @@ module BasicTest
|
|
197
267
|
m.map_string_int32 = {}
|
198
268
|
end
|
199
269
|
|
200
|
-
assert_raise TypeError do
|
270
|
+
assert_raise Google::Protobuf::TypeError do
|
201
271
|
m = MapMessage.new(:map_string_int32 => { 1 => "I am not a number" })
|
202
272
|
end
|
203
273
|
end
|
204
274
|
|
275
|
+
def test_map_field_with_symbol
|
276
|
+
m = MapMessage.new
|
277
|
+
assert m.map_string_int32 == {}
|
278
|
+
assert m.map_string_msg == {}
|
279
|
+
|
280
|
+
m = MapMessage.new(
|
281
|
+
:map_string_int32 => {a: 1, "b" => 2},
|
282
|
+
:map_string_msg => {a: TestMessage2.new(:foo => 1),
|
283
|
+
b: TestMessage2.new(:foo => 10)})
|
284
|
+
assert_equal 1, m.map_string_int32[:a]
|
285
|
+
assert_equal 2, m.map_string_int32[:b]
|
286
|
+
assert_equal 10, m.map_string_msg[:b].foo
|
287
|
+
end
|
288
|
+
|
205
289
|
def test_map_inspect
|
206
290
|
m = MapMessage.new(
|
207
291
|
:map_string_int32 => {"a" => 1, "b" => 2},
|
208
292
|
:map_string_msg => {"a" => TestMessage2.new(:foo => 1),
|
209
|
-
"b" => TestMessage2.new(:foo => 2)}
|
210
|
-
|
211
|
-
|
293
|
+
"b" => TestMessage2.new(:foo => 2)},
|
294
|
+
:map_string_enum => {"a" => :A, "b" => :B})
|
295
|
+
|
296
|
+
# JRuby doesn't keep consistent ordering so check for either version
|
297
|
+
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}>"
|
298
|
+
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}>"
|
299
|
+
inspect_result = m.inspect
|
300
|
+
assert expected_a == inspect_result || expected_b == inspect_result, "Incorrect inspect result: #{inspect_result}"
|
212
301
|
end
|
213
302
|
|
214
303
|
def test_map_corruption
|
@@ -218,6 +307,128 @@ module BasicTest
|
|
218
307
|
m.map_string_int32['aaa'] = 3
|
219
308
|
end
|
220
309
|
|
310
|
+
def test_map_wrappers
|
311
|
+
run_asserts = ->(m) {
|
312
|
+
assert_equal 2.0, m.map_double[0].value
|
313
|
+
assert_equal 4.0, m.map_float[0].value
|
314
|
+
assert_equal 3, m.map_int32[0].value
|
315
|
+
assert_equal 4, m.map_int64[0].value
|
316
|
+
assert_equal 5, m.map_uint32[0].value
|
317
|
+
assert_equal 6, m.map_uint64[0].value
|
318
|
+
assert_equal true, m.map_bool[0].value
|
319
|
+
assert_equal 'str', m.map_string[0].value
|
320
|
+
assert_equal 'fun', m.map_bytes[0].value
|
321
|
+
}
|
322
|
+
|
323
|
+
m = proto_module::Wrapper.new(
|
324
|
+
map_double: {0 => Google::Protobuf::DoubleValue.new(value: 2.0)},
|
325
|
+
map_float: {0 => Google::Protobuf::FloatValue.new(value: 4.0)},
|
326
|
+
map_int32: {0 => Google::Protobuf::Int32Value.new(value: 3)},
|
327
|
+
map_int64: {0 => Google::Protobuf::Int64Value.new(value: 4)},
|
328
|
+
map_uint32: {0 => Google::Protobuf::UInt32Value.new(value: 5)},
|
329
|
+
map_uint64: {0 => Google::Protobuf::UInt64Value.new(value: 6)},
|
330
|
+
map_bool: {0 => Google::Protobuf::BoolValue.new(value: true)},
|
331
|
+
map_string: {0 => Google::Protobuf::StringValue.new(value: 'str')},
|
332
|
+
map_bytes: {0 => Google::Protobuf::BytesValue.new(value: 'fun')},
|
333
|
+
)
|
334
|
+
|
335
|
+
run_asserts.call(m)
|
336
|
+
serialized = proto_module::Wrapper::encode(m)
|
337
|
+
m2 = proto_module::Wrapper::decode(serialized)
|
338
|
+
run_asserts.call(m2)
|
339
|
+
|
340
|
+
# Test the case where we are serializing directly from the parsed form
|
341
|
+
# (before anything lazy is materialized).
|
342
|
+
m3 = proto_module::Wrapper::decode(serialized)
|
343
|
+
serialized2 = proto_module::Wrapper::encode(m3)
|
344
|
+
m4 = proto_module::Wrapper::decode(serialized2)
|
345
|
+
run_asserts.call(m4)
|
346
|
+
|
347
|
+
# Test that the lazy form compares equal to the expanded form.
|
348
|
+
m5 = proto_module::Wrapper::decode(serialized2)
|
349
|
+
assert_equal m5, m
|
350
|
+
end
|
351
|
+
|
352
|
+
def test_map_wrappers_with_default_values
|
353
|
+
run_asserts = ->(m) {
|
354
|
+
assert_equal 0.0, m.map_double[0].value
|
355
|
+
assert_equal 0.0, m.map_float[0].value
|
356
|
+
assert_equal 0, m.map_int32[0].value
|
357
|
+
assert_equal 0, m.map_int64[0].value
|
358
|
+
assert_equal 0, m.map_uint32[0].value
|
359
|
+
assert_equal 0, m.map_uint64[0].value
|
360
|
+
assert_equal false, m.map_bool[0].value
|
361
|
+
assert_equal '', m.map_string[0].value
|
362
|
+
assert_equal '', m.map_bytes[0].value
|
363
|
+
}
|
364
|
+
|
365
|
+
m = proto_module::Wrapper.new(
|
366
|
+
map_double: {0 => Google::Protobuf::DoubleValue.new(value: 0.0)},
|
367
|
+
map_float: {0 => Google::Protobuf::FloatValue.new(value: 0.0)},
|
368
|
+
map_int32: {0 => Google::Protobuf::Int32Value.new(value: 0)},
|
369
|
+
map_int64: {0 => Google::Protobuf::Int64Value.new(value: 0)},
|
370
|
+
map_uint32: {0 => Google::Protobuf::UInt32Value.new(value: 0)},
|
371
|
+
map_uint64: {0 => Google::Protobuf::UInt64Value.new(value: 0)},
|
372
|
+
map_bool: {0 => Google::Protobuf::BoolValue.new(value: false)},
|
373
|
+
map_string: {0 => Google::Protobuf::StringValue.new(value: '')},
|
374
|
+
map_bytes: {0 => Google::Protobuf::BytesValue.new(value: '')},
|
375
|
+
)
|
376
|
+
|
377
|
+
run_asserts.call(m)
|
378
|
+
serialized = proto_module::Wrapper::encode(m)
|
379
|
+
m2 = proto_module::Wrapper::decode(serialized)
|
380
|
+
run_asserts.call(m2)
|
381
|
+
|
382
|
+
# Test the case where we are serializing directly from the parsed form
|
383
|
+
# (before anything lazy is materialized).
|
384
|
+
m3 = proto_module::Wrapper::decode(serialized)
|
385
|
+
serialized2 = proto_module::Wrapper::encode(m3)
|
386
|
+
m4 = proto_module::Wrapper::decode(serialized2)
|
387
|
+
run_asserts.call(m4)
|
388
|
+
|
389
|
+
# Test that the lazy form compares equal to the expanded form.
|
390
|
+
m5 = proto_module::Wrapper::decode(serialized2)
|
391
|
+
assert_equal m5, m
|
392
|
+
end
|
393
|
+
|
394
|
+
def test_map_wrappers_with_no_value
|
395
|
+
run_asserts = ->(m) {
|
396
|
+
assert_equal 0.0, m.map_double[0].value
|
397
|
+
assert_equal 0.0, m.map_float[0].value
|
398
|
+
assert_equal 0, m.map_int32[0].value
|
399
|
+
assert_equal 0, m.map_int64[0].value
|
400
|
+
assert_equal 0, m.map_uint32[0].value
|
401
|
+
assert_equal 0, m.map_uint64[0].value
|
402
|
+
assert_equal false, m.map_bool[0].value
|
403
|
+
assert_equal '', m.map_string[0].value
|
404
|
+
assert_equal '', m.map_bytes[0].value
|
405
|
+
}
|
406
|
+
|
407
|
+
m = proto_module::Wrapper.new(
|
408
|
+
map_double: {0 => Google::Protobuf::DoubleValue.new()},
|
409
|
+
map_float: {0 => Google::Protobuf::FloatValue.new()},
|
410
|
+
map_int32: {0 => Google::Protobuf::Int32Value.new()},
|
411
|
+
map_int64: {0 => Google::Protobuf::Int64Value.new()},
|
412
|
+
map_uint32: {0 => Google::Protobuf::UInt32Value.new()},
|
413
|
+
map_uint64: {0 => Google::Protobuf::UInt64Value.new()},
|
414
|
+
map_bool: {0 => Google::Protobuf::BoolValue.new()},
|
415
|
+
map_string: {0 => Google::Protobuf::StringValue.new()},
|
416
|
+
map_bytes: {0 => Google::Protobuf::BytesValue.new()},
|
417
|
+
)
|
418
|
+
run_asserts.call(m)
|
419
|
+
|
420
|
+
serialized = proto_module::Wrapper::encode(m)
|
421
|
+
m2 = proto_module::Wrapper::decode(serialized)
|
422
|
+
run_asserts.call(m2)
|
423
|
+
|
424
|
+
# Test the case where we are serializing directly from the parsed form
|
425
|
+
# (before anything lazy is materialized).
|
426
|
+
m3 = proto_module::Wrapper::decode(serialized)
|
427
|
+
serialized2 = proto_module::Wrapper::encode(m3)
|
428
|
+
m4 = proto_module::Wrapper::decode(serialized2)
|
429
|
+
run_asserts.call(m4)
|
430
|
+
end
|
431
|
+
|
221
432
|
def test_concurrent_decoding
|
222
433
|
o = Outer.new
|
223
434
|
o.items[0] = Inner.new
|
@@ -237,7 +448,8 @@ module BasicTest
|
|
237
448
|
m = MapMessage.new(
|
238
449
|
:map_string_int32 => {"a" => 1, "b" => 2},
|
239
450
|
:map_string_msg => {"a" => TestMessage2.new(:foo => 1),
|
240
|
-
"b" => TestMessage2.new(:foo => 2)}
|
451
|
+
"b" => TestMessage2.new(:foo => 2)},
|
452
|
+
:map_string_enum => {"a" => :A, "b" => :B})
|
241
453
|
m2 = MapMessage.decode(MapMessage.encode(m))
|
242
454
|
assert m == m2
|
243
455
|
|
@@ -267,6 +479,14 @@ module BasicTest
|
|
267
479
|
assert_match(/No such field: not_in_message/, e.message)
|
268
480
|
end
|
269
481
|
|
482
|
+
#def test_json_quoted_string
|
483
|
+
# m = TestMessage.decode_json(%q(
|
484
|
+
# "optionalInt64": "1",,
|
485
|
+
# }))
|
486
|
+
# puts(m)
|
487
|
+
# assert_equal 1, m.optional_int32
|
488
|
+
#end
|
489
|
+
|
270
490
|
def test_to_h
|
271
491
|
m = TestMessage.new(:optional_bool => true, :optional_double => -10.100001, :optional_string => 'foo', :repeated_string => ['bar1', 'bar2'], :repeated_msg => [TestMessage2.new(:foo => 100)])
|
272
492
|
expected_result = {
|
@@ -278,6 +498,8 @@ module BasicTest
|
|
278
498
|
:optional_int32=>0,
|
279
499
|
:optional_int64=>0,
|
280
500
|
:optional_msg=>nil,
|
501
|
+
:optional_msg2=>nil,
|
502
|
+
:optional_proto2_submessage=>nil,
|
281
503
|
:optional_string=>"foo",
|
282
504
|
:optional_uint32=>0,
|
283
505
|
:optional_uint64=>0,
|
@@ -298,10 +520,12 @@ module BasicTest
|
|
298
520
|
m = MapMessage.new(
|
299
521
|
:map_string_int32 => {"a" => 1, "b" => 2},
|
300
522
|
:map_string_msg => {"a" => TestMessage2.new(:foo => 1),
|
301
|
-
"b" => TestMessage2.new(:foo => 2)}
|
523
|
+
"b" => TestMessage2.new(:foo => 2)},
|
524
|
+
:map_string_enum => {"a" => :A, "b" => :B})
|
302
525
|
expected_result = {
|
303
526
|
:map_string_int32 => {"a" => 1, "b" => 2},
|
304
|
-
:map_string_msg => {"a" => {:foo => 1}, "b" => {:foo => 2}}
|
527
|
+
:map_string_msg => {"a" => {:foo => 1}, "b" => {:foo => 2}},
|
528
|
+
:map_string_enum => {"a" => :A, "b" => :B}
|
305
529
|
}
|
306
530
|
assert_equal expected_result, m.to_h
|
307
531
|
end
|
@@ -311,26 +535,50 @@ module BasicTest
|
|
311
535
|
# TODO: Fix JSON in JRuby version.
|
312
536
|
return if RUBY_PLATFORM == "java"
|
313
537
|
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
|
-
|
538
|
+
expected = {mapStringInt32: {a: 1}, mapStringMsg: {}, mapStringEnum: {}}
|
539
|
+
expected_preserve = {map_string_int32: {a: 1}, map_string_msg: {}, map_string_enum: {}}
|
540
|
+
assert_equal JSON.parse(MapMessage.encode_json(m, :emit_defaults=>true), :symbolize_names => true), expected
|
317
541
|
|
318
|
-
json = MapMessage.encode_json(m, :preserve_proto_fieldnames => true)
|
319
|
-
|
542
|
+
json = MapMessage.encode_json(m, :preserve_proto_fieldnames => true, :emit_defaults=>true)
|
543
|
+
assert_equal JSON.parse(json, :symbolize_names => true), expected_preserve
|
320
544
|
|
321
545
|
m2 = MapMessage.decode_json(MapMessage.encode_json(m))
|
322
|
-
|
546
|
+
assert_equal m, m2
|
323
547
|
end
|
324
548
|
|
325
549
|
def test_json_maps_emit_defaults_submsg
|
326
550
|
# TODO: Fix JSON in JRuby version.
|
327
551
|
return if RUBY_PLATFORM == "java"
|
328
|
-
m = MapMessage.new(:map_string_msg => {"a" => TestMessage2.new})
|
329
|
-
expected = {mapStringInt32: {}, mapStringMsg: {a: {foo: 0}}}
|
552
|
+
m = MapMessage.new(:map_string_msg => {"a" => TestMessage2.new(foo: 0)})
|
553
|
+
expected = {mapStringInt32: {}, mapStringMsg: {a: {foo: 0}}, mapStringEnum: {}}
|
330
554
|
|
331
555
|
actual = MapMessage.encode_json(m, :emit_defaults => true)
|
332
556
|
|
333
|
-
|
557
|
+
assert_equal JSON.parse(actual, :symbolize_names => true), expected
|
558
|
+
end
|
559
|
+
|
560
|
+
def test_json_emit_defaults_submsg
|
561
|
+
# TODO: Fix JSON in JRuby version.
|
562
|
+
return if RUBY_PLATFORM == "java"
|
563
|
+
m = TestSingularFields.new(singular_msg: proto_module::TestMessage2.new)
|
564
|
+
|
565
|
+
expected = {
|
566
|
+
singularInt32: 0,
|
567
|
+
singularInt64: "0",
|
568
|
+
singularUint32: 0,
|
569
|
+
singularUint64: "0",
|
570
|
+
singularBool: false,
|
571
|
+
singularFloat: 0,
|
572
|
+
singularDouble: 0,
|
573
|
+
singularString: "",
|
574
|
+
singularBytes: "",
|
575
|
+
singularMsg: {},
|
576
|
+
singularEnum: "Default",
|
577
|
+
}
|
578
|
+
|
579
|
+
actual = proto_module::TestMessage.encode_json(m, :emit_defaults => true)
|
580
|
+
|
581
|
+
assert_equal expected, JSON.parse(actual, :symbolize_names => true)
|
334
582
|
end
|
335
583
|
|
336
584
|
def test_respond_to
|
@@ -351,11 +599,42 @@ module BasicTest
|
|
351
599
|
assert nil != file_descriptor
|
352
600
|
assert_equal "tests/basic_test.proto", file_descriptor.name
|
353
601
|
assert_equal :proto3, file_descriptor.syntax
|
602
|
+
end
|
354
603
|
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
604
|
+
# Ruby 2.5 changed to raise FrozenError instead of RuntimeError
|
605
|
+
FrozenErrorType = Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.5') ? RuntimeError : FrozenError
|
606
|
+
|
607
|
+
def test_map_freeze
|
608
|
+
m = proto_module::MapMessage.new
|
609
|
+
m.map_string_int32['a'] = 5
|
610
|
+
m.map_string_msg['b'] = proto_module::TestMessage2.new
|
611
|
+
|
612
|
+
m.map_string_int32.freeze
|
613
|
+
m.map_string_msg.freeze
|
614
|
+
|
615
|
+
assert m.map_string_int32.frozen?
|
616
|
+
assert m.map_string_msg.frozen?
|
617
|
+
|
618
|
+
assert_raise(FrozenErrorType) { m.map_string_int32['foo'] = 1 }
|
619
|
+
assert_raise(FrozenErrorType) { m.map_string_msg['bar'] = proto_module::TestMessage2.new }
|
620
|
+
assert_raise(FrozenErrorType) { m.map_string_int32.delete('a') }
|
621
|
+
assert_raise(FrozenErrorType) { m.map_string_int32.clear }
|
622
|
+
end
|
623
|
+
|
624
|
+
def test_map_length
|
625
|
+
m = proto_module::MapMessage.new
|
626
|
+
assert_equal 0, m.map_string_int32.length
|
627
|
+
assert_equal 0, m.map_string_msg.length
|
628
|
+
assert_equal 0, m.map_string_int32.size
|
629
|
+
assert_equal 0, m.map_string_msg.size
|
630
|
+
|
631
|
+
m.map_string_int32['a'] = 1
|
632
|
+
m.map_string_int32['b'] = 2
|
633
|
+
m.map_string_msg['a'] = proto_module::TestMessage2.new
|
634
|
+
assert_equal 2, m.map_string_int32.length
|
635
|
+
assert_equal 1, m.map_string_msg.length
|
636
|
+
assert_equal 2, m.map_string_int32.size
|
637
|
+
assert_equal 1, m.map_string_msg.size
|
359
638
|
end
|
360
639
|
end
|
361
640
|
end
|
File without changes
|