protobuf 2.8.0.beta9 → 2.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (204) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +0 -1
  3. data/{UPGRADING.md → CHANGES.md} +20 -12
  4. data/LICENSE.txt +14 -0
  5. data/README.md +60 -74
  6. data/bin/protoc-gen-ruby +26 -0
  7. data/bin/rprotoc +4 -58
  8. data/lib/protobuf.rb +15 -13
  9. data/lib/protobuf/code_generator.rb +41 -0
  10. data/lib/protobuf/descriptors/google/protobuf/compiler/plugin.pb.rb +51 -0
  11. data/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb +248 -0
  12. data/lib/protobuf/field/base_field.rb +0 -1
  13. data/lib/protobuf/field/bytes_field.rb +1 -1
  14. data/lib/protobuf/field/message_field.rb +2 -4
  15. data/lib/protobuf/field/varint_field.rb +4 -7
  16. data/lib/protobuf/generators/base.rb +71 -0
  17. data/lib/protobuf/generators/enum_generator.rb +31 -0
  18. data/lib/protobuf/generators/extension_generator.rb +28 -0
  19. data/lib/protobuf/generators/field_generator.rb +132 -0
  20. data/lib/protobuf/generators/file_generator.rb +140 -0
  21. data/lib/protobuf/generators/group_generator.rb +113 -0
  22. data/lib/protobuf/generators/message_generator.rb +99 -0
  23. data/lib/protobuf/generators/printable.rb +161 -0
  24. data/lib/protobuf/generators/service_generator.rb +27 -0
  25. data/lib/protobuf/lifecycle.rb +7 -3
  26. data/lib/protobuf/message.rb +51 -34
  27. data/lib/protobuf/rpc/connectors/common.rb +4 -6
  28. data/lib/protobuf/rpc/server.rb +1 -1
  29. data/lib/protobuf/rpc/servers/zmq/server.rb +2 -2
  30. data/lib/protobuf/rpc/servers/zmq/worker.rb +2 -2
  31. data/lib/protobuf/rpc/service.rb +3 -3
  32. data/lib/protobuf/rpc/service_directory.rb +24 -12
  33. data/lib/protobuf/version.rb +1 -1
  34. data/proto/google/protobuf/compiler/plugin.proto +147 -0
  35. data/proto/google/protobuf/descriptor.proto +620 -0
  36. data/protobuf.gemspec +12 -21
  37. data/spec/bin/protoc-gen-ruby_spec.rb +18 -0
  38. data/spec/data/data.bin +3 -0
  39. data/{test/data/unk.png → spec/data/types.bin} +0 -0
  40. data/spec/encoding/all_types_spec.rb +91 -0
  41. data/spec/encoding/extreme_values_spec.rb +0 -0
  42. data/spec/lib/protobuf/code_generator_spec.rb +60 -0
  43. data/spec/lib/protobuf/generators/base_spec.rb +87 -0
  44. data/spec/lib/protobuf/generators/enum_generator_spec.rb +45 -0
  45. data/spec/lib/protobuf/generators/extension_generator_spec.rb +43 -0
  46. data/spec/lib/protobuf/generators/field_generator_spec.rb +99 -0
  47. data/spec/lib/protobuf/generators/file_generator_spec.rb +29 -0
  48. data/spec/lib/protobuf/generators/message_generator_spec.rb +0 -0
  49. data/spec/lib/protobuf/generators/service_generator_spec.rb +43 -0
  50. data/spec/lib/protobuf/lifecycle_spec.rb +31 -1
  51. data/spec/lib/protobuf/logger_spec.rb +5 -0
  52. data/spec/lib/protobuf/message_spec.rb +22 -8
  53. data/spec/lib/protobuf/rpc/connectors/common_spec.rb +1 -1
  54. data/spec/lib/protobuf/rpc/service_directory_spec.rb +45 -7
  55. data/spec/lib/protobuf/rpc/service_spec.rb +3 -3
  56. data/spec/spec_helper.rb +15 -16
  57. data/spec/support/test/all_types.data.bin +0 -0
  58. data/spec/support/test/all_types.data.txt +119 -0
  59. data/spec/support/test/defaults.pb.rb +25 -0
  60. data/spec/support/test/defaults.proto +9 -0
  61. data/spec/support/test/enum.pb.rb +10 -8
  62. data/spec/support/test/extended.pb.rb +5 -3
  63. data/spec/support/test/extreme_values.data.bin +0 -0
  64. data/spec/support/test/google_unittest.pb.rb +543 -0
  65. data/spec/support/test/google_unittest.proto +713 -0
  66. data/spec/support/test/google_unittest_import.pb.rb +37 -0
  67. data/{ext/protobuf-2.4.1/src/google/protobuf/io/package_info.h → spec/support/test/google_unittest_import.proto} +27 -17
  68. data/spec/support/test/google_unittest_import_public.pb.rb +8 -0
  69. data/{ext/protobuf-2.4.1/src/google/protobuf/io/coded_stream_inl.h → spec/support/test/google_unittest_import_public.proto} +5 -31
  70. data/spec/support/test/multi_field_extensions.pb.rb +29 -7
  71. data/spec/support/test/multi_field_extensions.proto +12 -2
  72. data/spec/support/test/resource.pb.rb +20 -16
  73. metadata +149 -236
  74. data/examples/addressbook.pb.rb +0 -55
  75. data/examples/addressbook.proto +0 -24
  76. data/examples/reading_a_message.rb +0 -32
  77. data/examples/writing_a_message.rb +0 -46
  78. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/code_generator.h +0 -142
  79. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/command_line_interface.h +0 -318
  80. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_enum.h +0 -99
  81. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_enum_field.h +0 -103
  82. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_extension.h +0 -85
  83. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_field.h +0 -167
  84. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_file.h +0 -98
  85. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_generator.h +0 -72
  86. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_helpers.h +0 -159
  87. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_message.h +0 -170
  88. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_message_field.h +0 -102
  89. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_primitive_field.h +0 -103
  90. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_service.h +0 -118
  91. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_string_field.h +0 -104
  92. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_test_bad_identifiers.pb.h +0 -2721
  93. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/importer.h +0 -303
  94. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_enum.h +0 -84
  95. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_enum_field.h +0 -121
  96. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_extension.h +0 -77
  97. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_field.h +0 -108
  98. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_file.h +0 -101
  99. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_generator.h +0 -72
  100. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_helpers.h +0 -213
  101. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_message.h +0 -109
  102. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_message_field.h +0 -134
  103. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_primitive_field.h +0 -121
  104. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_service.h +0 -113
  105. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_string_field.h +0 -120
  106. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/mock_code_generator.h +0 -113
  107. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/package_info.h +0 -64
  108. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/parser.h +0 -434
  109. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/plugin.h +0 -73
  110. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/plugin.pb.h +0 -790
  111. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/python/python_generator.h +0 -156
  112. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/subprocess.h +0 -108
  113. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/zip_writer.h +0 -93
  114. data/ext/protobuf-2.4.1/src/google/protobuf/descriptor.h +0 -1367
  115. data/ext/protobuf-2.4.1/src/google/protobuf/descriptor.pb.h +0 -5223
  116. data/ext/protobuf-2.4.1/src/google/protobuf/descriptor_database.h +0 -366
  117. data/ext/protobuf-2.4.1/src/google/protobuf/dynamic_message.h +0 -136
  118. data/ext/protobuf-2.4.1/src/google/protobuf/extension_set.h +0 -904
  119. data/ext/protobuf-2.4.1/src/google/protobuf/generated_message_reflection.h +0 -424
  120. data/ext/protobuf-2.4.1/src/google/protobuf/generated_message_util.h +0 -82
  121. data/ext/protobuf-2.4.1/src/google/protobuf/io/coded_stream.h +0 -1102
  122. data/ext/protobuf-2.4.1/src/google/protobuf/io/gzip_stream.h +0 -207
  123. data/ext/protobuf-2.4.1/src/google/protobuf/io/printer.h +0 -136
  124. data/ext/protobuf-2.4.1/src/google/protobuf/io/tokenizer.h +0 -313
  125. data/ext/protobuf-2.4.1/src/google/protobuf/io/zero_copy_stream.h +0 -238
  126. data/ext/protobuf-2.4.1/src/google/protobuf/io/zero_copy_stream_impl.h +0 -357
  127. data/ext/protobuf-2.4.1/src/google/protobuf/io/zero_copy_stream_impl_lite.h +0 -340
  128. data/ext/protobuf-2.4.1/src/google/protobuf/message.h +0 -692
  129. data/ext/protobuf-2.4.1/src/google/protobuf/message_lite.h +0 -239
  130. data/ext/protobuf-2.4.1/src/google/protobuf/package_info.h +0 -64
  131. data/ext/protobuf-2.4.1/src/google/protobuf/reflection_ops.h +0 -80
  132. data/ext/protobuf-2.4.1/src/google/protobuf/repeated_field.h +0 -1295
  133. data/ext/protobuf-2.4.1/src/google/protobuf/service.h +0 -291
  134. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/common.h +0 -1211
  135. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/hash.h +0 -220
  136. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/map-util.h +0 -119
  137. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/once.h +0 -123
  138. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/stl_util-inl.h +0 -121
  139. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/strutil.h +0 -457
  140. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/substitute.h +0 -170
  141. data/ext/protobuf-2.4.1/src/google/protobuf/test_util.h +0 -174
  142. data/ext/protobuf-2.4.1/src/google/protobuf/test_util_lite.h +0 -101
  143. data/ext/protobuf-2.4.1/src/google/protobuf/testing/file.h +0 -83
  144. data/ext/protobuf-2.4.1/src/google/protobuf/testing/googletest.h +0 -98
  145. data/ext/protobuf-2.4.1/src/google/protobuf/text_format.h +0 -285
  146. data/ext/protobuf-2.4.1/src/google/protobuf/unittest.pb.h +0 -11915
  147. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_custom_options.pb.h +0 -2895
  148. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_embed_optimize_for.pb.h +0 -211
  149. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_empty.pb.h +0 -56
  150. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_import.pb.h +0 -188
  151. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_import_lite.pb.h +0 -151
  152. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_lite.pb.h +0 -4752
  153. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_lite_imports_nonlite.pb.h +0 -150
  154. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_mset.pb.h +0 -816
  155. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_no_generic_services.pb.h +0 -197
  156. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_optimize_for.pb.h +0 -403
  157. data/ext/protobuf-2.4.1/src/google/protobuf/unknown_field_set.h +0 -268
  158. data/ext/protobuf-2.4.1/src/google/protobuf/wire_format.h +0 -304
  159. data/ext/protobuf-2.4.1/src/google/protobuf/wire_format_lite.h +0 -620
  160. data/ext/protobuf-2.4.1/src/google/protobuf/wire_format_lite_inl.h +0 -774
  161. data/ext/ruby_generator/Makefile +0 -10
  162. data/ext/ruby_generator/RubyGenerator.cpp +0 -544
  163. data/ext/ruby_generator/RubyGenerator.h +0 -206
  164. data/ext/ruby_generator/extconf.rb +0 -35
  165. data/test/data/data_source.py +0 -14
  166. data/test/data/types_source.py +0 -22
  167. data/test/proto/addressbook.pb.rb +0 -66
  168. data/test/proto/addressbook.proto +0 -33
  169. data/test/proto/addressbook_base.pb.rb +0 -58
  170. data/test/proto/addressbook_base.proto +0 -26
  171. data/test/proto/addressbook_ext.pb.rb +0 -20
  172. data/test/proto/addressbook_ext.proto +0 -6
  173. data/test/proto/collision.pb.rb +0 -17
  174. data/test/proto/collision.proto +0 -5
  175. data/test/proto/ext_collision.pb.rb +0 -24
  176. data/test/proto/ext_collision.proto +0 -8
  177. data/test/proto/ext_range.pb.rb +0 -22
  178. data/test/proto/ext_range.proto +0 -7
  179. data/test/proto/float_default.proto +0 -10
  180. data/test/proto/lowercase.pb.rb +0 -30
  181. data/test/proto/lowercase.proto +0 -9
  182. data/test/proto/merge.pb.rb +0 -39
  183. data/test/proto/merge.proto +0 -15
  184. data/test/proto/nested.pb.rb +0 -30
  185. data/test/proto/nested.proto +0 -9
  186. data/test/proto/optional_field.pb.rb +0 -35
  187. data/test/proto/optional_field.proto +0 -12
  188. data/test/proto/packed.pb.rb +0 -22
  189. data/test/proto/packed.proto +0 -6
  190. data/test/proto/rpc.proto +0 -6
  191. data/test/proto/types.pb.rb +0 -84
  192. data/test/proto/types.proto +0 -37
  193. data/test/test_addressbook.rb +0 -56
  194. data/test/test_enum_value.rb +0 -41
  195. data/test/test_extension.rb +0 -36
  196. data/test/test_lowercase.rb +0 -11
  197. data/test/test_message.rb +0 -128
  198. data/test/test_optional_field.rb +0 -103
  199. data/test/test_packed_field.rb +0 -40
  200. data/test/test_parse.rb +0 -15
  201. data/test/test_repeated_types.rb +0 -132
  202. data/test/test_serialize.rb +0 -61
  203. data/test/test_standard_message.rb +0 -96
  204. data/test/test_types.rb +0 -226
data/test/test_parse.rb DELETED
@@ -1,15 +0,0 @@
1
- require 'test/unit'
2
- require 'test/proto/addressbook.pb'
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
@@ -1,132 +0,0 @@
1
- require 'test/unit'
2
- require 'test/proto/types.pb'
3
-
4
- class RepeatedTypesTest < Test::Unit::TestCase
5
- def test_serialize
6
- # empty set
7
- types = Test::Types::RepeatedTypes.new
8
- assert_equal('', types.to_s)
9
-
10
- # has 1 member
11
- types = Test::Types::RepeatedTypes.new
12
- types.type1 << 0.01
13
- types.type2 << 0.1
14
- types.type3 << 1
15
- types.type4 << 10
16
- types.type5 << 100
17
- types.type6 << 1000
18
- types.type7 << -1
19
- types.type8 << -10
20
- types.type9 << 10000
21
- types.type10 << 100000
22
- types.type11 << false
23
- types.type12 << 'hello all types'
24
- image_bin = File.open('test/data/unk.png', 'r+b'){|f| f.read}
25
- types.type13 << image_bin
26
- types.type14 << -100
27
- types.type15 << -1000
28
-
29
- serialized_string = types.serialize_to_string
30
-
31
- types2 = Test::Types::RepeatedTypes.new
32
- types2.parse_from_string serialized_string
33
- assert_in_delta(0.01, types2.type1[0], 0.00001)
34
- assert_in_delta(0.1, types2.type2[0], 0.00001)
35
- assert_equal(1, types2.type3[0])
36
- assert_equal(10, types2.type4[0])
37
- assert_equal(100, types2.type5[0])
38
- assert_equal(1000, types2.type6[0])
39
- assert_equal(-1, types2.type7[0])
40
- assert_equal(-10, types2.type8[0])
41
- assert_equal(10000, types2.type9[0])
42
- assert_equal(100000, types2.type10[0])
43
- assert(!types2.type11[0])
44
- assert_equal('hello all types', types2.type12[0])
45
- assert_equal(10938, types2.type13[0].size)
46
- assert_equal(image_bin, types2.type13[0])
47
- assert_equal(-100, types2.type14[0])
48
- assert_equal(-1000, types2.type15[0])
49
-
50
- # has 2 members
51
- types.type1 << 1.0/0 # double (Inf)
52
- types.type2 << -1.0/0 # float (-Inf)
53
- types.type3 << -1 # int32
54
- types.type4 << -10 # int64
55
- types.type5 << 100 # uint32
56
- types.type6 << 1000 # uint64
57
- types.type7 << -1000 # sint32
58
- types.type8 << -10000 # sint64
59
- types.type9 << 10000 # fixed32
60
- types.type10 << 100000 # fixed64
61
- types.type11 << true
62
- types.type12 << 'hello all types'
63
- image_bin = File.open('test/data/unk.png', 'r+b'){|f| f.read}
64
- types.type13 << image_bin
65
- types.type14 << -2_000_000_000 # sfixed32
66
- types.type15 << -8_000_000_000_000_000_000 # sfixed64
67
-
68
- serialized_string = types.serialize_to_string
69
-
70
- types2 = Test::Types::RepeatedTypes.new
71
- types2.parse_from_string serialized_string
72
- assert_in_delta(0.01, types2.type1[0], 0.00001)
73
- assert_in_delta(0.1, types2.type2[0], 0.00001)
74
- assert_equal(1, types2.type3[0])
75
- assert_equal(10, types2.type4[0])
76
- assert_equal(100, types2.type5[0])
77
- assert_equal(1000, types2.type6[0])
78
- assert_equal(-1, types2.type7[0])
79
- assert_equal(-10, types2.type8[0])
80
- assert_equal(10000, types2.type9[0])
81
- assert_equal(100000, types2.type10[0])
82
- assert(!types2.type11[0])
83
- assert_equal('hello all types', types2.type12[0])
84
- assert_equal(10938, types2.type13[0].size)
85
- assert_equal(image_bin, types2.type13[0])
86
- assert_equal(-100, types2.type14[0])
87
- assert_equal(-1000, types2.type15[0])
88
-
89
- assert_equal(1.0/0.0, types2.type1[1])
90
- assert_equal(-1.0/0.0, types2.type2[1])
91
- assert_equal(-1, types2.type3[1])
92
- assert_equal(-10, types2.type4[1])
93
- assert_equal(100, types2.type5[1])
94
- assert_equal(1000, types2.type6[1])
95
- assert_equal(-1000, types2.type7[1])
96
- assert_equal(-10000, types2.type8[1])
97
- assert_equal(10000, types2.type9[1])
98
- assert_equal(100000, types2.type10[1])
99
- assert types2.type11[1]
100
- assert_equal('hello all types', types2.type12[1])
101
- assert_equal(10938, types2.type13[1].size)
102
- assert_equal(image_bin, types2.type13[1])
103
- assert_equal(-2_000_000_000, types2.type14[1])
104
- assert_equal(-8_000_000_000_000_000_000, types2.type15[1])
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
132
- end
@@ -1,61 +0,0 @@
1
- # encoding: utf-8
2
- require 'test/unit'
3
- require 'test/proto/addressbook.pb'
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
-
43
- def test_unknown_field
44
- person = Tutorial::Person.new
45
- person.id = 1234
46
- person.name = 'a b c'
47
- serialized_string = person.serialize_to_string
48
-
49
- # add invalid field
50
- tag = 1000
51
- wire_type = Protobuf::WireType::VARINT
52
- serialized_string << Protobuf::Field::VarintField.encode((tag << 3) | wire_type)
53
- serialized_string << Protobuf::Field::VarintField.encode(100)
54
-
55
- # decode
56
- person2 = Tutorial::Person.new
57
- person2.parse_from_string(serialized_string)
58
-
59
- assert_equal(person, person2)
60
- end
61
- end
@@ -1,96 +0,0 @@
1
- require 'test/unit'
2
- require 'protobuf/message/message'
3
- require 'protobuf/enum'
4
- require 'test/proto/addressbook.pb'
5
-
6
- class StandardMessageTest < Test::Unit::TestCase
7
- def test_initialized
8
- person = Tutorial::Person.new
9
- assert(!person.initialized?)
10
- assert_raise(Protobuf::NotInitializedError) { person.to_s }
11
- person.name = 'name'
12
- assert(!person.initialized?)
13
- person.id = 12
14
- assert( person.initialized?)
15
-
16
- # repeated field
17
- person.phone << Tutorial::Person::PhoneNumber.new
18
- assert(!person.initialized?)
19
- person.phone.last.number = '123-456'
20
- assert( person.initialized?)
21
- end
22
-
23
- def test_clear
24
- person = Tutorial::Person.new
25
- person.name = 'name'
26
- person.id = 1234
27
- person.email = 'abc@cde.fgh'
28
- person.phone << Tutorial::Person::PhoneNumber.new
29
- person.clear!
30
-
31
- assert_nil(person.name)
32
- assert_nil(person.id)
33
- assert_equal('', person.email)
34
- assert_equal(0, person.phone.size)
35
-
36
- assert(!person.has_field?(:name))
37
- assert(!person.has_field?(:id))
38
- assert(!person.has_field?(:email))
39
- end
40
-
41
- def test_dup
42
- person = Tutorial::Person.new
43
- person.name = 'name'
44
- person.id = 1234
45
- person.email = 'abc@cde.fgh'
46
- person.phone << Tutorial::Person::PhoneNumber.new
47
- person.phone.last.number = '123-456'
48
- person.phone.last.type = Tutorial::Person::PhoneType::MOBILE
49
- person.phone << Tutorial::Person::PhoneNumber.new
50
- person.phone.last.number = '456-123'
51
- person.phone.last.type = Tutorial::Person::PhoneType::WORK
52
-
53
- person2 = person.dup
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)
68
- end
69
-
70
- def test_inspect
71
- person = Tutorial::Person.new
72
- person.name = 'name'
73
- person.id = 1234
74
- person.email = 'abc@cde.fgh'
75
- person.phone << Tutorial::Person::PhoneNumber.new
76
- person.phone.last.number = '123-456'
77
- person.phone.last.type = Tutorial::Person::PhoneType::MOBILE
78
- person.phone << Tutorial::Person::PhoneNumber.new
79
- person.phone.last.number = '456-123'
80
- person.phone.last.type = Tutorial::Person::PhoneType::WORK
81
-
82
- assert_equal <<-EOS, person.inspect
83
- name: "name"
84
- id: 1234
85
- email: "abc@cde.fgh"
86
- phone {
87
- number: "123-456"
88
- type: MOBILE
89
- }
90
- phone {
91
- number: "456-123"
92
- type: WORK
93
- }
94
- EOS
95
- end
96
- end
data/test/test_types.rb DELETED
@@ -1,226 +0,0 @@
1
- require 'test/unit'
2
- require 'test/proto/types.pb'
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
- types.type14 = -100
22
- types.type15 = -1000
23
-
24
- serialized_string = types.serialize_to_string
25
-
26
- types2 = Test::Types::TestTypes.new
27
- types2.parse_from_string(serialized_string)
28
- assert_in_delta(0.01, types2.type1, 0.00001)
29
- assert_in_delta(0.1, types2.type2, 0.00001)
30
- assert_equal(1, types2.type3)
31
- assert_equal(10, types2.type4)
32
- assert_equal(100, types2.type5)
33
- assert_equal(1000, types2.type6)
34
- assert_equal(-1, types2.type7)
35
- assert_equal(-10, types2.type8)
36
- assert_equal(10000, types2.type9)
37
- assert_equal(100000, types2.type10)
38
- assert(!types2.type11)
39
- assert_equal('hello all types', types2.type12)
40
- assert_equal(10938, types2.type13.size)
41
- assert_equal(image_bin, types2.type13)
42
- assert_equal(-100, types2.type14)
43
- assert_equal(-1000, types2.type15)
44
- end
45
-
46
- def test_serialize2
47
- types = Test::Types::TestTypes.new
48
- types.type1 = 1.0/0 # double (Inf)
49
- types.type2 = -1.0/0 # float (-Inf)
50
- types.type3 = -1 # int32
51
- types.type4 = -10 # int64
52
- types.type5 = 100 # uint32
53
- types.type6 = 1000 # uint64
54
- types.type7 = -1000 # sint32
55
- types.type8 = -10000 # sint64
56
- types.type9 = 10000 # fixed32
57
- types.type10 = 100000 # fixed64
58
- types.type11 = true
59
- types.type12 = 'hello all types'
60
- image_bin = File.open('test/data/unk.png', 'r+b'){|f| f.read}
61
- types.type13 = image_bin
62
- types.type14 = -2_000_000_000 # sfixed32
63
- types.type15 = -8_000_000_000_000_000_000 # sfixed64
64
-
65
- serialized_string = types.serialize_to_string
66
-
67
- types2 = Test::Types::TestTypes.new
68
- types2.parse_from_string(serialized_string)
69
- assert_equal(1.0/0.0, types2.type1)
70
- assert_equal(-1.0/0.0, types2.type2)
71
- assert_equal(-1, types2.type3)
72
- assert_equal(-10, types2.type4)
73
- assert_equal(100, types2.type5)
74
- assert_equal(1000, types2.type6)
75
- assert_equal(-1000, types2.type7)
76
- assert_equal(-10000, types2.type8)
77
- assert_equal(10000, types2.type9)
78
- assert_equal(100000, types2.type10)
79
- assert(types2.type11)
80
- assert_equal('hello all types', types2.type12)
81
- assert_equal(10938, types2.type13.size)
82
- assert_equal(image_bin, types2.type13)
83
- assert_equal(-2_000_000_000, types2.type14)
84
- assert_equal(-8_000_000_000_000_000_000, types2.type15)
85
- end
86
-
87
- def test_parse
88
- types = Test::Types::TestTypes.new
89
- types.parse_from_file('test/data/types.bin')
90
- assert_in_delta(0.01, types.type1, 0.00001)
91
- assert_in_delta(0.1, types.type2, 0.00001)
92
- assert_equal(1, types.type3)
93
- assert_equal(10, types.type4)
94
- assert_equal(100, types.type5)
95
- assert_equal(1000, types.type6)
96
- assert_equal(-1, types.type7)
97
- assert_equal(-10, types.type8)
98
- assert_equal(10000, types.type9)
99
- assert_equal(100000, types.type10)
100
- assert_equal(false, !!types.type11)
101
- assert_equal('hello all types', types.type12)
102
- assert_equal(File.open('test/data/unk.png', 'r+b'){|f| f.read}, types.type13)
103
- end
104
-
105
- def test_double
106
- # double fixed 64-bit
107
- types = Test::Types::TestTypes.new
108
- assert_nothing_raised { types.type1 = 1 }
109
- assert_nothing_raised { types.type1 = 1.0 }
110
- assert_raise(TypeError) { types.type1 = '' }
111
- assert_nothing_raised { types.type1 = Protobuf::Field::DoubleField.max }
112
- assert_nothing_raised { types.type1 = Protobuf::Field::DoubleField.min }
113
- end
114
-
115
- def test_float
116
- # float fixed 32-bit
117
- types = Test::Types::TestTypes.new
118
- assert_nothing_raised { types.type2 = 1 }
119
- assert_nothing_raised { types.type2 = 1.0 }
120
- assert_raise(TypeError) { types.type2 = '' }
121
- assert_nothing_raised { types.type2 = Protobuf::Field::FloatField.max }
122
- assert_nothing_raised { types.type2 = Protobuf::Field::FloatField.min }
123
- end
124
-
125
- def test_int32
126
- types = Test::Types::TestTypes.new
127
- assert_nothing_raised { types.type3 = 1 }
128
- assert_nothing_raised { types.type3 = -1 }
129
- assert_raise(TypeError) { types.type3 = 1.0 }
130
- assert_raise(TypeError) { types.type3 = '' }
131
- end
132
-
133
- def test_int64
134
- types = Test::Types::TestTypes.new
135
- assert_nothing_raised { types.type4 = 1 }
136
- assert_nothing_raised { types.type4 = -1 }
137
- assert_raise(TypeError) { types.type4 = 1.0 }
138
- assert_raise(TypeError) { types.type4 = '' }
139
- end
140
-
141
- def test_uint32
142
- types = Test::Types::TestTypes.new
143
- assert_nothing_raised { types.type5 = 1 }
144
- assert_raise(RangeError) { types.type5 = -1 }
145
- assert_raise(TypeError) { types.type5 = 1.0 }
146
- assert_raise(TypeError) { types.type5 = '' }
147
- end
148
-
149
- def test_uint64
150
- types = Test::Types::TestTypes.new
151
- assert_nothing_raised { types.type6 = 1 }
152
- assert_raise(RangeError) { types.type6 = -1 }
153
- assert_raise(TypeError) { types.type6 = 1.0 }
154
- assert_raise(TypeError) { types.type6 = '' }
155
- end
156
-
157
- def test_sint32
158
- types = Test::Types::TestTypes.new
159
- assert_nothing_raised { types.type7 = 1 }
160
- assert_nothing_raised { types.type7 = -1 }
161
- assert_raise(TypeError) { types.type7 = 1.0 }
162
- assert_raise(TypeError) { types.type7 = '' }
163
- end
164
-
165
- def test_sint64
166
- types = Test::Types::TestTypes.new
167
- assert_nothing_raised { types.type8 = 1 }
168
- assert_nothing_raised { types.type8 = -1 }
169
- assert_raise(TypeError) { types.type8 = 1.0 }
170
- assert_raise(TypeError) { types.type8 = '' }
171
- end
172
-
173
- def test_fixed32
174
- types = Test::Types::TestTypes.new
175
- assert_nothing_raised { types.type9 = 1 }
176
- assert_raise(TypeError) { types.type9 = 1.0 }
177
- assert_raise(TypeError) { types.type9 = '' }
178
- assert_nothing_raised { types.type9 = Protobuf::Field::Fixed32Field.max }
179
- assert_raise(RangeError) { types.type9 = Protobuf::Field::Fixed32Field.max + 1 }
180
- assert_nothing_raised { types.type9 = Protobuf::Field::Fixed32Field.min }
181
- assert_raise(RangeError) { types.type9 = Protobuf::Field::Fixed32Field.min - 1 }
182
- end
183
-
184
- def test_fixed64
185
- types = Test::Types::TestTypes.new
186
- assert_nothing_raised { types.type10 = 1 }
187
- assert_raise(TypeError) { types.type10 = 1.0 }
188
- assert_raise(TypeError) { types.type10 = '' }
189
- assert_nothing_raised { types.type10 = Protobuf::Field::Fixed64Field.max }
190
- assert_raise(RangeError) { types.type10 = Protobuf::Field::Fixed64Field.max + 1 }
191
- assert_nothing_raised { types.type10 = Protobuf::Field::Fixed64Field.min }
192
- assert_raise(RangeError) { types.type10 = Protobuf::Field::Fixed64Field.min - 1 }
193
- end
194
-
195
- def test_bool
196
- types = Test::Types::TestTypes.new
197
- assert_nothing_raised { types.type11 = true }
198
- assert_nothing_raised { types.type11 = false }
199
- assert_nothing_raised { types.type11 = nil }
200
- assert_raise(TypeError) { types.type11 = 0 }
201
- assert_raise(TypeError) { types.type11 = '' }
202
- end
203
-
204
- def test_string
205
- types = Test::Types::TestTypes.new
206
- assert_nothing_raised { types.type12 = '' }
207
- assert_nothing_raised { types.type12 = 'hello' }
208
- assert_nothing_raised { types.type12 = nil }
209
- assert_raise(TypeError) { types.type12 = 0 }
210
- assert_raise(TypeError) { types.type12 = true }
211
- end
212
-
213
- def test_bytes
214
- types = Test::Types::TestTypes.new
215
- assert_nothing_raised { types.type13 = '' }
216
- assert_nothing_raised { types.type13 = 'hello' }
217
- assert_nothing_raised { types.type13 = nil }
218
- assert_raise(TypeError) { types.type13 = 0 }
219
- assert_raise(TypeError) { types.type13 = true }
220
- assert_raise(TypeError) { types.type13 = [] }
221
- end
222
-
223
- def test_varint_getbytes
224
- assert_equal("\xac\x02", Protobuf::Field::VarintField.encode(300))
225
- end
226
- end