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.
- data/History.txt +10 -2
- data/Manifest.txt +26 -12
- data/README.txt +6 -1
- data/Rakefile +51 -14
- data/TODO +15 -0
- data/VERSION +1 -0
- data/bin/rprotoc +11 -6
- data/lib/protobuf/common/exceptions.rb +11 -0
- data/lib/protobuf/common/util.rb +9 -0
- data/lib/protobuf/common/wire_type.rb +6 -6
- data/lib/protobuf/compiler/compiler.rb +16 -16
- data/lib/protobuf/compiler/nodes.rb +67 -65
- data/lib/protobuf/compiler/proto.y +31 -38
- data/lib/protobuf/compiler/proto_parser.rb +315 -294
- data/lib/protobuf/compiler/template/rpc_bin.erb +1 -1
- data/lib/protobuf/compiler/template/rpc_client.erb +1 -1
- data/lib/protobuf/compiler/template/rpc_service.erb +2 -2
- data/lib/protobuf/compiler/visitors.rb +45 -39
- data/lib/protobuf/descriptor/descriptor.proto +0 -0
- data/lib/protobuf/descriptor/descriptor.rb +11 -10
- data/lib/protobuf/descriptor/descriptor_builder.rb +6 -7
- data/lib/protobuf/descriptor/descriptor_proto.rb +51 -31
- data/lib/protobuf/descriptor/enum_descriptor.rb +5 -5
- data/lib/protobuf/descriptor/field_descriptor.rb +8 -9
- data/lib/protobuf/descriptor/file_descriptor.rb +0 -1
- data/lib/protobuf/message/decoder.rb +33 -35
- data/lib/protobuf/message/encoder.rb +23 -19
- data/lib/protobuf/message/enum.rb +43 -9
- data/lib/protobuf/message/field.rb +281 -193
- data/lib/protobuf/message/message.rb +166 -110
- data/lib/protobuf/message/protoable.rb +4 -3
- data/lib/protobuf/message/service.rb +1 -1
- data/lib/protobuf/rpc/client.rb +3 -3
- data/lib/protobuf/rpc/handler.rb +1 -1
- data/lib/protobuf/rpc/server.rb +8 -8
- data/lib/ruby_protobuf.rb +1 -1
- data/test/check_unbuild.rb +7 -7
- data/test/proto/addressbook.pb.rb +67 -0
- data/test/proto/addressbook.proto +2 -0
- data/test/proto/addressbook_base.pb.rb +59 -0
- data/test/proto/addressbook_base.proto +1 -1
- data/test/proto/addressbook_ext.pb.rb +21 -0
- data/test/proto/addressbook_ext.proto +2 -2
- data/test/{collision.rb → proto/collision.pb.rb} +0 -0
- data/test/{ext_collision.rb → proto/ext_collision.pb.rb} +1 -1
- data/test/{ext_range.rb → proto/ext_range.pb.rb} +4 -4
- data/test/proto/ext_range.proto +2 -2
- data/test/proto/float_default.proto +10 -0
- data/test/proto/lowercase.pb.rb +31 -0
- data/test/proto/lowercase.proto +9 -0
- data/test/{merge.rb → proto/merge.pb.rb} +2 -2
- data/test/proto/nested.pb.rb +31 -0
- data/test/proto/nested.proto +2 -0
- data/test/proto/optional_field.pb.rb +36 -0
- data/test/proto/optional_field.proto +12 -0
- data/test/proto/packed.pb.rb +23 -0
- data/test/proto/packed.proto +6 -0
- data/test/{types.rb → proto/types.pb.rb} +43 -1
- data/test/test_addressbook.rb +30 -17
- data/test/test_compiler.rb +79 -78
- data/test/test_descriptor.rb +12 -12
- data/test/test_enum_value.rb +41 -0
- data/test/test_extension.rb +10 -14
- data/test/test_lowercase.rb +11 -0
- data/test/test_message.rb +44 -41
- data/test/test_optional_field.rb +61 -38
- data/test/test_packed_field.rb +40 -0
- data/test/test_parse.rb +8 -8
- data/test/test_repeated_types.rb +29 -3
- data/test/test_serialize.rb +12 -12
- data/test/test_standard_message.rb +30 -30
- data/test/test_types.rb +95 -95
- metadata +69 -39
- data/test/addressbook.rb +0 -98
- data/test/addressbook_base.rb +0 -62
- data/test/addressbook_ext.rb +0 -12
- data/test/nested.rb +0 -25
- data/test/test_ruby_protobuf.rb +0 -1
data/test/test_optional_field.rb
CHANGED
@@ -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::
|
6
|
+
message = Test::OptionalField::Message.new
|
7
7
|
|
8
8
|
# default values
|
9
|
-
assert
|
10
|
-
assert_equal
|
9
|
+
assert(!message.has_field?(:number))
|
10
|
+
assert_equal(20, message.number)
|
11
11
|
|
12
|
-
assert
|
13
|
-
assert_equal
|
12
|
+
assert(!message.has_field?(:text))
|
13
|
+
assert_equal('default string', message.text)
|
14
14
|
|
15
|
-
assert
|
16
|
-
assert_equal
|
15
|
+
assert(!message.has_field?(:enum))
|
16
|
+
assert_equal(2, message.enum)
|
17
17
|
|
18
|
-
assert
|
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
|
24
|
-
assert_equal
|
23
|
+
assert(message.has_field?(:number))
|
24
|
+
assert_equal(100, message.number)
|
25
25
|
|
26
26
|
assert_nothing_raised { message.text = 'abc' }
|
27
|
-
assert
|
28
|
-
assert_equal
|
27
|
+
assert(message.has_field?(:text))
|
28
|
+
assert_equal('abc', message.text)
|
29
29
|
|
30
|
-
assert_nothing_raised { message.enum = Test::
|
31
|
-
assert
|
32
|
-
assert_equal
|
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
|
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::
|
41
|
-
message2 = Test::
|
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
|
45
|
+
assert(serialized_string.empty?)
|
46
46
|
message2.parse_from_string(serialized_string)
|
47
|
-
assert_equal
|
48
|
-
assert_equal
|
49
|
-
assert_equal
|
50
|
-
assert_equal
|
51
|
-
assert
|
52
|
-
assert
|
53
|
-
assert
|
54
|
-
assert
|
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::
|
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::
|
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
|
72
|
-
assert_equal
|
73
|
-
assert_equal
|
74
|
-
assert_equal
|
75
|
-
assert
|
76
|
-
assert
|
77
|
-
assert
|
78
|
-
assert
|
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
|
8
|
-
assert_equal
|
9
|
-
assert_equal
|
10
|
-
assert_equal
|
11
|
-
assert_equal
|
12
|
-
assert_equal
|
13
|
-
assert_equal
|
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
|
data/test/test_repeated_types.rb
CHANGED
@@ -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
|
data/test/test_serialize.rb
CHANGED
@@ -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
|
21
|
-
assert_equal
|
22
|
-
assert_equal
|
23
|
-
assert_equal
|
24
|
-
assert_equal
|
25
|
-
assert_equal
|
26
|
-
assert_equal
|
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
|
39
|
-
assert_equal
|
40
|
-
assert_equal
|
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
|
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
|
9
|
+
assert(!person.initialized?)
|
10
|
+
assert_raise(Protobuf::NotInitializedError) { person.to_s }
|
10
11
|
person.name = 'name'
|
11
|
-
assert
|
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
|
18
|
+
assert(!person.initialized?)
|
18
19
|
person.phone.last.number = '123-456'
|
19
|
-
assert
|
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
|
31
|
-
assert_nil
|
32
|
-
assert_equal
|
33
|
-
assert_equal
|
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
|
36
|
-
assert
|
37
|
-
assert
|
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
|
54
|
-
assert
|
55
|
-
assert_equal
|
56
|
-
assert_equal
|
57
|
-
assert_equal
|
58
|
-
assert_equal
|
59
|
-
assert
|
60
|
-
assert
|
61
|
-
assert_equal
|
62
|
-
assert_equal
|
63
|
-
assert
|
64
|
-
assert
|
65
|
-
assert_equal
|
66
|
-
assert_equal
|
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 <<-
|
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
|
-
|
94
|
+
EOS
|
94
95
|
end
|
95
96
|
end
|
96
|
-
|