google-protobuf 3.6.0 → 3.7.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/defs.c +554 -71
- data/ext/google/protobuf_c/encode_decode.c +245 -66
- data/ext/google/protobuf_c/extconf.rb +5 -1
- data/ext/google/protobuf_c/message.c +135 -69
- data/ext/google/protobuf_c/protobuf.c +4 -0
- data/ext/google/protobuf_c/protobuf.h +62 -2
- data/ext/google/protobuf_c/storage.c +213 -106
- data/ext/google/protobuf_c/upb.c +4126 -1721
- data/ext/google/protobuf_c/upb.h +1125 -339
- data/ext/google/protobuf_c/wrap_memcpy.c +1 -1
- data/lib/google/protobuf/any_pb.rb +5 -3
- data/lib/google/protobuf/api_pb.rb +23 -21
- data/lib/google/protobuf/duration_pb.rb +5 -3
- data/lib/google/protobuf/empty_pb.rb +3 -1
- data/lib/google/protobuf/field_mask_pb.rb +4 -2
- data/lib/google/protobuf/repeated_field.rb +1 -1
- data/lib/google/protobuf/source_context_pb.rb +4 -2
- data/lib/google/protobuf/struct_pb.rb +19 -17
- data/lib/google/protobuf/timestamp_pb.rb +5 -3
- data/lib/google/protobuf/type_pb.rb +68 -66
- data/lib/google/protobuf/well_known_types.rb +12 -0
- data/lib/google/protobuf/wrappers_pb.rb +28 -26
- data/lib/google/protobuf.rb +3 -2
- data/tests/basic.rb +137 -1179
- data/tests/generated_code_test.rb +5 -3
- metadata +3 -3
data/tests/basic.rb
CHANGED
@@ -1,5 +1,10 @@
|
|
1
1
|
#!/usr/bin/ruby
|
2
2
|
|
3
|
+
# basic_test_pb.rb is in the same directory as this test.
|
4
|
+
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)))
|
5
|
+
|
6
|
+
require 'basic_test_pb'
|
7
|
+
require 'common_tests'
|
3
8
|
require 'google/protobuf'
|
4
9
|
require 'json'
|
5
10
|
require 'test/unit'
|
@@ -9,680 +14,152 @@ require 'test/unit'
|
|
9
14
|
module BasicTest
|
10
15
|
pool = Google::Protobuf::DescriptorPool.new
|
11
16
|
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
17
|
add_message "BadFieldNames" do
|
80
18
|
optional :dup, :int32, 1
|
81
19
|
optional :class, :int32, 2
|
82
20
|
optional :"a.b", :int32, 3
|
83
21
|
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
22
|
end
|
118
23
|
|
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
24
|
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
25
|
|
141
26
|
# ------------ test cases ---------------
|
142
27
|
|
143
28
|
class MessageContainerTest < Test::Unit::TestCase
|
144
|
-
|
145
|
-
def
|
146
|
-
|
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
|
29
|
+
# Required by CommonTests module to resolve proto3 proto classes used in tests.
|
30
|
+
def proto_module
|
31
|
+
::BasicTest
|
158
32
|
end
|
33
|
+
include CommonTests
|
159
34
|
|
160
|
-
def
|
35
|
+
def test_has_field
|
161
36
|
m = TestMessage.new
|
162
|
-
m.
|
163
|
-
|
164
|
-
m.
|
165
|
-
assert m
|
166
|
-
|
167
|
-
|
168
|
-
m.
|
169
|
-
|
170
|
-
m.
|
171
|
-
|
172
|
-
|
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"
|
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)
|
41
|
+
|
42
|
+
m = OneofMessage.new
|
43
|
+
assert !m.has_my_oneof?
|
44
|
+
m.a = "foo"
|
45
|
+
assert m.has_my_oneof?
|
46
|
+
assert_raise NoMethodError do
|
47
|
+
m.has_a?
|
308
48
|
end
|
309
|
-
|
310
|
-
|
311
|
-
m.repeated_msg.push TestMessage.new
|
49
|
+
assert_raise ArgumentError do
|
50
|
+
OneofMessage.descriptor.lookup('a').has?(m)
|
312
51
|
end
|
313
|
-
end
|
314
52
|
|
315
|
-
def test_string_encoding
|
316
53
|
m = TestMessage.new
|
317
|
-
|
318
|
-
|
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')
|
54
|
+
assert_raise NoMethodError do
|
55
|
+
m.has_optional_int32?
|
327
56
|
end
|
328
|
-
|
329
|
-
|
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]
|
57
|
+
assert_raise ArgumentError do
|
58
|
+
TestMessage.descriptor.lookup('optional_int32').has?(m)
|
393
59
|
end
|
394
60
|
|
395
|
-
|
396
|
-
|
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
|
61
|
+
assert_raise NoMethodError do
|
62
|
+
m.has_optional_string?
|
428
63
|
end
|
429
|
-
assert_raise
|
430
|
-
|
64
|
+
assert_raise ArgumentError do
|
65
|
+
TestMessage.descriptor.lookup('optional_string').has?(m)
|
431
66
|
end
|
432
67
|
|
433
|
-
|
434
|
-
|
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
|
68
|
+
assert_raise NoMethodError do
|
69
|
+
m.has_optional_bool?
|
473
70
|
end
|
474
71
|
assert_raise ArgumentError do
|
475
|
-
|
72
|
+
TestMessage.descriptor.lookup('optional_bool').has?(m)
|
476
73
|
end
|
477
|
-
|
478
|
-
|
74
|
+
|
75
|
+
assert_raise NoMethodError do
|
76
|
+
m.has_repeated_msg?
|
479
77
|
end
|
480
78
|
assert_raise ArgumentError do
|
481
|
-
|
79
|
+
TestMessage.descriptor.lookup('repeated_msg').has?(m)
|
482
80
|
end
|
483
81
|
end
|
484
82
|
|
485
|
-
def
|
486
|
-
|
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
|
83
|
+
def test_set_clear_defaults
|
84
|
+
m = TestMessage.new
|
514
85
|
|
515
|
-
|
516
|
-
|
86
|
+
m.optional_int32 = -42
|
87
|
+
assert_equal -42, m.optional_int32
|
88
|
+
m.clear_optional_int32
|
89
|
+
assert_equal 0, m.optional_int32
|
517
90
|
|
518
|
-
|
519
|
-
|
520
|
-
|
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
|
521
95
|
|
522
|
-
m =
|
523
|
-
|
524
|
-
|
525
|
-
|
526
|
-
assert m == { "jkl;" => 42, "asdf" => 1 }
|
527
|
-
assert m.has_key?("asdf")
|
528
|
-
assert !m.has_key?("qwerty")
|
529
|
-
assert m.length == 2
|
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
|
530
100
|
|
531
|
-
|
532
|
-
assert_equal
|
533
|
-
|
534
|
-
assert_equal
|
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
|
535
105
|
|
536
|
-
|
537
|
-
|
538
|
-
assert
|
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?
|
539
112
|
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
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
|
544
117
|
|
545
|
-
|
546
|
-
|
547
|
-
|
548
|
-
|
118
|
+
m.repeated_int32.push(1)
|
119
|
+
assert_equal [1], m.repeated_int32
|
120
|
+
m.clear_repeated_int32
|
121
|
+
assert_equal [], m.repeated_int32
|
549
122
|
|
550
|
-
|
551
|
-
|
123
|
+
m.repeated_int32.push(1)
|
124
|
+
assert_equal [1], m.repeated_int32
|
125
|
+
TestMessage.descriptor.lookup('repeated_int32').clear(m)
|
126
|
+
assert_equal [], m.repeated_int32
|
552
127
|
|
553
|
-
m.
|
554
|
-
|
555
|
-
|
128
|
+
m = OneofMessage.new
|
129
|
+
m.a = "foo"
|
130
|
+
assert_equal "foo", m.a
|
131
|
+
assert m.has_my_oneof?
|
132
|
+
m.clear_a
|
133
|
+
assert !m.has_my_oneof?
|
556
134
|
|
557
|
-
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
m["asdf"] = 0x1_0000_0000
|
562
|
-
end
|
563
|
-
end
|
135
|
+
m.a = "foobar"
|
136
|
+
assert m.has_my_oneof?
|
137
|
+
m.clear_my_oneof
|
138
|
+
assert !m.has_my_oneof?
|
564
139
|
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
|
140
|
+
m.a = "bar"
|
141
|
+
assert_equal "bar", m.a
|
142
|
+
assert m.has_my_oneof?
|
143
|
+
OneofMessage.descriptor.lookup('a').clear(m)
|
144
|
+
assert !m.has_my_oneof?
|
569
145
|
end
|
570
146
|
|
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
147
|
|
619
|
-
|
620
|
-
|
621
|
-
|
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
|
148
|
+
def test_initialization_map_errors
|
149
|
+
e = assert_raise ArgumentError do
|
150
|
+
TestMessage.new(:hello => "world")
|
636
151
|
end
|
637
|
-
|
152
|
+
assert_match(/hello/, e.message)
|
638
153
|
|
639
|
-
|
640
|
-
|
641
|
-
m["asdf"] = TestMessage.new
|
642
|
-
assert_raise TypeError do
|
643
|
-
m["jkl;"] = TestMessage2.new
|
154
|
+
e = assert_raise ArgumentError do
|
155
|
+
MapMessage.new(:map_string_int32 => "hello")
|
644
156
|
end
|
157
|
+
assert_equal e.message, "Expected Hash object as initializer value for map field 'map_string_int32'."
|
645
158
|
|
646
|
-
|
647
|
-
:
|
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"
|
159
|
+
e = assert_raise ArgumentError do
|
160
|
+
TestMessage.new(:repeated_uint32 => "hello")
|
666
161
|
end
|
667
|
-
|
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
|
162
|
+
assert_equal e.message, "Expected array as initializer value for repeated field 'repeated_uint32'."
|
686
163
|
end
|
687
164
|
|
688
165
|
def test_map_field
|
@@ -706,17 +183,17 @@ module BasicTest
|
|
706
183
|
m.map_string_msg.delete("c")
|
707
184
|
assert m.map_string_msg == { "a" => TestMessage2.new(:foo => 1) }
|
708
185
|
|
709
|
-
assert_raise TypeError do
|
186
|
+
assert_raise Google::Protobuf::TypeError do
|
710
187
|
m.map_string_msg["e"] = TestMessage.new # wrong value type
|
711
188
|
end
|
712
189
|
# ensure nothing was added by the above
|
713
190
|
assert m.map_string_msg == { "a" => TestMessage2.new(:foo => 1) }
|
714
191
|
|
715
192
|
m.map_string_int32 = Google::Protobuf::Map.new(:string, :int32)
|
716
|
-
assert_raise TypeError do
|
193
|
+
assert_raise Google::Protobuf::TypeError do
|
717
194
|
m.map_string_int32 = Google::Protobuf::Map.new(:string, :int64)
|
718
195
|
end
|
719
|
-
assert_raise TypeError do
|
196
|
+
assert_raise Google::Protobuf::TypeError do
|
720
197
|
m.map_string_int32 = {}
|
721
198
|
end
|
722
199
|
|
@@ -725,6 +202,15 @@ module BasicTest
|
|
725
202
|
end
|
726
203
|
end
|
727
204
|
|
205
|
+
def test_map_inspect
|
206
|
+
m = MapMessage.new(
|
207
|
+
:map_string_int32 => {"a" => 1, "b" => 2},
|
208
|
+
: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
|
212
|
+
end
|
213
|
+
|
728
214
|
def test_map_corruption
|
729
215
|
# This pattern led to a crash in a previous version of upb/protobuf.
|
730
216
|
m = MapMessage.new(map_string_int32: { "aaa" => 1 })
|
@@ -768,209 +254,17 @@ module BasicTest
|
|
768
254
|
"b" => TestMessage2.new(:foo => 2)}
|
769
255
|
end
|
770
256
|
|
771
|
-
def
|
772
|
-
|
773
|
-
|
774
|
-
|
775
|
-
|
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
|
257
|
+
def test_protobuf_decode_json_ignore_unknown_fields
|
258
|
+
m = TestMessage.decode_json({
|
259
|
+
optional_string: "foo",
|
260
|
+
not_in_message: "some_value"
|
261
|
+
}.to_json, { ignore_unknown_fields: true })
|
823
262
|
|
824
|
-
|
825
|
-
|
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
|
263
|
+
assert_equal m.optional_string, "foo"
|
264
|
+
e = assert_raise Google::Protobuf::ParseError do
|
265
|
+
TestMessage.decode_json({ not_in_message: "some_value" }.to_json)
|
863
266
|
end
|
864
|
-
|
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
|
267
|
+
assert_match(/No such field: not_in_message/, e.message)
|
974
268
|
end
|
975
269
|
|
976
270
|
def test_to_h
|
@@ -1013,355 +307,6 @@ module BasicTest
|
|
1013
307
|
end
|
1014
308
|
|
1015
309
|
|
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
310
|
def test_json_maps
|
1366
311
|
# TODO: Fix JSON in JRuby version.
|
1367
312
|
return if RUBY_PLATFORM == "java"
|
@@ -1388,10 +333,6 @@ module BasicTest
|
|
1388
333
|
assert JSON.parse(actual, :symbolize_names => true) == expected
|
1389
334
|
end
|
1390
335
|
|
1391
|
-
def test_comparison_with_arbitrary_object
|
1392
|
-
assert MapMessage.new != nil
|
1393
|
-
end
|
1394
|
-
|
1395
336
|
def test_respond_to
|
1396
337
|
# This test fails with JRuby 1.7.23, likely because of an old JRuby bug.
|
1397
338
|
return if RUBY_PLATFORM == "java"
|
@@ -1399,5 +340,22 @@ module BasicTest
|
|
1399
340
|
assert msg.respond_to?(:map_string_int32)
|
1400
341
|
assert !msg.respond_to?(:bacon)
|
1401
342
|
end
|
343
|
+
|
344
|
+
def test_file_descriptor
|
345
|
+
file_descriptor = TestMessage.descriptor.file_descriptor
|
346
|
+
assert nil != file_descriptor
|
347
|
+
assert_equal "tests/basic_test.proto", file_descriptor.name
|
348
|
+
assert_equal :proto3, file_descriptor.syntax
|
349
|
+
|
350
|
+
file_descriptor = TestEnum.descriptor.file_descriptor
|
351
|
+
assert nil != file_descriptor
|
352
|
+
assert_equal "tests/basic_test.proto", file_descriptor.name
|
353
|
+
assert_equal :proto3, file_descriptor.syntax
|
354
|
+
|
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
|
359
|
+
end
|
1402
360
|
end
|
1403
361
|
end
|