ruby_protobuf 0.0.1

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.
@@ -0,0 +1,39 @@
1
+ require 'test/unit'
2
+ require 'protobuf/compiler'
3
+
4
+ class CompilerTest < Test::Unit::TestCase
5
+ def test_compile
6
+ assert_equal <<-eos.strip, Protobuf::Compiler.compile('test/addressbook.proto').strip
7
+ require 'protobuf/message'
8
+ require 'protobuf/enum'
9
+ require 'protobuf/service'
10
+ require 'protobuf/extend'
11
+
12
+ module Tutorial
13
+
14
+ class Person < Protobuf::Message
15
+ required :string, :name, 1
16
+ required :int32, :id, 2
17
+ optional :string, :email, 3
18
+
19
+ class PhoneType < Protobuf::Enum
20
+ MOBILE = 0
21
+ HOME = 1
22
+ WORK = 2
23
+ end
24
+
25
+ class PhoneNumber < Protobuf::Message
26
+ required :string, :number, 1
27
+ optional :PhoneType, :type, 2, {:default => :HOME}
28
+ end
29
+
30
+ repeated :PhoneNumber, :phone, 4
31
+ end
32
+
33
+ class AddressBook < Protobuf::Message
34
+ repeated :Person, :person, 1
35
+ end
36
+ end
37
+ eos
38
+ end
39
+ end
@@ -0,0 +1,20 @@
1
+ require 'test/unit'
2
+ require 'protobuf/message'
3
+ require 'test/addressbook'
4
+
5
+ class MessageTest < Test::Unit::TestCase
6
+ def test_bracketed_access
7
+ person = Tutorial::Person.new
8
+ name_tag = 1
9
+ person[name_tag] = 'Ichiro'
10
+ assert_equal 'Ichiro', person.name
11
+ assert_equal 'Ichiro', person[name_tag]
12
+
13
+ person[:id] = 100
14
+ assert_equal 100, person.id
15
+ person['id'] = 200
16
+ assert_equal 200, person.id
17
+ assert_equal 200, person[:id]
18
+ assert_equal 200, person['id']
19
+ end
20
+ end
@@ -0,0 +1,15 @@
1
+ require 'test/unit'
2
+ require 'test/addressbook'
3
+
4
+ class ParseTest < Test::Unit::TestCase
5
+ def test_parse
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
14
+ end
15
+ end
@@ -0,0 +1 @@
1
+
@@ -0,0 +1,27 @@
1
+ require 'test/unit'
2
+ require 'test/addressbook'
3
+
4
+ class SerializeTest < Test::Unit::TestCase
5
+ def test_serialize
6
+ # serialize to string
7
+ person = Tutorial::Person.new
8
+ person.id = 1234
9
+ person.name = 'John Doe'
10
+ person.email = 'jdoe@example.com'
11
+ phone = Tutorial::Person::PhoneNumber.new
12
+ phone.number = '555-4321'
13
+ phone.type = Tutorial::Person::PhoneType::HOME
14
+ person.phone << phone
15
+ serialized_string = person.serialize_to_string
16
+
17
+ # parse the serialized string
18
+ person2 = Tutorial::Person.new
19
+ person2.parse_from_string serialized_string
20
+ assert_equal 1234, person2.id
21
+ assert_equal 'John Doe', person2.name
22
+ assert_equal 'jdoe@example.com', person2.email
23
+ assert_equal 1, person2.phone.size
24
+ assert_equal '555-4321', person2.phone[0].number
25
+ assert_equal Tutorial::Person::PhoneType::HOME, person2.phone[0].type
26
+ end
27
+ end
@@ -0,0 +1,180 @@
1
+ require 'test/unit'
2
+ require 'test/types'
3
+
4
+ class TypesTest < Test::Unit::TestCase
5
+ def test_serialize
6
+ types = Test::Types::TestTypes.new
7
+ types.type1 = 0.01
8
+ types.type2 = 0.1
9
+ types.type3 = 1
10
+ types.type4 = 10
11
+ types.type5 = 100
12
+ types.type6 = 1000
13
+ types.type7 = -1
14
+ types.type8 = -10
15
+ types.type9 = 10000
16
+ types.type10 = 100000
17
+ types.type11 = false
18
+ types.type12 = 'hello all types'
19
+ #types.type13 =
20
+ serialized_string = types.serialize_to_string
21
+
22
+ types2 = Test::Types::TestTypes.new
23
+ types2.parse_from_string serialized_string
24
+ assert_in_delta 0.01, types2.type1, 0.00001
25
+ assert_in_delta 0.1, types2.type2, 0.00001
26
+ assert_equal 1, types2.type3
27
+ assert_equal 10, types2.type4
28
+ assert_equal 100, types2.type5
29
+ assert_equal 1000, types2.type6
30
+ assert_equal(-1, types2.type7)
31
+ assert_equal(-10, types2.type8)
32
+ assert_equal 10000, types2.type9
33
+ assert_equal 100000, types2.type10
34
+ assert_equal false, types2.type11
35
+ assert_equal 'hello all types', types2.type12
36
+ #types2.type13
37
+ end
38
+
39
+ def test_parse
40
+ types = Test::Types::TestTypes.new
41
+ types.parse_from_file 'test/data/types.bin'
42
+ assert_in_delta 0.01, types.type1, 0.00001
43
+ assert_in_delta 0.1, types.type2, 0.00001
44
+ assert_equal 1, types.type3
45
+ assert_equal 10, types.type4
46
+ assert_equal 100, types.type5
47
+ assert_equal 1000, types.type6
48
+ assert_equal(-1, types.type7)
49
+ assert_equal(-10, types.type8)
50
+ assert_equal 10000, types.type9
51
+ assert_equal 100000, types.type10
52
+ assert_equal false, types.type11
53
+ assert_equal 'hello all types', types.type12
54
+ # types.type13
55
+ end
56
+
57
+ def test_double
58
+ # double fixed 64-bit
59
+ types = Test::Types::TestTypes.new
60
+ assert_nothing_raised do types.type1 = 1 end
61
+ assert_nothing_raised do types.type1 = 1.0 end
62
+ assert_raise TypeError do types.type1 = '' end
63
+ assert_nothing_raised do types.type1 = Protobuf::Field::DoubleField.max end
64
+ assert_raise RangeError do types.type1 = Protobuf::Field::DoubleField.max * 2 end
65
+ assert_nothing_raised do types.type1 = Protobuf::Field::DoubleField.min end
66
+ assert_raise RangeError do types.type1 = Protobuf::Field::DoubleField.min * 2 end
67
+ end
68
+
69
+ def test_float
70
+ # float fixed 32-bit
71
+ types = Test::Types::TestTypes.new
72
+ assert_nothing_raised do types.type2 = 1 end
73
+ assert_nothing_raised do types.type2 = 1.0 end
74
+ assert_raise TypeError do types.type2 = '' end
75
+ assert_nothing_raised do types.type2 = Protobuf::Field::FloatField.max end
76
+ assert_raise RangeError do types.type2 = Protobuf::Field::FloatField.max * 2 end
77
+ assert_nothing_raised do types.type2 = Protobuf::Field::FloatField.min end
78
+ assert_raise RangeError do types.type2 = Protobuf::Field::FloatField.min * 2 end
79
+ end
80
+
81
+ def test_int32
82
+ types = Test::Types::TestTypes.new
83
+ assert_nothing_raised do types.type3 = 1 end
84
+ assert_nothing_raised do types.type3 = -1 end
85
+ assert_raise TypeError do types.type3 = 1.0 end
86
+ assert_raise TypeError do types.type3 = '' end
87
+ end
88
+
89
+ def test_int64
90
+ types = Test::Types::TestTypes.new
91
+ assert_nothing_raised do types.type4 = 1 end
92
+ assert_nothing_raised do types.type4 = -1 end
93
+ assert_raise TypeError do types.type4 = 1.0 end
94
+ assert_raise TypeError do types.type4 = '' end
95
+ end
96
+
97
+ def test_uint32
98
+ types = Test::Types::TestTypes.new
99
+ assert_nothing_raised do types.type5 = 1 end
100
+ assert_raise RangeError do types.type5 = -1 end
101
+ assert_raise TypeError do types.type5 = 1.0 end
102
+ assert_raise TypeError do types.type5 = '' end
103
+ end
104
+
105
+ def test_uint64
106
+ types = Test::Types::TestTypes.new
107
+ assert_nothing_raised do types.type6 = 1 end
108
+ assert_raise RangeError do types.type6 = -1 end
109
+ assert_raise TypeError do types.type6 = 1.0 end
110
+ assert_raise TypeError do types.type6 = '' end
111
+ end
112
+
113
+ def test_sint32
114
+ types = Test::Types::TestTypes.new
115
+ assert_nothing_raised do types.type7 = 1 end
116
+ assert_nothing_raised do types.type7 = -1 end
117
+ assert_raise TypeError do types.type7 = 1.0 end
118
+ assert_raise TypeError do types.type7 = '' end
119
+ end
120
+
121
+ def test_sint64
122
+ types = Test::Types::TestTypes.new
123
+ assert_nothing_raised do types.type8 = 1 end
124
+ assert_nothing_raised do types.type8 = -1 end
125
+ assert_raise TypeError do types.type8 = 1.0 end
126
+ assert_raise TypeError do types.type8 = '' end
127
+ end
128
+
129
+ def test_fixed32
130
+ types = Test::Types::TestTypes.new
131
+ assert_nothing_raised do types.type9 = 1 end
132
+ assert_raise TypeError do types.type9 = 1.0 end
133
+ assert_raise TypeError do types.type9 = '' end
134
+ assert_nothing_raised do types.type9 = Protobuf::Field::Fixed32Field.max end
135
+ assert_raise RangeError do types.type9 = Protobuf::Field::Fixed32Field.max + 1 end
136
+ assert_nothing_raised do types.type9 = Protobuf::Field::Fixed32Field.min end
137
+ assert_raise RangeError do types.type9 = Protobuf::Field::Fixed32Field.min - 1 end
138
+ end
139
+
140
+ def test_fixed64
141
+ types = Test::Types::TestTypes.new
142
+ assert_nothing_raised do types.type10 = 1 end
143
+ assert_raise TypeError do types.type10 = 1.0 end
144
+ assert_raise TypeError do types.type10 = '' end
145
+ assert_nothing_raised do types.type10 = Protobuf::Field::Fixed64Field.max end
146
+ assert_raise RangeError do types.type10 = Protobuf::Field::Fixed64Field.max + 1 end
147
+ assert_nothing_raised do types.type10 = Protobuf::Field::Fixed64Field.min end
148
+ assert_raise RangeError do types.type10 = Protobuf::Field::Fixed64Field.min - 1 end
149
+ end
150
+
151
+ def test_bool
152
+ types = Test::Types::TestTypes.new
153
+ assert_nothing_raised do types.type11 = true end
154
+ assert_nothing_raised do types.type11 = false end
155
+ assert_raise TypeError do types.type11 = nil end
156
+ assert_raise TypeError do types.type11 = 0 end
157
+ assert_raise TypeError do types.type11 = '' end
158
+ end
159
+
160
+ def test_string
161
+ types = Test::Types::TestTypes.new
162
+ assert_nothing_raised do types.type12 = '' end
163
+ assert_nothing_raised do types.type12 = 'hello' end
164
+ assert_raise TypeError do types.type12 = nil end
165
+ assert_raise TypeError do types.type12 = 0 end
166
+ assert_raise TypeError do types.type12 = true end
167
+ end
168
+
169
+ def test_bytes
170
+ types = Test::Types::TestTypes.new
171
+ # TODO
172
+ =begin
173
+ assert_nothing_raised do types.type13 = '' end
174
+ assert_nothing_raised do types.type13 = 'hello' end
175
+ assert_raise TypeError do types.type13 = nil end
176
+ assert_raise TypeError do types.type13 = 0 end
177
+ assert_raise TypeError do types.type13 = true end
178
+ =end
179
+ end
180
+ end
@@ -0,0 +1,17 @@
1
+ package test.types;
2
+
3
+ message TestTypes {
4
+ required double type1 = 1;
5
+ required float type2 = 2;
6
+ required int32 type3 = 3;
7
+ required int64 type4 = 4;
8
+ required uint32 type5 = 5;
9
+ required uint64 type6 = 6;
10
+ required sint32 type7 = 7;
11
+ required sint64 type8 = 8;
12
+ required fixed32 type9 = 9;
13
+ required fixed64 type10 = 10;
14
+ required bool type11 = 11;
15
+ required string type12 = 12;
16
+ required bytes type13 = 13;
17
+ }
@@ -0,0 +1,22 @@
1
+ require 'protobuf/message'
2
+
3
+ module Test
4
+ module Types
5
+
6
+ class TestTypes < Protobuf::Message
7
+ required :double, :type1, 1
8
+ required :float, :type2, 2
9
+ required :int32, :type3, 3
10
+ required :int64, :type4, 4
11
+ required :uint32, :type5, 5
12
+ required :uint64, :type6, 6
13
+ required :sint32, :type7, 7
14
+ required :sint64, :type8, 8
15
+ required :fixed32, :type9, 9
16
+ required :fixed64, :type10, 10
17
+ required :bool, :type11, 11
18
+ required :string, :type12, 12
19
+ required :bytes, :type13, 13
20
+ end
21
+ end
22
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby_protobuf
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - ANDO Yasushi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-07-31 00:00:00 +09:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hoe
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.5.0
23
+ version:
24
+ description: "== DESCRIPTION: Protocol Buffers for Ruby. == FEATURES/PROBLEMS: * Compile .proto file to ruby script * Parse the binary wire format for protocol buffer * Serialize data to the binary wire format for protocol buffer"
25
+ email: andyjpn@gmail.com
26
+ executables:
27
+ - rprotoc
28
+ - ruby_protobuf
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - History.txt
33
+ - Manifest.txt
34
+ - README.txt
35
+ files:
36
+ - History.txt
37
+ - Manifest.txt
38
+ - README.txt
39
+ - Rakefile
40
+ - bin/rprotoc
41
+ - bin/ruby_protobuf
42
+ - lib/protobuf/compiler.rb
43
+ - lib/protobuf/decoder.rb
44
+ - lib/protobuf/encoder.rb
45
+ - lib/protobuf/enum.rb
46
+ - lib/protobuf/extend.rb
47
+ - lib/protobuf/field.rb
48
+ - lib/protobuf/message.rb
49
+ - lib/protobuf/parser.y
50
+ - lib/protobuf/service.rb
51
+ - lib/protobuf/wire_type.rb
52
+ - lib/ruby_protobuf.rb
53
+ - test/addressbook.proto
54
+ - test/addressbook.rb
55
+ - test/data/data.bin
56
+ - test/data/data_source.py
57
+ - test/data/types.bin
58
+ - test/data/types_source.py
59
+ - test/test_addressbook.rb
60
+ - test/test_compiler.rb
61
+ - test/test_message.rb
62
+ - test/test_parse.rb
63
+ - test/test_ruby_protobuf.rb
64
+ - test/test_serialize.rb
65
+ - test/test_types.rb
66
+ - test/types.proto
67
+ - test/types.rb
68
+ has_rdoc: true
69
+ homepage:
70
+ post_install_message:
71
+ rdoc_options:
72
+ - --main
73
+ - README.txt
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: "0"
81
+ version:
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: "0"
87
+ version:
88
+ requirements: []
89
+
90
+ rubyforge_project: ruby-protobuf
91
+ rubygems_version: 1.1.1
92
+ signing_key:
93
+ specification_version: 2
94
+ summary: Protocol Buffers for Ruby
95
+ test_files:
96
+ - test/test_ruby_protobuf.rb
97
+ - test/test_addressbook.rb
98
+ - test/test_message.rb
99
+ - test/test_parse.rb
100
+ - test/test_types.rb
101
+ - test/test_serialize.rb
102
+ - test/test_compiler.rb