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_compiler.rb
CHANGED
@@ -1,11 +1,10 @@
|
|
1
1
|
require 'test/unit'
|
2
|
-
#require 'protobuf/compiler/compiler_old'
|
3
2
|
require 'protobuf/compiler/compiler'
|
4
3
|
|
5
4
|
class CompilerTest < Test::Unit::TestCase
|
6
5
|
# Issue 12: Parse error on float default value
|
7
6
|
def test_compile_float_default
|
8
|
-
assert_compile_proto
|
7
|
+
assert_compile_proto(<<-EOS, 'test/proto/float_default.proto')
|
9
8
|
### Generated by rprotoc. DO NOT EDIT!
|
10
9
|
### <proto file: test/proto/float_default.proto>
|
11
10
|
# message M {
|
@@ -34,11 +33,11 @@ class M < ::Protobuf::Message
|
|
34
33
|
optional :float, :k, 6, :default => 23
|
35
34
|
optional :float, :l, 7, :default => 23.42
|
36
35
|
end
|
37
|
-
|
36
|
+
EOS
|
38
37
|
end
|
39
38
|
|
40
39
|
def test_create_message
|
41
|
-
assert_compile_proto
|
40
|
+
assert_compile_proto(<<-EOS, 'test/proto/addressbook.proto')
|
42
41
|
### Generated by rprotoc. DO NOT EDIT!
|
43
42
|
### <proto file: test/proto/addressbook.proto>
|
44
43
|
# package tutorial;
|
@@ -65,9 +64,11 @@ end
|
|
65
64
|
# extensions 100 to 200;
|
66
65
|
# }
|
67
66
|
#
|
67
|
+
# /*
|
68
68
|
# extend Person {
|
69
69
|
# optional int32 age = 100;
|
70
70
|
# }
|
71
|
+
# */
|
71
72
|
#
|
72
73
|
# message AddressBook {
|
73
74
|
# repeated Person person = 1;
|
@@ -87,9 +88,9 @@ module Tutorial
|
|
87
88
|
|
88
89
|
class PhoneType < ::Protobuf::Enum
|
89
90
|
defined_in __FILE__
|
90
|
-
MOBILE
|
91
|
-
HOME
|
92
|
-
WORK
|
91
|
+
define :MOBILE, 0
|
92
|
+
define :HOME, 1
|
93
|
+
define :WORK, 2
|
93
94
|
end
|
94
95
|
|
95
96
|
class PhoneNumber < ::Protobuf::Message
|
@@ -104,23 +105,20 @@ module Tutorial
|
|
104
105
|
extensions 100..200
|
105
106
|
end
|
106
107
|
|
107
|
-
class Person < ::Protobuf::Message
|
108
|
-
defined_in __FILE__
|
109
|
-
optional :int32, :age, 100, :extension => true
|
110
|
-
end
|
111
|
-
|
112
108
|
class AddressBook < ::Protobuf::Message
|
113
109
|
defined_in __FILE__
|
114
110
|
repeated :Person, :person, 1
|
115
111
|
end
|
116
112
|
end
|
117
|
-
|
113
|
+
EOS
|
118
114
|
end
|
119
115
|
|
120
116
|
def test_create_nested_message
|
121
|
-
assert_compile_proto
|
117
|
+
assert_compile_proto(<<-EOS, 'test/proto/nested.proto')
|
122
118
|
### Generated by rprotoc. DO NOT EDIT!
|
123
119
|
### <proto file: test/proto/nested.proto>
|
120
|
+
# package test.nested;
|
121
|
+
#
|
124
122
|
# message Foo {
|
125
123
|
# message Bar {
|
126
124
|
# }
|
@@ -128,37 +126,40 @@ end
|
|
128
126
|
# message Baaz {
|
129
127
|
# optional Foo.Bar x = 1;
|
130
128
|
# }
|
129
|
+
|
131
130
|
require 'protobuf/message/message'
|
132
131
|
require 'protobuf/message/enum'
|
133
132
|
require 'protobuf/message/service'
|
134
133
|
require 'protobuf/message/extend'
|
135
134
|
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
135
|
+
module Test
|
136
|
+
module Nested
|
137
|
+
class Foo < ::Protobuf::Message
|
138
|
+
defined_in __FILE__
|
139
|
+
class Bar < ::Protobuf::Message
|
140
|
+
defined_in __FILE__
|
141
|
+
end
|
142
|
+
end
|
143
|
+
class Baaz < ::Protobuf::Message
|
144
|
+
defined_in __FILE__
|
145
|
+
optional :'Foo::Bar', :x, 1
|
146
|
+
end
|
140
147
|
end
|
141
148
|
end
|
142
|
-
|
143
|
-
class Baaz < ::Protobuf::Message
|
144
|
-
defined_in __FILE__
|
145
|
-
optional :'Foo::Bar', :x, 1
|
146
|
-
end
|
147
|
-
eos
|
149
|
+
EOS
|
148
150
|
end
|
149
151
|
|
150
152
|
def test_nested_message
|
151
|
-
Protobuf::Visitor::CreateMessageVisitor.instance_eval {remove_const :Baaz}
|
152
153
|
file_contents = Protobuf::Compiler.new.create_message('test/proto/nested.proto', '.', '.', false)
|
153
154
|
assert_nothing_raised {Object.class_eval file_contents}
|
154
|
-
assert_raise(TypeError) {Baaz.new.x = 1}
|
155
|
-
assert_nothing_raised {Baaz.new.x = Foo::Bar.new}
|
155
|
+
assert_raise(TypeError) {Test::Nested::Baaz.new.x = 1}
|
156
|
+
assert_nothing_raised {Test::Nested::Baaz.new.x = Test::Nested::Foo::Bar.new}
|
156
157
|
end
|
157
158
|
|
158
159
|
def test_create_rpc
|
159
160
|
file_contents = Protobuf::Compiler.new.create_rpc('test/proto/rpc.proto', '.', 'test/proto', false)
|
160
161
|
|
161
|
-
assert_source
|
162
|
+
assert_source(<<-EOS, file_contents['test/proto/address_book_service.rb'])
|
162
163
|
require 'protobuf/rpc/server'
|
163
164
|
require 'protobuf/rpc/handler'
|
164
165
|
require 'test/proto/rpc.pb'
|
@@ -189,16 +190,16 @@ class Tutorial::AddressBookService < Protobuf::Rpc::Server
|
|
189
190
|
}
|
190
191
|
end
|
191
192
|
end
|
192
|
-
|
193
|
+
EOS
|
193
194
|
|
194
|
-
assert_source
|
195
|
+
assert_source(<<-EOS, file_contents['test/proto/start_address_book_service'])
|
195
196
|
#!/usr/bin/env ruby
|
196
197
|
require 'address_book_service'
|
197
198
|
|
198
|
-
Tutorial::AddressBookService.new(:
|
199
|
-
|
199
|
+
Tutorial::AddressBookService.new(:Port => 9999).start
|
200
|
+
EOS
|
200
201
|
|
201
|
-
assert_source
|
202
|
+
assert_source(<<-EOS, file_contents['test/proto/client_search.rb'])
|
202
203
|
#!/usr/bin/env ruby
|
203
204
|
require 'protobuf/rpc/client'
|
204
205
|
require 'test/proto/rpc.pb'
|
@@ -206,7 +207,7 @@ require 'test/proto/rpc.pb'
|
|
206
207
|
# build request
|
207
208
|
request = Tutorial::Person.new
|
208
209
|
# TODO: setup a request
|
209
|
-
raise StandardError
|
210
|
+
raise StandardError, 'setup a request'
|
210
211
|
|
211
212
|
# create blunk response
|
212
213
|
response = Tutorial::AddressBook.new
|
@@ -216,9 +217,9 @@ Protobuf::Rpc::Client.new('localhost', 9999).call :search, request, response
|
|
216
217
|
|
217
218
|
# show response
|
218
219
|
puts response
|
219
|
-
|
220
|
+
EOS
|
220
221
|
|
221
|
-
assert_source
|
222
|
+
assert_source(<<-EOS, file_contents['test/proto/client_add.rb'])
|
222
223
|
#!/usr/bin/env ruby
|
223
224
|
require 'protobuf/rpc/client'
|
224
225
|
require 'test/proto/rpc.pb'
|
@@ -226,7 +227,7 @@ require 'test/proto/rpc.pb'
|
|
226
227
|
# build request
|
227
228
|
request = Tutorial::Person.new
|
228
229
|
# TODO: setup a request
|
229
|
-
raise StandardError
|
230
|
+
raise StandardError, 'setup a request'
|
230
231
|
|
231
232
|
# create blunk response
|
232
233
|
response = Tutorial::Person.new
|
@@ -236,92 +237,92 @@ Protobuf::Rpc::Client.new('localhost', 9999).call :add, request, response
|
|
236
237
|
|
237
238
|
# show response
|
238
239
|
puts response
|
239
|
-
|
240
|
+
EOS
|
240
241
|
end
|
241
242
|
|
242
243
|
def test_create_descriptor
|
243
244
|
proto_path = 'test/proto/addressbook.proto'
|
244
|
-
visitor = Protobuf::Visitor::CreateDescriptorVisitor.new
|
245
|
-
File.open
|
245
|
+
visitor = Protobuf::Visitor::CreateDescriptorVisitor.new(proto_path)
|
246
|
+
File.open(proto_path) do |file|
|
246
247
|
visitor.visit Protobuf::ProtoParser.new.parse(file)
|
247
248
|
end
|
248
249
|
file_descriptor = visitor.file_descriptor
|
249
|
-
assert_equal
|
250
|
-
assert_equal
|
251
|
-
|
250
|
+
assert_equal(proto_path, file_descriptor.name)
|
251
|
+
assert_equal('tutorial', file_descriptor.package)
|
252
|
+
|
252
253
|
person_descriptor = file_descriptor.message_type[0]
|
253
|
-
assert_equal
|
254
|
-
assert_equal
|
254
|
+
assert_equal('Person', person_descriptor.name)
|
255
|
+
assert_equal([:name, :id, :email, :phone, :age].size, person_descriptor.field.size)
|
255
256
|
|
256
257
|
name_field_descriptor = person_descriptor.field.find {|d| d.name == 'name'}
|
257
|
-
assert_equal
|
258
|
-
assert_equal
|
259
|
-
assert_equal
|
260
|
-
assert_equal
|
258
|
+
assert_equal(1, name_field_descriptor.number)
|
259
|
+
assert_equal(Google::Protobuf::FieldDescriptorProto::Type::TYPE_STRING, name_field_descriptor.type)
|
260
|
+
assert_equal(Google::Protobuf::FieldDescriptorProto::Label::LABEL_REQUIRED, name_field_descriptor.label)
|
261
|
+
assert_equal('string', name_field_descriptor.type_name)
|
261
262
|
|
262
263
|
phone_field_descriptor = person_descriptor.field.find {|d| d.name == 'phone'}
|
263
|
-
assert_equal
|
264
|
-
assert_equal
|
265
|
-
assert_equal
|
266
|
-
assert_equal
|
264
|
+
assert_equal(4, phone_field_descriptor.number)
|
265
|
+
assert_equal(0, phone_field_descriptor.type) #TODO: is this right?
|
266
|
+
assert_equal(Google::Protobuf::FieldDescriptorProto::Label::LABEL_REPEATED, phone_field_descriptor.label)
|
267
|
+
assert_equal('PhoneNumber', phone_field_descriptor.type_name)
|
267
268
|
|
268
269
|
age_field_descriptor = person_descriptor.field.find {|d| d.name == 'age'}
|
269
|
-
assert_equal
|
270
|
-
assert_equal
|
271
|
-
assert_equal
|
272
|
-
assert_equal
|
273
|
-
assert_equal
|
270
|
+
assert_equal(5, age_field_descriptor.number)
|
271
|
+
assert_equal(Google::Protobuf::FieldDescriptorProto::Type::TYPE_UINT32, age_field_descriptor.type)
|
272
|
+
assert_equal(Google::Protobuf::FieldDescriptorProto::Label::LABEL_OPTIONAL, age_field_descriptor.label)
|
273
|
+
assert_equal('uint32', age_field_descriptor.type_name)
|
274
|
+
assert_equal('20', age_field_descriptor.default_value)
|
274
275
|
|
275
276
|
phone_type_descriptor = person_descriptor.enum_type.first
|
276
|
-
assert_equal
|
277
|
-
assert_equal
|
277
|
+
assert_equal('PhoneType', phone_type_descriptor.name)
|
278
|
+
assert_equal(3, phone_type_descriptor.value.size)
|
278
279
|
|
279
280
|
phone_type_home_descriptor = phone_type_descriptor.value.find {|d| d.name == 'HOME'}
|
280
|
-
assert_equal
|
281
|
-
assert_equal
|
281
|
+
assert_equal('HOME', phone_type_home_descriptor.name)
|
282
|
+
assert_equal(1, phone_type_home_descriptor.number)
|
282
283
|
|
283
284
|
extensions_descriptor = person_descriptor.extension_range.first
|
284
|
-
assert_equal
|
285
|
-
assert_equal
|
285
|
+
assert_equal(100, extensions_descriptor.start)
|
286
|
+
assert_equal(200, extensions_descriptor.end)
|
286
287
|
|
287
288
|
phone_number_descriptor = person_descriptor.nested_type.first
|
288
|
-
assert_equal
|
289
|
-
assert_equal
|
289
|
+
assert_equal('PhoneNumber', phone_number_descriptor.name)
|
290
|
+
assert_equal([:number, :type].size, phone_number_descriptor.field.size)
|
290
291
|
|
291
292
|
#TODO: test extend
|
292
293
|
#extend_person_descriptor = ??
|
293
294
|
#assert_equal extend_person_descriptor
|
294
295
|
|
295
296
|
addressbook_descriptor = file_descriptor.message_type[1]
|
296
|
-
assert_equal
|
297
|
+
assert_equal('AddressBook', addressbook_descriptor.name)
|
297
298
|
end
|
298
299
|
|
299
300
|
def test_collision
|
300
|
-
assert_raise
|
301
|
-
assert_raise
|
302
|
-
Protobuf::Compiler.new.create_message('test/proto/collision.proto', '.', '.', false)
|
301
|
+
assert_raise(Protobuf::TagCollisionError) do require 'test/proto/collision.pb' end
|
302
|
+
assert_raise(Protobuf::TagCollisionError) do
|
303
|
+
Protobuf::Compiler.new.create_message('test/proto/collision.proto', '.', '.', false)
|
303
304
|
end
|
304
305
|
end
|
305
306
|
|
306
307
|
def test_ext_collision
|
307
|
-
assert_raise
|
308
|
-
assert_raise
|
309
|
-
Protobuf::Compiler.new.create_message('test/proto/ext_collision.proto', '.', '.', false)
|
308
|
+
assert_raise(Protobuf::TagCollisionError) do require 'test/proto/ext_collision.pb' end
|
309
|
+
assert_raise(Protobuf::TagCollisionError) do
|
310
|
+
Protobuf::Compiler.new.create_message('test/proto/ext_collision.proto', '.', '.', false)
|
310
311
|
end
|
311
312
|
end
|
312
313
|
|
313
314
|
def test_ext_range
|
314
|
-
assert_raise
|
315
|
-
assert_raise
|
316
|
-
Protobuf::Compiler.new.create_message('test/proto/ext_range.proto', '.', '.', false)
|
315
|
+
assert_raise(RangeError) do require 'test/proto/ext_range.pb' end
|
316
|
+
assert_raise(RangeError) do
|
317
|
+
Protobuf::Compiler.new.create_message('test/proto/ext_range.proto', '.', '.', false)
|
317
318
|
end
|
318
319
|
end
|
319
320
|
|
320
321
|
def assert_compile_proto(ideal, filename)
|
321
|
-
assert_equal
|
322
|
+
assert_equal(ideal.gsub(/^\s*\n/, '').strip, Protobuf::Compiler.new.create_message(filename, '.', '.', false).gsub(/^\s*\n/, '').strip)
|
322
323
|
end
|
323
324
|
|
324
325
|
def assert_source(ideal, real)
|
325
|
-
assert_equal
|
326
|
+
assert_equal(ideal.strip.gsub(/^\s*\n/, '').gsub(/\s+\n/, "\n"), real.strip.gsub(/^\s*\n/, '').gsub(/\s+\n/, "\n"))
|
326
327
|
end
|
327
328
|
end
|
data/test/test_descriptor.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
require 'test/unit'
|
2
|
-
require 'test/addressbook'
|
2
|
+
require 'test/proto/addressbook.pb'
|
3
3
|
require 'protobuf/descriptor/descriptor_builder'
|
4
4
|
require 'protobuf/descriptor/descriptor_proto'
|
5
5
|
|
@@ -63,7 +63,7 @@ class DescriptorTest < Test::Unit::TestCase
|
|
63
63
|
person_phone_number_number_proto.type = FieldDescriptorProto::Type::TYPE_STRING
|
64
64
|
person_phone_number_number_proto.name = 'number'
|
65
65
|
person_phone_number_number_proto.number = 1
|
66
|
-
|
66
|
+
|
67
67
|
person_phone_number_type_proto = FieldDescriptorProto.new
|
68
68
|
person_phone_number_proto.field << person_phone_number_type_proto
|
69
69
|
person_phone_number_type_proto.label = FieldDescriptorProto::Label::LABEL_OPTIONAL
|
@@ -93,29 +93,29 @@ class DescriptorTest < Test::Unit::TestCase
|
|
93
93
|
address_book_person_proto.name = 'person'
|
94
94
|
address_book_person_proto.number = 1
|
95
95
|
|
96
|
-
Protobuf::Descriptor::DescriptorBuilder.build
|
96
|
+
Protobuf::Descriptor::DescriptorBuilder.build(tutorial_proto)
|
97
97
|
|
98
98
|
assert_nothing_raised {Build::Tutorial::Person}
|
99
99
|
assert_nothing_raised {Build::Tutorial::Person.new}
|
100
|
-
assert_equal
|
101
|
-
Build::Tutorial::Person.fields.map{|tag, field| field.name}.sort
|
100
|
+
assert_equal(['email', 'id', 'name', 'phone'],
|
101
|
+
Build::Tutorial::Person.fields.map{|tag, field| field.name}.sort)
|
102
102
|
|
103
103
|
assert_nothing_raised {Build::Tutorial::Person::PhoneNumber}
|
104
104
|
assert_nothing_raised {Build::Tutorial::Person::PhoneNumber.new}
|
105
|
-
assert_equal
|
106
|
-
Build::Tutorial::Person::PhoneNumber.fields.map{|tag, field| field.name}.sort
|
105
|
+
assert_equal(['number', 'type'],
|
106
|
+
Build::Tutorial::Person::PhoneNumber.fields.map{|tag, field| field.name}.sort)
|
107
107
|
|
108
108
|
assert_nothing_raised {Build::Tutorial::Person::PhoneType}
|
109
|
-
assert_equal
|
110
|
-
assert_equal
|
111
|
-
assert_equal
|
109
|
+
assert_equal(0, Build::Tutorial::Person::PhoneType::MOBILE)
|
110
|
+
assert_equal(1, Build::Tutorial::Person::PhoneType::HOME)
|
111
|
+
assert_equal(2, Build::Tutorial::Person::PhoneType::WORK)
|
112
112
|
|
113
113
|
assert_nothing_raised {Build::Tutorial::AddressBook}
|
114
114
|
end
|
115
115
|
|
116
116
|
def test_unbuild
|
117
|
-
proto = Protobuf::Descriptor::FileDescriptor.unbuild
|
118
|
-
proto.serialize_to_file
|
117
|
+
proto = Protobuf::Descriptor::FileDescriptor.unbuild(Tutorial::Person)
|
118
|
+
proto.serialize_to_file('person.bin')
|
119
119
|
puts
|
120
120
|
puts "run `test/check_unbuild.rb'"
|
121
121
|
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'protobuf/message/message'
|
3
|
+
require 'protobuf/message/enum'
|
4
|
+
require 'test/proto/addressbook.pb'
|
5
|
+
require 'test/proto/addressbook_base.pb'
|
6
|
+
|
7
|
+
class EnumValueTest < Test::Unit::TestCase
|
8
|
+
def test_enum_value
|
9
|
+
e = Protobuf::EnumValue.new(Object, :name, 100)
|
10
|
+
assert_kind_of(Protobuf::EnumValue, e)
|
11
|
+
assert_equal(:name, e.name)
|
12
|
+
assert_equal(100, e.value)
|
13
|
+
assert_equal('name', e.to_s)
|
14
|
+
assert_equal('name', "#{e}")
|
15
|
+
assert(e == 100)
|
16
|
+
assert(100 == e)
|
17
|
+
assert(101, e + 1)
|
18
|
+
assert(101, 1 + e)
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_enum_field_and_enum_value
|
22
|
+
phone_number = Tutorial::Person::PhoneNumber.new
|
23
|
+
|
24
|
+
assert_kind_of(Protobuf::EnumValue, phone_number.type)
|
25
|
+
assert_equal(Tutorial::Person::PhoneType::HOME, phone_number.type)
|
26
|
+
|
27
|
+
phone_number.type = 1
|
28
|
+
assert_kind_of(Protobuf::EnumValue, phone_number.type)
|
29
|
+
assert_equal(Tutorial::Person::PhoneType::HOME, phone_number.type)
|
30
|
+
assert_equal(1, phone_number.type)
|
31
|
+
|
32
|
+
phone_number.type = :HOME
|
33
|
+
assert_kind_of(Protobuf::EnumValue, phone_number.type)
|
34
|
+
assert_equal(Tutorial::Person::PhoneType::HOME, phone_number.type)
|
35
|
+
assert_equal(1, phone_number.type)
|
36
|
+
|
37
|
+
assert_raise(TypeError) do
|
38
|
+
phone_number.type = TutorialExt::Person::PhoneType::HOME
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
data/test/test_extension.rb
CHANGED
@@ -1,16 +1,12 @@
|
|
1
1
|
require 'test/unit'
|
2
|
-
require 'test/addressbook_ext.
|
2
|
+
require 'test/proto/addressbook_ext.pb'
|
3
3
|
|
4
4
|
class ExtensionTest < Test::Unit::TestCase
|
5
5
|
def test_accessor
|
6
|
-
assert
|
6
|
+
assert(TutorialExt::Person.extension_fields.to_a.map{|t, f| f.name}.include?(:age))
|
7
7
|
person = TutorialExt::Person.new
|
8
8
|
assert_nothing_raised {person.age = 100}
|
9
|
-
|
10
|
-
#assert 100, person.extension.age
|
11
|
-
#assert_nothing_raised {person.extension.age = 200}
|
12
|
-
#assert 200, person.age
|
13
|
-
#assert 200, person.extension.age
|
9
|
+
assert_equal(100, person.age)
|
14
10
|
end
|
15
11
|
|
16
12
|
def test_serialize
|
@@ -29,12 +25,12 @@ class ExtensionTest < Test::Unit::TestCase
|
|
29
25
|
# parse the serialized string
|
30
26
|
person2 = TutorialExt::Person.new
|
31
27
|
person2.parse_from_string serialized_string
|
32
|
-
assert_equal
|
33
|
-
assert_equal
|
34
|
-
assert_equal
|
35
|
-
assert_equal
|
36
|
-
assert_equal
|
37
|
-
assert_equal
|
38
|
-
assert_equal
|
28
|
+
assert_equal(1234, person2.id)
|
29
|
+
assert_equal(70, person2.age)
|
30
|
+
assert_equal('John Doe', person2.name)
|
31
|
+
assert_equal('jdoe@example.com', person2.email)
|
32
|
+
assert_equal(1, person2.phone.size)
|
33
|
+
assert_equal('555-4321', person2.phone[0].number)
|
34
|
+
assert_equal(TutorialExt::Person::PhoneType::HOME, person2.phone[0].type)
|
39
35
|
end
|
40
36
|
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'test/proto/lowercase.pb'
|
3
|
+
|
4
|
+
class LowercaseTest < Test::Unit::TestCase
|
5
|
+
def test_lowercase
|
6
|
+
message = nil
|
7
|
+
assert_nothing_raised { message = Test::Lowercase::Baaz.new }
|
8
|
+
assert_nothing_raised { message.x = Test::Lowercase::Foo::Bar.new }
|
9
|
+
assert_equal(Test::Lowercase::Foo::Bar, message.get_field_by_name(:x).type)
|
10
|
+
end
|
11
|
+
end
|
data/test/test_message.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'protobuf/message/message'
|
2
|
-
require 'test/addressbook'
|
3
|
-
require 'test/merge'
|
2
|
+
require 'test/proto/addressbook.pb'
|
3
|
+
require 'test/proto/merge.pb'
|
4
4
|
require 'test/unit'
|
5
5
|
|
6
6
|
# It should not conflict with Test::InnerMessage1 which is included in merge.proto
|
@@ -11,63 +11,63 @@ class MessageTest < Test::Unit::TestCase
|
|
11
11
|
person1 = Tutorial::Person.new :name => 'ando'
|
12
12
|
person2 = Tutorial::Person.new :name => 'ando'
|
13
13
|
person3 = Tutorial::Person.new :name => 'Ando'
|
14
|
-
assert
|
15
|
-
assert
|
16
|
-
assert
|
14
|
+
assert(person1 == person2)
|
15
|
+
assert(person1 != person3)
|
16
|
+
assert(person1 != 'ando')
|
17
17
|
end
|
18
18
|
|
19
19
|
def test_bracketed_access
|
20
20
|
person = Tutorial::Person.new
|
21
21
|
name_tag = 1
|
22
22
|
person[name_tag] = 'Ichiro'
|
23
|
-
assert_equal
|
24
|
-
assert_equal
|
23
|
+
assert_equal('Ichiro', person.name)
|
24
|
+
assert_equal('Ichiro', person[name_tag])
|
25
25
|
|
26
26
|
person[:id] = 100
|
27
|
-
assert_equal
|
27
|
+
assert_equal(100, person.id)
|
28
28
|
person['id'] = 200
|
29
|
-
assert_equal
|
30
|
-
assert_equal
|
31
|
-
assert_equal
|
29
|
+
assert_equal(200, person.id)
|
30
|
+
assert_equal(200, person[:id])
|
31
|
+
assert_equal(200, person['id'])
|
32
32
|
end
|
33
33
|
|
34
34
|
def test_initialize_with_hash
|
35
|
-
person = Tutorial::Person.new
|
36
|
-
assert_equal
|
37
|
-
assert_equal
|
38
|
-
assert_equal
|
35
|
+
person = Tutorial::Person.new(:name => 'Jiro', :id => 300, :email => 'jiro@ema.il')
|
36
|
+
assert_equal('Jiro', person.name)
|
37
|
+
assert_equal(300, person.id)
|
38
|
+
assert_equal('jiro@ema.il', person.email)
|
39
39
|
|
40
40
|
# initialize with array of hash
|
41
41
|
person = Tutorial::Person.new(:phone => [{:number => 'phone1'}, {:number => 'phone2'}])
|
42
|
-
assert_equal
|
43
|
-
assert_equal
|
42
|
+
assert_equal('phone1', person.phone[0].number)
|
43
|
+
assert_equal('phone2', person.phone[1].number)
|
44
44
|
|
45
45
|
# initalize with hash in hash
|
46
46
|
message = Test::MergeMessage.new(:require_message => { :name => 'name1', :repeate_message => [{:name => 'name2'}] })
|
47
|
-
assert_equal
|
48
|
-
assert_equal
|
47
|
+
assert_equal('name1', message.require_message.name)
|
48
|
+
assert_equal('name2', message.require_message.repeate_message[0].name)
|
49
49
|
|
50
50
|
message.require_message = { :name => 'name21' }
|
51
51
|
message.require_message.repeate_message = [ {:name => 'name22'} ]
|
52
|
-
assert_equal
|
53
|
-
assert_equal
|
54
|
-
assert_equal
|
52
|
+
assert_equal('name21', message.require_message.name)
|
53
|
+
assert_equal('name22', message.require_message.repeate_message[0].name)
|
54
|
+
assert_equal(1, message.require_message.repeate_message.size)
|
55
55
|
end
|
56
56
|
|
57
57
|
def test_defined_filenames
|
58
|
-
assert
|
59
|
-
assert_equal
|
60
|
-
|
58
|
+
assert(Tutorial::Person.defined_filenames)
|
59
|
+
assert_equal(1, Tutorial::Person.defined_filenames.size)
|
60
|
+
assert_match(%r{/.*/proto/addressbook\.pb\.rb}, Tutorial::Person.defined_filenames.first)
|
61
61
|
end
|
62
62
|
|
63
63
|
def test_proto_filenames
|
64
|
-
assert
|
65
|
-
assert_equal
|
66
|
-
assert_equal
|
64
|
+
assert(Tutorial::Person.proto_filenames)
|
65
|
+
assert_equal(1, Tutorial::Person.proto_filenames.size)
|
66
|
+
assert_equal('test/proto/addressbook.proto', Tutorial::Person.proto_filenames.first)
|
67
67
|
end
|
68
68
|
|
69
69
|
def test_proto_contents
|
70
|
-
assert_equal
|
70
|
+
assert_equal(<<-EOS.strip, Tutorial::Person.proto_contents.values.first.strip)
|
71
71
|
package tutorial;
|
72
72
|
|
73
73
|
message Person {
|
@@ -87,39 +87,42 @@ message Person {
|
|
87
87
|
}
|
88
88
|
|
89
89
|
repeated PhoneNumber phone = 4;
|
90
|
+
optional uint32 age = 5 [default = 20];
|
90
91
|
|
91
92
|
extensions 100 to 200;
|
92
93
|
}
|
93
94
|
|
95
|
+
/*
|
94
96
|
extend Person {
|
95
97
|
optional int32 age = 100;
|
96
98
|
}
|
99
|
+
*/
|
97
100
|
|
98
101
|
message AddressBook {
|
99
102
|
repeated Person person = 1;
|
100
103
|
}
|
101
|
-
|
104
|
+
EOS
|
102
105
|
end
|
103
106
|
|
104
107
|
def test_merge_field
|
105
108
|
inner_message1_2 = Test::MergeMessage::InnerMessage2.new(:name => 'name12')
|
106
109
|
inner_message1_2.repeate_message << Test::MergeMessage::InnerMessage1.new(:name => 'name121')
|
107
|
-
message1 = Test::MergeMessage.new
|
110
|
+
message1 = Test::MergeMessage.new(:name => 'name1', :require_message => inner_message1_2)
|
108
111
|
message1.repeate_message << Test::MergeMessage::InnerMessage1.new(:name => 'name11')
|
109
112
|
|
110
113
|
inner_message2_2 = Test::MergeMessage::InnerMessage2.new(:name => 'name22')
|
111
114
|
inner_message2_2.repeate_message << Test::MergeMessage::InnerMessage1.new(:name => 'name221')
|
112
|
-
message2 = Test::MergeMessage.new
|
115
|
+
message2 = Test::MergeMessage.new(:name => 'name2', :require_message => inner_message2_2)
|
113
116
|
message2.repeate_message << Test::MergeMessage::InnerMessage1.new(:name => 'name21')
|
114
117
|
|
115
|
-
message1.merge_from
|
116
|
-
assert_equal
|
117
|
-
assert_equal
|
118
|
-
assert_equal
|
119
|
-
assert_equal
|
120
|
-
assert_equal
|
121
|
-
assert_equal
|
122
|
-
assert_equal
|
123
|
-
assert_equal
|
118
|
+
message1.merge_from(message2)
|
119
|
+
assert_equal('name2', message1.name)
|
120
|
+
assert_equal(2, message1.repeate_message.size)
|
121
|
+
assert_equal('name11', message1.repeate_message[0].name)
|
122
|
+
assert_equal('name21', message1.repeate_message[1].name)
|
123
|
+
assert_equal('name22', message1.require_message.name)
|
124
|
+
assert_equal(2, message1.require_message.repeate_message.size)
|
125
|
+
assert_equal('name121', message1.require_message.repeate_message[0].name)
|
126
|
+
assert_equal('name221', message1.require_message.repeate_message[1].name)
|
124
127
|
end
|
125
128
|
end
|