google-protobuf-z 3.5.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,348 @@
1
+ #!/usr/bin/ruby
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'
8
+ require 'google/protobuf'
9
+ require 'json'
10
+ require 'test/unit'
11
+
12
+ # ------------- generated code --------------
13
+
14
+ module BasicTest
15
+ pool = Google::Protobuf::DescriptorPool.new
16
+ pool.build do
17
+ add_message "BadFieldNames" do
18
+ optional :dup, :int32, 1
19
+ optional :class, :int32, 2
20
+ optional :"a.b", :int32, 3
21
+ end
22
+ end
23
+
24
+ BadFieldNames = pool.lookup("BadFieldNames").msgclass
25
+
26
+ # ------------ test cases ---------------
27
+
28
+ class MessageContainerTest < Test::Unit::TestCase
29
+ # Required by CommonTests module to resolve proto3 proto classes used in tests.
30
+ def proto_module
31
+ ::BasicTest
32
+ end
33
+ include CommonTests
34
+
35
+ def test_has_field
36
+ m = TestMessage.new
37
+ assert !m.has_optional_msg?
38
+ m.optional_msg = TestMessage2.new
39
+ assert m.has_optional_msg?
40
+ assert TestMessage.descriptor.lookup('optional_msg').has?(m)
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?
48
+ end
49
+ assert_raise ArgumentError do
50
+ OneofMessage.descriptor.lookup('a').has?(m)
51
+ end
52
+
53
+ m = TestMessage.new
54
+ assert_raise NoMethodError do
55
+ m.has_optional_int32?
56
+ end
57
+ assert_raise ArgumentError do
58
+ TestMessage.descriptor.lookup('optional_int32').has?(m)
59
+ end
60
+
61
+ assert_raise NoMethodError do
62
+ m.has_optional_string?
63
+ end
64
+ assert_raise ArgumentError do
65
+ TestMessage.descriptor.lookup('optional_string').has?(m)
66
+ end
67
+
68
+ assert_raise NoMethodError do
69
+ m.has_optional_bool?
70
+ end
71
+ assert_raise ArgumentError do
72
+ TestMessage.descriptor.lookup('optional_bool').has?(m)
73
+ end
74
+
75
+ assert_raise NoMethodError do
76
+ m.has_repeated_msg?
77
+ end
78
+ assert_raise ArgumentError do
79
+ TestMessage.descriptor.lookup('repeated_msg').has?(m)
80
+ end
81
+ end
82
+
83
+ def test_set_clear_defaults
84
+ m = TestMessage.new
85
+
86
+ m.optional_int32 = -42
87
+ assert_equal -42, m.optional_int32
88
+ m.clear_optional_int32
89
+ assert_equal 0, m.optional_int32
90
+
91
+ m.optional_int32 = 50
92
+ assert_equal 50, m.optional_int32
93
+ TestMessage.descriptor.lookup('optional_int32').clear(m)
94
+ assert_equal 0, m.optional_int32
95
+
96
+ m.optional_string = "foo bar"
97
+ assert_equal "foo bar", m.optional_string
98
+ m.clear_optional_string
99
+ assert_equal "", m.optional_string
100
+
101
+ m.optional_string = "foo"
102
+ assert_equal "foo", m.optional_string
103
+ TestMessage.descriptor.lookup('optional_string').clear(m)
104
+ assert_equal "", m.optional_string
105
+
106
+ m.optional_msg = TestMessage2.new(:foo => 42)
107
+ assert_equal TestMessage2.new(:foo => 42), m.optional_msg
108
+ assert m.has_optional_msg?
109
+ m.clear_optional_msg
110
+ assert_equal nil, m.optional_msg
111
+ assert !m.has_optional_msg?
112
+
113
+ m.optional_msg = TestMessage2.new(:foo => 42)
114
+ assert_equal TestMessage2.new(:foo => 42), m.optional_msg
115
+ TestMessage.descriptor.lookup('optional_msg').clear(m)
116
+ assert_equal nil, m.optional_msg
117
+
118
+ m.repeated_int32.push(1)
119
+ assert_equal [1], m.repeated_int32
120
+ m.clear_repeated_int32
121
+ assert_equal [], m.repeated_int32
122
+
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
127
+
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?
134
+
135
+ m.a = "foobar"
136
+ assert m.has_my_oneof?
137
+ m.clear_my_oneof
138
+ assert !m.has_my_oneof?
139
+
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?
145
+ end
146
+
147
+
148
+ def test_initialization_map_errors
149
+ e = assert_raise ArgumentError do
150
+ TestMessage.new(:hello => "world")
151
+ end
152
+ assert_match(/hello/, e.message)
153
+
154
+ e = assert_raise ArgumentError do
155
+ MapMessage.new(:map_string_int32 => "hello")
156
+ end
157
+ assert_equal e.message, "Expected Hash object as initializer value for map field 'map_string_int32'."
158
+
159
+ e = assert_raise ArgumentError do
160
+ TestMessage.new(:repeated_uint32 => "hello")
161
+ end
162
+ assert_equal e.message, "Expected array as initializer value for repeated field 'repeated_uint32'."
163
+ end
164
+
165
+ def test_map_field
166
+ m = MapMessage.new
167
+ assert m.map_string_int32 == {}
168
+ assert m.map_string_msg == {}
169
+
170
+ m = MapMessage.new(
171
+ :map_string_int32 => {"a" => 1, "b" => 2},
172
+ :map_string_msg => {"a" => TestMessage2.new(:foo => 1),
173
+ "b" => TestMessage2.new(:foo => 2)})
174
+ assert m.map_string_int32.keys.sort == ["a", "b"]
175
+ assert m.map_string_int32["a"] == 1
176
+ assert m.map_string_msg["b"].foo == 2
177
+
178
+ m.map_string_int32["c"] = 3
179
+ assert m.map_string_int32["c"] == 3
180
+ m.map_string_msg["c"] = TestMessage2.new(:foo => 3)
181
+ assert m.map_string_msg["c"] == TestMessage2.new(:foo => 3)
182
+ m.map_string_msg.delete("b")
183
+ m.map_string_msg.delete("c")
184
+ assert m.map_string_msg == { "a" => TestMessage2.new(:foo => 1) }
185
+
186
+ assert_raise Google::Protobuf::TypeError do
187
+ m.map_string_msg["e"] = TestMessage.new # wrong value type
188
+ end
189
+ # ensure nothing was added by the above
190
+ assert m.map_string_msg == { "a" => TestMessage2.new(:foo => 1) }
191
+
192
+ m.map_string_int32 = Google::Protobuf::Map.new(:string, :int32)
193
+ assert_raise Google::Protobuf::TypeError do
194
+ m.map_string_int32 = Google::Protobuf::Map.new(:string, :int64)
195
+ end
196
+ assert_raise Google::Protobuf::TypeError do
197
+ m.map_string_int32 = {}
198
+ end
199
+
200
+ assert_raise TypeError do
201
+ m = MapMessage.new(:map_string_int32 => { 1 => "I am not a number" })
202
+ end
203
+ end
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
+
214
+ def test_map_corruption
215
+ # This pattern led to a crash in a previous version of upb/protobuf.
216
+ m = MapMessage.new(map_string_int32: { "aaa" => 1 })
217
+ m.map_string_int32['podid'] = 2
218
+ m.map_string_int32['aaa'] = 3
219
+ end
220
+
221
+ def test_concurrent_decoding
222
+ o = Outer.new
223
+ o.items[0] = Inner.new
224
+ raw = Outer.encode(o)
225
+
226
+ thds = 2.times.map do
227
+ Thread.new do
228
+ 100000.times do
229
+ assert_equal o, Outer.decode(raw)
230
+ end
231
+ end
232
+ end
233
+ thds.map(&:join)
234
+ end
235
+
236
+ def test_map_encode_decode
237
+ m = MapMessage.new(
238
+ :map_string_int32 => {"a" => 1, "b" => 2},
239
+ :map_string_msg => {"a" => TestMessage2.new(:foo => 1),
240
+ "b" => TestMessage2.new(:foo => 2)})
241
+ m2 = MapMessage.decode(MapMessage.encode(m))
242
+ assert m == m2
243
+
244
+ m3 = MapMessageWireEquiv.decode(MapMessage.encode(m))
245
+ assert m3.map_string_int32.length == 2
246
+
247
+ kv = {}
248
+ m3.map_string_int32.map { |msg| kv[msg.key] = msg.value }
249
+ assert kv == {"a" => 1, "b" => 2}
250
+
251
+ kv = {}
252
+ m3.map_string_msg.map { |msg| kv[msg.key] = msg.value }
253
+ assert kv == {"a" => TestMessage2.new(:foo => 1),
254
+ "b" => TestMessage2.new(:foo => 2)}
255
+ end
256
+
257
+ def test_to_h
258
+ m = TestMessage.new(:optional_bool => true, :optional_double => -10.100001, :optional_string => 'foo', :repeated_string => ['bar1', 'bar2'], :repeated_msg => [TestMessage2.new(:foo => 100)])
259
+ expected_result = {
260
+ :optional_bool=>true,
261
+ :optional_bytes=>"",
262
+ :optional_double=>-10.100001,
263
+ :optional_enum=>:Default,
264
+ :optional_float=>0.0,
265
+ :optional_int32=>0,
266
+ :optional_int64=>0,
267
+ :optional_msg=>nil,
268
+ :optional_string=>"foo",
269
+ :optional_uint32=>0,
270
+ :optional_uint64=>0,
271
+ :repeated_bool=>[],
272
+ :repeated_bytes=>[],
273
+ :repeated_double=>[],
274
+ :repeated_enum=>[],
275
+ :repeated_float=>[],
276
+ :repeated_int32=>[],
277
+ :repeated_int64=>[],
278
+ :repeated_msg=>[{:foo => 100}],
279
+ :repeated_string=>["bar1", "bar2"],
280
+ :repeated_uint32=>[],
281
+ :repeated_uint64=>[]
282
+ }
283
+ assert_equal expected_result, m.to_h
284
+
285
+ m = MapMessage.new(
286
+ :map_string_int32 => {"a" => 1, "b" => 2},
287
+ :map_string_msg => {"a" => TestMessage2.new(:foo => 1),
288
+ "b" => TestMessage2.new(:foo => 2)})
289
+ expected_result = {
290
+ :map_string_int32 => {"a" => 1, "b" => 2},
291
+ :map_string_msg => {"a" => {:foo => 1}, "b" => {:foo => 2}}
292
+ }
293
+ assert_equal expected_result, m.to_h
294
+ end
295
+
296
+
297
+ def test_json_maps
298
+ # TODO: Fix JSON in JRuby version.
299
+ return if RUBY_PLATFORM == "java"
300
+ m = MapMessage.new(:map_string_int32 => {"a" => 1})
301
+ expected = {mapStringInt32: {a: 1}, mapStringMsg: {}}
302
+ expected_preserve = {map_string_int32: {a: 1}, map_string_msg: {}}
303
+ assert JSON.parse(MapMessage.encode_json(m), :symbolize_names => true) == expected
304
+
305
+ json = MapMessage.encode_json(m, :preserve_proto_fieldnames => true)
306
+ assert JSON.parse(json, :symbolize_names => true) == expected_preserve
307
+
308
+ m2 = MapMessage.decode_json(MapMessage.encode_json(m))
309
+ assert m == m2
310
+ end
311
+
312
+ def test_json_maps_emit_defaults_submsg
313
+ # TODO: Fix JSON in JRuby version.
314
+ return if RUBY_PLATFORM == "java"
315
+ m = MapMessage.new(:map_string_msg => {"a" => TestMessage2.new})
316
+ expected = {mapStringInt32: {}, mapStringMsg: {a: {foo: 0}}}
317
+
318
+ actual = MapMessage.encode_json(m, :emit_defaults => true)
319
+
320
+ assert JSON.parse(actual, :symbolize_names => true) == expected
321
+ end
322
+
323
+ def test_respond_to
324
+ # This test fails with JRuby 1.7.23, likely because of an old JRuby bug.
325
+ return if RUBY_PLATFORM == "java"
326
+ msg = MapMessage.new
327
+ assert msg.respond_to?(:map_string_int32)
328
+ assert !msg.respond_to?(:bacon)
329
+ end
330
+
331
+ def test_file_descriptor
332
+ file_descriptor = TestMessage.descriptor.file_descriptor
333
+ assert nil != file_descriptor
334
+ assert_equal "tests/basic_test.proto", file_descriptor.name
335
+ assert_equal :proto3, file_descriptor.syntax
336
+
337
+ file_descriptor = TestEnum.descriptor.file_descriptor
338
+ assert nil != file_descriptor
339
+ assert_equal "tests/basic_test.proto", file_descriptor.name
340
+ assert_equal :proto3, file_descriptor.syntax
341
+
342
+ file_descriptor = BadFieldNames.descriptor.file_descriptor
343
+ assert nil != file_descriptor
344
+ assert_equal nil, file_descriptor.name
345
+ assert_equal :proto3, file_descriptor.syntax
346
+ end
347
+ end
348
+ end
@@ -0,0 +1,21 @@
1
+ #!/usr/bin/ruby
2
+
3
+ # generated_code.rb is in the same directory as this test.
4
+ $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)))
5
+
6
+ require 'generated_code_pb'
7
+ require 'test_import_pb'
8
+ require 'test_ruby_package_pb'
9
+ require 'test/unit'
10
+
11
+ class GeneratedCodeTest < Test::Unit::TestCase
12
+ def test_generated_msg
13
+ # just test that we can instantiate the message. The purpose of this test
14
+ # is to ensure that the output of the code generator is valid Ruby and
15
+ # successfully creates message definitions and classes, not to test every
16
+ # aspect of the extension (basic.rb is for that).
17
+ m = A::B::C::TestMessage.new()
18
+ m2 = FooBar::TestImportedMessage.new()
19
+ m3 = A::B::TestRubyPackageMessage.new()
20
+ end
21
+ end
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/ruby
2
+
3
+ require 'google/protobuf'
4
+ require 'test/unit'
5
+
6
+ module StressTest
7
+ pool = Google::Protobuf::DescriptorPool.new
8
+ pool.build do
9
+ add_message "TestMessage" do
10
+ optional :a, :int32, 1
11
+ repeated :b, :message, 2, "M"
12
+ end
13
+ add_message "M" do
14
+ optional :foo, :string, 1
15
+ end
16
+ end
17
+
18
+ TestMessage = pool.lookup("TestMessage").msgclass
19
+ M = pool.lookup("M").msgclass
20
+
21
+ class StressTest < Test::Unit::TestCase
22
+ def get_msg
23
+ TestMessage.new(:a => 1000,
24
+ :b => [M.new(:foo => "hello"),
25
+ M.new(:foo => "world")])
26
+ end
27
+ def test_stress
28
+ m = get_msg
29
+ data = TestMessage.encode(m)
30
+ 100_000.times do
31
+ mnew = TestMessage.decode(data)
32
+ mnew = mnew.dup
33
+ assert_equal mnew.inspect, m.inspect
34
+ assert TestMessage.encode(mnew) == data
35
+ end
36
+ end
37
+ end
38
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: google-protobuf-z
3
+ version: !ruby/object:Gem::Version
4
+ version: 3.5.1
5
+ platform: ruby
6
+ authors:
7
+ - Protobuf Authors
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-11-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake-compiler-dock
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.6.0
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.6.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake-compiler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.9.5
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.9.5
41
+ - !ruby/object:Gem::Dependency
42
+ name: test-unit
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: 3.0.9
51
+ type: :development
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - "~>"
56
+ - !ruby/object:Gem::Version
57
+ version: '3.0'
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: 3.0.9
61
+ - !ruby/object:Gem::Dependency
62
+ name: rubygems-tasks
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: 0.2.4
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: 0.2.4
75
+ description: Protocol Buffers are Google's data interchange format.
76
+ email: protobuf@googlegroups.com
77
+ executables: []
78
+ extensions:
79
+ - ext/google/protobuf_c/extconf.rb
80
+ extra_rdoc_files: []
81
+ files:
82
+ - ext/google/protobuf_c/defs.c
83
+ - ext/google/protobuf_c/encode_decode.c
84
+ - ext/google/protobuf_c/extconf.rb
85
+ - ext/google/protobuf_c/map.c
86
+ - ext/google/protobuf_c/message.c
87
+ - ext/google/protobuf_c/protobuf.c
88
+ - ext/google/protobuf_c/protobuf.h
89
+ - ext/google/protobuf_c/repeated_field.c
90
+ - ext/google/protobuf_c/storage.c
91
+ - ext/google/protobuf_c/upb.c
92
+ - ext/google/protobuf_c/upb.h
93
+ - ext/google/protobuf_c/wrap_memcpy.c
94
+ - lib/google/protobuf.rb
95
+ - lib/google/protobuf/message_exts.rb
96
+ - lib/google/protobuf/repeated_field.rb
97
+ - lib/google/protobuf/well_known_types.rb
98
+ - tests/basic.rb
99
+ - tests/generated_code_test.rb
100
+ - tests/stress.rb
101
+ homepage: https://developers.google.com/protocol-buffers
102
+ licenses:
103
+ - BSD-3-Clause
104
+ metadata: {}
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 2.6.13
122
+ signing_key:
123
+ specification_version: 4
124
+ summary: Protocol Buffers
125
+ test_files:
126
+ - tests/basic.rb
127
+ - tests/stress.rb
128
+ - tests/generated_code_test.rb