macks-ruby_protobuf 0.3.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (76) hide show
  1. data/History.txt +14 -0
  2. data/Manifest.txt +74 -0
  3. data/README.txt +58 -0
  4. data/Rakefile +18 -0
  5. data/bin/mk_parser +2 -0
  6. data/bin/rprotoc +36 -0
  7. data/examples/addressbook.pb.rb +56 -0
  8. data/examples/addressbook.proto +24 -0
  9. data/examples/reading_a_message.rb +32 -0
  10. data/examples/writing_a_message.rb +46 -0
  11. data/lib/protobuf/common/wire_type.rb +10 -0
  12. data/lib/protobuf/compiler/compiler.rb +54 -0
  13. data/lib/protobuf/compiler/nodes.rb +319 -0
  14. data/lib/protobuf/compiler/proto.y +203 -0
  15. data/lib/protobuf/compiler/proto2.ebnf +79 -0
  16. data/lib/protobuf/compiler/proto_parser.rb +1394 -0
  17. data/lib/protobuf/compiler/template/rpc_bin.erb +4 -0
  18. data/lib/protobuf/compiler/template/rpc_client.erb +18 -0
  19. data/lib/protobuf/compiler/template/rpc_service.erb +25 -0
  20. data/lib/protobuf/compiler/visitors.rb +288 -0
  21. data/lib/protobuf/descriptor/descriptor.proto +286 -0
  22. data/lib/protobuf/descriptor/descriptor.rb +54 -0
  23. data/lib/protobuf/descriptor/descriptor_builder.rb +144 -0
  24. data/lib/protobuf/descriptor/descriptor_proto.rb +119 -0
  25. data/lib/protobuf/descriptor/enum_descriptor.rb +33 -0
  26. data/lib/protobuf/descriptor/field_descriptor.rb +50 -0
  27. data/lib/protobuf/descriptor/file_descriptor.rb +38 -0
  28. data/lib/protobuf/message/decoder.rb +93 -0
  29. data/lib/protobuf/message/encoder.rb +37 -0
  30. data/lib/protobuf/message/enum.rb +28 -0
  31. data/lib/protobuf/message/extend.rb +8 -0
  32. data/lib/protobuf/message/field.rb +654 -0
  33. data/lib/protobuf/message/message.rb +308 -0
  34. data/lib/protobuf/message/protoable.rb +37 -0
  35. data/lib/protobuf/message/service.rb +9 -0
  36. data/lib/protobuf/rpc/client.rb +19 -0
  37. data/lib/protobuf/rpc/handler.rb +17 -0
  38. data/lib/protobuf/rpc/server.rb +39 -0
  39. data/lib/ruby_protobuf.rb +3 -0
  40. data/test/addressbook.rb +98 -0
  41. data/test/addressbook_base.rb +62 -0
  42. data/test/addressbook_ext.rb +12 -0
  43. data/test/check_unbuild.rb +30 -0
  44. data/test/collision.rb +18 -0
  45. data/test/data/data.bin +3 -0
  46. data/test/data/data_source.py +14 -0
  47. data/test/data/types.bin +0 -0
  48. data/test/data/types_source.py +22 -0
  49. data/test/data/unk.png +0 -0
  50. data/test/ext_collision.rb +25 -0
  51. data/test/ext_range.rb +23 -0
  52. data/test/merge.rb +40 -0
  53. data/test/nested.rb +25 -0
  54. data/test/proto/addressbook.proto +31 -0
  55. data/test/proto/addressbook_base.proto +26 -0
  56. data/test/proto/addressbook_ext.proto +6 -0
  57. data/test/proto/collision.proto +5 -0
  58. data/test/proto/ext_collision.proto +8 -0
  59. data/test/proto/ext_range.proto +7 -0
  60. data/test/proto/merge.proto +15 -0
  61. data/test/proto/nested.proto +7 -0
  62. data/test/proto/rpc.proto +6 -0
  63. data/test/proto/types.proto +17 -0
  64. data/test/test_addressbook.rb +43 -0
  65. data/test/test_compiler.rb +313 -0
  66. data/test/test_descriptor.rb +122 -0
  67. data/test/test_extension.rb +40 -0
  68. data/test/test_message.rb +106 -0
  69. data/test/test_optional_field.rb +68 -0
  70. data/test/test_parse.rb +15 -0
  71. data/test/test_ruby_protobuf.rb +1 -0
  72. data/test/test_serialize.rb +42 -0
  73. data/test/test_standard_message.rb +96 -0
  74. data/test/test_types.rb +181 -0
  75. data/test/types.rb +22 -0
  76. metadata +150 -0
@@ -0,0 +1,122 @@
1
+ require 'test/unit'
2
+ require 'test/addressbook'
3
+ require 'protobuf/descriptor/descriptor_builder'
4
+ require 'protobuf/descriptor/descriptor_proto'
5
+
6
+ class DescriptorTest < Test::Unit::TestCase
7
+ include Google::Protobuf
8
+ def test_build
9
+ tutorial_proto = FileDescriptorProto.new
10
+ tutorial_proto.package = 'Build::Tutorial'
11
+
12
+ person_proto = DescriptorProto.new
13
+ tutorial_proto.message_type << person_proto
14
+ person_proto.name = 'Person'
15
+
16
+ person_name_proto = FieldDescriptorProto.new
17
+ person_proto.field << person_name_proto
18
+ person_name_proto.label = FieldDescriptorProto::Label::LABEL_REQUIRED
19
+ person_name_proto.type = FieldDescriptorProto::Type::TYPE_STRING
20
+ person_name_proto.name = 'name'
21
+ person_name_proto.number = 1
22
+
23
+ person_id_proto = FieldDescriptorProto.new
24
+ person_proto.field << person_id_proto
25
+ person_id_proto.label = FieldDescriptorProto::Label::LABEL_REQUIRED
26
+ person_id_proto.type = FieldDescriptorProto::Type::TYPE_INT32
27
+ person_id_proto.name = 'id'
28
+ person_id_proto.number = 2
29
+
30
+ person_email_proto = FieldDescriptorProto.new
31
+ person_proto.field << person_email_proto
32
+ person_email_proto.label = FieldDescriptorProto::Label::LABEL_OPTIONAL
33
+ person_email_proto.type = FieldDescriptorProto::Type::TYPE_STRING
34
+ person_email_proto.name = 'email'
35
+ person_email_proto.number = 3
36
+
37
+ person_phone_type_proto = EnumDescriptorProto.new
38
+ person_proto.enum_type << person_phone_type_proto
39
+ person_phone_type_proto.name = 'PhoneType'
40
+
41
+ person_phone_type_mobile_proto = EnumValueDescriptorProto.new
42
+ person_phone_type_proto.value << person_phone_type_mobile_proto
43
+ person_phone_type_mobile_proto.name = 'MOBILE'
44
+ person_phone_type_mobile_proto.number = 0
45
+
46
+ person_phone_type_home_proto = EnumValueDescriptorProto.new
47
+ person_phone_type_proto.value << person_phone_type_home_proto
48
+ person_phone_type_home_proto.name = 'HOME'
49
+ person_phone_type_home_proto.number = 1
50
+
51
+ person_phone_type_work_proto = EnumValueDescriptorProto.new
52
+ person_phone_type_proto.value << person_phone_type_work_proto
53
+ person_phone_type_work_proto.name = 'WORK'
54
+ person_phone_type_work_proto.number = 2
55
+
56
+ person_phone_number_proto = DescriptorProto.new
57
+ person_proto.nested_type << person_phone_number_proto
58
+ person_phone_number_proto.name = 'PhoneNumber'
59
+
60
+ person_phone_number_number_proto = FieldDescriptorProto.new
61
+ person_phone_number_proto.field << person_phone_number_number_proto
62
+ person_phone_number_number_proto.label = FieldDescriptorProto::Label::LABEL_REQUIRED
63
+ person_phone_number_number_proto.type = FieldDescriptorProto::Type::TYPE_STRING
64
+ person_phone_number_number_proto.name = 'number'
65
+ person_phone_number_number_proto.number = 1
66
+
67
+ person_phone_number_type_proto = FieldDescriptorProto.new
68
+ person_phone_number_proto.field << person_phone_number_type_proto
69
+ person_phone_number_type_proto.label = FieldDescriptorProto::Label::LABEL_OPTIONAL
70
+ person_phone_number_type_proto.type = FieldDescriptorProto::Type::TYPE_ENUM
71
+ person_phone_number_type_proto.type_name = 'PhoneType'
72
+ person_phone_number_type_proto.name = 'type'
73
+ person_phone_number_type_proto.number = 2
74
+ person_phone_number_type_proto.default_value = 'HOME'
75
+
76
+ person_phone_phone_number_proto = FieldDescriptorProto.new
77
+ person_proto.field << person_phone_phone_number_proto
78
+ person_phone_phone_number_proto.label = FieldDescriptorProto::Label::LABEL_REPEATED
79
+ person_phone_phone_number_proto.type = FieldDescriptorProto::Type::TYPE_MESSAGE
80
+ person_phone_phone_number_proto.type_name = 'PhoneNumber'
81
+ person_phone_phone_number_proto.name = 'phone'
82
+ person_phone_phone_number_proto.number = 4
83
+
84
+ address_book_proto = DescriptorProto.new
85
+ tutorial_proto.message_type << address_book_proto
86
+ address_book_proto.name = 'AddressBook'
87
+
88
+ address_book_person_proto = FieldDescriptorProto.new
89
+ address_book_proto.field << address_book_person_proto
90
+ address_book_person_proto.label = FieldDescriptorProto::Label::LABEL_REPEATED
91
+ address_book_person_proto.type = FieldDescriptorProto::Type::TYPE_MESSAGE
92
+ address_book_person_proto.type_name = 'Person'
93
+ address_book_person_proto.name = 'person'
94
+ address_book_person_proto.number = 1
95
+
96
+ Protobuf::Descriptor::DescriptorBuilder.build tutorial_proto
97
+
98
+ assert_nothing_raised {Build::Tutorial::Person}
99
+ assert_nothing_raised {Build::Tutorial::Person.new}
100
+ assert_equal ['email', 'id', 'name', 'phone'],
101
+ Build::Tutorial::Person.fields.map{|tag, field| field.name}.sort
102
+
103
+ assert_nothing_raised {Build::Tutorial::Person::PhoneNumber}
104
+ assert_nothing_raised {Build::Tutorial::Person::PhoneNumber.new}
105
+ assert_equal ['number', 'type'],
106
+ Build::Tutorial::Person::PhoneNumber.fields.map{|tag, field| field.name}.sort
107
+
108
+ assert_nothing_raised {Build::Tutorial::Person::PhoneType}
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
+
113
+ assert_nothing_raised {Build::Tutorial::AddressBook}
114
+ end
115
+
116
+ def test_unbuild
117
+ proto = Protobuf::Descriptor::FileDescriptor.unbuild Tutorial::Person
118
+ proto.serialize_to_file 'person.bin'
119
+ puts
120
+ puts "run `test/check_unbuild.rb'"
121
+ end
122
+ end
@@ -0,0 +1,40 @@
1
+ require 'test/unit'
2
+ require 'test/addressbook_ext.rb'
3
+
4
+ class ExtensionTest < Test::Unit::TestCase
5
+ def test_accessor
6
+ assert TutorialExt::Person.extension_fields.to_a.map{|t, f| f.name}.include?(:age)
7
+ person = TutorialExt::Person.new
8
+ assert_nothing_raised {person.age = 100}
9
+ assert 100, person.age
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
14
+ end
15
+
16
+ def test_serialize
17
+ # serialize to string
18
+ person = TutorialExt::Person.new
19
+ person.id = 1234
20
+ person.age = 70
21
+ person.name = 'John Doe'
22
+ person.email = 'jdoe@example.com'
23
+ phone = TutorialExt::Person::PhoneNumber.new
24
+ phone.number = '555-4321'
25
+ phone.type = TutorialExt::Person::PhoneType::HOME
26
+ person.phone << phone
27
+ serialized_string = person.serialize_to_string
28
+
29
+ # parse the serialized string
30
+ person2 = TutorialExt::Person.new
31
+ person2.parse_from_string serialized_string
32
+ assert_equal 1234, person2.id
33
+ assert_equal 70, person2.age
34
+ assert_equal 'John Doe', person2.name
35
+ assert_equal 'jdoe@example.com', person2.email
36
+ assert_equal 1, person2.phone.size
37
+ assert_equal '555-4321', person2.phone[0].number
38
+ assert_equal TutorialExt::Person::PhoneType::HOME, person2.phone[0].type
39
+ end
40
+ end
@@ -0,0 +1,106 @@
1
+ require 'protobuf/message/message'
2
+ require 'test/addressbook'
3
+ require 'test/merge'
4
+ require 'test/unit'
5
+
6
+ class MessageTest < Test::Unit::TestCase
7
+ def test_equality
8
+ person1 = Tutorial::Person.new :name => 'ando'
9
+ person2 = Tutorial::Person.new :name => 'ando'
10
+ person3 = Tutorial::Person.new :name => 'Ando'
11
+ assert person1 == person2
12
+ assert person1 != person3
13
+ assert person1 != 'ando'
14
+ end
15
+
16
+ def test_bracketed_access
17
+ person = Tutorial::Person.new
18
+ name_tag = 1
19
+ person[name_tag] = 'Ichiro'
20
+ assert_equal 'Ichiro', person.name
21
+ assert_equal 'Ichiro', person[name_tag]
22
+
23
+ person[:id] = 100
24
+ assert_equal 100, person.id
25
+ person['id'] = 200
26
+ assert_equal 200, person.id
27
+ assert_equal 200, person[:id]
28
+ assert_equal 200, person['id']
29
+ end
30
+
31
+ def test_initialize_with_hash
32
+ person = Tutorial::Person.new :name => 'Jiro', :id => 300, :email => 'jiro@ema.il'
33
+ assert_equal 'Jiro', person.name
34
+ assert_equal 300, person.id
35
+ assert_equal 'jiro@ema.il', person.email
36
+ end
37
+
38
+ def test_defined_filenames
39
+ assert Tutorial::Person.defined_filenames
40
+ assert_equal 1, Tutorial::Person.defined_filenames.size
41
+ assert Tutorial::Person.defined_filenames.first =~ %r{/.*/test/addressbook\.rb}
42
+ end
43
+
44
+ def test_proto_filenames
45
+ assert Tutorial::Person.proto_filenames
46
+ assert_equal 1, Tutorial::Person.proto_filenames.size
47
+ assert_equal 'test/addressbook.proto', Tutorial::Person.proto_filenames.first
48
+ end
49
+
50
+ def test_proto_contents
51
+ assert_equal <<-eos.strip, Tutorial::Person.proto_contents.values.first.strip
52
+ package tutorial;
53
+
54
+ message Person {
55
+ required string name = 1;
56
+ required int32 id = 2;
57
+ optional string email = 3;
58
+
59
+ enum PhoneType {
60
+ MOBILE = 0;
61
+ HOME = 1;
62
+ WORK = 2;
63
+ }
64
+
65
+ message PhoneNumber {
66
+ required string number = 1;
67
+ optional PhoneType type = 2 [default = HOME];
68
+ }
69
+
70
+ repeated PhoneNumber phone = 4;
71
+
72
+ extensions 100 to 200;
73
+ }
74
+
75
+ extend Person {
76
+ optional int32 age = 100;
77
+ }
78
+
79
+ message AddressBook {
80
+ repeated Person person = 1;
81
+ }
82
+ eos
83
+ end
84
+
85
+ def test_merge_field
86
+ inner_message1_2 = Test::MergeMessage::InnerMessage2.new(:name => 'name12')
87
+ inner_message1_2.repeate_message << Test::MergeMessage::InnerMessage1.new(:name => 'name121')
88
+ message1 = Test::MergeMessage.new :name => 'name1', :require_message => inner_message1_2
89
+ message1.repeate_message << Test::MergeMessage::InnerMessage1.new(:name => 'name11')
90
+
91
+ inner_message2_2 = Test::MergeMessage::InnerMessage2.new(:name => 'name22')
92
+ inner_message2_2.repeate_message << Test::MergeMessage::InnerMessage1.new(:name => 'name221')
93
+ message2 = Test::MergeMessage.new :name => 'name2', :require_message => inner_message2_2
94
+ message2.repeate_message << Test::MergeMessage::InnerMessage1.new(:name => 'name21')
95
+
96
+ message1.merge_from message2
97
+ assert_equal 'name2', message1.name
98
+ assert_equal 2, message1.repeate_message.size
99
+ assert_equal 'name11', message1.repeate_message[0].name
100
+ assert_equal 'name21', message1.repeate_message[1].name
101
+ assert_equal 'name22', message1.require_message.name
102
+ assert_equal 2, message1.require_message.repeate_message.size
103
+ assert_equal 'name121', message1.require_message.repeate_message[0].name
104
+ assert_equal 'name221', message1.require_message.repeate_message[1].name
105
+ end
106
+ end
@@ -0,0 +1,68 @@
1
+ require 'test/unit'
2
+ require 'test/optional_field'
3
+
4
+ class OptionalFieldTest < Test::Unit::TestCase
5
+ def test_accessor
6
+ message = Test::Optional_field::Message.new
7
+
8
+ # default values
9
+ assert !message.has_field?(:number)
10
+ assert_equal 20, message.number
11
+
12
+ assert !message.has_field?(:text)
13
+ assert_equal 'default string', message.text
14
+
15
+ assert !message.has_field?(:enum)
16
+ assert_equal 2, message.enum
17
+
18
+ # assign values
19
+ assert_nothing_raised { message.number = 100 }
20
+ assert message.has_field?(:number)
21
+ assert_equal 100, message.number
22
+
23
+ assert_nothing_raised { message.text = 'abc' }
24
+ assert message.has_field?(:text)
25
+ assert_equal 'abc', message.text
26
+
27
+ assert_nothing_raised { message.enum = Test::Optional_field::Message::Enum::A }
28
+ assert message.has_field?(:enum)
29
+ assert_equal 1, message.enum
30
+ end
31
+
32
+ def test_serialize
33
+ message1 = Test::Optional_field::Message.new
34
+ message2 = Test::Optional_field::Message.new
35
+
36
+ # all fields are empty
37
+ serialized_string = message1.to_s
38
+ assert serialized_string.empty?
39
+ message2.parse_from_string(serialized_string)
40
+ assert_equal message1.number, message2.number
41
+ assert_equal message1.text, message2.text
42
+ assert_equal message1.enum, message2.enum
43
+ assert !message2.has_field?(:number)
44
+ assert !message2.has_field?(:text)
45
+ assert !message2.has_field?(:enum)
46
+
47
+ # assign the value whith is equal to default value
48
+ message1 = Test::Optional_field::Message.new
49
+ message1.number = message1.number
50
+ message1.text = message1.text
51
+ message1.enum = message1.enum
52
+ serialized_string = message1.to_s
53
+ assert !serialized_string.empty?
54
+
55
+ # set some fields
56
+ message1 = Test::Optional_field::Message.new
57
+ message1.number = 100
58
+ message1.text = 'new text'
59
+ serialized_string = message1.to_s
60
+ message2.parse_from_string(serialized_string)
61
+ assert_equal message1.number, message2.number
62
+ assert_equal message1.text, message2.text
63
+ assert_equal message1.enum, message2.enum
64
+ assert message2.has_field?(:number)
65
+ assert message2.has_field?(:text)
66
+ assert !message2.has_field?(:enum)
67
+ end
68
+ 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,42 @@
1
+ # encoding: utf-8
2
+ require 'test/unit'
3
+ require 'test/addressbook'
4
+
5
+ class SerializeTest < Test::Unit::TestCase
6
+ def test_serialize
7
+ # serialize to string
8
+ person = Tutorial::Person.new
9
+ person.id = 1234
10
+ person.name = 'John Doe'
11
+ person.email = 'jdoe@example.com'
12
+ phone = Tutorial::Person::PhoneNumber.new
13
+ phone.number = '555-4321'
14
+ phone.type = Tutorial::Person::PhoneType::HOME
15
+ person.phone << phone
16
+ serialized_string = person.serialize_to_string
17
+
18
+ # parse the serialized string
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
27
+ end
28
+
29
+ def test_serialize_utf8string
30
+ # serialize to string
31
+ person = Tutorial::Person.new
32
+ person.id = 1234
33
+ person.name = '山田 太郎' # kanji characters
34
+ serialized_string = person.serialize_to_string
35
+
36
+ # parse the serialized string
37
+ person2 = Tutorial::Person.new
38
+ person2.parse_from_string serialized_string
39
+ assert_equal 1234, person2.id
40
+ assert_equal '山田 太郎', person2.name
41
+ end
42
+ end
@@ -0,0 +1,96 @@
1
+ require 'test/unit'
2
+ require 'protobuf/message/message'
3
+ require 'protobuf/message/enum'
4
+ require 'test/addressbook'
5
+
6
+ class StandardMessageTest < Test::Unit::TestCase
7
+ def test_initialized
8
+ person = Tutorial::Person.new
9
+ assert !person.initialized?
10
+ person.name = 'name'
11
+ assert !person.initialized?
12
+ person.id = 12
13
+ assert person.initialized?
14
+
15
+ # repeated field
16
+ person.phone << Tutorial::Person::PhoneNumber.new
17
+ assert !person.initialized?
18
+ person.phone.last.number = '123-456'
19
+ assert person.initialized?
20
+ end
21
+
22
+ def test_clear
23
+ person = Tutorial::Person.new
24
+ person.name = 'name'
25
+ person.id = 1234
26
+ person.email = 'abc@cde.fgh'
27
+ person.phone << Tutorial::Person::PhoneNumber.new
28
+ person.clear!
29
+
30
+ assert_nil person.name
31
+ assert_nil person.id
32
+ assert_equal '', person.email
33
+ assert_equal 0, person.phone.size
34
+
35
+ assert !person.has_field?(:name)
36
+ assert !person.has_field?(:id)
37
+ assert !person.has_field?(:email)
38
+ end
39
+
40
+ def test_dup
41
+ person = Tutorial::Person.new
42
+ person.name = 'name'
43
+ person.id = 1234
44
+ person.email = 'abc@cde.fgh'
45
+ person.phone << Tutorial::Person::PhoneNumber.new
46
+ person.phone.last.number = '123-456'
47
+ person.phone.last.type = Tutorial::Person::PhoneType::MOBILE
48
+ person.phone << Tutorial::Person::PhoneNumber.new
49
+ person.phone.last.number = '456-123'
50
+ person.phone.last.type = Tutorial::Person::PhoneType::WORK
51
+
52
+ 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
67
+ end
68
+
69
+ def test_inspect
70
+ person = Tutorial::Person.new
71
+ person.name = 'name'
72
+ person.id = 1234
73
+ person.email = 'abc@cde.fgh'
74
+ person.phone << Tutorial::Person::PhoneNumber.new
75
+ person.phone.last.number = '123-456'
76
+ person.phone.last.type = Tutorial::Person::PhoneType::MOBILE
77
+ person.phone << Tutorial::Person::PhoneNumber.new
78
+ person.phone.last.number = '456-123'
79
+ person.phone.last.type = Tutorial::Person::PhoneType::WORK
80
+
81
+ assert_equal <<-eos, person.inspect
82
+ name: "name"
83
+ id: 1234
84
+ email: "abc@cde.fgh"
85
+ phone {
86
+ number: "123-456"
87
+ type: MOBILE
88
+ }
89
+ phone {
90
+ number: "456-123"
91
+ type: WORK
92
+ }
93
+ eos
94
+ end
95
+ end
96
+
@@ -0,0 +1,181 @@
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
+ image_bin = File.open('test/data/unk.png', 'r+b'){|f| f.read}
20
+ types.type13 = image_bin
21
+
22
+ serialized_string = types.serialize_to_string
23
+
24
+ types2 = Test::Types::TestTypes.new
25
+ types2.parse_from_string serialized_string
26
+ assert_in_delta 0.01, types2.type1, 0.00001
27
+ assert_in_delta 0.1, types2.type2, 0.00001
28
+ assert_equal 1, types2.type3
29
+ assert_equal 10, types2.type4
30
+ assert_equal 100, types2.type5
31
+ assert_equal 1000, types2.type6
32
+ assert_equal(-1, types2.type7)
33
+ assert_equal(-10, types2.type8)
34
+ assert_equal 10000, types2.type9
35
+ assert_equal 100000, types2.type10
36
+ assert !types2.type11
37
+ assert_equal 'hello all types', types2.type12
38
+ assert_equal 10938, types2.type13.size
39
+ assert_equal image_bin, types2.type13
40
+ end
41
+
42
+ def test_parse
43
+ types = Test::Types::TestTypes.new
44
+ types.parse_from_file 'test/data/types.bin'
45
+ assert_in_delta 0.01, types.type1, 0.00001
46
+ assert_in_delta 0.1, types.type2, 0.00001
47
+ assert_equal 1, types.type3
48
+ assert_equal 10, types.type4
49
+ assert_equal 100, types.type5
50
+ assert_equal 1000, types.type6
51
+ assert_equal(-1, types.type7)
52
+ assert_equal(-10, types.type8)
53
+ assert_equal 10000, types.type9
54
+ assert_equal 100000, types.type10
55
+ assert_equal false, !!types.type11
56
+ assert_equal 'hello all types', types.type12
57
+ assert_equal File.open('test/data/unk.png', 'r+b'){|f| f.read}, types.type13
58
+ end
59
+
60
+ def test_double
61
+ # double fixed 64-bit
62
+ types = Test::Types::TestTypes.new
63
+ assert_nothing_raised do types.type1 = 1 end
64
+ assert_nothing_raised do types.type1 = 1.0 end
65
+ assert_raise TypeError do types.type1 = '' end
66
+ assert_nothing_raised do types.type1 = Protobuf::Field::DoubleField.max end
67
+ assert_raise RangeError do types.type1 = Protobuf::Field::DoubleField.max * 2 end
68
+ assert_nothing_raised do types.type1 = Protobuf::Field::DoubleField.min end
69
+ assert_raise RangeError do types.type1 = Protobuf::Field::DoubleField.min * 2 end
70
+ end
71
+
72
+ def test_float
73
+ # float fixed 32-bit
74
+ types = Test::Types::TestTypes.new
75
+ assert_nothing_raised do types.type2 = 1 end
76
+ assert_nothing_raised do types.type2 = 1.0 end
77
+ assert_raise TypeError do types.type2 = '' end
78
+ assert_nothing_raised do types.type2 = Protobuf::Field::FloatField.max end
79
+ assert_raise RangeError do types.type2 = Protobuf::Field::FloatField.max * 2 end
80
+ assert_nothing_raised do types.type2 = Protobuf::Field::FloatField.min end
81
+ assert_raise RangeError do types.type2 = Protobuf::Field::FloatField.min * 2 end
82
+ end
83
+
84
+ def test_int32
85
+ types = Test::Types::TestTypes.new
86
+ assert_nothing_raised do types.type3 = 1 end
87
+ assert_nothing_raised do types.type3 = -1 end
88
+ assert_raise TypeError do types.type3 = 1.0 end
89
+ assert_raise TypeError do types.type3 = '' end
90
+ end
91
+
92
+ def test_int64
93
+ types = Test::Types::TestTypes.new
94
+ assert_nothing_raised do types.type4 = 1 end
95
+ assert_nothing_raised do types.type4 = -1 end
96
+ assert_raise TypeError do types.type4 = 1.0 end
97
+ assert_raise TypeError do types.type4 = '' end
98
+ end
99
+
100
+ def test_uint32
101
+ types = Test::Types::TestTypes.new
102
+ assert_nothing_raised do types.type5 = 1 end
103
+ assert_raise RangeError do types.type5 = -1 end
104
+ assert_raise TypeError do types.type5 = 1.0 end
105
+ assert_raise TypeError do types.type5 = '' end
106
+ end
107
+
108
+ def test_uint64
109
+ types = Test::Types::TestTypes.new
110
+ assert_nothing_raised do types.type6 = 1 end
111
+ assert_raise RangeError do types.type6 = -1 end
112
+ assert_raise TypeError do types.type6 = 1.0 end
113
+ assert_raise TypeError do types.type6 = '' end
114
+ end
115
+
116
+ def test_sint32
117
+ types = Test::Types::TestTypes.new
118
+ assert_nothing_raised do types.type7 = 1 end
119
+ assert_nothing_raised do types.type7 = -1 end
120
+ assert_raise TypeError do types.type7 = 1.0 end
121
+ assert_raise TypeError do types.type7 = '' end
122
+ end
123
+
124
+ def test_sint64
125
+ types = Test::Types::TestTypes.new
126
+ assert_nothing_raised do types.type8 = 1 end
127
+ assert_nothing_raised do types.type8 = -1 end
128
+ assert_raise TypeError do types.type8 = 1.0 end
129
+ assert_raise TypeError do types.type8 = '' end
130
+ end
131
+
132
+ def test_fixed32
133
+ types = Test::Types::TestTypes.new
134
+ assert_nothing_raised do types.type9 = 1 end
135
+ assert_raise TypeError do types.type9 = 1.0 end
136
+ assert_raise TypeError do types.type9 = '' end
137
+ assert_nothing_raised do types.type9 = Protobuf::Field::Fixed32Field.max end
138
+ assert_raise RangeError do types.type9 = Protobuf::Field::Fixed32Field.max + 1 end
139
+ assert_nothing_raised do types.type9 = Protobuf::Field::Fixed32Field.min end
140
+ assert_raise RangeError do types.type9 = Protobuf::Field::Fixed32Field.min - 1 end
141
+ end
142
+
143
+ def test_fixed64
144
+ types = Test::Types::TestTypes.new
145
+ assert_nothing_raised do types.type10 = 1 end
146
+ assert_raise TypeError do types.type10 = 1.0 end
147
+ assert_raise TypeError do types.type10 = '' end
148
+ assert_nothing_raised do types.type10 = Protobuf::Field::Fixed64Field.max end
149
+ assert_raise RangeError do types.type10 = Protobuf::Field::Fixed64Field.max + 1 end
150
+ assert_nothing_raised do types.type10 = Protobuf::Field::Fixed64Field.min end
151
+ assert_raise RangeError do types.type10 = Protobuf::Field::Fixed64Field.min - 1 end
152
+ end
153
+
154
+ def test_bool
155
+ types = Test::Types::TestTypes.new
156
+ assert_nothing_raised do types.type11 = true end
157
+ assert_nothing_raised do types.type11 = false end
158
+ assert_nothing_raised do types.type11 = nil end
159
+ assert_raise TypeError do types.type11 = 0 end
160
+ assert_raise TypeError do types.type11 = '' end
161
+ end
162
+
163
+ def test_string
164
+ types = Test::Types::TestTypes.new
165
+ assert_nothing_raised do types.type12 = '' end
166
+ assert_nothing_raised do types.type12 = 'hello' end
167
+ assert_nothing_raised do types.type12 = nil end
168
+ assert_raise TypeError do types.type12 = 0 end
169
+ assert_raise TypeError do types.type12 = true end
170
+ end
171
+
172
+ def test_bytes
173
+ types = Test::Types::TestTypes.new
174
+ assert_nothing_raised do types.type13 = '' end
175
+ assert_nothing_raised do types.type13 = 'hello' end
176
+ assert_nothing_raised do types.type13 = nil end
177
+ assert_raise TypeError do types.type13 = 0 end
178
+ assert_raise TypeError do types.type13 = true end
179
+ assert_raise TypeError do types.type13 = [] end
180
+ end
181
+ end