google-protobuf 3.5.0 → 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.
- checksums.yaml +5 -5
- data/ext/google/protobuf_c/convert.c +361 -0
- data/ext/google/protobuf_c/convert.h +75 -0
- data/ext/google/protobuf_c/defs.c +770 -1254
- data/ext/google/protobuf_c/defs.h +107 -0
- data/ext/google/protobuf_c/extconf.rb +15 -5
- data/ext/google/protobuf_c/map.c +312 -474
- data/ext/google/protobuf_c/map.h +66 -0
- data/ext/google/protobuf_c/message.c +1139 -372
- data/ext/google/protobuf_c/message.h +104 -0
- data/ext/google/protobuf_c/protobuf.c +418 -51
- data/ext/google/protobuf_c/protobuf.h +53 -485
- data/ext/google/protobuf_c/repeated_field.c +319 -316
- data/ext/google/protobuf_c/repeated_field.h +63 -0
- data/ext/google/protobuf_c/ruby-upb.c +13974 -0
- data/ext/google/protobuf_c/ruby-upb.h +11780 -0
- data/ext/google/protobuf_c/third_party/utf8_range/LICENSE +22 -0
- data/ext/google/protobuf_c/third_party/utf8_range/naive.c +92 -0
- data/ext/google/protobuf_c/third_party/utf8_range/range2-neon.c +157 -0
- data/ext/google/protobuf_c/third_party/utf8_range/range2-sse.c +170 -0
- data/ext/google/protobuf_c/third_party/utf8_range/utf8_range.h +21 -0
- data/ext/google/protobuf_c/wrap_memcpy.c +4 -3
- data/lib/google/protobuf/any_pb.rb +26 -5
- data/lib/google/protobuf/api_pb.rb +31 -25
- data/lib/google/protobuf/descriptor_dsl.rb +465 -0
- data/lib/google/protobuf/descriptor_pb.rb +75 -0
- data/lib/google/protobuf/duration_pb.rb +26 -5
- data/lib/google/protobuf/empty_pb.rb +26 -3
- data/lib/google/protobuf/field_mask_pb.rb +26 -4
- data/lib/google/protobuf/message_exts.rb +9 -4
- data/lib/google/protobuf/plugin_pb.rb +47 -0
- data/lib/google/protobuf/repeated_field.rb +17 -4
- data/lib/google/protobuf/source_context_pb.rb +26 -4
- data/lib/google/protobuf/struct_pb.rb +28 -22
- data/lib/google/protobuf/timestamp_pb.rb +26 -5
- data/lib/google/protobuf/type_pb.rb +37 -76
- data/lib/google/protobuf/well_known_types.rb +32 -4
- data/lib/google/protobuf/wrappers_pb.rb +35 -37
- data/lib/google/protobuf.rb +11 -8
- metadata +29 -37
- data/ext/google/protobuf_c/encode_decode.c +0 -1307
- data/ext/google/protobuf_c/storage.c +0 -904
- data/ext/google/protobuf_c/upb.c +0 -14913
- data/ext/google/protobuf_c/upb.h +0 -8969
- data/tests/basic.rb +0 -1403
- data/tests/generated_code_test.rb +0 -19
- data/tests/stress.rb +0 -38
data/tests/basic.rb
DELETED
@@ -1,1403 +0,0 @@
|
|
1
|
-
#!/usr/bin/ruby
|
2
|
-
|
3
|
-
require 'google/protobuf'
|
4
|
-
require 'json'
|
5
|
-
require 'test/unit'
|
6
|
-
|
7
|
-
# ------------- generated code --------------
|
8
|
-
|
9
|
-
module BasicTest
|
10
|
-
pool = Google::Protobuf::DescriptorPool.new
|
11
|
-
pool.build do
|
12
|
-
add_message "Foo" do
|
13
|
-
optional :bar, :message, 1, "Bar"
|
14
|
-
repeated :baz, :message, 2, "Baz"
|
15
|
-
end
|
16
|
-
|
17
|
-
add_message "Bar" do
|
18
|
-
optional :msg, :string, 1
|
19
|
-
end
|
20
|
-
|
21
|
-
add_message "Baz" do
|
22
|
-
optional :msg, :string, 1
|
23
|
-
end
|
24
|
-
|
25
|
-
add_message "TestMessage" do
|
26
|
-
optional :optional_int32, :int32, 1
|
27
|
-
optional :optional_int64, :int64, 2
|
28
|
-
optional :optional_uint32, :uint32, 3
|
29
|
-
optional :optional_uint64, :uint64, 4
|
30
|
-
optional :optional_bool, :bool, 5
|
31
|
-
optional :optional_float, :float, 6
|
32
|
-
optional :optional_double, :double, 7
|
33
|
-
optional :optional_string, :string, 8
|
34
|
-
optional :optional_bytes, :bytes, 9
|
35
|
-
optional :optional_msg, :message, 10, "TestMessage2"
|
36
|
-
optional :optional_enum, :enum, 11, "TestEnum"
|
37
|
-
|
38
|
-
repeated :repeated_int32, :int32, 12
|
39
|
-
repeated :repeated_int64, :int64, 13
|
40
|
-
repeated :repeated_uint32, :uint32, 14
|
41
|
-
repeated :repeated_uint64, :uint64, 15
|
42
|
-
repeated :repeated_bool, :bool, 16
|
43
|
-
repeated :repeated_float, :float, 17
|
44
|
-
repeated :repeated_double, :double, 18
|
45
|
-
repeated :repeated_string, :string, 19
|
46
|
-
repeated :repeated_bytes, :bytes, 20
|
47
|
-
repeated :repeated_msg, :message, 21, "TestMessage2"
|
48
|
-
repeated :repeated_enum, :enum, 22, "TestEnum"
|
49
|
-
end
|
50
|
-
add_message "TestMessage2" do
|
51
|
-
optional :foo, :int32, 1
|
52
|
-
end
|
53
|
-
|
54
|
-
add_message "TestEmbeddedMessageParent" do
|
55
|
-
optional :child_msg, :message, 1, "TestEmbeddedMessageChild"
|
56
|
-
optional :number, :int32, 2
|
57
|
-
|
58
|
-
repeated :repeated_msg, :message, 3, "TestEmbeddedMessageChild"
|
59
|
-
repeated :repeated_number, :int32, 4
|
60
|
-
end
|
61
|
-
add_message "TestEmbeddedMessageChild" do
|
62
|
-
optional :sub_child, :message, 1, "TestMessage"
|
63
|
-
end
|
64
|
-
|
65
|
-
add_message "Recursive1" do
|
66
|
-
optional :foo, :message, 1, "Recursive2"
|
67
|
-
end
|
68
|
-
add_message "Recursive2" do
|
69
|
-
optional :foo, :message, 1, "Recursive1"
|
70
|
-
end
|
71
|
-
|
72
|
-
add_enum "TestEnum" do
|
73
|
-
value :Default, 0
|
74
|
-
value :A, 1
|
75
|
-
value :B, 2
|
76
|
-
value :C, 3
|
77
|
-
end
|
78
|
-
|
79
|
-
add_message "BadFieldNames" do
|
80
|
-
optional :dup, :int32, 1
|
81
|
-
optional :class, :int32, 2
|
82
|
-
optional :"a.b", :int32, 3
|
83
|
-
end
|
84
|
-
|
85
|
-
add_message "MapMessage" do
|
86
|
-
map :map_string_int32, :string, :int32, 1
|
87
|
-
map :map_string_msg, :string, :message, 2, "TestMessage2"
|
88
|
-
end
|
89
|
-
add_message "MapMessageWireEquiv" do
|
90
|
-
repeated :map_string_int32, :message, 1, "MapMessageWireEquiv_entry1"
|
91
|
-
repeated :map_string_msg, :message, 2, "MapMessageWireEquiv_entry2"
|
92
|
-
end
|
93
|
-
add_message "MapMessageWireEquiv_entry1" do
|
94
|
-
optional :key, :string, 1
|
95
|
-
optional :value, :int32, 2
|
96
|
-
end
|
97
|
-
add_message "MapMessageWireEquiv_entry2" do
|
98
|
-
optional :key, :string, 1
|
99
|
-
optional :value, :message, 2, "TestMessage2"
|
100
|
-
end
|
101
|
-
|
102
|
-
add_message "OneofMessage" do
|
103
|
-
oneof :my_oneof do
|
104
|
-
optional :a, :string, 1
|
105
|
-
optional :b, :int32, 2
|
106
|
-
optional :c, :message, 3, "TestMessage2"
|
107
|
-
optional :d, :enum, 4, "TestEnum"
|
108
|
-
end
|
109
|
-
end
|
110
|
-
|
111
|
-
add_message "repro.Outer" do
|
112
|
-
map :items, :int32, :message, 1, "repro.Inner"
|
113
|
-
end
|
114
|
-
|
115
|
-
add_message "repro.Inner" do
|
116
|
-
end
|
117
|
-
end
|
118
|
-
|
119
|
-
|
120
|
-
Outer = pool.lookup("repro.Outer").msgclass
|
121
|
-
Inner = pool.lookup("repro.Inner").msgclass
|
122
|
-
Foo = pool.lookup("Foo").msgclass
|
123
|
-
Bar = pool.lookup("Bar").msgclass
|
124
|
-
Baz = pool.lookup("Baz").msgclass
|
125
|
-
TestMessage = pool.lookup("TestMessage").msgclass
|
126
|
-
TestMessage2 = pool.lookup("TestMessage2").msgclass
|
127
|
-
TestEmbeddedMessageParent = pool.lookup("TestEmbeddedMessageParent").msgclass
|
128
|
-
TestEmbeddedMessageChild = pool.lookup("TestEmbeddedMessageChild").msgclass
|
129
|
-
Recursive1 = pool.lookup("Recursive1").msgclass
|
130
|
-
Recursive2 = pool.lookup("Recursive2").msgclass
|
131
|
-
TestEnum = pool.lookup("TestEnum").enummodule
|
132
|
-
BadFieldNames = pool.lookup("BadFieldNames").msgclass
|
133
|
-
MapMessage = pool.lookup("MapMessage").msgclass
|
134
|
-
MapMessageWireEquiv = pool.lookup("MapMessageWireEquiv").msgclass
|
135
|
-
MapMessageWireEquiv_entry1 =
|
136
|
-
pool.lookup("MapMessageWireEquiv_entry1").msgclass
|
137
|
-
MapMessageWireEquiv_entry2 =
|
138
|
-
pool.lookup("MapMessageWireEquiv_entry2").msgclass
|
139
|
-
OneofMessage = pool.lookup("OneofMessage").msgclass
|
140
|
-
|
141
|
-
# ------------ test cases ---------------
|
142
|
-
|
143
|
-
class MessageContainerTest < Test::Unit::TestCase
|
144
|
-
|
145
|
-
def test_defaults
|
146
|
-
m = TestMessage.new
|
147
|
-
assert m.optional_int32 == 0
|
148
|
-
assert m.optional_int64 == 0
|
149
|
-
assert m.optional_uint32 == 0
|
150
|
-
assert m.optional_uint64 == 0
|
151
|
-
assert m.optional_bool == false
|
152
|
-
assert m.optional_float == 0.0
|
153
|
-
assert m.optional_double == 0.0
|
154
|
-
assert m.optional_string == ""
|
155
|
-
assert m.optional_bytes == ""
|
156
|
-
assert m.optional_msg == nil
|
157
|
-
assert m.optional_enum == :Default
|
158
|
-
end
|
159
|
-
|
160
|
-
def test_setters
|
161
|
-
m = TestMessage.new
|
162
|
-
m.optional_int32 = -42
|
163
|
-
assert m.optional_int32 == -42
|
164
|
-
m.optional_int64 = -0x1_0000_0000
|
165
|
-
assert m.optional_int64 == -0x1_0000_0000
|
166
|
-
m.optional_uint32 = 0x9000_0000
|
167
|
-
assert m.optional_uint32 == 0x9000_0000
|
168
|
-
m.optional_uint64 = 0x9000_0000_0000_0000
|
169
|
-
assert m.optional_uint64 == 0x9000_0000_0000_0000
|
170
|
-
m.optional_bool = true
|
171
|
-
assert m.optional_bool == true
|
172
|
-
m.optional_float = 0.5
|
173
|
-
assert m.optional_float == 0.5
|
174
|
-
m.optional_double = 0.5
|
175
|
-
m.optional_string = "hello"
|
176
|
-
assert m.optional_string == "hello"
|
177
|
-
m.optional_string = :hello
|
178
|
-
assert m.optional_string == "hello"
|
179
|
-
m.optional_bytes = "world".encode!('ASCII-8BIT')
|
180
|
-
assert m.optional_bytes == "world"
|
181
|
-
m.optional_msg = TestMessage2.new(:foo => 42)
|
182
|
-
assert m.optional_msg == TestMessage2.new(:foo => 42)
|
183
|
-
m.optional_msg = nil
|
184
|
-
assert m.optional_msg == nil
|
185
|
-
m.optional_enum = :C
|
186
|
-
assert m.optional_enum == :C
|
187
|
-
m.optional_enum = 'C'
|
188
|
-
assert m.optional_enum == :C
|
189
|
-
end
|
190
|
-
|
191
|
-
def test_ctor_args
|
192
|
-
m = TestMessage.new(:optional_int32 => -42,
|
193
|
-
:optional_msg => TestMessage2.new,
|
194
|
-
:optional_enum => :C,
|
195
|
-
:repeated_string => ["hello", "there", "world"])
|
196
|
-
assert m.optional_int32 == -42
|
197
|
-
assert m.optional_msg.class == TestMessage2
|
198
|
-
assert m.repeated_string.length == 3
|
199
|
-
assert m.optional_enum == :C
|
200
|
-
assert m.repeated_string[0] == "hello"
|
201
|
-
assert m.repeated_string[1] == "there"
|
202
|
-
assert m.repeated_string[2] == "world"
|
203
|
-
end
|
204
|
-
|
205
|
-
def test_ctor_string_symbol_args
|
206
|
-
m = TestMessage.new(:optional_enum => 'C', :repeated_enum => ['A', 'B'])
|
207
|
-
assert_equal :C, m.optional_enum
|
208
|
-
assert_equal [:A, :B], m.repeated_enum
|
209
|
-
|
210
|
-
m = TestMessage.new(:optional_string => :foo, :repeated_string => [:foo, :bar])
|
211
|
-
assert_equal 'foo', m.optional_string
|
212
|
-
assert_equal ['foo', 'bar'], m.repeated_string
|
213
|
-
end
|
214
|
-
|
215
|
-
def test_embeddedmsg_hash_init
|
216
|
-
m = TestEmbeddedMessageParent.new(:child_msg => {sub_child: {optional_int32: 1}},
|
217
|
-
:number => 2,
|
218
|
-
:repeated_msg => [{sub_child: {optional_int32: 3}}],
|
219
|
-
:repeated_number => [10, 20, 30])
|
220
|
-
|
221
|
-
assert_equal 2, m.number
|
222
|
-
assert_equal [10, 20, 30], m.repeated_number
|
223
|
-
|
224
|
-
assert_not_nil m.child_msg
|
225
|
-
assert_not_nil m.child_msg.sub_child
|
226
|
-
assert_equal m.child_msg.sub_child.optional_int32, 1
|
227
|
-
|
228
|
-
assert_not_nil m.repeated_msg
|
229
|
-
assert_equal 1, m.repeated_msg.length
|
230
|
-
assert_equal 3, m.repeated_msg.first.sub_child.optional_int32
|
231
|
-
end
|
232
|
-
|
233
|
-
def test_inspect
|
234
|
-
m = TestMessage.new(:optional_int32 => -42,
|
235
|
-
:optional_enum => :A,
|
236
|
-
:optional_msg => TestMessage2.new,
|
237
|
-
:repeated_string => ["hello", "there", "world"])
|
238
|
-
expected = '<BasicTest::TestMessage: optional_int32: -42, optional_int64: 0, optional_uint32: 0, optional_uint64: 0, optional_bool: false, optional_float: 0.0, optional_double: 0.0, optional_string: "", optional_bytes: "", optional_msg: <BasicTest::TestMessage2: foo: 0>, optional_enum: :A, repeated_int32: [], repeated_int64: [], repeated_uint32: [], repeated_uint64: [], repeated_bool: [], repeated_float: [], repeated_double: [], repeated_string: ["hello", "there", "world"], repeated_bytes: [], repeated_msg: [], repeated_enum: []>'
|
239
|
-
assert_equal expected, m.inspect
|
240
|
-
end
|
241
|
-
|
242
|
-
def test_hash
|
243
|
-
m1 = TestMessage.new(:optional_int32 => 42)
|
244
|
-
m2 = TestMessage.new(:optional_int32 => 102, repeated_string: ['please', 'work', 'ok?'])
|
245
|
-
m3 = TestMessage.new(:optional_int32 => 102, repeated_string: ['please', 'work', 'ok?'])
|
246
|
-
assert m1.hash != 0
|
247
|
-
assert m2.hash != 0
|
248
|
-
assert m3.hash != 0
|
249
|
-
# relying on the randomness here -- if hash function changes and we are
|
250
|
-
# unlucky enough to get a collision, then change the values above.
|
251
|
-
assert m1.hash != m2.hash
|
252
|
-
assert_equal m2.hash, m3.hash
|
253
|
-
end
|
254
|
-
|
255
|
-
def test_unknown_field_errors
|
256
|
-
e = assert_raise NoMethodError do
|
257
|
-
TestMessage.new.hello
|
258
|
-
end
|
259
|
-
assert_match(/hello/, e.message)
|
260
|
-
|
261
|
-
e = assert_raise NoMethodError do
|
262
|
-
TestMessage.new.hello = "world"
|
263
|
-
end
|
264
|
-
assert_match(/hello/, e.message)
|
265
|
-
end
|
266
|
-
|
267
|
-
def test_initialization_map_errors
|
268
|
-
e = assert_raise ArgumentError do
|
269
|
-
TestMessage.new(:hello => "world")
|
270
|
-
end
|
271
|
-
assert_match(/hello/, e.message)
|
272
|
-
|
273
|
-
e = assert_raise ArgumentError do
|
274
|
-
MapMessage.new(:map_string_int32 => "hello")
|
275
|
-
end
|
276
|
-
assert_equal e.message, "Expected Hash object as initializer value for map field 'map_string_int32'."
|
277
|
-
|
278
|
-
e = assert_raise ArgumentError do
|
279
|
-
TestMessage.new(:repeated_uint32 => "hello")
|
280
|
-
end
|
281
|
-
assert_equal e.message, "Expected array as initializer value for repeated field 'repeated_uint32'."
|
282
|
-
end
|
283
|
-
|
284
|
-
def test_type_errors
|
285
|
-
m = TestMessage.new
|
286
|
-
assert_raise TypeError do
|
287
|
-
m.optional_int32 = "hello"
|
288
|
-
end
|
289
|
-
assert_raise TypeError do
|
290
|
-
m.optional_string = 42
|
291
|
-
end
|
292
|
-
assert_raise TypeError do
|
293
|
-
m.optional_string = nil
|
294
|
-
end
|
295
|
-
assert_raise TypeError do
|
296
|
-
m.optional_bool = 42
|
297
|
-
end
|
298
|
-
assert_raise TypeError do
|
299
|
-
m.optional_msg = TestMessage.new # expects TestMessage2
|
300
|
-
end
|
301
|
-
|
302
|
-
assert_raise TypeError do
|
303
|
-
m.repeated_int32 = [] # needs RepeatedField
|
304
|
-
end
|
305
|
-
|
306
|
-
assert_raise TypeError do
|
307
|
-
m.repeated_int32.push "hello"
|
308
|
-
end
|
309
|
-
|
310
|
-
assert_raise TypeError do
|
311
|
-
m.repeated_msg.push TestMessage.new
|
312
|
-
end
|
313
|
-
end
|
314
|
-
|
315
|
-
def test_string_encoding
|
316
|
-
m = TestMessage.new
|
317
|
-
|
318
|
-
# Assigning a normal (ASCII or UTF8) string to a bytes field, or
|
319
|
-
# ASCII-8BIT to a string field will convert to the proper encoding.
|
320
|
-
m.optional_bytes = "Test string ASCII".encode!('ASCII')
|
321
|
-
assert m.optional_bytes.frozen?
|
322
|
-
assert_equal Encoding::ASCII_8BIT, m.optional_bytes.encoding
|
323
|
-
assert_equal "Test string ASCII", m.optional_bytes
|
324
|
-
|
325
|
-
assert_raise Encoding::UndefinedConversionError do
|
326
|
-
m.optional_bytes = "Test string UTF-8 \u0100".encode!('UTF-8')
|
327
|
-
end
|
328
|
-
|
329
|
-
assert_raise Encoding::UndefinedConversionError do
|
330
|
-
m.optional_string = ["FFFF"].pack('H*')
|
331
|
-
end
|
332
|
-
|
333
|
-
# "Ordinary" use case.
|
334
|
-
m.optional_bytes = ["FFFF"].pack('H*')
|
335
|
-
m.optional_string = "\u0100"
|
336
|
-
|
337
|
-
# strings are immutable so we can't do this, but serialize should catch it.
|
338
|
-
m.optional_string = "asdf".encode!('UTF-8')
|
339
|
-
assert_raise RuntimeError do
|
340
|
-
m.optional_string.encode!('ASCII-8BIT')
|
341
|
-
end
|
342
|
-
end
|
343
|
-
|
344
|
-
def test_rptfield_int32
|
345
|
-
l = Google::Protobuf::RepeatedField.new(:int32)
|
346
|
-
assert l.count == 0
|
347
|
-
l = Google::Protobuf::RepeatedField.new(:int32, [1, 2, 3])
|
348
|
-
assert l.count == 3
|
349
|
-
assert_equal [1, 2, 3], l
|
350
|
-
assert_equal l, [1, 2, 3]
|
351
|
-
l.push 4
|
352
|
-
assert l == [1, 2, 3, 4]
|
353
|
-
dst_list = []
|
354
|
-
l.each { |val| dst_list.push val }
|
355
|
-
assert dst_list == [1, 2, 3, 4]
|
356
|
-
assert l.to_a == [1, 2, 3, 4]
|
357
|
-
assert l[0] == 1
|
358
|
-
assert l[3] == 4
|
359
|
-
l[0] = 5
|
360
|
-
assert l == [5, 2, 3, 4]
|
361
|
-
|
362
|
-
l2 = l.dup
|
363
|
-
assert l == l2
|
364
|
-
assert l.object_id != l2.object_id
|
365
|
-
l2.push 6
|
366
|
-
assert l.count == 4
|
367
|
-
assert l2.count == 5
|
368
|
-
|
369
|
-
assert l.inspect == '[5, 2, 3, 4]'
|
370
|
-
|
371
|
-
l.concat([7, 8, 9])
|
372
|
-
assert l == [5, 2, 3, 4, 7, 8, 9]
|
373
|
-
assert l.pop == 9
|
374
|
-
assert l == [5, 2, 3, 4, 7, 8]
|
375
|
-
|
376
|
-
assert_raise TypeError do
|
377
|
-
m = TestMessage.new
|
378
|
-
l.push m
|
379
|
-
end
|
380
|
-
|
381
|
-
m = TestMessage.new
|
382
|
-
m.repeated_int32 = l
|
383
|
-
assert m.repeated_int32 == [5, 2, 3, 4, 7, 8]
|
384
|
-
assert m.repeated_int32.object_id == l.object_id
|
385
|
-
l.push 42
|
386
|
-
assert m.repeated_int32.pop == 42
|
387
|
-
|
388
|
-
l3 = l + l.dup
|
389
|
-
assert l3.count == l.count * 2
|
390
|
-
l.count.times do |i|
|
391
|
-
assert l3[i] == l[i]
|
392
|
-
assert l3[l.count + i] == l[i]
|
393
|
-
end
|
394
|
-
|
395
|
-
l.clear
|
396
|
-
assert l.count == 0
|
397
|
-
l += [1, 2, 3, 4]
|
398
|
-
l.replace([5, 6, 7, 8])
|
399
|
-
assert l == [5, 6, 7, 8]
|
400
|
-
|
401
|
-
l4 = Google::Protobuf::RepeatedField.new(:int32)
|
402
|
-
l4[5] = 42
|
403
|
-
assert l4 == [0, 0, 0, 0, 0, 42]
|
404
|
-
|
405
|
-
l4 << 100
|
406
|
-
assert l4 == [0, 0, 0, 0, 0, 42, 100]
|
407
|
-
l4 << 101 << 102
|
408
|
-
assert l4 == [0, 0, 0, 0, 0, 42, 100, 101, 102]
|
409
|
-
end
|
410
|
-
|
411
|
-
def test_parent_rptfield
|
412
|
-
#make sure we set the RepeatedField and can add to it
|
413
|
-
m = TestMessage.new
|
414
|
-
assert m.repeated_string == []
|
415
|
-
m.repeated_string << 'ok'
|
416
|
-
m.repeated_string.push('ok2')
|
417
|
-
assert m.repeated_string == ['ok', 'ok2']
|
418
|
-
m.repeated_string += ['ok3']
|
419
|
-
assert m.repeated_string == ['ok', 'ok2', 'ok3']
|
420
|
-
end
|
421
|
-
|
422
|
-
def test_rptfield_msg
|
423
|
-
l = Google::Protobuf::RepeatedField.new(:message, TestMessage)
|
424
|
-
l.push TestMessage.new
|
425
|
-
assert l.count == 1
|
426
|
-
assert_raise TypeError do
|
427
|
-
l.push TestMessage2.new
|
428
|
-
end
|
429
|
-
assert_raise TypeError do
|
430
|
-
l.push 42
|
431
|
-
end
|
432
|
-
|
433
|
-
l2 = l.dup
|
434
|
-
assert l2[0] == l[0]
|
435
|
-
assert l2[0].object_id == l[0].object_id
|
436
|
-
|
437
|
-
l2 = Google::Protobuf.deep_copy(l)
|
438
|
-
assert l2[0] == l[0]
|
439
|
-
assert l2[0].object_id != l[0].object_id
|
440
|
-
|
441
|
-
l3 = l + l2
|
442
|
-
assert l3.count == 2
|
443
|
-
assert l3[0] == l[0]
|
444
|
-
assert l3[1] == l2[0]
|
445
|
-
l3[0].optional_int32 = 1000
|
446
|
-
assert l[0].optional_int32 == 1000
|
447
|
-
|
448
|
-
new_msg = TestMessage.new(:optional_int32 => 200)
|
449
|
-
l4 = l + [new_msg]
|
450
|
-
assert l4.count == 2
|
451
|
-
new_msg.optional_int32 = 1000
|
452
|
-
assert l4[1].optional_int32 == 1000
|
453
|
-
end
|
454
|
-
|
455
|
-
def test_rptfield_enum
|
456
|
-
l = Google::Protobuf::RepeatedField.new(:enum, TestEnum)
|
457
|
-
l.push :A
|
458
|
-
l.push :B
|
459
|
-
l.push :C
|
460
|
-
assert l.count == 3
|
461
|
-
assert_raise RangeError do
|
462
|
-
l.push :D
|
463
|
-
end
|
464
|
-
assert l[0] == :A
|
465
|
-
|
466
|
-
l.push 4
|
467
|
-
assert l[3] == 4
|
468
|
-
end
|
469
|
-
|
470
|
-
def test_rptfield_initialize
|
471
|
-
assert_raise ArgumentError do
|
472
|
-
l = Google::Protobuf::RepeatedField.new
|
473
|
-
end
|
474
|
-
assert_raise ArgumentError do
|
475
|
-
l = Google::Protobuf::RepeatedField.new(:message)
|
476
|
-
end
|
477
|
-
assert_raise ArgumentError do
|
478
|
-
l = Google::Protobuf::RepeatedField.new([1, 2, 3])
|
479
|
-
end
|
480
|
-
assert_raise ArgumentError do
|
481
|
-
l = Google::Protobuf::RepeatedField.new(:message, [TestMessage2.new])
|
482
|
-
end
|
483
|
-
end
|
484
|
-
|
485
|
-
def test_rptfield_array_ducktyping
|
486
|
-
l = Google::Protobuf::RepeatedField.new(:int32)
|
487
|
-
length_methods = %w(count length size)
|
488
|
-
length_methods.each do |lm|
|
489
|
-
assert l.send(lm) == 0
|
490
|
-
end
|
491
|
-
# out of bounds returns a nil
|
492
|
-
assert l[0] == nil
|
493
|
-
assert l[1] == nil
|
494
|
-
assert l[-1] == nil
|
495
|
-
l.push 4
|
496
|
-
length_methods.each do |lm|
|
497
|
-
assert l.send(lm) == 1
|
498
|
-
end
|
499
|
-
assert l[0] == 4
|
500
|
-
assert l[1] == nil
|
501
|
-
assert l[-1] == 4
|
502
|
-
assert l[-2] == nil
|
503
|
-
|
504
|
-
l.push 2
|
505
|
-
length_methods.each do |lm|
|
506
|
-
assert l.send(lm) == 2
|
507
|
-
end
|
508
|
-
assert l[0] == 4
|
509
|
-
assert l[1] == 2
|
510
|
-
assert l[2] == nil
|
511
|
-
assert l[-1] == 2
|
512
|
-
assert l[-2] == 4
|
513
|
-
assert l[-3] == nil
|
514
|
-
|
515
|
-
#adding out of scope will backfill with empty objects
|
516
|
-
end
|
517
|
-
|
518
|
-
def test_map_basic
|
519
|
-
# allowed key types:
|
520
|
-
# :int32, :int64, :uint32, :uint64, :bool, :string, :bytes.
|
521
|
-
|
522
|
-
m = Google::Protobuf::Map.new(:string, :int32)
|
523
|
-
m["asdf"] = 1
|
524
|
-
assert m["asdf"] == 1
|
525
|
-
m["jkl;"] = 42
|
526
|
-
assert m == { "jkl;" => 42, "asdf" => 1 }
|
527
|
-
assert m.has_key?("asdf")
|
528
|
-
assert !m.has_key?("qwerty")
|
529
|
-
assert m.length == 2
|
530
|
-
|
531
|
-
m2 = m.dup
|
532
|
-
assert_equal m, m2
|
533
|
-
assert m.hash != 0
|
534
|
-
assert_equal m.hash, m2.hash
|
535
|
-
|
536
|
-
collected = {}
|
537
|
-
m.each { |k,v| collected[v] = k }
|
538
|
-
assert collected == { 42 => "jkl;", 1 => "asdf" }
|
539
|
-
|
540
|
-
assert m.delete("asdf") == 1
|
541
|
-
assert !m.has_key?("asdf")
|
542
|
-
assert m["asdf"] == nil
|
543
|
-
assert !m.has_key?("asdf")
|
544
|
-
|
545
|
-
# We only assert on inspect value when there is one map entry because the
|
546
|
-
# order in which elements appear is unspecified (depends on the internal
|
547
|
-
# hash function). We don't want a brittle test.
|
548
|
-
assert m.inspect == "{\"jkl;\"=>42}"
|
549
|
-
|
550
|
-
assert m.keys == ["jkl;"]
|
551
|
-
assert m.values == [42]
|
552
|
-
|
553
|
-
m.clear
|
554
|
-
assert m.length == 0
|
555
|
-
assert m == {}
|
556
|
-
|
557
|
-
assert_raise TypeError do
|
558
|
-
m[1] = 1
|
559
|
-
end
|
560
|
-
assert_raise RangeError do
|
561
|
-
m["asdf"] = 0x1_0000_0000
|
562
|
-
end
|
563
|
-
end
|
564
|
-
|
565
|
-
def test_map_ctor
|
566
|
-
m = Google::Protobuf::Map.new(:string, :int32,
|
567
|
-
{"a" => 1, "b" => 2, "c" => 3})
|
568
|
-
assert m == {"a" => 1, "c" => 3, "b" => 2}
|
569
|
-
end
|
570
|
-
|
571
|
-
def test_map_keytypes
|
572
|
-
m = Google::Protobuf::Map.new(:int32, :int32)
|
573
|
-
m[1] = 42
|
574
|
-
m[-1] = 42
|
575
|
-
assert_raise RangeError do
|
576
|
-
m[0x8000_0000] = 1
|
577
|
-
end
|
578
|
-
assert_raise TypeError do
|
579
|
-
m["asdf"] = 1
|
580
|
-
end
|
581
|
-
|
582
|
-
m = Google::Protobuf::Map.new(:int64, :int32)
|
583
|
-
m[0x1000_0000_0000_0000] = 1
|
584
|
-
assert_raise RangeError do
|
585
|
-
m[0x1_0000_0000_0000_0000] = 1
|
586
|
-
end
|
587
|
-
assert_raise TypeError do
|
588
|
-
m["asdf"] = 1
|
589
|
-
end
|
590
|
-
|
591
|
-
m = Google::Protobuf::Map.new(:uint32, :int32)
|
592
|
-
m[0x8000_0000] = 1
|
593
|
-
assert_raise RangeError do
|
594
|
-
m[0x1_0000_0000] = 1
|
595
|
-
end
|
596
|
-
assert_raise RangeError do
|
597
|
-
m[-1] = 1
|
598
|
-
end
|
599
|
-
|
600
|
-
m = Google::Protobuf::Map.new(:uint64, :int32)
|
601
|
-
m[0x8000_0000_0000_0000] = 1
|
602
|
-
assert_raise RangeError do
|
603
|
-
m[0x1_0000_0000_0000_0000] = 1
|
604
|
-
end
|
605
|
-
assert_raise RangeError do
|
606
|
-
m[-1] = 1
|
607
|
-
end
|
608
|
-
|
609
|
-
m = Google::Protobuf::Map.new(:bool, :int32)
|
610
|
-
m[true] = 1
|
611
|
-
m[false] = 2
|
612
|
-
assert_raise TypeError do
|
613
|
-
m[1] = 1
|
614
|
-
end
|
615
|
-
assert_raise TypeError do
|
616
|
-
m["asdf"] = 1
|
617
|
-
end
|
618
|
-
|
619
|
-
m = Google::Protobuf::Map.new(:string, :int32)
|
620
|
-
m["asdf"] = 1
|
621
|
-
assert_raise TypeError do
|
622
|
-
m[1] = 1
|
623
|
-
end
|
624
|
-
assert_raise Encoding::UndefinedConversionError do
|
625
|
-
bytestring = ["FFFF"].pack("H*")
|
626
|
-
m[bytestring] = 1
|
627
|
-
end
|
628
|
-
|
629
|
-
m = Google::Protobuf::Map.new(:bytes, :int32)
|
630
|
-
bytestring = ["FFFF"].pack("H*")
|
631
|
-
m[bytestring] = 1
|
632
|
-
# Allowed -- we will automatically convert to ASCII-8BIT.
|
633
|
-
m["asdf"] = 1
|
634
|
-
assert_raise TypeError do
|
635
|
-
m[1] = 1
|
636
|
-
end
|
637
|
-
end
|
638
|
-
|
639
|
-
def test_map_msg_enum_valuetypes
|
640
|
-
m = Google::Protobuf::Map.new(:string, :message, TestMessage)
|
641
|
-
m["asdf"] = TestMessage.new
|
642
|
-
assert_raise TypeError do
|
643
|
-
m["jkl;"] = TestMessage2.new
|
644
|
-
end
|
645
|
-
|
646
|
-
m = Google::Protobuf::Map.new(
|
647
|
-
:string, :message, TestMessage,
|
648
|
-
{ "a" => TestMessage.new(:optional_int32 => 42),
|
649
|
-
"b" => TestMessage.new(:optional_int32 => 84) })
|
650
|
-
assert m.length == 2
|
651
|
-
assert m.values.map{|msg| msg.optional_int32}.sort == [42, 84]
|
652
|
-
|
653
|
-
m = Google::Protobuf::Map.new(:string, :enum, TestEnum,
|
654
|
-
{ "x" => :A, "y" => :B, "z" => :C })
|
655
|
-
assert m.length == 3
|
656
|
-
assert m["z"] == :C
|
657
|
-
m["z"] = 2
|
658
|
-
assert m["z"] == :B
|
659
|
-
m["z"] = 4
|
660
|
-
assert m["z"] == 4
|
661
|
-
assert_raise RangeError do
|
662
|
-
m["z"] = :Z
|
663
|
-
end
|
664
|
-
assert_raise RangeError do
|
665
|
-
m["z"] = "z"
|
666
|
-
end
|
667
|
-
end
|
668
|
-
|
669
|
-
def test_map_dup_deep_copy
|
670
|
-
m = Google::Protobuf::Map.new(
|
671
|
-
:string, :message, TestMessage,
|
672
|
-
{ "a" => TestMessage.new(:optional_int32 => 42),
|
673
|
-
"b" => TestMessage.new(:optional_int32 => 84) })
|
674
|
-
|
675
|
-
m2 = m.dup
|
676
|
-
assert m == m2
|
677
|
-
assert m.object_id != m2.object_id
|
678
|
-
assert m["a"].object_id == m2["a"].object_id
|
679
|
-
assert m["b"].object_id == m2["b"].object_id
|
680
|
-
|
681
|
-
m2 = Google::Protobuf.deep_copy(m)
|
682
|
-
assert m == m2
|
683
|
-
assert m.object_id != m2.object_id
|
684
|
-
assert m["a"].object_id != m2["a"].object_id
|
685
|
-
assert m["b"].object_id != m2["b"].object_id
|
686
|
-
end
|
687
|
-
|
688
|
-
def test_map_field
|
689
|
-
m = MapMessage.new
|
690
|
-
assert m.map_string_int32 == {}
|
691
|
-
assert m.map_string_msg == {}
|
692
|
-
|
693
|
-
m = MapMessage.new(
|
694
|
-
:map_string_int32 => {"a" => 1, "b" => 2},
|
695
|
-
:map_string_msg => {"a" => TestMessage2.new(:foo => 1),
|
696
|
-
"b" => TestMessage2.new(:foo => 2)})
|
697
|
-
assert m.map_string_int32.keys.sort == ["a", "b"]
|
698
|
-
assert m.map_string_int32["a"] == 1
|
699
|
-
assert m.map_string_msg["b"].foo == 2
|
700
|
-
|
701
|
-
m.map_string_int32["c"] = 3
|
702
|
-
assert m.map_string_int32["c"] == 3
|
703
|
-
m.map_string_msg["c"] = TestMessage2.new(:foo => 3)
|
704
|
-
assert m.map_string_msg["c"] == TestMessage2.new(:foo => 3)
|
705
|
-
m.map_string_msg.delete("b")
|
706
|
-
m.map_string_msg.delete("c")
|
707
|
-
assert m.map_string_msg == { "a" => TestMessage2.new(:foo => 1) }
|
708
|
-
|
709
|
-
assert_raise TypeError do
|
710
|
-
m.map_string_msg["e"] = TestMessage.new # wrong value type
|
711
|
-
end
|
712
|
-
# ensure nothing was added by the above
|
713
|
-
assert m.map_string_msg == { "a" => TestMessage2.new(:foo => 1) }
|
714
|
-
|
715
|
-
m.map_string_int32 = Google::Protobuf::Map.new(:string, :int32)
|
716
|
-
assert_raise TypeError do
|
717
|
-
m.map_string_int32 = Google::Protobuf::Map.new(:string, :int64)
|
718
|
-
end
|
719
|
-
assert_raise TypeError do
|
720
|
-
m.map_string_int32 = {}
|
721
|
-
end
|
722
|
-
|
723
|
-
assert_raise TypeError do
|
724
|
-
m = MapMessage.new(:map_string_int32 => { 1 => "I am not a number" })
|
725
|
-
end
|
726
|
-
end
|
727
|
-
|
728
|
-
def test_map_corruption
|
729
|
-
# This pattern led to a crash in a previous version of upb/protobuf.
|
730
|
-
m = MapMessage.new(map_string_int32: { "aaa" => 1 })
|
731
|
-
m.map_string_int32['podid'] = 2
|
732
|
-
m.map_string_int32['aaa'] = 3
|
733
|
-
end
|
734
|
-
|
735
|
-
def test_concurrent_decoding
|
736
|
-
o = Outer.new
|
737
|
-
o.items[0] = Inner.new
|
738
|
-
raw = Outer.encode(o)
|
739
|
-
|
740
|
-
thds = 2.times.map do
|
741
|
-
Thread.new do
|
742
|
-
100000.times do
|
743
|
-
assert_equal o, Outer.decode(raw)
|
744
|
-
end
|
745
|
-
end
|
746
|
-
end
|
747
|
-
thds.map(&:join)
|
748
|
-
end
|
749
|
-
|
750
|
-
def test_map_encode_decode
|
751
|
-
m = MapMessage.new(
|
752
|
-
:map_string_int32 => {"a" => 1, "b" => 2},
|
753
|
-
:map_string_msg => {"a" => TestMessage2.new(:foo => 1),
|
754
|
-
"b" => TestMessage2.new(:foo => 2)})
|
755
|
-
m2 = MapMessage.decode(MapMessage.encode(m))
|
756
|
-
assert m == m2
|
757
|
-
|
758
|
-
m3 = MapMessageWireEquiv.decode(MapMessage.encode(m))
|
759
|
-
assert m3.map_string_int32.length == 2
|
760
|
-
|
761
|
-
kv = {}
|
762
|
-
m3.map_string_int32.map { |msg| kv[msg.key] = msg.value }
|
763
|
-
assert kv == {"a" => 1, "b" => 2}
|
764
|
-
|
765
|
-
kv = {}
|
766
|
-
m3.map_string_msg.map { |msg| kv[msg.key] = msg.value }
|
767
|
-
assert kv == {"a" => TestMessage2.new(:foo => 1),
|
768
|
-
"b" => TestMessage2.new(:foo => 2)}
|
769
|
-
end
|
770
|
-
|
771
|
-
def test_oneof_descriptors
|
772
|
-
d = OneofMessage.descriptor
|
773
|
-
o = d.lookup_oneof("my_oneof")
|
774
|
-
assert o != nil
|
775
|
-
assert o.class == Google::Protobuf::OneofDescriptor
|
776
|
-
assert o.name == "my_oneof"
|
777
|
-
oneof_count = 0
|
778
|
-
d.each_oneof{ |oneof|
|
779
|
-
oneof_count += 1
|
780
|
-
assert oneof == o
|
781
|
-
}
|
782
|
-
assert oneof_count == 1
|
783
|
-
assert o.count == 4
|
784
|
-
field_names = o.map{|f| f.name}.sort
|
785
|
-
assert field_names == ["a", "b", "c", "d"]
|
786
|
-
end
|
787
|
-
|
788
|
-
def test_oneof
|
789
|
-
d = OneofMessage.new
|
790
|
-
assert d.a == ""
|
791
|
-
assert d.b == 0
|
792
|
-
assert d.c == nil
|
793
|
-
assert d.d == :Default
|
794
|
-
assert d.my_oneof == nil
|
795
|
-
|
796
|
-
d.a = "hi"
|
797
|
-
assert d.a == "hi"
|
798
|
-
assert d.b == 0
|
799
|
-
assert d.c == nil
|
800
|
-
assert d.d == :Default
|
801
|
-
assert d.my_oneof == :a
|
802
|
-
|
803
|
-
d.b = 42
|
804
|
-
assert d.a == ""
|
805
|
-
assert d.b == 42
|
806
|
-
assert d.c == nil
|
807
|
-
assert d.d == :Default
|
808
|
-
assert d.my_oneof == :b
|
809
|
-
|
810
|
-
d.c = TestMessage2.new(:foo => 100)
|
811
|
-
assert d.a == ""
|
812
|
-
assert d.b == 0
|
813
|
-
assert d.c.foo == 100
|
814
|
-
assert d.d == :Default
|
815
|
-
assert d.my_oneof == :c
|
816
|
-
|
817
|
-
d.d = :C
|
818
|
-
assert d.a == ""
|
819
|
-
assert d.b == 0
|
820
|
-
assert d.c == nil
|
821
|
-
assert d.d == :C
|
822
|
-
assert d.my_oneof == :d
|
823
|
-
|
824
|
-
d2 = OneofMessage.decode(OneofMessage.encode(d))
|
825
|
-
assert d2 == d
|
826
|
-
|
827
|
-
encoded_field_a = OneofMessage.encode(OneofMessage.new(:a => "string"))
|
828
|
-
encoded_field_b = OneofMessage.encode(OneofMessage.new(:b => 1000))
|
829
|
-
encoded_field_c = OneofMessage.encode(
|
830
|
-
OneofMessage.new(:c => TestMessage2.new(:foo => 1)))
|
831
|
-
encoded_field_d = OneofMessage.encode(OneofMessage.new(:d => :B))
|
832
|
-
|
833
|
-
d3 = OneofMessage.decode(
|
834
|
-
encoded_field_c + encoded_field_a + encoded_field_d)
|
835
|
-
assert d3.a == ""
|
836
|
-
assert d3.b == 0
|
837
|
-
assert d3.c == nil
|
838
|
-
assert d3.d == :B
|
839
|
-
|
840
|
-
d4 = OneofMessage.decode(
|
841
|
-
encoded_field_c + encoded_field_a + encoded_field_d +
|
842
|
-
encoded_field_c)
|
843
|
-
assert d4.a == ""
|
844
|
-
assert d4.b == 0
|
845
|
-
assert d4.c.foo == 1
|
846
|
-
assert d4.d == :Default
|
847
|
-
|
848
|
-
d5 = OneofMessage.new(:a => "hello")
|
849
|
-
assert d5.a == "hello"
|
850
|
-
d5.a = nil
|
851
|
-
assert d5.a == ""
|
852
|
-
assert OneofMessage.encode(d5) == ''
|
853
|
-
assert d5.my_oneof == nil
|
854
|
-
end
|
855
|
-
|
856
|
-
def test_enum_field
|
857
|
-
m = TestMessage.new
|
858
|
-
assert m.optional_enum == :Default
|
859
|
-
m.optional_enum = :A
|
860
|
-
assert m.optional_enum == :A
|
861
|
-
assert_raise RangeError do
|
862
|
-
m.optional_enum = :ASDF
|
863
|
-
end
|
864
|
-
m.optional_enum = 1
|
865
|
-
assert m.optional_enum == :A
|
866
|
-
m.optional_enum = 100
|
867
|
-
assert m.optional_enum == 100
|
868
|
-
end
|
869
|
-
|
870
|
-
def test_dup
|
871
|
-
m = TestMessage.new
|
872
|
-
m.optional_string = "hello"
|
873
|
-
m.optional_int32 = 42
|
874
|
-
tm1 = TestMessage2.new(:foo => 100)
|
875
|
-
tm2 = TestMessage2.new(:foo => 200)
|
876
|
-
m.repeated_msg.push tm1
|
877
|
-
assert m.repeated_msg[-1] == tm1
|
878
|
-
m.repeated_msg.push tm2
|
879
|
-
assert m.repeated_msg[-1] == tm2
|
880
|
-
m2 = m.dup
|
881
|
-
assert m == m2
|
882
|
-
m.optional_int32 += 1
|
883
|
-
assert m != m2
|
884
|
-
assert m.repeated_msg[0] == m2.repeated_msg[0]
|
885
|
-
assert m.repeated_msg[0].object_id == m2.repeated_msg[0].object_id
|
886
|
-
end
|
887
|
-
|
888
|
-
def test_deep_copy
|
889
|
-
m = TestMessage.new(:optional_int32 => 42,
|
890
|
-
:repeated_msg => [TestMessage2.new(:foo => 100)])
|
891
|
-
m2 = Google::Protobuf.deep_copy(m)
|
892
|
-
assert m == m2
|
893
|
-
assert m.repeated_msg == m2.repeated_msg
|
894
|
-
assert m.repeated_msg.object_id != m2.repeated_msg.object_id
|
895
|
-
assert m.repeated_msg[0].object_id != m2.repeated_msg[0].object_id
|
896
|
-
end
|
897
|
-
|
898
|
-
def test_eq
|
899
|
-
m = TestMessage.new(:optional_int32 => 42,
|
900
|
-
:repeated_int32 => [1, 2, 3])
|
901
|
-
m2 = TestMessage.new(:optional_int32 => 43,
|
902
|
-
:repeated_int32 => [1, 2, 3])
|
903
|
-
assert m != m2
|
904
|
-
end
|
905
|
-
|
906
|
-
def test_enum_lookup
|
907
|
-
assert TestEnum::A == 1
|
908
|
-
assert TestEnum::B == 2
|
909
|
-
assert TestEnum::C == 3
|
910
|
-
|
911
|
-
assert TestEnum::lookup(1) == :A
|
912
|
-
assert TestEnum::lookup(2) == :B
|
913
|
-
assert TestEnum::lookup(3) == :C
|
914
|
-
|
915
|
-
assert TestEnum::resolve(:A) == 1
|
916
|
-
assert TestEnum::resolve(:B) == 2
|
917
|
-
assert TestEnum::resolve(:C) == 3
|
918
|
-
end
|
919
|
-
|
920
|
-
def test_parse_serialize
|
921
|
-
m = TestMessage.new(:optional_int32 => 42,
|
922
|
-
:optional_string => "hello world",
|
923
|
-
:optional_enum => :B,
|
924
|
-
:repeated_string => ["a", "b", "c"],
|
925
|
-
:repeated_int32 => [42, 43, 44],
|
926
|
-
:repeated_enum => [:A, :B, :C, 100],
|
927
|
-
:repeated_msg => [TestMessage2.new(:foo => 1),
|
928
|
-
TestMessage2.new(:foo => 2)])
|
929
|
-
data = TestMessage.encode m
|
930
|
-
m2 = TestMessage.decode data
|
931
|
-
assert m == m2
|
932
|
-
|
933
|
-
data = Google::Protobuf.encode m
|
934
|
-
m2 = Google::Protobuf.decode(TestMessage, data)
|
935
|
-
assert m == m2
|
936
|
-
end
|
937
|
-
|
938
|
-
def test_encode_decode_helpers
|
939
|
-
m = TestMessage.new(:optional_string => 'foo', :repeated_string => ['bar1', 'bar2'])
|
940
|
-
assert_equal 'foo', m.optional_string
|
941
|
-
assert_equal ['bar1', 'bar2'], m.repeated_string
|
942
|
-
|
943
|
-
json = m.to_json
|
944
|
-
m2 = TestMessage.decode_json(json)
|
945
|
-
assert_equal 'foo', m2.optional_string
|
946
|
-
assert_equal ['bar1', 'bar2'], m2.repeated_string
|
947
|
-
if RUBY_PLATFORM != "java"
|
948
|
-
assert m2.optional_string.frozen?
|
949
|
-
assert m2.repeated_string[0].frozen?
|
950
|
-
end
|
951
|
-
|
952
|
-
proto = m.to_proto
|
953
|
-
m2 = TestMessage.decode(proto)
|
954
|
-
assert_equal 'foo', m2.optional_string
|
955
|
-
assert_equal ['bar1', 'bar2'], m2.repeated_string
|
956
|
-
end
|
957
|
-
|
958
|
-
def test_protobuf_encode_decode_helpers
|
959
|
-
m = TestMessage.new(:optional_string => 'foo', :repeated_string => ['bar1', 'bar2'])
|
960
|
-
encoded_msg = Google::Protobuf.encode(m)
|
961
|
-
assert_equal m.to_proto, encoded_msg
|
962
|
-
|
963
|
-
decoded_msg = Google::Protobuf.decode(TestMessage, encoded_msg)
|
964
|
-
assert_equal TestMessage.decode(m.to_proto), decoded_msg
|
965
|
-
end
|
966
|
-
|
967
|
-
def test_protobuf_encode_decode_json_helpers
|
968
|
-
m = TestMessage.new(:optional_string => 'foo', :repeated_string => ['bar1', 'bar2'])
|
969
|
-
encoded_msg = Google::Protobuf.encode_json(m)
|
970
|
-
assert_equal m.to_json, encoded_msg
|
971
|
-
|
972
|
-
decoded_msg = Google::Protobuf.decode_json(TestMessage, encoded_msg)
|
973
|
-
assert_equal TestMessage.decode_json(m.to_json), decoded_msg
|
974
|
-
end
|
975
|
-
|
976
|
-
def test_to_h
|
977
|
-
m = TestMessage.new(:optional_bool => true, :optional_double => -10.100001, :optional_string => 'foo', :repeated_string => ['bar1', 'bar2'], :repeated_msg => [TestMessage2.new(:foo => 100)])
|
978
|
-
expected_result = {
|
979
|
-
:optional_bool=>true,
|
980
|
-
:optional_bytes=>"",
|
981
|
-
:optional_double=>-10.100001,
|
982
|
-
:optional_enum=>:Default,
|
983
|
-
:optional_float=>0.0,
|
984
|
-
:optional_int32=>0,
|
985
|
-
:optional_int64=>0,
|
986
|
-
:optional_msg=>nil,
|
987
|
-
:optional_string=>"foo",
|
988
|
-
:optional_uint32=>0,
|
989
|
-
:optional_uint64=>0,
|
990
|
-
:repeated_bool=>[],
|
991
|
-
:repeated_bytes=>[],
|
992
|
-
:repeated_double=>[],
|
993
|
-
:repeated_enum=>[],
|
994
|
-
:repeated_float=>[],
|
995
|
-
:repeated_int32=>[],
|
996
|
-
:repeated_int64=>[],
|
997
|
-
:repeated_msg=>[{:foo => 100}],
|
998
|
-
:repeated_string=>["bar1", "bar2"],
|
999
|
-
:repeated_uint32=>[],
|
1000
|
-
:repeated_uint64=>[]
|
1001
|
-
}
|
1002
|
-
assert_equal expected_result, m.to_h
|
1003
|
-
|
1004
|
-
m = MapMessage.new(
|
1005
|
-
:map_string_int32 => {"a" => 1, "b" => 2},
|
1006
|
-
:map_string_msg => {"a" => TestMessage2.new(:foo => 1),
|
1007
|
-
"b" => TestMessage2.new(:foo => 2)})
|
1008
|
-
expected_result = {
|
1009
|
-
:map_string_int32 => {"a" => 1, "b" => 2},
|
1010
|
-
:map_string_msg => {"a" => {:foo => 1}, "b" => {:foo => 2}}
|
1011
|
-
}
|
1012
|
-
assert_equal expected_result, m.to_h
|
1013
|
-
end
|
1014
|
-
|
1015
|
-
|
1016
|
-
def test_def_errors
|
1017
|
-
s = Google::Protobuf::DescriptorPool.new
|
1018
|
-
assert_raise TypeError do
|
1019
|
-
s.build do
|
1020
|
-
# enum with no default (integer value 0)
|
1021
|
-
add_enum "MyEnum" do
|
1022
|
-
value :A, 1
|
1023
|
-
end
|
1024
|
-
end
|
1025
|
-
end
|
1026
|
-
assert_raise TypeError do
|
1027
|
-
s.build do
|
1028
|
-
# message with required field (unsupported in proto3)
|
1029
|
-
add_message "MyMessage" do
|
1030
|
-
required :foo, :int32, 1
|
1031
|
-
end
|
1032
|
-
end
|
1033
|
-
end
|
1034
|
-
end
|
1035
|
-
|
1036
|
-
def test_corecursive
|
1037
|
-
# just be sure that we can instantiate types with corecursive field-type
|
1038
|
-
# references.
|
1039
|
-
m = Recursive1.new(:foo => Recursive2.new(:foo => Recursive1.new))
|
1040
|
-
assert Recursive1.descriptor.lookup("foo").subtype ==
|
1041
|
-
Recursive2.descriptor
|
1042
|
-
assert Recursive2.descriptor.lookup("foo").subtype ==
|
1043
|
-
Recursive1.descriptor
|
1044
|
-
|
1045
|
-
serialized = Recursive1.encode(m)
|
1046
|
-
m2 = Recursive1.decode(serialized)
|
1047
|
-
assert m == m2
|
1048
|
-
end
|
1049
|
-
|
1050
|
-
def test_serialize_cycle
|
1051
|
-
m = Recursive1.new(:foo => Recursive2.new)
|
1052
|
-
m.foo.foo = m
|
1053
|
-
assert_raise RuntimeError do
|
1054
|
-
serialized = Recursive1.encode(m)
|
1055
|
-
end
|
1056
|
-
end
|
1057
|
-
|
1058
|
-
def test_bad_field_names
|
1059
|
-
m = BadFieldNames.new(:dup => 1, :class => 2)
|
1060
|
-
m2 = m.dup
|
1061
|
-
assert m == m2
|
1062
|
-
assert m['dup'] == 1
|
1063
|
-
assert m['class'] == 2
|
1064
|
-
m['dup'] = 3
|
1065
|
-
assert m['dup'] == 3
|
1066
|
-
m['a.b'] = 4
|
1067
|
-
assert m['a.b'] == 4
|
1068
|
-
end
|
1069
|
-
|
1070
|
-
def test_int_ranges
|
1071
|
-
m = TestMessage.new
|
1072
|
-
|
1073
|
-
m.optional_int32 = 0
|
1074
|
-
m.optional_int32 = -0x8000_0000
|
1075
|
-
m.optional_int32 = +0x7fff_ffff
|
1076
|
-
m.optional_int32 = 1.0
|
1077
|
-
m.optional_int32 = -1.0
|
1078
|
-
m.optional_int32 = 2e9
|
1079
|
-
assert_raise RangeError do
|
1080
|
-
m.optional_int32 = -0x8000_0001
|
1081
|
-
end
|
1082
|
-
assert_raise RangeError do
|
1083
|
-
m.optional_int32 = +0x8000_0000
|
1084
|
-
end
|
1085
|
-
assert_raise RangeError do
|
1086
|
-
m.optional_int32 = +0x1000_0000_0000_0000_0000_0000 # force Bignum
|
1087
|
-
end
|
1088
|
-
assert_raise RangeError do
|
1089
|
-
m.optional_int32 = 1e12
|
1090
|
-
end
|
1091
|
-
assert_raise RangeError do
|
1092
|
-
m.optional_int32 = 1.5
|
1093
|
-
end
|
1094
|
-
|
1095
|
-
m.optional_uint32 = 0
|
1096
|
-
m.optional_uint32 = +0xffff_ffff
|
1097
|
-
m.optional_uint32 = 1.0
|
1098
|
-
m.optional_uint32 = 4e9
|
1099
|
-
assert_raise RangeError do
|
1100
|
-
m.optional_uint32 = -1
|
1101
|
-
end
|
1102
|
-
assert_raise RangeError do
|
1103
|
-
m.optional_uint32 = -1.5
|
1104
|
-
end
|
1105
|
-
assert_raise RangeError do
|
1106
|
-
m.optional_uint32 = -1.5e12
|
1107
|
-
end
|
1108
|
-
assert_raise RangeError do
|
1109
|
-
m.optional_uint32 = -0x1000_0000_0000_0000
|
1110
|
-
end
|
1111
|
-
assert_raise RangeError do
|
1112
|
-
m.optional_uint32 = +0x1_0000_0000
|
1113
|
-
end
|
1114
|
-
assert_raise RangeError do
|
1115
|
-
m.optional_uint32 = +0x1000_0000_0000_0000_0000_0000 # force Bignum
|
1116
|
-
end
|
1117
|
-
assert_raise RangeError do
|
1118
|
-
m.optional_uint32 = 1e12
|
1119
|
-
end
|
1120
|
-
assert_raise RangeError do
|
1121
|
-
m.optional_uint32 = 1.5
|
1122
|
-
end
|
1123
|
-
|
1124
|
-
m.optional_int64 = 0
|
1125
|
-
m.optional_int64 = -0x8000_0000_0000_0000
|
1126
|
-
m.optional_int64 = +0x7fff_ffff_ffff_ffff
|
1127
|
-
m.optional_int64 = 1.0
|
1128
|
-
m.optional_int64 = -1.0
|
1129
|
-
m.optional_int64 = 8e18
|
1130
|
-
m.optional_int64 = -8e18
|
1131
|
-
assert_raise RangeError do
|
1132
|
-
m.optional_int64 = -0x8000_0000_0000_0001
|
1133
|
-
end
|
1134
|
-
assert_raise RangeError do
|
1135
|
-
m.optional_int64 = +0x8000_0000_0000_0000
|
1136
|
-
end
|
1137
|
-
assert_raise RangeError do
|
1138
|
-
m.optional_int64 = +0x1000_0000_0000_0000_0000_0000 # force Bignum
|
1139
|
-
end
|
1140
|
-
assert_raise RangeError do
|
1141
|
-
m.optional_int64 = 1e50
|
1142
|
-
end
|
1143
|
-
assert_raise RangeError do
|
1144
|
-
m.optional_int64 = 1.5
|
1145
|
-
end
|
1146
|
-
|
1147
|
-
m.optional_uint64 = 0
|
1148
|
-
m.optional_uint64 = +0xffff_ffff_ffff_ffff
|
1149
|
-
m.optional_uint64 = 1.0
|
1150
|
-
m.optional_uint64 = 16e18
|
1151
|
-
assert_raise RangeError do
|
1152
|
-
m.optional_uint64 = -1
|
1153
|
-
end
|
1154
|
-
assert_raise RangeError do
|
1155
|
-
m.optional_uint64 = -1.5
|
1156
|
-
end
|
1157
|
-
assert_raise RangeError do
|
1158
|
-
m.optional_uint64 = -1.5e12
|
1159
|
-
end
|
1160
|
-
assert_raise RangeError do
|
1161
|
-
m.optional_uint64 = -0x1_0000_0000_0000_0000
|
1162
|
-
end
|
1163
|
-
assert_raise RangeError do
|
1164
|
-
m.optional_uint64 = +0x1_0000_0000_0000_0000
|
1165
|
-
end
|
1166
|
-
assert_raise RangeError do
|
1167
|
-
m.optional_uint64 = +0x1000_0000_0000_0000_0000_0000 # force Bignum
|
1168
|
-
end
|
1169
|
-
assert_raise RangeError do
|
1170
|
-
m.optional_uint64 = 1e50
|
1171
|
-
end
|
1172
|
-
assert_raise RangeError do
|
1173
|
-
m.optional_uint64 = 1.5
|
1174
|
-
end
|
1175
|
-
end
|
1176
|
-
|
1177
|
-
def test_stress_test
|
1178
|
-
m = TestMessage.new
|
1179
|
-
m.optional_int32 = 42
|
1180
|
-
m.optional_int64 = 0x100000000
|
1181
|
-
m.optional_string = "hello world"
|
1182
|
-
10.times do m.repeated_msg.push TestMessage2.new(:foo => 42) end
|
1183
|
-
10.times do m.repeated_string.push "hello world" end
|
1184
|
-
|
1185
|
-
data = TestMessage.encode(m)
|
1186
|
-
|
1187
|
-
l = 0
|
1188
|
-
10_000.times do
|
1189
|
-
m = TestMessage.decode(data)
|
1190
|
-
data_new = TestMessage.encode(m)
|
1191
|
-
assert data_new == data
|
1192
|
-
data = data_new
|
1193
|
-
end
|
1194
|
-
end
|
1195
|
-
|
1196
|
-
def test_reflection
|
1197
|
-
m = TestMessage.new(:optional_int32 => 1234)
|
1198
|
-
msgdef = m.class.descriptor
|
1199
|
-
assert msgdef.class == Google::Protobuf::Descriptor
|
1200
|
-
assert msgdef.any? {|field| field.name == "optional_int32"}
|
1201
|
-
optional_int32 = msgdef.lookup "optional_int32"
|
1202
|
-
assert optional_int32.class == Google::Protobuf::FieldDescriptor
|
1203
|
-
assert optional_int32 != nil
|
1204
|
-
assert optional_int32.name == "optional_int32"
|
1205
|
-
assert optional_int32.type == :int32
|
1206
|
-
optional_int32.set(m, 5678)
|
1207
|
-
assert m.optional_int32 == 5678
|
1208
|
-
m.optional_int32 = 1000
|
1209
|
-
assert optional_int32.get(m) == 1000
|
1210
|
-
|
1211
|
-
optional_msg = msgdef.lookup "optional_msg"
|
1212
|
-
assert optional_msg.subtype == TestMessage2.descriptor
|
1213
|
-
|
1214
|
-
optional_msg.set(m, optional_msg.subtype.msgclass.new)
|
1215
|
-
|
1216
|
-
assert msgdef.msgclass == TestMessage
|
1217
|
-
|
1218
|
-
optional_enum = msgdef.lookup "optional_enum"
|
1219
|
-
assert optional_enum.subtype == TestEnum.descriptor
|
1220
|
-
assert optional_enum.subtype.class == Google::Protobuf::EnumDescriptor
|
1221
|
-
optional_enum.subtype.each do |k, v|
|
1222
|
-
# set with integer, check resolution to symbolic name
|
1223
|
-
optional_enum.set(m, v)
|
1224
|
-
assert optional_enum.get(m) == k
|
1225
|
-
end
|
1226
|
-
end
|
1227
|
-
|
1228
|
-
def test_json
|
1229
|
-
# TODO: Fix JSON in JRuby version.
|
1230
|
-
return if RUBY_PLATFORM == "java"
|
1231
|
-
m = TestMessage.new(:optional_int32 => 1234,
|
1232
|
-
:optional_int64 => -0x1_0000_0000,
|
1233
|
-
:optional_uint32 => 0x8000_0000,
|
1234
|
-
:optional_uint64 => 0xffff_ffff_ffff_ffff,
|
1235
|
-
:optional_bool => true,
|
1236
|
-
:optional_float => 1.0,
|
1237
|
-
:optional_double => -1e100,
|
1238
|
-
:optional_string => "Test string",
|
1239
|
-
:optional_bytes => ["FFFFFFFF"].pack('H*'),
|
1240
|
-
:optional_msg => TestMessage2.new(:foo => 42),
|
1241
|
-
:repeated_int32 => [1, 2, 3, 4],
|
1242
|
-
:repeated_string => ["a", "b", "c"],
|
1243
|
-
:repeated_bool => [true, false, true, false],
|
1244
|
-
:repeated_msg => [TestMessage2.new(:foo => 1),
|
1245
|
-
TestMessage2.new(:foo => 2)])
|
1246
|
-
|
1247
|
-
json_text = TestMessage.encode_json(m)
|
1248
|
-
m2 = TestMessage.decode_json(json_text)
|
1249
|
-
puts m.inspect
|
1250
|
-
puts m2.inspect
|
1251
|
-
assert m == m2
|
1252
|
-
|
1253
|
-
# Crash case from GitHub issue 283.
|
1254
|
-
bar = Bar.new(msg: "bar")
|
1255
|
-
baz1 = Baz.new(msg: "baz")
|
1256
|
-
baz2 = Baz.new(msg: "quux")
|
1257
|
-
Foo.encode_json(Foo.new)
|
1258
|
-
Foo.encode_json(Foo.new(bar: bar))
|
1259
|
-
Foo.encode_json(Foo.new(bar: bar, baz: [baz1, baz2]))
|
1260
|
-
end
|
1261
|
-
|
1262
|
-
def test_json_emit_defaults
|
1263
|
-
# TODO: Fix JSON in JRuby version.
|
1264
|
-
return if RUBY_PLATFORM == "java"
|
1265
|
-
m = TestMessage.new
|
1266
|
-
|
1267
|
-
expected = {
|
1268
|
-
optionalInt32: 0,
|
1269
|
-
optionalInt64: 0,
|
1270
|
-
optionalUint32: 0,
|
1271
|
-
optionalUint64: 0,
|
1272
|
-
optionalBool: false,
|
1273
|
-
optionalFloat: 0,
|
1274
|
-
optionalDouble: 0,
|
1275
|
-
optionalString: "",
|
1276
|
-
optionalBytes: "",
|
1277
|
-
optionalEnum: "Default",
|
1278
|
-
repeatedInt32: [],
|
1279
|
-
repeatedInt64: [],
|
1280
|
-
repeatedUint32: [],
|
1281
|
-
repeatedUint64: [],
|
1282
|
-
repeatedBool: [],
|
1283
|
-
repeatedFloat: [],
|
1284
|
-
repeatedDouble: [],
|
1285
|
-
repeatedString: [],
|
1286
|
-
repeatedBytes: [],
|
1287
|
-
repeatedMsg: [],
|
1288
|
-
repeatedEnum: []
|
1289
|
-
}
|
1290
|
-
|
1291
|
-
actual = TestMessage.encode_json(m, :emit_defaults => true)
|
1292
|
-
|
1293
|
-
assert JSON.parse(actual, :symbolize_names => true) == expected
|
1294
|
-
end
|
1295
|
-
|
1296
|
-
def test_json_emit_defaults_submsg
|
1297
|
-
# TODO: Fix JSON in JRuby version.
|
1298
|
-
return if RUBY_PLATFORM == "java"
|
1299
|
-
m = TestMessage.new(optional_msg: TestMessage2.new)
|
1300
|
-
|
1301
|
-
expected = {
|
1302
|
-
optionalInt32: 0,
|
1303
|
-
optionalInt64: 0,
|
1304
|
-
optionalUint32: 0,
|
1305
|
-
optionalUint64: 0,
|
1306
|
-
optionalBool: false,
|
1307
|
-
optionalFloat: 0,
|
1308
|
-
optionalDouble: 0,
|
1309
|
-
optionalString: "",
|
1310
|
-
optionalBytes: "",
|
1311
|
-
optionalMsg: {foo: 0},
|
1312
|
-
optionalEnum: "Default",
|
1313
|
-
repeatedInt32: [],
|
1314
|
-
repeatedInt64: [],
|
1315
|
-
repeatedUint32: [],
|
1316
|
-
repeatedUint64: [],
|
1317
|
-
repeatedBool: [],
|
1318
|
-
repeatedFloat: [],
|
1319
|
-
repeatedDouble: [],
|
1320
|
-
repeatedString: [],
|
1321
|
-
repeatedBytes: [],
|
1322
|
-
repeatedMsg: [],
|
1323
|
-
repeatedEnum: []
|
1324
|
-
}
|
1325
|
-
|
1326
|
-
actual = TestMessage.encode_json(m, :emit_defaults => true)
|
1327
|
-
|
1328
|
-
assert JSON.parse(actual, :symbolize_names => true) == expected
|
1329
|
-
end
|
1330
|
-
|
1331
|
-
def test_json_emit_defaults_repeated_submsg
|
1332
|
-
# TODO: Fix JSON in JRuby version.
|
1333
|
-
return if RUBY_PLATFORM == "java"
|
1334
|
-
m = TestMessage.new(repeated_msg: [TestMessage2.new])
|
1335
|
-
|
1336
|
-
expected = {
|
1337
|
-
optionalInt32: 0,
|
1338
|
-
optionalInt64: 0,
|
1339
|
-
optionalUint32: 0,
|
1340
|
-
optionalUint64: 0,
|
1341
|
-
optionalBool: false,
|
1342
|
-
optionalFloat: 0,
|
1343
|
-
optionalDouble: 0,
|
1344
|
-
optionalString: "",
|
1345
|
-
optionalBytes: "",
|
1346
|
-
optionalEnum: "Default",
|
1347
|
-
repeatedInt32: [],
|
1348
|
-
repeatedInt64: [],
|
1349
|
-
repeatedUint32: [],
|
1350
|
-
repeatedUint64: [],
|
1351
|
-
repeatedBool: [],
|
1352
|
-
repeatedFloat: [],
|
1353
|
-
repeatedDouble: [],
|
1354
|
-
repeatedString: [],
|
1355
|
-
repeatedBytes: [],
|
1356
|
-
repeatedMsg: [{foo: 0}],
|
1357
|
-
repeatedEnum: []
|
1358
|
-
}
|
1359
|
-
|
1360
|
-
actual = TestMessage.encode_json(m, :emit_defaults => true)
|
1361
|
-
|
1362
|
-
assert JSON.parse(actual, :symbolize_names => true) == expected
|
1363
|
-
end
|
1364
|
-
|
1365
|
-
def test_json_maps
|
1366
|
-
# TODO: Fix JSON in JRuby version.
|
1367
|
-
return if RUBY_PLATFORM == "java"
|
1368
|
-
m = MapMessage.new(:map_string_int32 => {"a" => 1})
|
1369
|
-
expected = {mapStringInt32: {a: 1}, mapStringMsg: {}}
|
1370
|
-
expected_preserve = {map_string_int32: {a: 1}, map_string_msg: {}}
|
1371
|
-
assert JSON.parse(MapMessage.encode_json(m), :symbolize_names => true) == expected
|
1372
|
-
|
1373
|
-
json = MapMessage.encode_json(m, :preserve_proto_fieldnames => true)
|
1374
|
-
assert JSON.parse(json, :symbolize_names => true) == expected_preserve
|
1375
|
-
|
1376
|
-
m2 = MapMessage.decode_json(MapMessage.encode_json(m))
|
1377
|
-
assert m == m2
|
1378
|
-
end
|
1379
|
-
|
1380
|
-
def test_json_maps_emit_defaults_submsg
|
1381
|
-
# TODO: Fix JSON in JRuby version.
|
1382
|
-
return if RUBY_PLATFORM == "java"
|
1383
|
-
m = MapMessage.new(:map_string_msg => {"a" => TestMessage2.new})
|
1384
|
-
expected = {mapStringInt32: {}, mapStringMsg: {a: {foo: 0}}}
|
1385
|
-
|
1386
|
-
actual = MapMessage.encode_json(m, :emit_defaults => true)
|
1387
|
-
|
1388
|
-
assert JSON.parse(actual, :symbolize_names => true) == expected
|
1389
|
-
end
|
1390
|
-
|
1391
|
-
def test_comparison_with_arbitrary_object
|
1392
|
-
assert MapMessage.new != nil
|
1393
|
-
end
|
1394
|
-
|
1395
|
-
def test_respond_to
|
1396
|
-
# This test fails with JRuby 1.7.23, likely because of an old JRuby bug.
|
1397
|
-
return if RUBY_PLATFORM == "java"
|
1398
|
-
msg = MapMessage.new
|
1399
|
-
assert msg.respond_to?(:map_string_int32)
|
1400
|
-
assert !msg.respond_to?(:bacon)
|
1401
|
-
end
|
1402
|
-
end
|
1403
|
-
end
|