ruby_protobuf 0.3.3 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (78) hide show
  1. data/History.txt +10 -2
  2. data/Manifest.txt +26 -12
  3. data/README.txt +6 -1
  4. data/Rakefile +51 -14
  5. data/TODO +15 -0
  6. data/VERSION +1 -0
  7. data/bin/rprotoc +11 -6
  8. data/lib/protobuf/common/exceptions.rb +11 -0
  9. data/lib/protobuf/common/util.rb +9 -0
  10. data/lib/protobuf/common/wire_type.rb +6 -6
  11. data/lib/protobuf/compiler/compiler.rb +16 -16
  12. data/lib/protobuf/compiler/nodes.rb +67 -65
  13. data/lib/protobuf/compiler/proto.y +31 -38
  14. data/lib/protobuf/compiler/proto_parser.rb +315 -294
  15. data/lib/protobuf/compiler/template/rpc_bin.erb +1 -1
  16. data/lib/protobuf/compiler/template/rpc_client.erb +1 -1
  17. data/lib/protobuf/compiler/template/rpc_service.erb +2 -2
  18. data/lib/protobuf/compiler/visitors.rb +45 -39
  19. data/lib/protobuf/descriptor/descriptor.proto +0 -0
  20. data/lib/protobuf/descriptor/descriptor.rb +11 -10
  21. data/lib/protobuf/descriptor/descriptor_builder.rb +6 -7
  22. data/lib/protobuf/descriptor/descriptor_proto.rb +51 -31
  23. data/lib/protobuf/descriptor/enum_descriptor.rb +5 -5
  24. data/lib/protobuf/descriptor/field_descriptor.rb +8 -9
  25. data/lib/protobuf/descriptor/file_descriptor.rb +0 -1
  26. data/lib/protobuf/message/decoder.rb +33 -35
  27. data/lib/protobuf/message/encoder.rb +23 -19
  28. data/lib/protobuf/message/enum.rb +43 -9
  29. data/lib/protobuf/message/field.rb +281 -193
  30. data/lib/protobuf/message/message.rb +166 -110
  31. data/lib/protobuf/message/protoable.rb +4 -3
  32. data/lib/protobuf/message/service.rb +1 -1
  33. data/lib/protobuf/rpc/client.rb +3 -3
  34. data/lib/protobuf/rpc/handler.rb +1 -1
  35. data/lib/protobuf/rpc/server.rb +8 -8
  36. data/lib/ruby_protobuf.rb +1 -1
  37. data/test/check_unbuild.rb +7 -7
  38. data/test/proto/addressbook.pb.rb +67 -0
  39. data/test/proto/addressbook.proto +2 -0
  40. data/test/proto/addressbook_base.pb.rb +59 -0
  41. data/test/proto/addressbook_base.proto +1 -1
  42. data/test/proto/addressbook_ext.pb.rb +21 -0
  43. data/test/proto/addressbook_ext.proto +2 -2
  44. data/test/{collision.rb → proto/collision.pb.rb} +0 -0
  45. data/test/{ext_collision.rb → proto/ext_collision.pb.rb} +1 -1
  46. data/test/{ext_range.rb → proto/ext_range.pb.rb} +4 -4
  47. data/test/proto/ext_range.proto +2 -2
  48. data/test/proto/float_default.proto +10 -0
  49. data/test/proto/lowercase.pb.rb +31 -0
  50. data/test/proto/lowercase.proto +9 -0
  51. data/test/{merge.rb → proto/merge.pb.rb} +2 -2
  52. data/test/proto/nested.pb.rb +31 -0
  53. data/test/proto/nested.proto +2 -0
  54. data/test/proto/optional_field.pb.rb +36 -0
  55. data/test/proto/optional_field.proto +12 -0
  56. data/test/proto/packed.pb.rb +23 -0
  57. data/test/proto/packed.proto +6 -0
  58. data/test/{types.rb → proto/types.pb.rb} +43 -1
  59. data/test/test_addressbook.rb +30 -17
  60. data/test/test_compiler.rb +79 -78
  61. data/test/test_descriptor.rb +12 -12
  62. data/test/test_enum_value.rb +41 -0
  63. data/test/test_extension.rb +10 -14
  64. data/test/test_lowercase.rb +11 -0
  65. data/test/test_message.rb +44 -41
  66. data/test/test_optional_field.rb +61 -38
  67. data/test/test_packed_field.rb +40 -0
  68. data/test/test_parse.rb +8 -8
  69. data/test/test_repeated_types.rb +29 -3
  70. data/test/test_serialize.rb +12 -12
  71. data/test/test_standard_message.rb +30 -30
  72. data/test/test_types.rb +95 -95
  73. metadata +69 -39
  74. data/test/addressbook.rb +0 -98
  75. data/test/addressbook_base.rb +0 -62
  76. data/test/addressbook_ext.rb +0 -12
  77. data/test/nested.rb +0 -25
  78. data/test/test_ruby_protobuf.rb +0 -1
@@ -1,60 +1,60 @@
1
1
  require 'test/unit'
2
- require 'test/optional_field'
2
+ require 'test/proto/optional_field.pb'
3
3
 
4
4
  class OptionalFieldTest < Test::Unit::TestCase
5
5
  def test_accessor
6
- message = Test::Optional_field::Message.new
6
+ message = Test::OptionalField::Message.new
7
7
 
8
8
  # default values
9
- assert !message.has_field?(:number)
10
- assert_equal 20, message.number
9
+ assert(!message.has_field?(:number))
10
+ assert_equal(20, message.number)
11
11
 
12
- assert !message.has_field?(:text)
13
- assert_equal 'default string', message.text
12
+ assert(!message.has_field?(:text))
13
+ assert_equal('default string', message.text)
14
14
 
15
- assert !message.has_field?(:enum)
16
- assert_equal 2, message.enum
15
+ assert(!message.has_field?(:enum))
16
+ assert_equal(2, message.enum)
17
17
 
18
- assert !message.has_field?(:signed)
18
+ assert(!message.has_field?(:signed))
19
19
  assert_equal(-100, message.signed)
20
20
 
21
21
  # assign values
22
22
  assert_nothing_raised { message.number = 100 }
23
- assert message.has_field?(:number)
24
- assert_equal 100, message.number
23
+ assert(message.has_field?(:number))
24
+ assert_equal(100, message.number)
25
25
 
26
26
  assert_nothing_raised { message.text = 'abc' }
27
- assert message.has_field?(:text)
28
- assert_equal 'abc', message.text
27
+ assert(message.has_field?(:text))
28
+ assert_equal('abc', message.text)
29
29
 
30
- assert_nothing_raised { message.enum = Test::Optional_field::Message::Enum::A }
31
- assert message.has_field?(:enum)
32
- assert_equal 1, message.enum
30
+ assert_nothing_raised { message.enum = Test::OptionalField::Message::Enum::A }
31
+ assert(message.has_field?(:enum))
32
+ assert_equal(1, message.enum)
33
33
 
34
34
  assert_nothing_raised { message.signed = -20 }
35
- assert message.has_field?(:signed)
35
+ assert(message.has_field?(:signed))
36
36
  assert_equal(-20, message.signed)
37
37
  end
38
38
 
39
39
  def test_serialize
40
- message1 = Test::Optional_field::Message.new
41
- message2 = Test::Optional_field::Message.new
40
+ message1 = Test::OptionalField::Message.new
41
+ message2 = Test::OptionalField::Message.new
42
42
 
43
43
  # all fields are empty
44
44
  serialized_string = message1.to_s
45
- assert serialized_string.empty?
45
+ assert(serialized_string.empty?)
46
46
  message2.parse_from_string(serialized_string)
47
- assert_equal message1.number, message2.number
48
- assert_equal message1.text, message2.text
49
- assert_equal message1.enum, message2.enum
50
- assert_equal message1.signed, message2.signed
51
- assert !message2.has_field?(:number)
52
- assert !message2.has_field?(:text)
53
- assert !message2.has_field?(:enum)
54
- assert !message2.has_field?(:signed)
47
+ assert_equal(message1.number, message2.number)
48
+ assert_equal(message1.text, message2.text)
49
+ assert_equal(message1.enum, message2.enum)
50
+ assert_equal(message1.signed, message2.signed)
51
+ assert(!message2.has_field?(:number))
52
+ assert(!message2.has_field?(:text))
53
+ assert(!message2.has_field?(:enum))
54
+ assert(!message2.has_field?(:signed))
55
55
 
56
56
  # assign the value whith is equal to default value
57
- message1 = Test::Optional_field::Message.new
57
+ message1 = Test::OptionalField::Message.new
58
58
  message1.number = message1.number
59
59
  message1.text = message1.text
60
60
  message1.enum = message1.enum
@@ -63,18 +63,41 @@ class OptionalFieldTest < Test::Unit::TestCase
63
63
  assert !serialized_string.empty?
64
64
 
65
65
  # set some fields
66
- message1 = Test::Optional_field::Message.new
66
+ message1 = Test::OptionalField::Message.new
67
67
  message1.number = 100
68
68
  message1.text = 'new text'
69
69
  serialized_string = message1.to_s
70
70
  message2.parse_from_string(serialized_string)
71
- assert_equal message1.number, message2.number
72
- assert_equal message1.text, message2.text
73
- assert_equal message1.enum, message2.enum
74
- assert_equal message1.signed, message2.signed
75
- assert message2.has_field?(:number)
76
- assert message2.has_field?(:text)
77
- assert !message2.has_field?(:enum)
78
- assert !message2.has_field?(:signed)
71
+ assert_equal(message1.number, message2.number)
72
+ assert_equal(message1.text, message2.text)
73
+ assert_equal(message1.enum, message2.enum)
74
+ assert_equal(message1.signed, message2.signed)
75
+ assert( message2.has_field?(:number))
76
+ assert( message2.has_field?(:text))
77
+ assert(!message2.has_field?(:enum))
78
+ assert(!message2.has_field?(:signed))
79
79
  end
80
+
81
+ def test_merge_optional_fields
82
+ src1 = Test::OptionalField::Message.new
83
+ src2 = Test::OptionalField::Message.new
84
+ dst = Test::OptionalField::Message.new
85
+
86
+ src1.number = 100
87
+ src1.text = 'old'
88
+ src2.text = 'new'
89
+ src2.signed = 20
90
+
91
+ serialized_string = src1.to_s + src2.to_s
92
+ dst.parse_from_string(serialized_string)
93
+
94
+ assert_equal(100, dst.number)
95
+ assert_equal('new', dst.text)
96
+ assert_equal(20, dst.signed)
97
+ assert dst.has_field?(:number)
98
+ assert dst.has_field?(:text)
99
+ assert !dst.has_field?(:enum)
100
+ assert dst.has_field?(:signed)
101
+ end
102
+
80
103
  end
@@ -0,0 +1,40 @@
1
+ require 'test/unit'
2
+ require 'test/proto/packed.pb'
3
+
4
+ class PackedFieldTest < Test::Unit::TestCase
5
+
6
+ def test_packed_field
7
+ values = [1, 1_000, 10_000, 1_000_000, 1_000_000_000]
8
+
9
+ # encode
10
+ message = Test::PackedField::Message.new
11
+ message.a = values
12
+ message.b = values
13
+
14
+ expected = ''
15
+ serialized_string = nil
16
+ assert_nothing_raised { serialized_string = message.serialize_to_string }
17
+
18
+ value = values.map {|v| Protobuf::Field::VarintField.encode(v) }.join
19
+ expected << Protobuf::Field::VarintField.encode((1 << 3) | 2) # tag=1, wire_type=2 (length-delimited)
20
+ expected << Protobuf::Field::VarintField.encode(value.size)
21
+ expected << value
22
+
23
+ value = values.pack('V*')
24
+ expected << Protobuf::Field::VarintField.encode((2 << 3) | 2) # tag=2, wire_type=2 (length-delimited)
25
+ expected << Protobuf::Field::VarintField.encode(value.size)
26
+ expected << value
27
+
28
+ assert_equal(expected, serialized_string)
29
+
30
+ # decode
31
+ message2 = Test::PackedField::Message.new
32
+ assert_nothing_raised { message2.parse_from_string(serialized_string) }
33
+
34
+ values.each_with_index do |val, n|
35
+ assert_equal(val, message.a[n])
36
+ assert_equal(val, message.b[n])
37
+ end
38
+ end
39
+
40
+ end
data/test/test_parse.rb CHANGED
@@ -1,15 +1,15 @@
1
1
  require 'test/unit'
2
- require 'test/addressbook'
2
+ require 'test/proto/addressbook.pb'
3
3
 
4
4
  class ParseTest < Test::Unit::TestCase
5
5
  def test_parse
6
6
  person = Tutorial::Person.new
7
- person.parse_from_file 'test/data/data.bin'
8
- assert_equal 1234, person.id
9
- assert_equal 'John Doe', person.name
10
- assert_equal 'jdoe@example.com', person.email
11
- assert_equal 1, person.phone.size
12
- assert_equal '555-4321', person.phone[0].number
13
- assert_equal Tutorial::Person::PhoneType::HOME, person.phone[0].type
7
+ person.parse_from_file('test/data/data.bin')
8
+ assert_equal(1234, person.id)
9
+ assert_equal('John Doe', person.name)
10
+ assert_equal('jdoe@example.com', person.email)
11
+ assert_equal(1, person.phone.size)
12
+ assert_equal('555-4321', person.phone[0].number)
13
+ assert_equal(Tutorial::Person::PhoneType::HOME, person.phone[0].type)
14
14
  end
15
15
  end
@@ -1,5 +1,5 @@
1
1
  require 'test/unit'
2
- require 'test/types'
2
+ require 'test/proto/types.pb'
3
3
 
4
4
  class RepeatedTypesTest < Test::Unit::TestCase
5
5
  def test_serialize
@@ -10,9 +10,9 @@ class RepeatedTypesTest < Test::Unit::TestCase
10
10
  # has 1 member
11
11
  types = Test::Types::RepeatedTypes.new
12
12
  types.type1 << 0.01
13
- types.type2 << 0.1
13
+ types.type2 << 0.1
14
14
  types.type3 << 1
15
- types.type4 << 10
15
+ types.type4 << 10
16
16
  types.type5 << 100
17
17
  types.type6 << 1000
18
18
  types.type7 << -1
@@ -103,4 +103,30 @@ class RepeatedTypesTest < Test::Unit::TestCase
103
103
  assert_equal(-2_000_000_000, types2.type14[1])
104
104
  assert_equal(-8_000_000_000_000_000_000, types2.type15[1])
105
105
  end
106
+
107
+ def test_repeated_types
108
+ types = Test::Types::RepeatedTypes.new
109
+ # types.type3 is a repeated int32 field.
110
+ assert(types.type3.empty?)
111
+ types.type3 << 0
112
+ types.type3 << 1
113
+ assert_equal(0, types.type3[0])
114
+ assert_equal(1, types.type3[1])
115
+ assert_equal(2, types.type3.size)
116
+
117
+ assert_raise(TypeError) do
118
+ types.type3 << 'string'
119
+ end
120
+
121
+ types.type3[1] = 10
122
+ assert_equal(0, types.type3[0])
123
+ assert_equal(10, types.type3[1])
124
+ assert_equal(2, types.type3.size)
125
+
126
+ types.type3.replace([10, 20, 30])
127
+ assert_equal(10, types.type3[0])
128
+ assert_equal(20, types.type3[1])
129
+ assert_equal(30, types.type3[2])
130
+ assert_equal(3, types.type3.size)
131
+ end
106
132
  end
@@ -1,6 +1,6 @@
1
1
  # encoding: utf-8
2
2
  require 'test/unit'
3
- require 'test/addressbook'
3
+ require 'test/proto/addressbook.pb'
4
4
 
5
5
  class SerializeTest < Test::Unit::TestCase
6
6
  def test_serialize
@@ -17,13 +17,13 @@ class SerializeTest < Test::Unit::TestCase
17
17
 
18
18
  # parse the serialized string
19
19
  person2 = Tutorial::Person.new
20
- person2.parse_from_string serialized_string
21
- assert_equal 1234, person2.id
22
- assert_equal 'John Doe', person2.name
23
- assert_equal 'jdoe@example.com', person2.email
24
- assert_equal 1, person2.phone.size
25
- assert_equal '555-4321', person2.phone[0].number
26
- assert_equal Tutorial::Person::PhoneType::HOME, person2.phone[0].type
20
+ person2.parse_from_string(serialized_string)
21
+ assert_equal(1234, person2.id)
22
+ assert_equal('John Doe', person2.name)
23
+ assert_equal('jdoe@example.com', person2.email)
24
+ assert_equal(1, person2.phone.size)
25
+ assert_equal('555-4321', person2.phone[0].number)
26
+ assert_equal(Tutorial::Person::PhoneType::HOME, person2.phone[0].type)
27
27
  end
28
28
 
29
29
  def test_serialize_utf8string
@@ -35,9 +35,9 @@ class SerializeTest < Test::Unit::TestCase
35
35
 
36
36
  # parse the serialized string
37
37
  person2 = Tutorial::Person.new
38
- person2.parse_from_string serialized_string
39
- assert_equal 1234, person2.id
40
- assert_equal '山田 太郎', person2.name
38
+ person2.parse_from_string(serialized_string)
39
+ assert_equal(1234, person2.id)
40
+ assert_equal('山田 太郎', person2.name)
41
41
  end
42
42
 
43
43
  def test_unknown_field
@@ -54,7 +54,7 @@ class SerializeTest < Test::Unit::TestCase
54
54
 
55
55
  # decode
56
56
  person2 = Tutorial::Person.new
57
- person2.parse_from_string serialized_string
57
+ person2.parse_from_string(serialized_string)
58
58
 
59
59
  assert_equal(person, person2)
60
60
  end
@@ -1,22 +1,23 @@
1
1
  require 'test/unit'
2
2
  require 'protobuf/message/message'
3
3
  require 'protobuf/message/enum'
4
- require 'test/addressbook'
4
+ require 'test/proto/addressbook.pb'
5
5
 
6
6
  class StandardMessageTest < Test::Unit::TestCase
7
7
  def test_initialized
8
8
  person = Tutorial::Person.new
9
- assert !person.initialized?
9
+ assert(!person.initialized?)
10
+ assert_raise(Protobuf::NotInitializedError) { person.to_s }
10
11
  person.name = 'name'
11
- assert !person.initialized?
12
+ assert(!person.initialized?)
12
13
  person.id = 12
13
- assert person.initialized?
14
+ assert( person.initialized?)
14
15
 
15
16
  # repeated field
16
17
  person.phone << Tutorial::Person::PhoneNumber.new
17
- assert !person.initialized?
18
+ assert(!person.initialized?)
18
19
  person.phone.last.number = '123-456'
19
- assert person.initialized?
20
+ assert( person.initialized?)
20
21
  end
21
22
 
22
23
  def test_clear
@@ -27,14 +28,14 @@ class StandardMessageTest < Test::Unit::TestCase
27
28
  person.phone << Tutorial::Person::PhoneNumber.new
28
29
  person.clear!
29
30
 
30
- assert_nil person.name
31
- assert_nil person.id
32
- assert_equal '', person.email
33
- assert_equal 0, person.phone.size
31
+ assert_nil(person.name)
32
+ assert_nil(person.id)
33
+ assert_equal('', person.email)
34
+ assert_equal(0, person.phone.size)
34
35
 
35
- assert !person.has_field?(:name)
36
- assert !person.has_field?(:id)
37
- assert !person.has_field?(:email)
36
+ assert(!person.has_field?(:name))
37
+ assert(!person.has_field?(:id))
38
+ assert(!person.has_field?(:email))
38
39
  end
39
40
 
40
41
  def test_dup
@@ -50,20 +51,20 @@ class StandardMessageTest < Test::Unit::TestCase
50
51
  person.phone.last.type = Tutorial::Person::PhoneType::WORK
51
52
 
52
53
  person2 = person.dup
53
- assert person == person2
54
- assert !person.eql?(person2)
55
- assert_equal person.name, person2.name
56
- assert_equal person.id, person2.id
57
- assert_equal person.email, person2.email
58
- assert_equal person.phone.size, person2.phone.size
59
- assert person.phone.first == person2.phone.first
60
- assert !person.phone.first.eql?(person2.phone.first)
61
- assert_equal person.phone.first.number, person2.phone.first.number
62
- assert_equal person.phone.first.type, person2.phone.first.type
63
- assert person.phone.last == person2.phone.last
64
- assert !person.phone.last.eql?(person2.phone.last)
65
- assert_equal person.phone.last.number, person2.phone.last.number
66
- assert_equal person.phone.last.type, person2.phone.last.type
54
+ assert(person == person2)
55
+ assert(!person.eql?(person2))
56
+ assert_equal(person.name, person2.name)
57
+ assert_equal(person.id, person2.id)
58
+ assert_equal(person.email, person2.email)
59
+ assert_equal(person.phone.size, person2.phone.size)
60
+ assert(person.phone.first == person2.phone.first)
61
+ assert(!person.phone.first.eql?(person2.phone.first))
62
+ assert_equal(person.phone.first.number, person2.phone.first.number)
63
+ assert_equal(person.phone.first.type, person2.phone.first.type)
64
+ assert(person.phone.last == person2.phone.last)
65
+ assert(!person.phone.last.eql?(person2.phone.last))
66
+ assert_equal(person.phone.last.number, person2.phone.last.number)
67
+ assert_equal(person.phone.last.type, person2.phone.last.type)
67
68
  end
68
69
 
69
70
  def test_inspect
@@ -78,7 +79,7 @@ class StandardMessageTest < Test::Unit::TestCase
78
79
  person.phone.last.number = '456-123'
79
80
  person.phone.last.type = Tutorial::Person::PhoneType::WORK
80
81
 
81
- assert_equal <<-eos, person.inspect
82
+ assert_equal <<-EOS, person.inspect
82
83
  name: "name"
83
84
  id: 1234
84
85
  email: "abc@cde.fgh"
@@ -90,7 +91,6 @@ phone {
90
91
  number: "456-123"
91
92
  type: WORK
92
93
  }
93
- eos
94
+ EOS
94
95
  end
95
96
  end
96
-