ruby-protocol-buffers 1.3.0 → 1.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,37 @@
1
+ #!/usr/bin/env ruby
2
+ # Generated by the protocol buffer compiler. DO NOT EDIT!
3
+
4
+ require 'protocol_buffers'
5
+
6
+ module Packed
7
+ # forward declarations
8
+ class Test < ::ProtocolBuffers::Message; end
9
+
10
+ class Test < ::ProtocolBuffers::Message
11
+ # forward declarations
12
+
13
+ # enums
14
+ module N
15
+ include ::ProtocolBuffers::Enum
16
+ A = 1
17
+ B = 2
18
+ C = 3
19
+ end
20
+
21
+ repeated :int32, :a, 4, :packed => true
22
+ repeated :int64, :b, 5, :packed => true
23
+ repeated :uint32, :c, 6, :packed => true
24
+ repeated :uint64, :d, 7, :packed => true
25
+ repeated :sint32, :e, 8, :packed => true
26
+ repeated :sint64, :f, 9, :packed => true
27
+ repeated :fixed64, :g, 10, :packed => true
28
+ repeated :sfixed64, :h, 11, :packed => true
29
+ repeated :double, :i, 12, :packed => true
30
+ repeated :fixed32, :j, 13, :packed => true
31
+ repeated :sfixed32, :k, 14, :packed => true
32
+ repeated :float, :l, 15, :packed => true
33
+ repeated :bool, :m, 16, :packed => true
34
+ repeated ::Packed::Test::N, :n, 17, :packed => true
35
+ end
36
+
37
+ end
@@ -0,0 +1,30 @@
1
+ package packed;
2
+
3
+ message Test {
4
+ repeated int32 a = 4 [packed=true];
5
+ repeated int64 b = 5 [packed=true];
6
+
7
+ repeated uint32 c = 6 [packed=true];
8
+ repeated uint64 d = 7 [packed=true];
9
+
10
+ repeated sint32 e = 8 [packed=true];
11
+ repeated sint64 f = 9 [packed=true];
12
+
13
+ repeated fixed64 g = 10 [packed=true];
14
+ repeated sfixed64 h = 11 [packed=true];
15
+ repeated double i = 12 [packed=true];
16
+
17
+ repeated fixed32 j = 13 [packed=true];
18
+ repeated sfixed32 k = 14 [packed=true];
19
+ repeated float l = 15 [packed=true];
20
+
21
+ repeated bool m = 16 [packed=true];
22
+
23
+ enum N {
24
+ A = 1;
25
+ B = 2;
26
+ C = 3;
27
+ }
28
+ repeated N n = 17 [packed=true];
29
+
30
+ };
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env ruby
2
+ # Generated by the protocol buffer compiler. DO NOT EDIT!
3
+
4
+ require 'protocol_buffers'
5
+
6
+ module Simple
7
+ # forward declarations
8
+ class Test1 < ::ProtocolBuffers::Message; end
9
+ class Foo < ::ProtocolBuffers::Message; end
10
+ class Bar < ::ProtocolBuffers::Message; end
11
+
12
+ class Test1 < ::ProtocolBuffers::Message
13
+ optional :string, :test_field, 1
14
+ end
15
+
16
+ class Foo < ::ProtocolBuffers::Message
17
+ end
18
+
19
+ class Bar < ::ProtocolBuffers::Message
20
+ optional ::Simple::Foo, :foo, 1
21
+ end
22
+
23
+ end
data/spec/runtime_spec.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # encoding: binary
2
+
1
3
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
4
 
3
5
  require 'stringio'
@@ -9,17 +11,85 @@ require 'protocol_buffers/compiler'
9
11
  describe ProtocolBuffers, "runtime" do
10
12
  before(:each) do
11
13
  # clear our namespaces
12
- Object.send(:remove_const, :Simple) if defined?(Simple)
13
- Object.send(:remove_const, :Featureful) if defined?(Featureful)
14
- Object.send(:remove_const, :Foo) if defined?(Foo)
15
- Object.send(:remove_const, :TehUnknown) if defined?(TehUnknown)
16
- Object.send(:remove_const, :TehUnknown2) if defined?(TehUnknown2)
17
- Object.send(:remove_const, :TehUnknown3) if defined?(TehUnknown3)
18
-
19
- ProtocolBuffers::Compiler.compile_and_load(
20
- File.join(File.dirname(__FILE__), "proto_files", "simple.proto"))
21
- ProtocolBuffers::Compiler.compile_and_load(
22
- File.join(File.dirname(__FILE__), "proto_files", "featureful.proto"))
14
+ %w( Simple Featureful Foo Packed TehUnknown TehUnknown2 TehUnknown3 ).each do |klass|
15
+ Object.send(:remove_const, klass.to_sym) if Object.const_defined?(klass.to_sym)
16
+ end
17
+
18
+ # load test protos
19
+ %w( simple featureful packed ).each do |proto|
20
+ load File.join(File.dirname(__FILE__), "proto_files", "#{proto}.pb.rb")
21
+ end
22
+ end
23
+
24
+ context "packed field handling" do
25
+
26
+ before :each do
27
+ @packed = Packed::Test.new
28
+ end
29
+
30
+ it "does not encode empty field" do
31
+ @packed.a = [ ]
32
+ @packed.to_s.should == ""
33
+
34
+ ser = ProtocolBuffers.bin_sio(@packed.to_s)
35
+ unpacked = Packed::Test.parse(ser)
36
+ unpacked.a.should == [ ]
37
+ end
38
+
39
+ it "correctly encodes repeated field" do
40
+ # Example values from https://developers.google.com/protocol-buffers/docs/encoding.
41
+ @packed.a = [ 3, 270 ]
42
+ @packed.a << 86942
43
+ @packed.to_s.should == "\x22\x06\x03\x8e\x02\x9e\xa7\x05"
44
+
45
+ ser = ProtocolBuffers.bin_sio(@packed.to_s)
46
+ unpacked = Packed::Test.parse(ser)
47
+ unpacked.a.should == [ 3, 270, 86942 ]
48
+ end
49
+
50
+ it "handles primitive numeric data types" do
51
+ types_to_be_packed = {
52
+ :int32 => { :field => :a, :value => [ 0, 1, 1 ] },
53
+ :int64 => { :field => :b, :value => [ 2, 3, 5 ] },
54
+
55
+ :uint32 => { :field => :c, :value => [ 8, 13, 21 ] },
56
+ :uint64 => { :field => :d, :value => [ 34, 55, 89 ] },
57
+
58
+ :sint32 => { :field => :e, :value => [ -114, 233, -377 ] },
59
+ :sint64 => { :field => :f, :value => [ 610, -987, 1597 ] },
60
+
61
+ :fixed64 => { :field => :g, :value => [ 2584, 4181, 6765 ] },
62
+ :sfixed64 => { :field => :h, :value => [ -10946, 17711, -28657 ] },
63
+ :double => { :field => :i, :value => [ 46.368, -75025, 121.393 ] },
64
+
65
+ :fixed32 => { :field => :j, :value => [ 196418, 317811, 514229 ] },
66
+ :sfixed32 => { :field => :k, :value => [ -832040, 1346269, -2178309 ] },
67
+ :float => { :field => :l, :value => [ 3524.578, -5702887, 92274.65 ] },
68
+
69
+ :bool => { :field => :m, :value => [ false, false, true, false ] },
70
+ :enum => { :field => :n, :value => [ Packed::Test::N::A, Packed::Test::N::B, Packed::Test::N::A, Packed::Test::N::C ] }
71
+ }
72
+
73
+ types_to_be_packed.values.each do |v|
74
+ @packed.send("#{v[:field]}=", v[:value])
75
+ end
76
+
77
+ ser = ProtocolBuffers.bin_sio(@packed.to_s)
78
+ unpacked = Packed::Test.parse(ser)
79
+
80
+ types_to_be_packed.each_pair do |k, v|
81
+ if [ :float, :double ].include? k
82
+ act = unpacked.send(v[:field]).map{|i| (i * 100).round}
83
+ exp = v[:value].map{|i| (i * 100).round}
84
+
85
+ act.should == exp
86
+ else
87
+ unpacked.send(v[:field]).should == v[:value]
88
+ end
89
+ end
90
+
91
+ end
92
+
23
93
  end
24
94
 
25
95
  it "can handle basic operations" do
@@ -91,13 +161,12 @@ describe ProtocolBuffers, "runtime" do
91
161
  end
92
162
 
93
163
  it "allows directly recursive sub-messages" do
94
- ProtocolBuffers::Compiler.compile_and_load_string <<-EOS
95
- package foo;
96
- message Foo {
97
- optional int32 payload = 1;
98
- optional Foo foo = 2;
99
- }
100
- EOS
164
+ module Foo
165
+ class Foo < ProtocolBuffers::Message
166
+ optional :int32, :payload, 1
167
+ optional Foo, :foo, 2
168
+ end
169
+ end
101
170
 
102
171
  foo = Foo::Foo.new
103
172
  foo.has_foo?.should == false
@@ -107,18 +176,19 @@ describe ProtocolBuffers, "runtime" do
107
176
  end
108
177
 
109
178
  it "allows indirectly recursive sub-messages" do
110
- ProtocolBuffers::Compiler.compile_and_load_string <<-EOS
111
- package foo;
112
- message Foo {
113
- optional int32 payload = 1;
114
- optional Bar bar = 2;
115
- }
179
+ module Foo
180
+ class Bar < ProtocolBuffers::Message; end
116
181
 
117
- message Bar {
118
- optional Foo foo = 1;
119
- optional int32 payload = 2;
120
- }
121
- EOS
182
+ class Foo < ProtocolBuffers::Message
183
+ optional :int32, :payload, 1
184
+ optional Bar, :bar, 2
185
+ end
186
+
187
+ class Bar
188
+ optional Foo, :foo, 1
189
+ optional :int32, :payload, 2
190
+ end
191
+ end
122
192
 
123
193
  foo = Foo::Foo.new
124
194
  foo.has_bar?.should == false
@@ -131,12 +201,11 @@ describe ProtocolBuffers, "runtime" do
131
201
 
132
202
  it "pretends that repeated fields are arrays" do
133
203
  # make sure our RepeatedField class acts like a normal Array
134
- ProtocolBuffers::Compiler.compile_and_load_string <<-EOS
135
- package foo;
136
- message Foo {
137
- repeated int32 nums = 1;
138
- }
139
- EOS
204
+ module Foo
205
+ class Foo < ProtocolBuffers::Message
206
+ repeated :int32, :nums, 1
207
+ end
208
+ end
140
209
 
141
210
  foo = Foo::Foo.new
142
211
  foo2 = Foo::Foo.new(:nums => [1,2,3])
@@ -171,12 +240,11 @@ describe ProtocolBuffers, "runtime" do
171
240
  end
172
241
 
173
242
  it "does value checking of repeated fields" do
174
- ProtocolBuffers::Compiler.compile_and_load_string <<-EOS
175
- package foo;
176
- message Foo {
177
- repeated int32 nums = 1;
178
- }
179
- EOS
243
+ module Foo
244
+ class Foo < ProtocolBuffers::Message
245
+ repeated :int32, :nums, 1
246
+ end
247
+ end
180
248
 
181
249
  foo = Foo::Foo.new
182
250
  proc do
@@ -187,15 +255,14 @@ describe ProtocolBuffers, "runtime" do
187
255
  # sort of redundant test, but let's check the example in the docs for
188
256
  # correctness
189
257
  it "handles singular message fields exactly as in the documentation" do
190
- ProtocolBuffers::Compiler.compile_and_load_string <<-EOS
191
- package foo;
192
- message Foo {
193
- optional Bar bar = 1;
194
- }
195
- message Bar {
196
- optional int32 i = 1;
197
- }
198
- EOS
258
+ module Foo
259
+ class Bar < ProtocolBuffers::Message
260
+ optional :int32, :i, 1
261
+ end
262
+ class Foo < ProtocolBuffers::Message
263
+ optional Bar, :bar, 1
264
+ end
265
+ end
199
266
 
200
267
  foo = Foo::Foo.new
201
268
  foo.has_bar?.should == false
@@ -209,18 +276,17 @@ describe ProtocolBuffers, "runtime" do
209
276
 
210
277
  foo = Foo::Foo.new
211
278
  foo.has_bar?.should == false
212
- local_i = foo.bar.i
279
+ _local_i = foo.bar.i
213
280
  foo.has_bar?.should == false
214
281
  end
215
282
 
216
283
  # another example from the docs
217
284
  it "handles repeated field logic" do
218
- ProtocolBuffers::Compiler.compile_and_load_string <<-EOS
219
- package foo;
220
- message Foo {
221
- repeated int32 nums = 1;
222
- }
223
- EOS
285
+ module Foo
286
+ class Foo < ProtocolBuffers::Message
287
+ repeated :int32, :nums, 1
288
+ end
289
+ end
224
290
 
225
291
  foo = Foo::Foo.new
226
292
  foo.has_nums?.should == true
@@ -256,15 +322,15 @@ describe ProtocolBuffers, "runtime" do
256
322
  end
257
323
 
258
324
  it "can assign any object with an each method to a repeated field" do
259
- ProtocolBuffers::Compiler.compile_and_load_string <<-EOS
260
- package foo;
261
- message Foo {
262
- repeated Bar nums = 1;
263
- }
264
- message Bar {
265
- optional int32 i = 1;
266
- }
267
- EOS
325
+ module Foo
326
+ class Bar < ProtocolBuffers::Message
327
+ optional :int32, :i, 1
328
+ end
329
+
330
+ class Foo < ProtocolBuffers::Message
331
+ repeated Bar, :nums, 1
332
+ end
333
+ end
268
334
 
269
335
  class Blah
270
336
  def each
@@ -288,7 +354,7 @@ describe ProtocolBuffers, "runtime" do
288
354
  end
289
355
 
290
356
  it "responds to gen_methods! for backwards compat" do
291
- A.gen_methods!
357
+ Featureful::A.gen_methods!
292
358
  end
293
359
 
294
360
  def filled_in_bit
@@ -368,13 +434,12 @@ describe ProtocolBuffers, "runtime" do
368
434
  end
369
435
 
370
436
  it "enforces required fields on serialization" do
371
- ProtocolBuffers::Compiler.compile_and_load_string <<-EOS
372
- package tehUnknown;
373
- message MyResult {
374
- required string field_1 = 1;
375
- optional string field_2 = 2;
376
- }
377
- EOS
437
+ module TehUnknown
438
+ class MyResult < ProtocolBuffers::Message
439
+ required :string, :field_1, 1
440
+ optional :string, :field_2, 2
441
+ end
442
+ end
378
443
 
379
444
  res1 = TehUnknown::MyResult.new(:field_2 => 'b')
380
445
 
@@ -392,73 +457,67 @@ describe ProtocolBuffers, "runtime" do
392
457
  end
393
458
 
394
459
  it "enforces required fields on deserialization" do
395
- ProtocolBuffers::Compiler.compile_and_load_string <<-EOS
396
- package tehUnknown;
397
- message MyResult {
398
- optional string field_1 = 1;
399
- optional string field_2 = 2;
400
- }
401
- EOS
460
+ module TehUnknown
461
+ class MyResult < ProtocolBuffers::Message
462
+ optional :string, :field_1, 1
463
+ optional :string, :field_2, 2
464
+ end
465
+ end
402
466
 
403
467
  res1 = TehUnknown::MyResult.new(:field_2 => 'b')
404
468
  buf = res1.to_s
405
469
 
406
470
  # now make field_1 required
407
- ProtocolBuffers::Compiler.compile_and_load_string <<-EOS
408
- package tehUnknown2;
409
- message MyResult {
410
- required string field_1 = 1;
411
- optional string field_2 = 2;
412
- }
413
- EOS
471
+ module TehUnknown2
472
+ class MyResult < ProtocolBuffers::Message
473
+ required :string, :field_1, 1
474
+ optional :string, :field_2, 2
475
+ end
476
+ end
414
477
 
415
478
  proc { TehUnknown2::MyResult.parse(buf) }.should raise_error(ProtocolBuffers::DecodeError)
416
479
  end
417
480
 
418
481
  it "enforces valid values on deserialization" do
419
- ProtocolBuffers::Compiler.compile_and_load_string <<-EOS
420
- package tehUnknown;
421
- message MyResult {
422
- optional int64 field_1 = 1;
423
- }
424
- EOS
482
+ module TehUnknown
483
+ class MyResult < ProtocolBuffers::Message
484
+ optional :int64, :field_1, 1
485
+ end
486
+ end
425
487
 
426
488
  res1 = TehUnknown::MyResult.new(:field_1 => (2**33))
427
489
  buf = res1.to_s
428
490
 
429
- ProtocolBuffers::Compiler.compile_and_load_string <<-EOS
430
- package tehUnknown2;
431
- message MyResult {
432
- optional int32 field_1 = 1;
433
- }
434
- EOS
491
+ module TehUnknown2
492
+ class MyResult < ProtocolBuffers::Message
493
+ optional :int32, :field_1, 1
494
+ end
495
+ end
435
496
 
436
497
  proc { TehUnknown2::MyResult.parse(buf) }.should raise_error(ProtocolBuffers::DecodeError)
437
498
  end
438
499
 
439
500
  it "ignores and passes on unknown fields" do
440
- ProtocolBuffers::Compiler.compile_and_load_string <<-EOS
441
- package tehUnknown;
442
- message MyResult {
443
- optional int32 field_1 = 1;
444
- optional int32 field_2 = 2;
445
- optional int32 field_3 = 3;
446
- optional int32 field_4 = 4;
447
- }
448
- EOS
501
+ module TehUnknown
502
+ class MyResult < ProtocolBuffers::Message
503
+ optional :int32, :field_1, 1
504
+ optional :int32, :field_2, 2
505
+ optional :int32, :field_3, 3
506
+ optional :int32, :field_4, 4
507
+ end
508
+ end
449
509
 
450
510
  res1 = TehUnknown::MyResult.new(:field_1 => 0xffff, :field_2 => 0xfffe,
451
511
  :field_3 => 0xfffd, :field_4 => 0xfffc)
452
512
  serialized = res1.to_s
453
513
 
454
514
  # remove field_2 to pretend we never knew about it
455
- ProtocolBuffers::Compiler.compile_and_load_string <<-EOS
456
- package tehUnknown2;
457
- message MyResult {
458
- optional int32 field_1 = 1;
459
- optional int32 field_3 = 3;
460
- }
461
- EOS
515
+ module TehUnknown2
516
+ class MyResult < ProtocolBuffers::Message
517
+ optional :int32, :field_1, 1
518
+ optional :int32, :field_3, 3
519
+ end
520
+ end
462
521
 
463
522
  res2 = nil
464
523
  proc do
@@ -475,14 +534,13 @@ describe ProtocolBuffers, "runtime" do
475
534
  serialized2 = res2.to_s
476
535
 
477
536
  # now we know about field_2 again
478
- ProtocolBuffers::Compiler.compile_and_load_string <<-EOS
479
- package tehUnknown3;
480
- message MyResult {
481
- optional int32 field_1 = 1;
482
- optional int32 field_2 = 2;
483
- optional int32 field_4 = 4;
484
- }
485
- EOS
537
+ module TehUnknown3
538
+ class MyResult < ProtocolBuffers::Message
539
+ optional :int32, :field_1, 1
540
+ optional :int32, :field_2, 2
541
+ optional :int32, :field_4, 4
542
+ end
543
+ end
486
544
 
487
545
  res3 = TehUnknown3::MyResult.parse(serialized2)
488
546
  res3.field_1.should == 0xffff
@@ -492,30 +550,30 @@ describe ProtocolBuffers, "runtime" do
492
550
  end
493
551
 
494
552
  it "ignores and passes on unknown enum values" do
495
- ProtocolBuffers::Compiler.compile_and_load_string <<-EOS
496
- package tehUnknown;
497
- message MyResult {
498
- enum E {
499
- V1 = 1;
500
- V2 = 2;
501
- }
502
- optional E field_1 = 1;
503
- }
504
- EOS
553
+ module TehUnknown
554
+ class MyResult < ProtocolBuffers::Message
555
+ module E
556
+ include ProtocolBuffers::Enum
557
+ V1 = 1
558
+ V2 = 2
559
+ end
560
+ optional E, :field_1, 1
561
+ end
562
+ end
505
563
 
506
564
  res1 = TehUnknown::MyResult.new(:field_1 => TehUnknown::MyResult::E::V2)
507
565
  serialized = res1.to_s
508
566
 
509
567
  # remove field_2 to pretend we never knew about it
510
- ProtocolBuffers::Compiler.compile_and_load_string <<-EOS
511
- package tehUnknown2;
512
- message MyResult {
513
- enum E {
514
- V1 = 1;
515
- }
516
- optional E field_1 = 1;
517
- }
518
- EOS
568
+ module TehUnknown2
569
+ class MyResult < ProtocolBuffers::Message
570
+ module E
571
+ include ProtocolBuffers::Enum
572
+ V1 = 1
573
+ end
574
+ optional E, :field_1, 1
575
+ end
576
+ end
519
577
 
520
578
  res2 = nil
521
579
  proc do
@@ -528,32 +586,21 @@ describe ProtocolBuffers, "runtime" do
528
586
  serialized2 = res2.to_s
529
587
 
530
588
  # now we know about field_2 again
531
- ProtocolBuffers::Compiler.compile_and_load_string <<-EOS
532
- package tehUnknown3;
533
- message MyResult {
534
- enum E {
535
- V1 = 1;
536
- V2 = 2;
537
- }
538
- optional E field_1 = 1;
539
- }
540
- EOS
589
+ module TehUnknown3
590
+ class MyResult < ProtocolBuffers::Message
591
+ module E
592
+ include ProtocolBuffers::Enum
593
+ V1 = 1
594
+ V2 = 2
595
+ end
596
+ optional E, :field_1, 1
597
+ end
598
+ end
541
599
 
542
600
  res3 = TehUnknown3::MyResult.parse(serialized2)
543
601
  res3.field_1.should == 2
544
602
  end
545
603
 
546
- it "can compile and instantiate a message in a package with under_scores" do
547
- Object.send(:remove_const, :UnderScore) if defined?(UnderScore)
548
-
549
- ProtocolBuffers::Compiler.compile_and_load(
550
- File.join(File.dirname(__FILE__), "proto_files", "under_score_package.proto"))
551
-
552
- proc do
553
- under_test = UnderScore::UnderTest.new
554
- end.should_not raise_error()
555
- end
556
-
557
604
  describe "Message#valid?" do
558
605
  it "should validate sub-messages" do
559
606
  f = Featureful::A.new