protobuf-cucumber 3.10.4

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 +21 -0
  3. data/.rubocop.yml +70 -0
  4. data/.rubocop_todo.yml +145 -0
  5. data/.travis.yml +40 -0
  6. data/.yardopts +5 -0
  7. data/CHANGES.md +344 -0
  8. data/CONTRIBUTING.md +16 -0
  9. data/Gemfile +3 -0
  10. data/LICENSE.txt +22 -0
  11. data/README.md +33 -0
  12. data/Rakefile +64 -0
  13. data/bin/protoc-gen-ruby +22 -0
  14. data/bin/rpc_server +5 -0
  15. data/install-protobuf.sh +28 -0
  16. data/lib/protobuf.rb +129 -0
  17. data/lib/protobuf/cli.rb +257 -0
  18. data/lib/protobuf/code_generator.rb +120 -0
  19. data/lib/protobuf/decoder.rb +28 -0
  20. data/lib/protobuf/deprecation.rb +117 -0
  21. data/lib/protobuf/descriptors.rb +3 -0
  22. data/lib/protobuf/descriptors/google/protobuf/compiler/plugin.pb.rb +62 -0
  23. data/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb +301 -0
  24. data/lib/protobuf/encoder.rb +11 -0
  25. data/lib/protobuf/enum.rb +365 -0
  26. data/lib/protobuf/exceptions.rb +9 -0
  27. data/lib/protobuf/field.rb +74 -0
  28. data/lib/protobuf/field/base_field.rb +380 -0
  29. data/lib/protobuf/field/base_field_object_definitions.rb +504 -0
  30. data/lib/protobuf/field/bool_field.rb +64 -0
  31. data/lib/protobuf/field/bytes_field.rb +78 -0
  32. data/lib/protobuf/field/double_field.rb +25 -0
  33. data/lib/protobuf/field/enum_field.rb +61 -0
  34. data/lib/protobuf/field/field_array.rb +104 -0
  35. data/lib/protobuf/field/field_hash.rb +122 -0
  36. data/lib/protobuf/field/fixed32_field.rb +25 -0
  37. data/lib/protobuf/field/fixed64_field.rb +28 -0
  38. data/lib/protobuf/field/float_field.rb +43 -0
  39. data/lib/protobuf/field/int32_field.rb +21 -0
  40. data/lib/protobuf/field/int64_field.rb +34 -0
  41. data/lib/protobuf/field/integer_field.rb +23 -0
  42. data/lib/protobuf/field/message_field.rb +51 -0
  43. data/lib/protobuf/field/sfixed32_field.rb +27 -0
  44. data/lib/protobuf/field/sfixed64_field.rb +28 -0
  45. data/lib/protobuf/field/signed_integer_field.rb +29 -0
  46. data/lib/protobuf/field/sint32_field.rb +21 -0
  47. data/lib/protobuf/field/sint64_field.rb +21 -0
  48. data/lib/protobuf/field/string_field.rb +51 -0
  49. data/lib/protobuf/field/uint32_field.rb +21 -0
  50. data/lib/protobuf/field/uint64_field.rb +21 -0
  51. data/lib/protobuf/field/varint_field.rb +77 -0
  52. data/lib/protobuf/generators/base.rb +85 -0
  53. data/lib/protobuf/generators/enum_generator.rb +39 -0
  54. data/lib/protobuf/generators/extension_generator.rb +27 -0
  55. data/lib/protobuf/generators/field_generator.rb +193 -0
  56. data/lib/protobuf/generators/file_generator.rb +262 -0
  57. data/lib/protobuf/generators/group_generator.rb +122 -0
  58. data/lib/protobuf/generators/message_generator.rb +104 -0
  59. data/lib/protobuf/generators/option_generator.rb +17 -0
  60. data/lib/protobuf/generators/printable.rb +160 -0
  61. data/lib/protobuf/generators/service_generator.rb +50 -0
  62. data/lib/protobuf/lifecycle.rb +33 -0
  63. data/lib/protobuf/logging.rb +39 -0
  64. data/lib/protobuf/message.rb +260 -0
  65. data/lib/protobuf/message/fields.rb +233 -0
  66. data/lib/protobuf/message/serialization.rb +85 -0
  67. data/lib/protobuf/optionable.rb +70 -0
  68. data/lib/protobuf/rpc/buffer.rb +78 -0
  69. data/lib/protobuf/rpc/client.rb +140 -0
  70. data/lib/protobuf/rpc/connectors/base.rb +221 -0
  71. data/lib/protobuf/rpc/connectors/ping.rb +89 -0
  72. data/lib/protobuf/rpc/connectors/socket.rb +78 -0
  73. data/lib/protobuf/rpc/connectors/zmq.rb +319 -0
  74. data/lib/protobuf/rpc/dynamic_discovery.pb.rb +50 -0
  75. data/lib/protobuf/rpc/env.rb +60 -0
  76. data/lib/protobuf/rpc/error.rb +28 -0
  77. data/lib/protobuf/rpc/error/client_error.rb +31 -0
  78. data/lib/protobuf/rpc/error/server_error.rb +43 -0
  79. data/lib/protobuf/rpc/middleware.rb +25 -0
  80. data/lib/protobuf/rpc/middleware/exception_handler.rb +40 -0
  81. data/lib/protobuf/rpc/middleware/logger.rb +95 -0
  82. data/lib/protobuf/rpc/middleware/request_decoder.rb +79 -0
  83. data/lib/protobuf/rpc/middleware/response_encoder.rb +83 -0
  84. data/lib/protobuf/rpc/middleware/runner.rb +18 -0
  85. data/lib/protobuf/rpc/rpc.pb.rb +64 -0
  86. data/lib/protobuf/rpc/rpc_method.rb +16 -0
  87. data/lib/protobuf/rpc/server.rb +39 -0
  88. data/lib/protobuf/rpc/servers/socket/server.rb +121 -0
  89. data/lib/protobuf/rpc/servers/socket/worker.rb +56 -0
  90. data/lib/protobuf/rpc/servers/socket_runner.rb +46 -0
  91. data/lib/protobuf/rpc/servers/zmq/broker.rb +194 -0
  92. data/lib/protobuf/rpc/servers/zmq/server.rb +321 -0
  93. data/lib/protobuf/rpc/servers/zmq/util.rb +48 -0
  94. data/lib/protobuf/rpc/servers/zmq/worker.rb +105 -0
  95. data/lib/protobuf/rpc/servers/zmq_runner.rb +70 -0
  96. data/lib/protobuf/rpc/service.rb +172 -0
  97. data/lib/protobuf/rpc/service_directory.rb +261 -0
  98. data/lib/protobuf/rpc/service_dispatcher.rb +45 -0
  99. data/lib/protobuf/rpc/service_filters.rb +250 -0
  100. data/lib/protobuf/rpc/stat.rb +119 -0
  101. data/lib/protobuf/socket.rb +21 -0
  102. data/lib/protobuf/tasks.rb +1 -0
  103. data/lib/protobuf/tasks/compile.rake +80 -0
  104. data/lib/protobuf/varint.rb +20 -0
  105. data/lib/protobuf/varint_pure.rb +31 -0
  106. data/lib/protobuf/version.rb +3 -0
  107. data/lib/protobuf/wire_type.rb +10 -0
  108. data/lib/protobuf/zmq.rb +21 -0
  109. data/profile.html +5103 -0
  110. data/proto/dynamic_discovery.proto +44 -0
  111. data/proto/google/protobuf/compiler/plugin.proto +147 -0
  112. data/proto/google/protobuf/descriptor.proto +779 -0
  113. data/proto/rpc.proto +69 -0
  114. data/protobuf-cucumber.gemspec +57 -0
  115. data/spec/benchmark/tasks.rb +143 -0
  116. data/spec/bin/protoc-gen-ruby_spec.rb +23 -0
  117. data/spec/encoding/all_types_spec.rb +103 -0
  118. data/spec/encoding/extreme_values_spec.rb +0 -0
  119. data/spec/functional/class_inheritance_spec.rb +52 -0
  120. data/spec/functional/code_generator_spec.rb +58 -0
  121. data/spec/functional/socket_server_spec.rb +59 -0
  122. data/spec/functional/zmq_server_spec.rb +105 -0
  123. data/spec/lib/protobuf/cli_spec.rb +317 -0
  124. data/spec/lib/protobuf/code_generator_spec.rb +87 -0
  125. data/spec/lib/protobuf/enum_spec.rb +307 -0
  126. data/spec/lib/protobuf/field/bool_field_spec.rb +91 -0
  127. data/spec/lib/protobuf/field/double_field_spec.rb +9 -0
  128. data/spec/lib/protobuf/field/enum_field_spec.rb +44 -0
  129. data/spec/lib/protobuf/field/field_array_spec.rb +105 -0
  130. data/spec/lib/protobuf/field/field_hash_spec.rb +168 -0
  131. data/spec/lib/protobuf/field/fixed32_field_spec.rb +7 -0
  132. data/spec/lib/protobuf/field/fixed64_field_spec.rb +7 -0
  133. data/spec/lib/protobuf/field/float_field_spec.rb +90 -0
  134. data/spec/lib/protobuf/field/int32_field_spec.rb +120 -0
  135. data/spec/lib/protobuf/field/int64_field_spec.rb +7 -0
  136. data/spec/lib/protobuf/field/message_field_spec.rb +132 -0
  137. data/spec/lib/protobuf/field/sfixed32_field_spec.rb +9 -0
  138. data/spec/lib/protobuf/field/sfixed64_field_spec.rb +9 -0
  139. data/spec/lib/protobuf/field/sint32_field_spec.rb +9 -0
  140. data/spec/lib/protobuf/field/sint64_field_spec.rb +9 -0
  141. data/spec/lib/protobuf/field/string_field_spec.rb +79 -0
  142. data/spec/lib/protobuf/field/uint32_field_spec.rb +7 -0
  143. data/spec/lib/protobuf/field/uint64_field_spec.rb +7 -0
  144. data/spec/lib/protobuf/field_spec.rb +192 -0
  145. data/spec/lib/protobuf/generators/base_spec.rb +154 -0
  146. data/spec/lib/protobuf/generators/enum_generator_spec.rb +82 -0
  147. data/spec/lib/protobuf/generators/extension_generator_spec.rb +42 -0
  148. data/spec/lib/protobuf/generators/field_generator_spec.rb +197 -0
  149. data/spec/lib/protobuf/generators/file_generator_spec.rb +119 -0
  150. data/spec/lib/protobuf/generators/message_generator_spec.rb +0 -0
  151. data/spec/lib/protobuf/generators/service_generator_spec.rb +99 -0
  152. data/spec/lib/protobuf/lifecycle_spec.rb +94 -0
  153. data/spec/lib/protobuf/message_spec.rb +944 -0
  154. data/spec/lib/protobuf/optionable_spec.rb +265 -0
  155. data/spec/lib/protobuf/rpc/client_spec.rb +66 -0
  156. data/spec/lib/protobuf/rpc/connectors/base_spec.rb +226 -0
  157. data/spec/lib/protobuf/rpc/connectors/ping_spec.rb +69 -0
  158. data/spec/lib/protobuf/rpc/connectors/socket_spec.rb +34 -0
  159. data/spec/lib/protobuf/rpc/connectors/zmq_spec.rb +110 -0
  160. data/spec/lib/protobuf/rpc/middleware/exception_handler_spec.rb +62 -0
  161. data/spec/lib/protobuf/rpc/middleware/logger_spec.rb +49 -0
  162. data/spec/lib/protobuf/rpc/middleware/request_decoder_spec.rb +115 -0
  163. data/spec/lib/protobuf/rpc/middleware/response_encoder_spec.rb +91 -0
  164. data/spec/lib/protobuf/rpc/servers/socket_server_spec.rb +38 -0
  165. data/spec/lib/protobuf/rpc/servers/zmq/server_spec.rb +43 -0
  166. data/spec/lib/protobuf/rpc/servers/zmq/util_spec.rb +55 -0
  167. data/spec/lib/protobuf/rpc/servers/zmq/worker_spec.rb +35 -0
  168. data/spec/lib/protobuf/rpc/service_directory_spec.rb +293 -0
  169. data/spec/lib/protobuf/rpc/service_dispatcher_spec.rb +35 -0
  170. data/spec/lib/protobuf/rpc/service_filters_spec.rb +517 -0
  171. data/spec/lib/protobuf/rpc/service_spec.rb +162 -0
  172. data/spec/lib/protobuf/rpc/stat_spec.rb +101 -0
  173. data/spec/lib/protobuf/varint_spec.rb +29 -0
  174. data/spec/lib/protobuf_spec.rb +105 -0
  175. data/spec/spec_helper.rb +42 -0
  176. data/spec/support/all.rb +6 -0
  177. data/spec/support/packed_field.rb +23 -0
  178. data/spec/support/protos/all_types.data.bin +0 -0
  179. data/spec/support/protos/all_types.data.txt +119 -0
  180. data/spec/support/protos/enum.pb.rb +63 -0
  181. data/spec/support/protos/enum.proto +37 -0
  182. data/spec/support/protos/extreme_values.data.bin +0 -0
  183. data/spec/support/protos/google_unittest.bin +0 -0
  184. data/spec/support/protos/google_unittest.pb.rb +798 -0
  185. data/spec/support/protos/google_unittest.proto +884 -0
  186. data/spec/support/protos/google_unittest_custom_options.bin +0 -0
  187. data/spec/support/protos/google_unittest_custom_options.pb.rb +361 -0
  188. data/spec/support/protos/google_unittest_custom_options.proto +424 -0
  189. data/spec/support/protos/google_unittest_import.pb.rb +55 -0
  190. data/spec/support/protos/google_unittest_import.proto +73 -0
  191. data/spec/support/protos/google_unittest_import_public.pb.rb +31 -0
  192. data/spec/support/protos/google_unittest_import_public.proto +41 -0
  193. data/spec/support/protos/map-test.bin +157 -0
  194. data/spec/support/protos/map-test.pb.rb +85 -0
  195. data/spec/support/protos/map-test.proto +68 -0
  196. data/spec/support/protos/multi_field_extensions.pb.rb +59 -0
  197. data/spec/support/protos/multi_field_extensions.proto +35 -0
  198. data/spec/support/protos/resource.pb.rb +172 -0
  199. data/spec/support/protos/resource.proto +137 -0
  200. data/spec/support/resource_service.rb +23 -0
  201. data/spec/support/server.rb +65 -0
  202. data/spec/support/test_app_file.rb +2 -0
  203. data/varint_prof.rb +82 -0
  204. metadata +579 -0
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Protobuf::Field::DoubleField do
4
+
5
+ it_behaves_like :packable_field, described_class do
6
+ let(:value) { [1.0, 2.0, 3.0] }
7
+ end
8
+
9
+ end
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Protobuf::Field::EnumField do
4
+ let(:message) do
5
+ Class.new(::Protobuf::Message) do
6
+ enum_class = Class.new(::Protobuf::Enum) do
7
+ define :POSITIVE, 22
8
+ define :NEGATIVE, -33
9
+ end
10
+
11
+ optional enum_class, :enum, 1
12
+ end
13
+ end
14
+
15
+ describe '.{encode, decode}' do
16
+ it 'handles positive enum constants' do
17
+ instance = message.new(:enum => :POSITIVE)
18
+ expect(message.decode(instance.encode).enum).to eq(22)
19
+ end
20
+
21
+ it 'handles negative enum constants' do
22
+ instance = message.new(:enum => :NEGATIVE)
23
+ expect(message.decode(instance.encode).enum).to eq(-33)
24
+ end
25
+ end
26
+
27
+ # https://developers.google.com/protocol-buffers/docs/proto3#json
28
+ describe '.{to_json, from_json}' do
29
+ it 'serialises enum value as string' do
30
+ instance = message.new(:enum => :POSITIVE)
31
+ expect(instance.to_json).to eq('{"enum":"POSITIVE"}')
32
+ end
33
+
34
+ it 'deserialises enum value as integer' do
35
+ instance = message.from_json('{"enum":22}')
36
+ expect(instance.enum).to eq(22)
37
+ end
38
+
39
+ it 'deserialises enum value as string' do
40
+ instance = message.from_json('{"enum":"NEGATIVE"}')
41
+ expect(instance.enum).to eq(-33)
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,105 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Protobuf::Field::FieldArray do
4
+
5
+ let(:basic_message) do
6
+ Class.new(::Protobuf::Message) do
7
+ optional :string, :field, 1
8
+ end
9
+ end
10
+
11
+ let(:more_complex_message) do
12
+ Class.new(BasicMessage) do
13
+ end
14
+ end
15
+
16
+ let(:some_enum) do
17
+ Class.new(::Protobuf::Enum) do
18
+ define :FOO, 1
19
+ define :BAR, 2
20
+ define :BAZ, 3
21
+ end
22
+ end
23
+
24
+ let(:repeat_message) do
25
+ Class.new(::Protobuf::Message) do
26
+ optional :string, :some_string, 1
27
+ repeated :string, :multiple_strings, 2
28
+ repeated BasicMessage, :multiple_basic_msgs, 3
29
+ repeated SomeEnum, :multiple_enums, 4
30
+ end
31
+ end
32
+
33
+ before do
34
+ stub_const('BasicMessage', basic_message)
35
+ stub_const('MoreComplexMessage', more_complex_message)
36
+ stub_const('SomeEnum', some_enum)
37
+ stub_const('SomeRepeatMessage', repeat_message)
38
+ end
39
+
40
+ let(:instance) { SomeRepeatMessage.new }
41
+
42
+ %w(<< push).each do |method|
43
+ describe "\##{method}" do
44
+ context 'when applied to a string field array' do
45
+ it 'adds a string' do
46
+ expect(instance.multiple_strings).to be_empty
47
+ instance.multiple_strings.send(method, 'string 1')
48
+ expect(instance.multiple_strings).to eq(['string 1'])
49
+ instance.multiple_strings.send(method, 'string 2')
50
+ expect(instance.multiple_strings).to eq(['string 1', 'string 2'])
51
+ end
52
+
53
+ it 'fails if not adding a string' do
54
+ expect { instance.multiple_strings.send(method, 100.0) }.to raise_error(TypeError)
55
+ end
56
+ end
57
+
58
+ context 'when applied to a MessageField field array' do
59
+ it 'adds a MessageField object' do
60
+ expect(instance.multiple_basic_msgs).to be_empty
61
+ basic_msg1 = BasicMessage.new
62
+ instance.multiple_basic_msgs.send(method, basic_msg1)
63
+ expect(instance.multiple_basic_msgs).to eq([basic_msg1])
64
+ basic_msg2 = BasicMessage.new
65
+ instance.multiple_basic_msgs.send(method, basic_msg2)
66
+ expect(instance.multiple_basic_msgs).to eq([basic_msg1, basic_msg2])
67
+ end
68
+
69
+ it 'fails if not adding a MessageField' do
70
+ expect { instance.multiple_basic_msgs.send(method, 100.0) }.to raise_error(TypeError)
71
+ end
72
+
73
+ it 'adds a Hash from a MessageField object' do
74
+ expect(instance.multiple_basic_msgs).to be_empty
75
+ basic_msg1 = BasicMessage.new
76
+ basic_msg1.field = 'my value'
77
+ instance.multiple_basic_msgs.send(method, basic_msg1.to_hash)
78
+ expect(instance.multiple_basic_msgs).to eq([basic_msg1])
79
+ end
80
+
81
+ it 'does not downcast a MessageField' do
82
+ expect(instance.multiple_basic_msgs).to be_empty
83
+ basic_msg1 = MoreComplexMessage.new
84
+ instance.multiple_basic_msgs.send(method, basic_msg1)
85
+ expect(instance.multiple_basic_msgs).to eq([basic_msg1])
86
+ expect(instance.multiple_basic_msgs.first).to be_a(MoreComplexMessage)
87
+ end
88
+ end
89
+
90
+ context 'when applied to an EnumField field array' do
91
+ it 'adds an EnumField object' do
92
+ expect(instance.multiple_enums).to be_empty
93
+ instance.multiple_enums.send(method, SomeEnum::FOO)
94
+ expect(instance.multiple_enums).to eq([SomeEnum::FOO])
95
+ instance.multiple_enums.send(method, SomeEnum::BAR)
96
+ expect(instance.multiple_enums).to eq([SomeEnum::FOO, SomeEnum::BAR])
97
+ end
98
+
99
+ it 'fails if not adding an EnumField' do
100
+ expect { instance.multiple_basic_msgs.send(method, 100.0) }.to raise_error(TypeError)
101
+ end
102
+ end
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,168 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Protobuf::Field::FieldHash do
4
+
5
+ let(:basic_message) do
6
+ Class.new(::Protobuf::Message) do
7
+ optional :string, :field, 1
8
+ end
9
+ end
10
+
11
+ let(:more_complex_message) do
12
+ Class.new(BasicMessage) do
13
+ end
14
+ end
15
+
16
+ let(:some_enum) do
17
+ Class.new(::Protobuf::Enum) do
18
+ define :FOO, 1
19
+ define :BAR, 2
20
+ define :BAZ, 3
21
+ end
22
+ end
23
+
24
+ let(:map_message) do
25
+ Class.new(::Protobuf::Message) do
26
+ optional :string, :some_string, 1
27
+ map :int32, :string, :map_int32_to_string, 2
28
+ map :string, BasicMessage, :map_string_to_msg, 3
29
+ map :string, SomeEnum, :map_string_to_enum, 4
30
+ end
31
+ end
32
+
33
+ before do
34
+ stub_const('BasicMessage', basic_message)
35
+ stub_const('MoreComplexMessage', more_complex_message)
36
+ stub_const('SomeEnum', some_enum)
37
+ stub_const('SomeMapMessage', map_message)
38
+ end
39
+
40
+ let(:instance) { SomeMapMessage.new }
41
+
42
+ %w([]= store).each do |method|
43
+ describe "##{method}" do
44
+ context 'when applied to an int32->string field hash' do
45
+ it 'adds an int -> string entry' do
46
+ expect(instance.map_int32_to_string).to be_empty
47
+ instance.map_int32_to_string.send(method, 1, 'string 1')
48
+ expect(instance.map_int32_to_string).to eq(1 => 'string 1')
49
+ instance.map_int32_to_string.send(method, 2, 'string 2')
50
+ expect(instance.map_int32_to_string).to eq(1 => 'string 1', 2 => 'string 2')
51
+ end
52
+
53
+ it 'fails if not adding an int -> string' do
54
+ expect { instance.map_int32_to_string.send(method, 1, 100.0) }.to raise_error(TypeError)
55
+ expect { instance.map_int32_to_string.send(method, 'foo', 100.0) }.to raise_error(TypeError)
56
+ expect { instance.map_int32_to_string.send(method, BasicMessage.new, 100.0) }.to raise_error(TypeError)
57
+ expect { instance.map_int32_to_string.send(method, 'foo', 'bar') }.to raise_error(TypeError)
58
+ expect { instance.map_int32_to_string.send(method, nil, 'foo') }.to raise_error(TypeError)
59
+ expect { instance.map_int32_to_string.send(method, 1, nil) }.to raise_error(TypeError)
60
+ end
61
+ end
62
+
63
+ context 'when applied to a string->MessageField field hash' do
64
+ it 'adds a string -> MessageField entry' do
65
+ expect(instance.map_string_to_msg).to be_empty
66
+ basic_msg1 = BasicMessage.new
67
+ instance.map_string_to_msg.send(method, 'msg1', basic_msg1)
68
+ expect(instance.map_string_to_msg).to eq('msg1' => basic_msg1)
69
+ basic_msg2 = BasicMessage.new
70
+ instance.map_string_to_msg.send(method, 'msg2', basic_msg2)
71
+ expect(instance.map_string_to_msg).to eq('msg1' => basic_msg1, 'msg2' => basic_msg2)
72
+ end
73
+
74
+ it 'fails if not adding a string -> MessageField entry' do
75
+ expect { instance.map_string_to_msg.send(method, 1, 100.0) }.to raise_error(TypeError)
76
+ expect { instance.map_string_to_msg.send(method, 'foo', SomeEnum::FOO) }.to raise_error(TypeError)
77
+ expect { instance.map_string_to_msg.send(method, SomeEnum::FOO, BasicMessage.new) }.to raise_error(TypeError)
78
+ expect { instance.map_string_to_msg.send(method, nil, BasicMessage.new) }.to raise_error(TypeError)
79
+ expect { instance.map_string_to_msg.send(method, 'foo', nil) }.to raise_error(TypeError)
80
+ end
81
+
82
+ it 'adds a Hash from a MessageField object' do
83
+ expect(instance.map_string_to_msg).to be_empty
84
+ basic_msg1 = BasicMessage.new
85
+ basic_msg1.field = 'my value'
86
+ instance.map_string_to_msg.send(method, 'foo', basic_msg1.to_hash)
87
+ expect(instance.map_string_to_msg).to eq('foo' => basic_msg1)
88
+ end
89
+
90
+ it 'does not downcast a MessageField' do
91
+ expect(instance.map_string_to_msg).to be_empty
92
+ basic_msg1 = MoreComplexMessage.new
93
+ instance.map_string_to_msg.send(method, 'foo', basic_msg1)
94
+ expect(instance.map_string_to_msg).to eq('foo' => basic_msg1)
95
+ expect(instance.map_string_to_msg['foo']).to be_a(MoreComplexMessage)
96
+ end
97
+ end
98
+
99
+ context 'when applied to a string->EnumField field hash' do
100
+ it 'adds a string -> EnumField entry' do
101
+ expect(instance.map_string_to_enum).to be_empty
102
+ instance.map_string_to_enum.send(method, 'msg1', SomeEnum::FOO)
103
+ expect(instance.map_string_to_enum).to eq('msg1' => SomeEnum::FOO)
104
+ instance.map_string_to_enum.send(method, 'msg2', SomeEnum::BAR)
105
+ expect(instance.map_string_to_enum).to eq('msg1' => SomeEnum::FOO, 'msg2' => SomeEnum::BAR)
106
+ end
107
+
108
+ it 'fails if not adding a string -> EnumField entry' do
109
+ expect { instance.map_string_to_enum.send(method, 1, 100.0) }.to raise_error(TypeError)
110
+ expect { instance.map_string_to_enum.send(method, nil, 100.0) }.to raise_error(TypeError)
111
+ expect { instance.map_string_to_enum.send(method, 1, nil) }.to raise_error(TypeError)
112
+ expect { instance.map_string_to_enum.send(method, 'foo', BasicMessage.new) }.to raise_error(TypeError)
113
+ expect { instance.map_string_to_enum.send(method, 1, SomeEnum::FOO) }.to raise_error(TypeError)
114
+ expect { instance.map_string_to_enum.send(method, nil, SomeEnum::FOO) }.to raise_error(TypeError)
115
+ expect { instance.map_string_to_enum.send(method, 'foo', nil) }.to raise_error(TypeError)
116
+ end
117
+ end
118
+ end
119
+
120
+ describe '#to_hash_value' do
121
+ context 'when applied to an int32->string field hash' do
122
+ before do
123
+ instance.map_int32_to_string[1] = 'string 1'
124
+ instance.map_int32_to_string[2] = 'string 2'
125
+ end
126
+
127
+ it 'converts properly' do
128
+ expect(instance.to_hash_value).to eq(:map_int32_to_string => {
129
+ 1 => 'string 1',
130
+ 2 => 'string 2',
131
+ })
132
+ end
133
+ end
134
+
135
+ context 'when applied to a string->MessageField field hash' do
136
+ before do
137
+ instance.map_string_to_msg['msg1'] = BasicMessage.new(:field => 'string 1')
138
+ instance.map_string_to_msg['msg2'] = BasicMessage.new(:field => 'string 2')
139
+ end
140
+
141
+ it 'converts properly' do
142
+ expect(instance.to_hash_value).to eq(:map_string_to_msg => {
143
+ 'msg1' => {
144
+ :field => 'string 1',
145
+ },
146
+ 'msg2' => {
147
+ :field => 'string 2',
148
+ },
149
+ })
150
+ end
151
+ end
152
+
153
+ context 'when applied to a string->EnumField field hash' do
154
+ before do
155
+ instance.map_string_to_enum['msg1'] = SomeEnum::FOO
156
+ instance.map_string_to_enum['msg2'] = SomeEnum::BAR
157
+ end
158
+
159
+ it 'converts properly' do
160
+ expect(instance.to_hash_value).to eq(:map_string_to_enum => {
161
+ 'msg1' => 1,
162
+ 'msg2' => 2,
163
+ })
164
+ end
165
+ end
166
+ end
167
+ end
168
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Protobuf::Field::Fixed32Field do
4
+
5
+ it_behaves_like :packable_field, described_class
6
+
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Protobuf::Field::Fixed64Field do
4
+
5
+ it_behaves_like :packable_field, described_class
6
+
7
+ end
@@ -0,0 +1,90 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Protobuf::Field::FloatField do
4
+
5
+ it_behaves_like :packable_field, described_class do
6
+ let(:value) { [1.0, 2.0, 3.0] }
7
+ end
8
+
9
+ class SomeFloatMessage < ::Protobuf::Message
10
+ optional :float, :some_float, 1
11
+ end
12
+
13
+ let(:instance) { SomeFloatMessage.new }
14
+
15
+ describe 'setting and getting field' do
16
+ subject { instance.some_float = value; instance.some_float }
17
+
18
+ context 'when set with an int' do
19
+ let(:value) { 100 }
20
+
21
+ it 'is readable as a float' do
22
+ expect(subject).to eq(100.0)
23
+ end
24
+ end
25
+
26
+ context 'when set with a float' do
27
+ let(:value) { 100.1 }
28
+
29
+ it 'is readable as a float' do
30
+ expect(subject).to eq(100.1)
31
+ end
32
+ end
33
+
34
+ context 'when set with a string of a float' do
35
+ let(:value) { "101.1" }
36
+
37
+ it 'is readable as a float' do
38
+ expect(subject).to eq(101.1)
39
+ end
40
+ end
41
+
42
+ context 'when set with a non-numeric string' do
43
+ let(:value) { "aaaa" }
44
+
45
+ it 'throws an error' do
46
+ expect { subject }.to raise_error(TypeError)
47
+ end
48
+ end
49
+
50
+ context 'when set with something that is not a float' do
51
+ let(:value) { [1, 2, 3] }
52
+
53
+ it 'throws an error' do
54
+ expect { subject }.to raise_error(TypeError)
55
+ end
56
+ end
57
+ end
58
+
59
+ describe '#default_value' do
60
+ context 'optional and required fields' do
61
+ it 'returns the class default' do
62
+ expect(SomeFloatMessage.get_field('some_float').default).to be nil
63
+ expect(::Protobuf::Field::FloatField.default).to eq 0.0
64
+ expect(instance.some_float).to eq 0.0
65
+ end
66
+
67
+ context 'with field default' do
68
+ class AnotherFloatMessage < ::Protobuf::Message
69
+ optional :float, :set_float, 1, :default => 3.6
70
+ end
71
+
72
+ it 'returns the set default' do
73
+ expect(AnotherFloatMessage.get_field('set_float').default).to eq 3.6
74
+ expect(AnotherFloatMessage.new.set_float).to eq 3.6
75
+ end
76
+ end
77
+ end
78
+
79
+ context 'repeated field' do
80
+ class RepeatedFloatMessage < ::Protobuf::Message
81
+ repeated :float, :repeated_float, 1
82
+ end
83
+
84
+ it 'returns the set default' do
85
+ expect(RepeatedFloatMessage.new.repeated_float).to eq []
86
+ end
87
+ end
88
+ end
89
+
90
+ end
@@ -0,0 +1,120 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Protobuf::Field::Int32Field do
4
+
5
+ it_behaves_like :packable_field, described_class
6
+
7
+ class SomeInt32Message < ::Protobuf::Message
8
+ optional :int32, :some_int, 1
9
+ end
10
+
11
+ let(:instance) { SomeInt32Message.new }
12
+
13
+ describe 'setting and getting a field' do
14
+ subject { instance.some_int = value; instance.some_int }
15
+
16
+ context 'when set with an int' do
17
+ let(:value) { 100 }
18
+
19
+ it 'is readable as an int' do
20
+ expect(subject).to eq(100)
21
+ end
22
+ end
23
+
24
+ context 'when set with a float' do
25
+ let(:value) { 100.1 }
26
+
27
+ it 'is readable as an int' do
28
+ expect(subject).to eq(100)
29
+ end
30
+ end
31
+
32
+ context 'when set with a string of an int' do
33
+ let(:value) { "101" }
34
+
35
+ it 'is readable as an int' do
36
+ expect(subject).to eq(101)
37
+ end
38
+ end
39
+
40
+ context 'when set with a negative representation of an int as string' do
41
+ let(:value) { "-101" }
42
+
43
+ it 'is readable as a negative int' do
44
+ expect(subject).to eq(-101)
45
+ end
46
+ end
47
+
48
+ context 'when set with a non-numeric string' do
49
+ let(:value) { "aaaa" }
50
+
51
+ it 'throws an error' do
52
+ expect { subject }.to raise_error(TypeError)
53
+ end
54
+ end
55
+
56
+ context 'when set with a string of an int in hex format' do
57
+ let(:value) { "0x101" }
58
+
59
+ it 'throws an error' do
60
+ expect { subject }.to raise_error(TypeError)
61
+ end
62
+ end
63
+
64
+ context 'when set with a string of an int larger than int32 max' do
65
+ let(:value) { (described_class.max + 1).to_s }
66
+
67
+ it 'throws an error' do
68
+ expect { subject }.to raise_error(TypeError)
69
+ end
70
+ end
71
+
72
+ context 'when set with something that is not an int' do
73
+ let(:value) { [1, 2, 3] }
74
+
75
+ it 'throws an error' do
76
+ expect { subject }.to raise_error(TypeError)
77
+ end
78
+ end
79
+
80
+ context 'when set with a timestamp' do
81
+ let(:value) { Time.now }
82
+
83
+ it 'throws an error' do
84
+ expect { subject }.to raise_error(TypeError)
85
+ end
86
+ end
87
+ end
88
+
89
+ describe '#default_value' do
90
+ context 'optional and required fields' do
91
+ it 'returns the class default' do
92
+ expect(SomeInt32Message.get_field('some_int').default).to be nil
93
+ expect(::Protobuf::Field::Int32Field.default).to eq 0
94
+ expect(instance.some_int).to eq 0
95
+ end
96
+
97
+ context 'with field default' do
98
+ class AnotherIntMessage < ::Protobuf::Message
99
+ optional :int32, :set_int, 1, :default => 3
100
+ end
101
+
102
+ it 'returns the set default' do
103
+ expect(AnotherIntMessage.get_field('set_int').default).to eq 3
104
+ expect(AnotherIntMessage.new.set_int).to eq 3
105
+ end
106
+ end
107
+ end
108
+
109
+ context 'repeated field' do
110
+ class RepeatedIntMessage < ::Protobuf::Message
111
+ repeated :int32, :repeated_int, 1
112
+ end
113
+
114
+ it 'returns the set default' do
115
+ expect(RepeatedIntMessage.new.repeated_int).to eq []
116
+ end
117
+ end
118
+ end
119
+
120
+ end