google-protobuf 3.5.0.pre-java → 3.19.1-java
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/lib/google/protobuf/any_pb.rb +6 -4
- data/lib/google/protobuf/api_pb.rb +27 -24
- data/lib/google/protobuf/descriptor_dsl.rb +458 -0
- data/lib/google/protobuf/descriptor_pb.rb +268 -0
- data/lib/google/protobuf/duration_pb.rb +6 -4
- data/lib/google/protobuf/empty_pb.rb +4 -2
- data/lib/google/protobuf/field_mask_pb.rb +5 -3
- data/lib/google/protobuf/message_exts.rb +2 -2
- data/lib/google/protobuf/repeated_field.rb +3 -3
- data/lib/google/protobuf/source_context_pb.rb +5 -3
- data/lib/google/protobuf/struct_pb.rb +23 -21
- data/lib/google/protobuf/timestamp_pb.rb +6 -4
- data/lib/google/protobuf/type_pb.rb +77 -74
- data/lib/google/protobuf/well_known_types.rb +25 -2
- data/lib/google/protobuf/wrappers_pb.rb +37 -35
- data/lib/google/protobuf.rb +7 -4
- data/lib/google/protobuf_java.jar +0 -0
- data/tests/basic.rb +407 -1170
- data/tests/generated_code_test.rb +6 -2
- data/tests/stress.rb +1 -1
- metadata +14 -26
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,220 @@ 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
|
-
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
20
|
end
|
117
21
|
end
|
118
22
|
|
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
23
|
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
24
|
|
141
25
|
# ------------ test cases ---------------
|
142
26
|
|
143
27
|
class MessageContainerTest < Test::Unit::TestCase
|
144
|
-
|
145
|
-
def
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
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
|
28
|
+
# Required by CommonTests module to resolve proto3 proto classes used in tests.
|
29
|
+
def proto_module
|
30
|
+
::BasicTest
|
31
|
+
end
|
32
|
+
include CommonTests
|
33
|
+
|
34
|
+
def test_issue_8311_crash
|
35
|
+
Google::Protobuf::DescriptorPool.generated_pool.build do
|
36
|
+
add_file("inner.proto", :syntax => :proto3) do
|
37
|
+
add_message "Inner" do
|
38
|
+
# Removing either of these fixes the segfault.
|
39
|
+
optional :foo, :string, 1
|
40
|
+
optional :bar, :string, 2
|
41
|
+
end
|
42
|
+
end
|
258
43
|
end
|
259
|
-
assert_match(/hello/, e.message)
|
260
44
|
|
261
|
-
|
262
|
-
|
45
|
+
Google::Protobuf::DescriptorPool.generated_pool.build do
|
46
|
+
add_file("outer.proto", :syntax => :proto3) do
|
47
|
+
add_message "Outer" do
|
48
|
+
repeated :inners, :message, 1, "Inner"
|
49
|
+
end
|
50
|
+
end
|
263
51
|
end
|
264
|
-
assert_match(/hello/, e.message)
|
265
|
-
end
|
266
52
|
|
267
|
-
|
268
|
-
e = assert_raise ArgumentError do
|
269
|
-
TestMessage.new(:hello => "world")
|
270
|
-
end
|
271
|
-
assert_match(/hello/, e.message)
|
53
|
+
outer = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("Outer").msgclass
|
272
54
|
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
assert_equal e.message, "Expected Hash object as initializer value for map field 'map_string_int32'."
|
55
|
+
outer.new(
|
56
|
+
inners: []
|
57
|
+
)['inners'].to_s
|
277
58
|
|
278
|
-
|
279
|
-
|
59
|
+
assert_raise Google::Protobuf::TypeError do
|
60
|
+
outer.new(
|
61
|
+
inners: [nil]
|
62
|
+
).to_s
|
280
63
|
end
|
281
|
-
assert_equal e.message, "Expected array as initializer value for repeated field 'repeated_uint32'."
|
282
64
|
end
|
283
65
|
|
284
|
-
def
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
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
|
66
|
+
def test_issue_8559_crash
|
67
|
+
msg = TestMessage.new
|
68
|
+
msg.repeated_int32 = ::Google::Protobuf::RepeatedField.new(:int32, [1, 2, 3])
|
69
|
+
# TODO: Remove the platform check once https://github.com/jruby/jruby/issues/6818 is released in JRuby 9.3.0.0
|
70
|
+
GC.start(full_mark: true, immediate_sweep: true) unless RUBY_PLATFORM == "java"
|
71
|
+
TestMessage.encode(msg)
|
72
|
+
end
|
301
73
|
|
302
|
-
|
303
|
-
|
304
|
-
|
74
|
+
def test_has_field
|
75
|
+
m = TestSingularFields.new
|
76
|
+
assert !m.has_singular_msg?
|
77
|
+
m.singular_msg = TestMessage2.new
|
78
|
+
assert m.has_singular_msg?
|
79
|
+
assert TestSingularFields.descriptor.lookup('singular_msg').has?(m)
|
305
80
|
|
306
|
-
|
307
|
-
|
81
|
+
m = OneofMessage.new
|
82
|
+
assert !m.has_my_oneof?
|
83
|
+
m.a = "foo"
|
84
|
+
assert m.has_my_oneof?
|
85
|
+
assert_raise NoMethodError do
|
86
|
+
m.has_a?
|
308
87
|
end
|
88
|
+
assert_true OneofMessage.descriptor.lookup('a').has?(m)
|
309
89
|
|
310
|
-
|
311
|
-
|
90
|
+
m = TestSingularFields.new
|
91
|
+
assert_raise NoMethodError do
|
92
|
+
m.has_singular_int32?
|
312
93
|
end
|
313
|
-
|
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')
|
94
|
+
assert_raise ArgumentError do
|
95
|
+
TestSingularFields.descriptor.lookup('singular_int32').has?(m)
|
327
96
|
end
|
328
97
|
|
329
|
-
assert_raise
|
330
|
-
m.
|
98
|
+
assert_raise NoMethodError do
|
99
|
+
m.has_singular_string?
|
331
100
|
end
|
332
|
-
|
333
|
-
|
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')
|
101
|
+
assert_raise ArgumentError do
|
102
|
+
TestSingularFields.descriptor.lookup('singular_string').has?(m)
|
341
103
|
end
|
342
|
-
end
|
343
104
|
|
344
|
-
|
345
|
-
|
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
|
105
|
+
assert_raise NoMethodError do
|
106
|
+
m.has_singular_bool?
|
379
107
|
end
|
380
|
-
|
381
|
-
|
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]
|
108
|
+
assert_raise ArgumentError do
|
109
|
+
TestSingularFields.descriptor.lookup('singular_bool').has?(m)
|
393
110
|
end
|
394
111
|
|
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
112
|
m = TestMessage.new
|
414
|
-
|
415
|
-
|
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
|
113
|
+
assert_raise NoMethodError do
|
114
|
+
m.has_repeated_msg?
|
428
115
|
end
|
429
|
-
assert_raise
|
430
|
-
|
116
|
+
assert_raise ArgumentError do
|
117
|
+
TestMessage.descriptor.lookup('repeated_msg').has?(m)
|
431
118
|
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
119
|
end
|
454
120
|
|
455
|
-
def
|
456
|
-
|
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
|
121
|
+
def test_no_presence
|
122
|
+
m = TestSingularFields.new
|
465
123
|
|
466
|
-
|
467
|
-
|
468
|
-
|
124
|
+
# Explicitly setting to zero does not cause anything to be serialized.
|
125
|
+
m.singular_int32 = 0
|
126
|
+
assert_equal "", TestSingularFields.encode(m)
|
469
127
|
|
470
|
-
|
471
|
-
|
472
|
-
|
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
|
128
|
+
# Explicitly setting to a non-zero value *does* cause serialization.
|
129
|
+
m.singular_int32 = 1
|
130
|
+
assert_not_equal "", TestSingularFields.encode(m)
|
484
131
|
|
485
|
-
|
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
|
514
|
-
|
515
|
-
#adding out of scope will backfill with empty objects
|
132
|
+
m.singular_int32 = 0
|
133
|
+
assert_equal "", TestSingularFields.encode(m)
|
516
134
|
end
|
517
135
|
|
518
|
-
def
|
519
|
-
|
520
|
-
# :int32, :int64, :uint32, :uint64, :bool, :string, :bytes.
|
136
|
+
def test_set_clear_defaults
|
137
|
+
m = TestSingularFields.new
|
521
138
|
|
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
|
139
|
+
m.singular_int32 = -42
|
140
|
+
assert_equal -42, m.singular_int32
|
141
|
+
m.clear_singular_int32
|
142
|
+
assert_equal 0, m.singular_int32
|
530
143
|
|
531
|
-
|
532
|
-
assert_equal
|
533
|
-
|
534
|
-
assert_equal
|
144
|
+
m.singular_int32 = 50
|
145
|
+
assert_equal 50, m.singular_int32
|
146
|
+
TestSingularFields.descriptor.lookup('singular_int32').clear(m)
|
147
|
+
assert_equal 0, m.singular_int32
|
535
148
|
|
536
|
-
|
537
|
-
|
538
|
-
|
149
|
+
m.singular_string = "foo bar"
|
150
|
+
assert_equal "foo bar", m.singular_string
|
151
|
+
m.clear_singular_string
|
152
|
+
assert_equal "", m.singular_string
|
539
153
|
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
154
|
+
m.singular_string = "foo"
|
155
|
+
assert_equal "foo", m.singular_string
|
156
|
+
TestSingularFields.descriptor.lookup('singular_string').clear(m)
|
157
|
+
assert_equal "", m.singular_string
|
544
158
|
|
545
|
-
|
546
|
-
|
547
|
-
|
548
|
-
|
159
|
+
m.singular_msg = TestMessage2.new(:foo => 42)
|
160
|
+
assert_equal TestMessage2.new(:foo => 42), m.singular_msg
|
161
|
+
assert m.has_singular_msg?
|
162
|
+
m.clear_singular_msg
|
163
|
+
assert_equal nil, m.singular_msg
|
164
|
+
assert !m.has_singular_msg?
|
549
165
|
|
550
|
-
|
551
|
-
|
166
|
+
m.singular_msg = TestMessage2.new(:foo => 42)
|
167
|
+
assert_equal TestMessage2.new(:foo => 42), m.singular_msg
|
168
|
+
TestSingularFields.descriptor.lookup('singular_msg').clear(m)
|
169
|
+
assert_equal nil, m.singular_msg
|
170
|
+
end
|
552
171
|
|
553
|
-
|
554
|
-
|
555
|
-
assert m
|
172
|
+
def test_import_proto2
|
173
|
+
m = TestMessage.new
|
174
|
+
assert !m.has_optional_proto2_submessage?
|
175
|
+
m.optional_proto2_submessage = ::FooBar::Proto2::TestImportedMessage.new
|
176
|
+
assert m.has_optional_proto2_submessage?
|
177
|
+
assert TestMessage.descriptor.lookup('optional_proto2_submessage').has?(m)
|
556
178
|
|
557
|
-
|
558
|
-
|
559
|
-
end
|
560
|
-
assert_raise RangeError do
|
561
|
-
m["asdf"] = 0x1_0000_0000
|
562
|
-
end
|
179
|
+
m.clear_optional_proto2_submessage
|
180
|
+
assert !m.has_optional_proto2_submessage?
|
563
181
|
end
|
564
182
|
|
565
|
-
def
|
566
|
-
m =
|
567
|
-
{"a" => 1, "b" => 2, "c" => 3})
|
568
|
-
assert m == {"a" => 1, "c" => 3, "b" => 2}
|
569
|
-
end
|
183
|
+
def test_clear_repeated_fields
|
184
|
+
m = TestMessage.new
|
570
185
|
|
571
|
-
|
572
|
-
|
573
|
-
m
|
574
|
-
|
575
|
-
assert_raise RangeError do
|
576
|
-
m[0x8000_0000] = 1
|
577
|
-
end
|
578
|
-
assert_raise TypeError do
|
579
|
-
m["asdf"] = 1
|
580
|
-
end
|
186
|
+
m.repeated_int32.push(1)
|
187
|
+
assert_equal [1], m.repeated_int32
|
188
|
+
m.clear_repeated_int32
|
189
|
+
assert_equal [], m.repeated_int32
|
581
190
|
|
582
|
-
m
|
583
|
-
|
584
|
-
|
585
|
-
|
586
|
-
end
|
587
|
-
assert_raise TypeError do
|
588
|
-
m["asdf"] = 1
|
589
|
-
end
|
191
|
+
m.repeated_int32.push(1)
|
192
|
+
assert_equal [1], m.repeated_int32
|
193
|
+
TestMessage.descriptor.lookup('repeated_int32').clear(m)
|
194
|
+
assert_equal [], m.repeated_int32
|
590
195
|
|
591
|
-
m =
|
592
|
-
m
|
593
|
-
|
594
|
-
|
595
|
-
|
596
|
-
|
597
|
-
|
598
|
-
end
|
196
|
+
m = OneofMessage.new
|
197
|
+
m.a = "foo"
|
198
|
+
assert_equal "foo", m.a
|
199
|
+
assert m.has_my_oneof?
|
200
|
+
assert_equal :a, m.my_oneof
|
201
|
+
m.clear_a
|
202
|
+
assert !m.has_my_oneof?
|
599
203
|
|
600
|
-
m =
|
601
|
-
m
|
602
|
-
|
603
|
-
|
604
|
-
end
|
605
|
-
assert_raise RangeError do
|
606
|
-
m[-1] = 1
|
607
|
-
end
|
204
|
+
m.a = "foobar"
|
205
|
+
assert m.has_my_oneof?
|
206
|
+
m.clear_my_oneof
|
207
|
+
assert !m.has_my_oneof?
|
608
208
|
|
609
|
-
m =
|
610
|
-
|
611
|
-
m
|
612
|
-
|
613
|
-
|
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
|
209
|
+
m.a = "bar"
|
210
|
+
assert_equal "bar", m.a
|
211
|
+
assert m.has_my_oneof?
|
212
|
+
OneofMessage.descriptor.lookup('a').clear(m)
|
213
|
+
assert !m.has_my_oneof?
|
637
214
|
end
|
638
215
|
|
639
|
-
def
|
640
|
-
|
641
|
-
|
642
|
-
assert_raise TypeError do
|
643
|
-
m["jkl;"] = TestMessage2.new
|
216
|
+
def test_initialization_map_errors
|
217
|
+
e = assert_raise ArgumentError do
|
218
|
+
TestMessage.new(:hello => "world")
|
644
219
|
end
|
220
|
+
assert_match(/hello/, e.message)
|
645
221
|
|
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"
|
222
|
+
e = assert_raise ArgumentError do
|
223
|
+
MapMessage.new(:map_string_int32 => "hello")
|
666
224
|
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) })
|
225
|
+
assert_equal e.message, "Expected Hash object as initializer value for map field 'map_string_int32' (given String)."
|
674
226
|
|
675
|
-
|
676
|
-
|
677
|
-
|
678
|
-
|
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
|
227
|
+
e = assert_raise ArgumentError do
|
228
|
+
TestMessage.new(:repeated_uint32 => "hello")
|
229
|
+
end
|
230
|
+
assert_equal e.message, "Expected array as initializer value for repeated field 'repeated_uint32' (given String)."
|
686
231
|
end
|
687
232
|
|
688
233
|
def test_map_field
|
@@ -693,10 +238,12 @@ module BasicTest
|
|
693
238
|
m = MapMessage.new(
|
694
239
|
:map_string_int32 => {"a" => 1, "b" => 2},
|
695
240
|
:map_string_msg => {"a" => TestMessage2.new(:foo => 1),
|
696
|
-
"b" => TestMessage2.new(:foo => 2)}
|
241
|
+
"b" => TestMessage2.new(:foo => 2)},
|
242
|
+
:map_string_enum => {"a" => :A, "b" => :B})
|
697
243
|
assert m.map_string_int32.keys.sort == ["a", "b"]
|
698
244
|
assert m.map_string_int32["a"] == 1
|
699
245
|
assert m.map_string_msg["b"].foo == 2
|
246
|
+
assert m.map_string_enum["a"] == :A
|
700
247
|
|
701
248
|
m.map_string_int32["c"] = 3
|
702
249
|
assert m.map_string_int32["c"] == 3
|
@@ -706,25 +253,53 @@ module BasicTest
|
|
706
253
|
m.map_string_msg.delete("c")
|
707
254
|
assert m.map_string_msg == { "a" => TestMessage2.new(:foo => 1) }
|
708
255
|
|
709
|
-
assert_raise TypeError do
|
256
|
+
assert_raise Google::Protobuf::TypeError do
|
710
257
|
m.map_string_msg["e"] = TestMessage.new # wrong value type
|
711
258
|
end
|
712
259
|
# ensure nothing was added by the above
|
713
260
|
assert m.map_string_msg == { "a" => TestMessage2.new(:foo => 1) }
|
714
261
|
|
715
262
|
m.map_string_int32 = Google::Protobuf::Map.new(:string, :int32)
|
716
|
-
assert_raise TypeError do
|
263
|
+
assert_raise Google::Protobuf::TypeError do
|
717
264
|
m.map_string_int32 = Google::Protobuf::Map.new(:string, :int64)
|
718
265
|
end
|
719
|
-
assert_raise TypeError do
|
266
|
+
assert_raise Google::Protobuf::TypeError do
|
720
267
|
m.map_string_int32 = {}
|
721
268
|
end
|
722
269
|
|
723
|
-
assert_raise TypeError do
|
270
|
+
assert_raise Google::Protobuf::TypeError do
|
724
271
|
m = MapMessage.new(:map_string_int32 => { 1 => "I am not a number" })
|
725
272
|
end
|
726
273
|
end
|
727
274
|
|
275
|
+
def test_map_field_with_symbol
|
276
|
+
m = MapMessage.new
|
277
|
+
assert m.map_string_int32 == {}
|
278
|
+
assert m.map_string_msg == {}
|
279
|
+
|
280
|
+
m = MapMessage.new(
|
281
|
+
:map_string_int32 => {a: 1, "b" => 2},
|
282
|
+
:map_string_msg => {a: TestMessage2.new(:foo => 1),
|
283
|
+
b: TestMessage2.new(:foo => 10)})
|
284
|
+
assert_equal 1, m.map_string_int32[:a]
|
285
|
+
assert_equal 2, m.map_string_int32[:b]
|
286
|
+
assert_equal 10, m.map_string_msg[:b].foo
|
287
|
+
end
|
288
|
+
|
289
|
+
def test_map_inspect
|
290
|
+
m = MapMessage.new(
|
291
|
+
:map_string_int32 => {"a" => 1, "b" => 2},
|
292
|
+
:map_string_msg => {"a" => TestMessage2.new(:foo => 1),
|
293
|
+
"b" => TestMessage2.new(:foo => 2)},
|
294
|
+
:map_string_enum => {"a" => :A, "b" => :B})
|
295
|
+
|
296
|
+
# JRuby doesn't keep consistent ordering so check for either version
|
297
|
+
expected_a = "<BasicTest::MapMessage: map_string_int32: {\"b\"=>2, \"a\"=>1}, map_string_msg: {\"b\"=><BasicTest::TestMessage2: foo: 2>, \"a\"=><BasicTest::TestMessage2: foo: 1>}, map_string_enum: {\"b\"=>:B, \"a\"=>:A}>"
|
298
|
+
expected_b = "<BasicTest::MapMessage: map_string_int32: {\"a\"=>1, \"b\"=>2}, map_string_msg: {\"a\"=><BasicTest::TestMessage2: foo: 1>, \"b\"=><BasicTest::TestMessage2: foo: 2>}, map_string_enum: {\"a\"=>:A, \"b\"=>:B}>"
|
299
|
+
inspect_result = m.inspect
|
300
|
+
assert expected_a == inspect_result || expected_b == inspect_result, "Incorrect inspect result: #{inspect_result}"
|
301
|
+
end
|
302
|
+
|
728
303
|
def test_map_corruption
|
729
304
|
# This pattern led to a crash in a previous version of upb/protobuf.
|
730
305
|
m = MapMessage.new(map_string_int32: { "aaa" => 1 })
|
@@ -732,6 +307,128 @@ module BasicTest
|
|
732
307
|
m.map_string_int32['aaa'] = 3
|
733
308
|
end
|
734
309
|
|
310
|
+
def test_map_wrappers
|
311
|
+
run_asserts = ->(m) {
|
312
|
+
assert_equal 2.0, m.map_double[0].value
|
313
|
+
assert_equal 4.0, m.map_float[0].value
|
314
|
+
assert_equal 3, m.map_int32[0].value
|
315
|
+
assert_equal 4, m.map_int64[0].value
|
316
|
+
assert_equal 5, m.map_uint32[0].value
|
317
|
+
assert_equal 6, m.map_uint64[0].value
|
318
|
+
assert_equal true, m.map_bool[0].value
|
319
|
+
assert_equal 'str', m.map_string[0].value
|
320
|
+
assert_equal 'fun', m.map_bytes[0].value
|
321
|
+
}
|
322
|
+
|
323
|
+
m = proto_module::Wrapper.new(
|
324
|
+
map_double: {0 => Google::Protobuf::DoubleValue.new(value: 2.0)},
|
325
|
+
map_float: {0 => Google::Protobuf::FloatValue.new(value: 4.0)},
|
326
|
+
map_int32: {0 => Google::Protobuf::Int32Value.new(value: 3)},
|
327
|
+
map_int64: {0 => Google::Protobuf::Int64Value.new(value: 4)},
|
328
|
+
map_uint32: {0 => Google::Protobuf::UInt32Value.new(value: 5)},
|
329
|
+
map_uint64: {0 => Google::Protobuf::UInt64Value.new(value: 6)},
|
330
|
+
map_bool: {0 => Google::Protobuf::BoolValue.new(value: true)},
|
331
|
+
map_string: {0 => Google::Protobuf::StringValue.new(value: 'str')},
|
332
|
+
map_bytes: {0 => Google::Protobuf::BytesValue.new(value: 'fun')},
|
333
|
+
)
|
334
|
+
|
335
|
+
run_asserts.call(m)
|
336
|
+
serialized = proto_module::Wrapper::encode(m)
|
337
|
+
m2 = proto_module::Wrapper::decode(serialized)
|
338
|
+
run_asserts.call(m2)
|
339
|
+
|
340
|
+
# Test the case where we are serializing directly from the parsed form
|
341
|
+
# (before anything lazy is materialized).
|
342
|
+
m3 = proto_module::Wrapper::decode(serialized)
|
343
|
+
serialized2 = proto_module::Wrapper::encode(m3)
|
344
|
+
m4 = proto_module::Wrapper::decode(serialized2)
|
345
|
+
run_asserts.call(m4)
|
346
|
+
|
347
|
+
# Test that the lazy form compares equal to the expanded form.
|
348
|
+
m5 = proto_module::Wrapper::decode(serialized2)
|
349
|
+
assert_equal m5, m
|
350
|
+
end
|
351
|
+
|
352
|
+
def test_map_wrappers_with_default_values
|
353
|
+
run_asserts = ->(m) {
|
354
|
+
assert_equal 0.0, m.map_double[0].value
|
355
|
+
assert_equal 0.0, m.map_float[0].value
|
356
|
+
assert_equal 0, m.map_int32[0].value
|
357
|
+
assert_equal 0, m.map_int64[0].value
|
358
|
+
assert_equal 0, m.map_uint32[0].value
|
359
|
+
assert_equal 0, m.map_uint64[0].value
|
360
|
+
assert_equal false, m.map_bool[0].value
|
361
|
+
assert_equal '', m.map_string[0].value
|
362
|
+
assert_equal '', m.map_bytes[0].value
|
363
|
+
}
|
364
|
+
|
365
|
+
m = proto_module::Wrapper.new(
|
366
|
+
map_double: {0 => Google::Protobuf::DoubleValue.new(value: 0.0)},
|
367
|
+
map_float: {0 => Google::Protobuf::FloatValue.new(value: 0.0)},
|
368
|
+
map_int32: {0 => Google::Protobuf::Int32Value.new(value: 0)},
|
369
|
+
map_int64: {0 => Google::Protobuf::Int64Value.new(value: 0)},
|
370
|
+
map_uint32: {0 => Google::Protobuf::UInt32Value.new(value: 0)},
|
371
|
+
map_uint64: {0 => Google::Protobuf::UInt64Value.new(value: 0)},
|
372
|
+
map_bool: {0 => Google::Protobuf::BoolValue.new(value: false)},
|
373
|
+
map_string: {0 => Google::Protobuf::StringValue.new(value: '')},
|
374
|
+
map_bytes: {0 => Google::Protobuf::BytesValue.new(value: '')},
|
375
|
+
)
|
376
|
+
|
377
|
+
run_asserts.call(m)
|
378
|
+
serialized = proto_module::Wrapper::encode(m)
|
379
|
+
m2 = proto_module::Wrapper::decode(serialized)
|
380
|
+
run_asserts.call(m2)
|
381
|
+
|
382
|
+
# Test the case where we are serializing directly from the parsed form
|
383
|
+
# (before anything lazy is materialized).
|
384
|
+
m3 = proto_module::Wrapper::decode(serialized)
|
385
|
+
serialized2 = proto_module::Wrapper::encode(m3)
|
386
|
+
m4 = proto_module::Wrapper::decode(serialized2)
|
387
|
+
run_asserts.call(m4)
|
388
|
+
|
389
|
+
# Test that the lazy form compares equal to the expanded form.
|
390
|
+
m5 = proto_module::Wrapper::decode(serialized2)
|
391
|
+
assert_equal m5, m
|
392
|
+
end
|
393
|
+
|
394
|
+
def test_map_wrappers_with_no_value
|
395
|
+
run_asserts = ->(m) {
|
396
|
+
assert_equal 0.0, m.map_double[0].value
|
397
|
+
assert_equal 0.0, m.map_float[0].value
|
398
|
+
assert_equal 0, m.map_int32[0].value
|
399
|
+
assert_equal 0, m.map_int64[0].value
|
400
|
+
assert_equal 0, m.map_uint32[0].value
|
401
|
+
assert_equal 0, m.map_uint64[0].value
|
402
|
+
assert_equal false, m.map_bool[0].value
|
403
|
+
assert_equal '', m.map_string[0].value
|
404
|
+
assert_equal '', m.map_bytes[0].value
|
405
|
+
}
|
406
|
+
|
407
|
+
m = proto_module::Wrapper.new(
|
408
|
+
map_double: {0 => Google::Protobuf::DoubleValue.new()},
|
409
|
+
map_float: {0 => Google::Protobuf::FloatValue.new()},
|
410
|
+
map_int32: {0 => Google::Protobuf::Int32Value.new()},
|
411
|
+
map_int64: {0 => Google::Protobuf::Int64Value.new()},
|
412
|
+
map_uint32: {0 => Google::Protobuf::UInt32Value.new()},
|
413
|
+
map_uint64: {0 => Google::Protobuf::UInt64Value.new()},
|
414
|
+
map_bool: {0 => Google::Protobuf::BoolValue.new()},
|
415
|
+
map_string: {0 => Google::Protobuf::StringValue.new()},
|
416
|
+
map_bytes: {0 => Google::Protobuf::BytesValue.new()},
|
417
|
+
)
|
418
|
+
run_asserts.call(m)
|
419
|
+
|
420
|
+
serialized = proto_module::Wrapper::encode(m)
|
421
|
+
m2 = proto_module::Wrapper::decode(serialized)
|
422
|
+
run_asserts.call(m2)
|
423
|
+
|
424
|
+
# Test the case where we are serializing directly from the parsed form
|
425
|
+
# (before anything lazy is materialized).
|
426
|
+
m3 = proto_module::Wrapper::decode(serialized)
|
427
|
+
serialized2 = proto_module::Wrapper::encode(m3)
|
428
|
+
m4 = proto_module::Wrapper::decode(serialized2)
|
429
|
+
run_asserts.call(m4)
|
430
|
+
end
|
431
|
+
|
735
432
|
def test_concurrent_decoding
|
736
433
|
o = Outer.new
|
737
434
|
o.items[0] = Inner.new
|
@@ -751,7 +448,8 @@ module BasicTest
|
|
751
448
|
m = MapMessage.new(
|
752
449
|
:map_string_int32 => {"a" => 1, "b" => 2},
|
753
450
|
:map_string_msg => {"a" => TestMessage2.new(:foo => 1),
|
754
|
-
"b" => TestMessage2.new(:foo => 2)}
|
451
|
+
"b" => TestMessage2.new(:foo => 2)},
|
452
|
+
:map_string_enum => {"a" => :A, "b" => :B})
|
755
453
|
m2 = MapMessage.decode(MapMessage.encode(m))
|
756
454
|
assert m == m2
|
757
455
|
|
@@ -768,210 +466,26 @@ module BasicTest
|
|
768
466
|
"b" => TestMessage2.new(:foo => 2)}
|
769
467
|
end
|
770
468
|
|
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
|
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
|
469
|
+
def test_protobuf_decode_json_ignore_unknown_fields
|
470
|
+
m = TestMessage.decode_json({
|
471
|
+
optional_string: "foo",
|
472
|
+
not_in_message: "some_value"
|
473
|
+
}.to_json, { ignore_unknown_fields: true })
|
855
474
|
|
856
|
-
|
857
|
-
|
858
|
-
|
859
|
-
m.optional_enum = :A
|
860
|
-
assert m.optional_enum == :A
|
861
|
-
assert_raise RangeError do
|
862
|
-
m.optional_enum = :ASDF
|
475
|
+
assert_equal m.optional_string, "foo"
|
476
|
+
e = assert_raise Google::Protobuf::ParseError do
|
477
|
+
TestMessage.decode_json({ not_in_message: "some_value" }.to_json)
|
863
478
|
end
|
864
|
-
|
865
|
-
assert m.optional_enum == :A
|
866
|
-
m.optional_enum = 100
|
867
|
-
assert m.optional_enum == 100
|
479
|
+
assert_match(/No such field: not_in_message/, e.message)
|
868
480
|
end
|
869
481
|
|
870
|
-
def
|
871
|
-
|
872
|
-
|
873
|
-
|
874
|
-
|
875
|
-
|
876
|
-
|
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
|
482
|
+
#def test_json_quoted_string
|
483
|
+
# m = TestMessage.decode_json(%q(
|
484
|
+
# "optionalInt64": "1",,
|
485
|
+
# }))
|
486
|
+
# puts(m)
|
487
|
+
# assert_equal 1, m.optional_int32
|
488
|
+
#end
|
975
489
|
|
976
490
|
def test_to_h
|
977
491
|
m = TestMessage.new(:optional_bool => true, :optional_double => -10.100001, :optional_string => 'foo', :repeated_string => ['bar1', 'bar2'], :repeated_msg => [TestMessage2.new(:foo => 100)])
|
@@ -984,6 +498,8 @@ module BasicTest
|
|
984
498
|
:optional_int32=>0,
|
985
499
|
:optional_int64=>0,
|
986
500
|
:optional_msg=>nil,
|
501
|
+
:optional_msg2=>nil,
|
502
|
+
:optional_proto2_submessage=>nil,
|
987
503
|
:optional_string=>"foo",
|
988
504
|
:optional_uint32=>0,
|
989
505
|
:optional_uint64=>0,
|
@@ -1004,400 +520,121 @@ module BasicTest
|
|
1004
520
|
m = MapMessage.new(
|
1005
521
|
:map_string_int32 => {"a" => 1, "b" => 2},
|
1006
522
|
:map_string_msg => {"a" => TestMessage2.new(:foo => 1),
|
1007
|
-
"b" => TestMessage2.new(:foo => 2)}
|
523
|
+
"b" => TestMessage2.new(:foo => 2)},
|
524
|
+
:map_string_enum => {"a" => :A, "b" => :B})
|
1008
525
|
expected_result = {
|
1009
526
|
:map_string_int32 => {"a" => 1, "b" => 2},
|
1010
|
-
:map_string_msg => {"a" => {:foo => 1}, "b" => {:foo => 2}}
|
527
|
+
:map_string_msg => {"a" => {:foo => 1}, "b" => {:foo => 2}},
|
528
|
+
:map_string_enum => {"a" => :A, "b" => :B}
|
1011
529
|
}
|
1012
530
|
assert_equal expected_result, m.to_h
|
1013
531
|
end
|
1014
532
|
|
1015
533
|
|
1016
|
-
def
|
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
|
534
|
+
def test_json_maps
|
1229
535
|
# TODO: Fix JSON in JRuby version.
|
1230
536
|
return if RUBY_PLATFORM == "java"
|
1231
|
-
m =
|
1232
|
-
|
1233
|
-
|
1234
|
-
|
1235
|
-
|
1236
|
-
|
1237
|
-
|
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
|
537
|
+
m = MapMessage.new(:map_string_int32 => {"a" => 1})
|
538
|
+
expected = {mapStringInt32: {a: 1}, mapStringMsg: {}, mapStringEnum: {}}
|
539
|
+
expected_preserve = {map_string_int32: {a: 1}, map_string_msg: {}, map_string_enum: {}}
|
540
|
+
assert_equal JSON.parse(MapMessage.encode_json(m, :emit_defaults=>true), :symbolize_names => true), expected
|
541
|
+
|
542
|
+
json = MapMessage.encode_json(m, :preserve_proto_fieldnames => true, :emit_defaults=>true)
|
543
|
+
assert_equal JSON.parse(json, :symbolize_names => true), expected_preserve
|
1252
544
|
|
1253
|
-
|
1254
|
-
|
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]))
|
545
|
+
m2 = MapMessage.decode_json(MapMessage.encode_json(m))
|
546
|
+
assert_equal m, m2
|
1260
547
|
end
|
1261
548
|
|
1262
|
-
def
|
549
|
+
def test_json_maps_emit_defaults_submsg
|
1263
550
|
# TODO: Fix JSON in JRuby version.
|
1264
551
|
return if RUBY_PLATFORM == "java"
|
1265
|
-
m =
|
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
|
-
}
|
552
|
+
m = MapMessage.new(:map_string_msg => {"a" => TestMessage2.new(foo: 0)})
|
553
|
+
expected = {mapStringInt32: {}, mapStringMsg: {a: {foo: 0}}, mapStringEnum: {}}
|
1290
554
|
|
1291
|
-
actual =
|
555
|
+
actual = MapMessage.encode_json(m, :emit_defaults => true)
|
1292
556
|
|
1293
|
-
|
557
|
+
assert_equal JSON.parse(actual, :symbolize_names => true), expected
|
1294
558
|
end
|
1295
559
|
|
1296
560
|
def test_json_emit_defaults_submsg
|
1297
561
|
# TODO: Fix JSON in JRuby version.
|
1298
562
|
return if RUBY_PLATFORM == "java"
|
1299
|
-
m =
|
563
|
+
m = TestSingularFields.new(singular_msg: proto_module::TestMessage2.new)
|
1300
564
|
|
1301
565
|
expected = {
|
1302
|
-
|
1303
|
-
|
1304
|
-
|
1305
|
-
|
1306
|
-
|
1307
|
-
|
1308
|
-
|
1309
|
-
|
1310
|
-
|
1311
|
-
|
1312
|
-
|
1313
|
-
repeatedInt32: [],
|
1314
|
-
repeatedInt64: [],
|
1315
|
-
repeatedUint32: [],
|
1316
|
-
repeatedUint64: [],
|
1317
|
-
repeatedBool: [],
|
1318
|
-
repeatedFloat: [],
|
1319
|
-
repeatedDouble: [],
|
1320
|
-
repeatedString: [],
|
1321
|
-
repeatedBytes: [],
|
1322
|
-
repeatedMsg: [],
|
1323
|
-
repeatedEnum: []
|
566
|
+
singularInt32: 0,
|
567
|
+
singularInt64: "0",
|
568
|
+
singularUint32: 0,
|
569
|
+
singularUint64: "0",
|
570
|
+
singularBool: false,
|
571
|
+
singularFloat: 0,
|
572
|
+
singularDouble: 0,
|
573
|
+
singularString: "",
|
574
|
+
singularBytes: "",
|
575
|
+
singularMsg: {},
|
576
|
+
singularEnum: "Default",
|
1324
577
|
}
|
1325
578
|
|
1326
|
-
actual = TestMessage.encode_json(m, :emit_defaults => true)
|
579
|
+
actual = proto_module::TestMessage.encode_json(m, :emit_defaults => true)
|
1327
580
|
|
1328
|
-
|
581
|
+
assert_equal expected, JSON.parse(actual, :symbolize_names => true)
|
1329
582
|
end
|
1330
583
|
|
1331
|
-
def
|
1332
|
-
#
|
584
|
+
def test_respond_to
|
585
|
+
# This test fails with JRuby 1.7.23, likely because of an old JRuby bug.
|
1333
586
|
return if RUBY_PLATFORM == "java"
|
1334
|
-
|
1335
|
-
|
1336
|
-
|
1337
|
-
|
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
|
-
}
|
587
|
+
msg = MapMessage.new
|
588
|
+
assert msg.respond_to?(:map_string_int32)
|
589
|
+
assert !msg.respond_to?(:bacon)
|
590
|
+
end
|
1359
591
|
|
1360
|
-
|
592
|
+
def test_file_descriptor
|
593
|
+
file_descriptor = TestMessage.descriptor.file_descriptor
|
594
|
+
assert nil != file_descriptor
|
595
|
+
assert_equal "tests/basic_test.proto", file_descriptor.name
|
596
|
+
assert_equal :proto3, file_descriptor.syntax
|
1361
597
|
|
1362
|
-
|
598
|
+
file_descriptor = TestEnum.descriptor.file_descriptor
|
599
|
+
assert nil != file_descriptor
|
600
|
+
assert_equal "tests/basic_test.proto", file_descriptor.name
|
601
|
+
assert_equal :proto3, file_descriptor.syntax
|
1363
602
|
end
|
1364
603
|
|
1365
|
-
|
1366
|
-
|
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
|
604
|
+
# Ruby 2.5 changed to raise FrozenError instead of RuntimeError
|
605
|
+
FrozenErrorType = Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.5') ? RuntimeError : FrozenError
|
1372
606
|
|
1373
|
-
|
1374
|
-
|
607
|
+
def test_map_freeze
|
608
|
+
m = proto_module::MapMessage.new
|
609
|
+
m.map_string_int32['a'] = 5
|
610
|
+
m.map_string_msg['b'] = proto_module::TestMessage2.new
|
1375
611
|
|
1376
|
-
|
1377
|
-
|
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}}}
|
612
|
+
m.map_string_int32.freeze
|
613
|
+
m.map_string_msg.freeze
|
1385
614
|
|
1386
|
-
|
615
|
+
assert m.map_string_int32.frozen?
|
616
|
+
assert m.map_string_msg.frozen?
|
1387
617
|
|
1388
|
-
|
618
|
+
assert_raise(FrozenErrorType) { m.map_string_int32['foo'] = 1 }
|
619
|
+
assert_raise(FrozenErrorType) { m.map_string_msg['bar'] = proto_module::TestMessage2.new }
|
620
|
+
assert_raise(FrozenErrorType) { m.map_string_int32.delete('a') }
|
621
|
+
assert_raise(FrozenErrorType) { m.map_string_int32.clear }
|
1389
622
|
end
|
1390
623
|
|
1391
|
-
def
|
1392
|
-
|
1393
|
-
|
624
|
+
def test_map_length
|
625
|
+
m = proto_module::MapMessage.new
|
626
|
+
assert_equal 0, m.map_string_int32.length
|
627
|
+
assert_equal 0, m.map_string_msg.length
|
628
|
+
assert_equal 0, m.map_string_int32.size
|
629
|
+
assert_equal 0, m.map_string_msg.size
|
1394
630
|
|
1395
|
-
|
1396
|
-
|
1397
|
-
|
1398
|
-
|
1399
|
-
|
1400
|
-
|
631
|
+
m.map_string_int32['a'] = 1
|
632
|
+
m.map_string_int32['b'] = 2
|
633
|
+
m.map_string_msg['a'] = proto_module::TestMessage2.new
|
634
|
+
assert_equal 2, m.map_string_int32.length
|
635
|
+
assert_equal 1, m.map_string_msg.length
|
636
|
+
assert_equal 2, m.map_string_int32.size
|
637
|
+
assert_equal 1, m.map_string_msg.size
|
1401
638
|
end
|
1402
639
|
end
|
1403
640
|
end
|