protobuf-cucumber 3.10.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +21 -0
- data/.rubocop.yml +70 -0
- data/.rubocop_todo.yml +145 -0
- data/.travis.yml +40 -0
- data/.yardopts +5 -0
- data/CHANGES.md +344 -0
- data/CONTRIBUTING.md +16 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +22 -0
- data/README.md +33 -0
- data/Rakefile +64 -0
- data/bin/protoc-gen-ruby +22 -0
- data/bin/rpc_server +5 -0
- data/install-protobuf.sh +28 -0
- data/lib/protobuf.rb +129 -0
- data/lib/protobuf/cli.rb +257 -0
- data/lib/protobuf/code_generator.rb +120 -0
- data/lib/protobuf/decoder.rb +28 -0
- data/lib/protobuf/deprecation.rb +117 -0
- data/lib/protobuf/descriptors.rb +3 -0
- data/lib/protobuf/descriptors/google/protobuf/compiler/plugin.pb.rb +62 -0
- data/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb +301 -0
- data/lib/protobuf/encoder.rb +11 -0
- data/lib/protobuf/enum.rb +365 -0
- data/lib/protobuf/exceptions.rb +9 -0
- data/lib/protobuf/field.rb +74 -0
- data/lib/protobuf/field/base_field.rb +380 -0
- data/lib/protobuf/field/base_field_object_definitions.rb +504 -0
- data/lib/protobuf/field/bool_field.rb +64 -0
- data/lib/protobuf/field/bytes_field.rb +78 -0
- data/lib/protobuf/field/double_field.rb +25 -0
- data/lib/protobuf/field/enum_field.rb +61 -0
- data/lib/protobuf/field/field_array.rb +104 -0
- data/lib/protobuf/field/field_hash.rb +122 -0
- data/lib/protobuf/field/fixed32_field.rb +25 -0
- data/lib/protobuf/field/fixed64_field.rb +28 -0
- data/lib/protobuf/field/float_field.rb +43 -0
- data/lib/protobuf/field/int32_field.rb +21 -0
- data/lib/protobuf/field/int64_field.rb +34 -0
- data/lib/protobuf/field/integer_field.rb +23 -0
- data/lib/protobuf/field/message_field.rb +51 -0
- data/lib/protobuf/field/sfixed32_field.rb +27 -0
- data/lib/protobuf/field/sfixed64_field.rb +28 -0
- data/lib/protobuf/field/signed_integer_field.rb +29 -0
- data/lib/protobuf/field/sint32_field.rb +21 -0
- data/lib/protobuf/field/sint64_field.rb +21 -0
- data/lib/protobuf/field/string_field.rb +51 -0
- data/lib/protobuf/field/uint32_field.rb +21 -0
- data/lib/protobuf/field/uint64_field.rb +21 -0
- data/lib/protobuf/field/varint_field.rb +77 -0
- data/lib/protobuf/generators/base.rb +85 -0
- data/lib/protobuf/generators/enum_generator.rb +39 -0
- data/lib/protobuf/generators/extension_generator.rb +27 -0
- data/lib/protobuf/generators/field_generator.rb +193 -0
- data/lib/protobuf/generators/file_generator.rb +262 -0
- data/lib/protobuf/generators/group_generator.rb +122 -0
- data/lib/protobuf/generators/message_generator.rb +104 -0
- data/lib/protobuf/generators/option_generator.rb +17 -0
- data/lib/protobuf/generators/printable.rb +160 -0
- data/lib/protobuf/generators/service_generator.rb +50 -0
- data/lib/protobuf/lifecycle.rb +33 -0
- data/lib/protobuf/logging.rb +39 -0
- data/lib/protobuf/message.rb +260 -0
- data/lib/protobuf/message/fields.rb +233 -0
- data/lib/protobuf/message/serialization.rb +85 -0
- data/lib/protobuf/optionable.rb +70 -0
- data/lib/protobuf/rpc/buffer.rb +78 -0
- data/lib/protobuf/rpc/client.rb +140 -0
- data/lib/protobuf/rpc/connectors/base.rb +221 -0
- data/lib/protobuf/rpc/connectors/ping.rb +89 -0
- data/lib/protobuf/rpc/connectors/socket.rb +78 -0
- data/lib/protobuf/rpc/connectors/zmq.rb +319 -0
- data/lib/protobuf/rpc/dynamic_discovery.pb.rb +50 -0
- data/lib/protobuf/rpc/env.rb +60 -0
- data/lib/protobuf/rpc/error.rb +28 -0
- data/lib/protobuf/rpc/error/client_error.rb +31 -0
- data/lib/protobuf/rpc/error/server_error.rb +43 -0
- data/lib/protobuf/rpc/middleware.rb +25 -0
- data/lib/protobuf/rpc/middleware/exception_handler.rb +40 -0
- data/lib/protobuf/rpc/middleware/logger.rb +95 -0
- data/lib/protobuf/rpc/middleware/request_decoder.rb +79 -0
- data/lib/protobuf/rpc/middleware/response_encoder.rb +83 -0
- data/lib/protobuf/rpc/middleware/runner.rb +18 -0
- data/lib/protobuf/rpc/rpc.pb.rb +64 -0
- data/lib/protobuf/rpc/rpc_method.rb +16 -0
- data/lib/protobuf/rpc/server.rb +39 -0
- data/lib/protobuf/rpc/servers/socket/server.rb +121 -0
- data/lib/protobuf/rpc/servers/socket/worker.rb +56 -0
- data/lib/protobuf/rpc/servers/socket_runner.rb +46 -0
- data/lib/protobuf/rpc/servers/zmq/broker.rb +194 -0
- data/lib/protobuf/rpc/servers/zmq/server.rb +321 -0
- data/lib/protobuf/rpc/servers/zmq/util.rb +48 -0
- data/lib/protobuf/rpc/servers/zmq/worker.rb +105 -0
- data/lib/protobuf/rpc/servers/zmq_runner.rb +70 -0
- data/lib/protobuf/rpc/service.rb +172 -0
- data/lib/protobuf/rpc/service_directory.rb +261 -0
- data/lib/protobuf/rpc/service_dispatcher.rb +45 -0
- data/lib/protobuf/rpc/service_filters.rb +250 -0
- data/lib/protobuf/rpc/stat.rb +119 -0
- data/lib/protobuf/socket.rb +21 -0
- data/lib/protobuf/tasks.rb +1 -0
- data/lib/protobuf/tasks/compile.rake +80 -0
- data/lib/protobuf/varint.rb +20 -0
- data/lib/protobuf/varint_pure.rb +31 -0
- data/lib/protobuf/version.rb +3 -0
- data/lib/protobuf/wire_type.rb +10 -0
- data/lib/protobuf/zmq.rb +21 -0
- data/profile.html +5103 -0
- data/proto/dynamic_discovery.proto +44 -0
- data/proto/google/protobuf/compiler/plugin.proto +147 -0
- data/proto/google/protobuf/descriptor.proto +779 -0
- data/proto/rpc.proto +69 -0
- data/protobuf-cucumber.gemspec +57 -0
- data/spec/benchmark/tasks.rb +143 -0
- data/spec/bin/protoc-gen-ruby_spec.rb +23 -0
- data/spec/encoding/all_types_spec.rb +103 -0
- data/spec/encoding/extreme_values_spec.rb +0 -0
- data/spec/functional/class_inheritance_spec.rb +52 -0
- data/spec/functional/code_generator_spec.rb +58 -0
- data/spec/functional/socket_server_spec.rb +59 -0
- data/spec/functional/zmq_server_spec.rb +105 -0
- data/spec/lib/protobuf/cli_spec.rb +317 -0
- data/spec/lib/protobuf/code_generator_spec.rb +87 -0
- data/spec/lib/protobuf/enum_spec.rb +307 -0
- data/spec/lib/protobuf/field/bool_field_spec.rb +91 -0
- data/spec/lib/protobuf/field/double_field_spec.rb +9 -0
- data/spec/lib/protobuf/field/enum_field_spec.rb +44 -0
- data/spec/lib/protobuf/field/field_array_spec.rb +105 -0
- data/spec/lib/protobuf/field/field_hash_spec.rb +168 -0
- data/spec/lib/protobuf/field/fixed32_field_spec.rb +7 -0
- data/spec/lib/protobuf/field/fixed64_field_spec.rb +7 -0
- data/spec/lib/protobuf/field/float_field_spec.rb +90 -0
- data/spec/lib/protobuf/field/int32_field_spec.rb +120 -0
- data/spec/lib/protobuf/field/int64_field_spec.rb +7 -0
- data/spec/lib/protobuf/field/message_field_spec.rb +132 -0
- data/spec/lib/protobuf/field/sfixed32_field_spec.rb +9 -0
- data/spec/lib/protobuf/field/sfixed64_field_spec.rb +9 -0
- data/spec/lib/protobuf/field/sint32_field_spec.rb +9 -0
- data/spec/lib/protobuf/field/sint64_field_spec.rb +9 -0
- data/spec/lib/protobuf/field/string_field_spec.rb +79 -0
- data/spec/lib/protobuf/field/uint32_field_spec.rb +7 -0
- data/spec/lib/protobuf/field/uint64_field_spec.rb +7 -0
- data/spec/lib/protobuf/field_spec.rb +192 -0
- data/spec/lib/protobuf/generators/base_spec.rb +154 -0
- data/spec/lib/protobuf/generators/enum_generator_spec.rb +82 -0
- data/spec/lib/protobuf/generators/extension_generator_spec.rb +42 -0
- data/spec/lib/protobuf/generators/field_generator_spec.rb +197 -0
- data/spec/lib/protobuf/generators/file_generator_spec.rb +119 -0
- data/spec/lib/protobuf/generators/message_generator_spec.rb +0 -0
- data/spec/lib/protobuf/generators/service_generator_spec.rb +99 -0
- data/spec/lib/protobuf/lifecycle_spec.rb +94 -0
- data/spec/lib/protobuf/message_spec.rb +944 -0
- data/spec/lib/protobuf/optionable_spec.rb +265 -0
- data/spec/lib/protobuf/rpc/client_spec.rb +66 -0
- data/spec/lib/protobuf/rpc/connectors/base_spec.rb +226 -0
- data/spec/lib/protobuf/rpc/connectors/ping_spec.rb +69 -0
- data/spec/lib/protobuf/rpc/connectors/socket_spec.rb +34 -0
- data/spec/lib/protobuf/rpc/connectors/zmq_spec.rb +110 -0
- data/spec/lib/protobuf/rpc/middleware/exception_handler_spec.rb +62 -0
- data/spec/lib/protobuf/rpc/middleware/logger_spec.rb +49 -0
- data/spec/lib/protobuf/rpc/middleware/request_decoder_spec.rb +115 -0
- data/spec/lib/protobuf/rpc/middleware/response_encoder_spec.rb +91 -0
- data/spec/lib/protobuf/rpc/servers/socket_server_spec.rb +38 -0
- data/spec/lib/protobuf/rpc/servers/zmq/server_spec.rb +43 -0
- data/spec/lib/protobuf/rpc/servers/zmq/util_spec.rb +55 -0
- data/spec/lib/protobuf/rpc/servers/zmq/worker_spec.rb +35 -0
- data/spec/lib/protobuf/rpc/service_directory_spec.rb +293 -0
- data/spec/lib/protobuf/rpc/service_dispatcher_spec.rb +35 -0
- data/spec/lib/protobuf/rpc/service_filters_spec.rb +517 -0
- data/spec/lib/protobuf/rpc/service_spec.rb +162 -0
- data/spec/lib/protobuf/rpc/stat_spec.rb +101 -0
- data/spec/lib/protobuf/varint_spec.rb +29 -0
- data/spec/lib/protobuf_spec.rb +105 -0
- data/spec/spec_helper.rb +42 -0
- data/spec/support/all.rb +6 -0
- data/spec/support/packed_field.rb +23 -0
- data/spec/support/protos/all_types.data.bin +0 -0
- data/spec/support/protos/all_types.data.txt +119 -0
- data/spec/support/protos/enum.pb.rb +63 -0
- data/spec/support/protos/enum.proto +37 -0
- data/spec/support/protos/extreme_values.data.bin +0 -0
- data/spec/support/protos/google_unittest.bin +0 -0
- data/spec/support/protos/google_unittest.pb.rb +798 -0
- data/spec/support/protos/google_unittest.proto +884 -0
- data/spec/support/protos/google_unittest_custom_options.bin +0 -0
- data/spec/support/protos/google_unittest_custom_options.pb.rb +361 -0
- data/spec/support/protos/google_unittest_custom_options.proto +424 -0
- data/spec/support/protos/google_unittest_import.pb.rb +55 -0
- data/spec/support/protos/google_unittest_import.proto +73 -0
- data/spec/support/protos/google_unittest_import_public.pb.rb +31 -0
- data/spec/support/protos/google_unittest_import_public.proto +41 -0
- data/spec/support/protos/map-test.bin +157 -0
- data/spec/support/protos/map-test.pb.rb +85 -0
- data/spec/support/protos/map-test.proto +68 -0
- data/spec/support/protos/multi_field_extensions.pb.rb +59 -0
- data/spec/support/protos/multi_field_extensions.proto +35 -0
- data/spec/support/protos/resource.pb.rb +172 -0
- data/spec/support/protos/resource.proto +137 -0
- data/spec/support/resource_service.rb +23 -0
- data/spec/support/server.rb +65 -0
- data/spec/support/test_app_file.rb +2 -0
- data/varint_prof.rb +82 -0
- metadata +579 -0
@@ -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,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
|