protobuffy 3.6.0 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.rubocop.yml +67 -0
- data/.rubocop_todo.yml +145 -0
- data/.travis.yml +25 -5
- data/CHANGES.md +55 -0
- data/CONTRIBUTING.md +1 -1
- data/LICENSE.txt +17 -9
- data/README.md +13 -12
- data/Rakefile +15 -11
- data/bin/protoc-gen-ruby +8 -3
- data/bin/rpc_server +1 -0
- data/examples/lib/example/reverse-client.rb +2 -2
- data/install-protobuf.sh +28 -0
- data/lib/protobuf.rb +57 -53
- data/lib/protobuf/cli.rb +94 -74
- data/lib/protobuf/code_generator.rb +60 -9
- data/lib/protobuf/decoder.rb +19 -65
- data/lib/protobuf/deprecation.rb +117 -0
- data/lib/protobuf/descriptors/google/protobuf/compiler/plugin.pb.rb +11 -1
- data/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb +55 -3
- data/lib/protobuf/encoder.rb +13 -53
- data/lib/protobuf/enum.rb +58 -63
- data/lib/protobuf/field.rb +4 -4
- data/lib/protobuf/field/base_field.rb +101 -173
- data/lib/protobuf/field/bool_field.rb +17 -11
- data/lib/protobuf/field/bytes_field.rb +21 -35
- data/lib/protobuf/field/double_field.rb +0 -1
- data/lib/protobuf/field/enum_field.rb +23 -22
- data/lib/protobuf/field/field_array.rb +5 -4
- data/lib/protobuf/field/fixed32_field.rb +1 -1
- data/lib/protobuf/field/fixed64_field.rb +0 -1
- data/lib/protobuf/field/float_field.rb +4 -1
- data/lib/protobuf/field/int32_field.rb +0 -1
- data/lib/protobuf/field/int64_field.rb +0 -1
- data/lib/protobuf/field/integer_field.rb +0 -1
- data/lib/protobuf/field/message_field.rb +13 -28
- data/lib/protobuf/field/sfixed32_field.rb +0 -1
- data/lib/protobuf/field/sfixed64_field.rb +0 -1
- data/lib/protobuf/field/signed_integer_field.rb +0 -1
- data/lib/protobuf/field/sint32_field.rb +0 -1
- data/lib/protobuf/field/sint64_field.rb +0 -1
- data/lib/protobuf/field/string_field.rb +2 -4
- data/lib/protobuf/field/uint32_field.rb +0 -1
- data/lib/protobuf/field/uint64_field.rb +0 -1
- data/lib/protobuf/field/varint_field.rb +30 -13
- data/lib/protobuf/generators/base.rb +30 -16
- data/lib/protobuf/generators/enum_generator.rb +6 -9
- data/lib/protobuf/generators/extension_generator.rb +1 -2
- data/lib/protobuf/generators/field_generator.rb +25 -13
- data/lib/protobuf/generators/file_generator.rb +157 -35
- data/lib/protobuf/generators/group_generator.rb +22 -17
- data/lib/protobuf/generators/message_generator.rb +13 -14
- data/lib/protobuf/generators/option_generator.rb +17 -0
- data/lib/protobuf/generators/printable.rb +12 -13
- data/lib/protobuf/generators/service_generator.rb +2 -3
- data/lib/protobuf/http.rb +2 -2
- data/lib/protobuf/lifecycle.rb +20 -33
- data/lib/protobuf/logging.rb +39 -0
- data/lib/protobuf/message.rb +114 -47
- data/lib/protobuf/message/fields.rb +170 -88
- data/lib/protobuf/message/serialization.rb +19 -18
- data/lib/protobuf/optionable.rb +53 -6
- data/lib/protobuf/rpc/buffer.rb +18 -19
- data/lib/protobuf/rpc/client.rb +22 -50
- data/lib/protobuf/rpc/connectors/base.rb +177 -12
- data/lib/protobuf/rpc/connectors/http.rb +14 -9
- data/lib/protobuf/rpc/connectors/ping.rb +89 -0
- data/lib/protobuf/rpc/connectors/socket.rb +13 -8
- data/lib/protobuf/rpc/connectors/zmq.rb +178 -73
- data/lib/protobuf/rpc/dynamic_discovery.pb.rb +4 -1
- data/lib/protobuf/rpc/env.rb +12 -12
- data/lib/protobuf/rpc/error.rb +3 -3
- data/lib/protobuf/rpc/error/client_error.rb +4 -4
- data/lib/protobuf/rpc/error/server_error.rb +9 -9
- data/lib/protobuf/rpc/middleware/exception_handler.rb +6 -2
- data/lib/protobuf/rpc/middleware/logger.rb +8 -4
- data/lib/protobuf/rpc/middleware/request_decoder.rb +17 -21
- data/lib/protobuf/rpc/middleware/response_encoder.rb +22 -27
- data/lib/protobuf/rpc/middleware/statsd.rb +3 -3
- data/lib/protobuf/rpc/rpc.pb.rb +4 -1
- data/lib/protobuf/rpc/server.rb +1 -1
- data/lib/protobuf/rpc/servers/http/server.rb +19 -17
- data/lib/protobuf/rpc/servers/socket/server.rb +78 -70
- data/lib/protobuf/rpc/servers/socket/worker.rb +4 -4
- data/lib/protobuf/rpc/servers/socket_runner.rb +27 -15
- data/lib/protobuf/rpc/servers/zmq/broker.rb +70 -31
- data/lib/protobuf/rpc/servers/zmq/server.rb +55 -47
- data/lib/protobuf/rpc/servers/zmq/util.rb +14 -13
- data/lib/protobuf/rpc/servers/zmq/worker.rb +16 -16
- data/lib/protobuf/rpc/servers/zmq_runner.rb +26 -7
- data/lib/protobuf/rpc/service.rb +21 -27
- data/lib/protobuf/rpc/service_directory.rb +43 -27
- data/lib/protobuf/rpc/service_dispatcher.rb +9 -10
- data/lib/protobuf/rpc/service_filters.rb +32 -55
- data/lib/protobuf/rpc/stat.rb +4 -8
- data/lib/protobuf/socket.rb +1 -2
- data/lib/protobuf/tasks/compile.rake +3 -4
- data/lib/protobuf/varint.rb +9 -0
- data/lib/protobuf/varint_pure.rb +13 -0
- data/lib/protobuf/version.rb +1 -1
- data/lib/protobuf/zmq.rb +2 -2
- data/proto/google/protobuf/descriptor.proto +190 -31
- data/protobuffy.gemspec +30 -17
- data/spec/benchmark/tasks.rb +27 -19
- data/spec/bin/protoc-gen-ruby_spec.rb +11 -6
- data/spec/encoding/all_types_spec.rb +96 -84
- 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 +38 -0
- data/spec/functional/socket_server_spec.rb +15 -15
- data/spec/functional/zmq_server_spec.rb +29 -27
- data/spec/lib/protobuf/cli_spec.rb +82 -67
- data/spec/lib/protobuf/code_generator_spec.rb +37 -10
- data/spec/lib/protobuf/enum_spec.rb +77 -46
- 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 +26 -0
- data/spec/lib/protobuf/field/field_array_spec.rb +69 -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 +114 -1
- 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 +44 -11
- 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 +4 -6
- data/spec/lib/protobuf/generators/base_spec.rb +80 -13
- data/spec/lib/protobuf/generators/enum_generator_spec.rb +35 -21
- data/spec/lib/protobuf/generators/extension_generator_spec.rb +12 -13
- data/spec/lib/protobuf/generators/field_generator_spec.rb +73 -21
- data/spec/lib/protobuf/generators/file_generator_spec.rb +89 -6
- data/spec/lib/protobuf/generators/service_generator_spec.rb +25 -13
- data/spec/lib/protobuf/lifecycle_spec.rb +25 -20
- data/spec/lib/protobuf/message_spec.rb +578 -79
- data/spec/lib/protobuf/optionable_spec.rb +202 -26
- data/spec/lib/protobuf/rpc/client_spec.rb +16 -16
- data/spec/lib/protobuf/rpc/connectors/base_spec.rb +167 -13
- data/spec/lib/protobuf/rpc/connectors/connector_spec.rb +4 -5
- data/spec/lib/protobuf/rpc/connectors/http_spec.rb +13 -11
- data/spec/lib/protobuf/rpc/connectors/ping_spec.rb +69 -0
- data/spec/lib/protobuf/rpc/connectors/socket_spec.rb +6 -7
- data/spec/lib/protobuf/rpc/connectors/zmq_spec.rb +35 -52
- data/spec/lib/protobuf/rpc/middleware/exception_handler_spec.rb +10 -10
- data/spec/lib/protobuf/rpc/middleware/logger_spec.rb +11 -11
- data/spec/lib/protobuf/rpc/middleware/request_decoder_spec.rb +23 -23
- data/spec/lib/protobuf/rpc/middleware/response_encoder_spec.rb +11 -11
- data/spec/lib/protobuf/rpc/middleware/statsd_spec.rb +6 -6
- data/spec/lib/protobuf/rpc/servers/http/server_spec.rb +47 -44
- data/spec/lib/protobuf/rpc/servers/socket_server_spec.rb +6 -6
- data/spec/lib/protobuf/rpc/servers/zmq/server_spec.rb +12 -10
- data/spec/lib/protobuf/rpc/servers/zmq/util_spec.rb +11 -11
- data/spec/lib/protobuf/rpc/servers/zmq/worker_spec.rb +7 -7
- data/spec/lib/protobuf/rpc/service_directory_spec.rb +47 -49
- data/spec/lib/protobuf/rpc/service_dispatcher_spec.rb +8 -25
- data/spec/lib/protobuf/rpc/service_filters_spec.rb +102 -69
- data/spec/lib/protobuf/rpc/service_spec.rb +37 -36
- data/spec/lib/protobuf/rpc/stat_spec.rb +7 -9
- data/spec/lib/protobuf/varint_spec.rb +29 -0
- data/spec/lib/protobuf_spec.rb +55 -28
- data/spec/spec_helper.rb +12 -27
- data/spec/support/all.rb +0 -1
- data/spec/support/packed_field.rb +4 -3
- data/spec/support/{test → protos}/all_types.data.bin +0 -0
- data/spec/support/{test → protos}/all_types.data.txt +0 -0
- data/spec/support/{test → protos}/enum.pb.rb +8 -4
- data/spec/support/{test → protos}/enum.proto +4 -1
- data/spec/support/{test → 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/{test → protos}/google_unittest.proto +237 -66
- data/spec/support/protos/google_unittest_custom_options.bin +0 -0
- data/spec/support/protos/google_unittest_custom_options.pb.rb +268 -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/{test → protos}/google_unittest_import.proto +19 -10
- data/spec/support/protos/google_unittest_import_public.pb.rb +31 -0
- data/spec/support/{test → protos}/google_unittest_import_public.proto +8 -5
- data/spec/support/{test → protos}/multi_field_extensions.pb.rb +5 -2
- data/spec/support/{test → protos}/multi_field_extensions.proto +2 -0
- data/spec/support/{test → protos}/resource.pb.rb +47 -11
- data/spec/support/{test → protos}/resource.proto +24 -1
- data/spec/support/resource_service.rb +23 -0
- data/spec/support/server.rb +32 -61
- metadata +119 -59
- data/lib/protobuf/deprecator.rb +0 -42
- data/lib/protobuf/logger.rb +0 -93
- data/lib/protobuf/rpc/connector.rb +0 -21
- data/lib/protobuf/rpc/connectors/common.rb +0 -172
- data/spec/data/data.bin +0 -3
- data/spec/data/types.bin +0 -0
- data/spec/lib/protobuf/logger_spec.rb +0 -145
- data/spec/lib/protobuf/rpc/connector_spec.rb +0 -26
- data/spec/lib/protobuf/rpc/connectors/common_spec.rb +0 -170
- data/spec/support/test/defaults.pb.rb +0 -25
- data/spec/support/test/defaults.proto +0 -9
- data/spec/support/test/extended.pb.rb +0 -22
- data/spec/support/test/extended.proto +0 -10
- data/spec/support/test/google_unittest.pb.rb +0 -543
- data/spec/support/test/google_unittest_import.pb.rb +0 -37
- data/spec/support/test/google_unittest_import_public.pb.rb +0 -8
- data/spec/support/test/resource_service.rb +0 -26
- data/spec/support/tolerance_matcher.rb +0 -40
@@ -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(ArgumentError)
|
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
|
@@ -1,7 +1,120 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe Protobuf::Field::Int32Field do
|
3
|
+
RSpec.describe Protobuf::Field::Int32Field do
|
4
4
|
|
5
5
|
it_behaves_like :packable_field, described_class
|
6
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
|
+
|
7
120
|
end
|
@@ -0,0 +1,132 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe Protobuf::Field::MessageField do
|
4
|
+
let(:inner_message) do
|
5
|
+
Class.new(::Protobuf::Message) do
|
6
|
+
optional :int32, :field, 0
|
7
|
+
optional :int32, :field2, 1
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
let(:field_message) do
|
12
|
+
Class.new(::Protobuf::Message) do
|
13
|
+
optional :int32, :field, 1
|
14
|
+
repeated :int64, :repeated_field, 2
|
15
|
+
optional InnerMessage, :message_field, 3
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
let(:message) do
|
20
|
+
Class.new(::Protobuf::Message) do
|
21
|
+
optional FieldMessage, :message_field, 1
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
before do
|
26
|
+
stub_const('InnerMessage', inner_message)
|
27
|
+
stub_const('FieldMessage', field_message)
|
28
|
+
stub_const('Message', message)
|
29
|
+
end
|
30
|
+
|
31
|
+
let(:instance) { message.new }
|
32
|
+
|
33
|
+
describe 'setting and getting field' do
|
34
|
+
context "when set with the message type" do
|
35
|
+
it 'is readable as a message' do
|
36
|
+
value = field_message.new(:field => 34)
|
37
|
+
instance.message_field = value
|
38
|
+
expect(instance.message_field).to eq(value)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context "when set with #to_proto" do
|
43
|
+
let(:to_proto_message) do
|
44
|
+
Class.new do
|
45
|
+
def to_proto
|
46
|
+
FieldMessage.new(:field => 42)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'is readable as a message' do
|
52
|
+
value = to_proto_message.new
|
53
|
+
instance.message_field = value
|
54
|
+
expect(instance.message_field).to eq(value.to_proto)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
context "when set with #to_proto that returns the wrong message type" do
|
59
|
+
let(:to_proto_is_wrong_message) do
|
60
|
+
Class.new do
|
61
|
+
def to_proto
|
62
|
+
Message.new
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'fails' do
|
68
|
+
value = to_proto_is_wrong_message.new
|
69
|
+
expect { instance.message_field = value }.to raise_error TypeError
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
context "when set with #to_hash" do
|
74
|
+
let(:to_hash_message) do
|
75
|
+
Class.new do
|
76
|
+
def to_hash
|
77
|
+
{ :field => 989 }
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
it 'is readable as a message' do
|
83
|
+
value = to_hash_message.new
|
84
|
+
instance.message_field = value
|
85
|
+
expect(instance.message_field).to eq(field_message.new(value.to_hash))
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
describe '#option_set' do
|
91
|
+
let(:message_field) { Message.fields[0] }
|
92
|
+
it 'returns unless yield' do
|
93
|
+
# No Error thrown
|
94
|
+
message_field.__send__(:option_set, nil, nil, nil) { false }
|
95
|
+
expect do
|
96
|
+
message_field.__send__(:option_set, nil, nil, nil) { true }
|
97
|
+
end.to raise_error StandardError
|
98
|
+
end
|
99
|
+
|
100
|
+
it 'sets repeated fields' do
|
101
|
+
repeated = field_message.fields[1]
|
102
|
+
instance = field_message.new
|
103
|
+
expect(instance.repeated_field!).to eq(nil)
|
104
|
+
message_field.__send__(:option_set, instance, repeated, [53]) { true }
|
105
|
+
expect(instance.repeated_field!).to eq([53])
|
106
|
+
message_field.__send__(:option_set, instance, repeated, [54]) { true }
|
107
|
+
expect(instance.repeated_field!).to eq([53, 54])
|
108
|
+
end
|
109
|
+
|
110
|
+
it 'sets optional non-message fields' do
|
111
|
+
optional = field_message.fields[0]
|
112
|
+
instance = field_message.new
|
113
|
+
expect(instance.field!).to eq(nil)
|
114
|
+
message_field.__send__(:option_set, instance, optional, 53) { true }
|
115
|
+
expect(instance.field!).to eq(53)
|
116
|
+
message_field.__send__(:option_set, instance, optional, 52) { true }
|
117
|
+
expect(instance.field!).to eq(52)
|
118
|
+
end
|
119
|
+
|
120
|
+
it 'sets nested inner messages fields one at a time' do
|
121
|
+
inner = field_message.fields[2]
|
122
|
+
inner_val = InnerMessage.new(:field => 21)
|
123
|
+
inner_val2 = InnerMessage.new(:field2 => 9)
|
124
|
+
instance = field_message.new
|
125
|
+
expect(instance.message_field!).to eq(nil)
|
126
|
+
message_field.__send__(:option_set, instance, inner, inner_val) { true }
|
127
|
+
expect(instance.message_field!).to eq(InnerMessage.new(:field => 21))
|
128
|
+
message_field.__send__(:option_set, instance, inner, inner_val2) { true }
|
129
|
+
expect(instance.message_field!).to eq(InnerMessage.new(:field => 21, :field2 => 9))
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
@@ -2,24 +2,24 @@
|
|
2
2
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
|
-
describe ::Protobuf::Field::StringField do
|
5
|
+
RSpec.describe ::Protobuf::Field::StringField do
|
6
6
|
|
7
7
|
describe '#encode' do
|
8
8
|
context 'when a repeated string field contains frozen strings' do
|
9
9
|
it 'does not raise an encoding error' do
|
10
|
-
expect
|
11
|
-
frozen_strings = [
|
10
|
+
expect do
|
11
|
+
frozen_strings = ["foo".freeze, "bar".freeze, "baz".freeze]
|
12
12
|
::Test::ResourceFindRequest.encode(:name => 'resource', :widgets => frozen_strings)
|
13
|
-
|
13
|
+
end.not_to raise_error
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
17
|
context 'when a repeated bytes field contains frozen strings' do
|
18
18
|
it 'does not raise an encoding error' do
|
19
|
-
expect
|
20
|
-
frozen_strings = [
|
19
|
+
expect do
|
20
|
+
frozen_strings = ["foo".freeze, "bar".freeze, "baz".freeze]
|
21
21
|
::Test::ResourceFindRequest.encode(:name => 'resource', :widget_bytes => frozen_strings)
|
22
|
-
|
22
|
+
end.not_to raise_error
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
@@ -27,20 +27,53 @@ describe ::Protobuf::Field::StringField do
|
|
27
27
|
source_string = "foo"
|
28
28
|
proto = ::Test::Resource.new(:name => source_string)
|
29
29
|
proto.encode
|
30
|
-
proto.name.
|
30
|
+
expect(proto.name).to eq source_string
|
31
31
|
proto.encode
|
32
|
-
proto.name.
|
32
|
+
expect(proto.name).to eq source_string
|
33
33
|
end
|
34
34
|
|
35
35
|
it 'does not alter unicode string values after encoding multiple times' do
|
36
36
|
source_string = "¢"
|
37
37
|
proto = ::Test::Resource.new(:name => source_string)
|
38
38
|
proto.encode
|
39
|
-
proto.name.
|
39
|
+
expect(proto.name).to eq source_string
|
40
40
|
proto.encode
|
41
|
-
proto.name.
|
41
|
+
expect(proto.name).to eq source_string
|
42
42
|
end
|
43
43
|
end
|
44
44
|
|
45
|
+
describe '#default_value' do
|
46
|
+
context 'optional and required fields' do
|
47
|
+
it 'returns the class default' do
|
48
|
+
class SomeStringMessage < ::Protobuf::Message
|
49
|
+
optional :string, :some_string, 1
|
50
|
+
end
|
51
|
+
expect(SomeStringMessage.get_field('some_string').default).to be nil
|
52
|
+
expect(::Protobuf::Field::StringField.default).to eq ""
|
53
|
+
expect(SomeStringMessage.new.some_string).to eq ""
|
54
|
+
end
|
55
|
+
|
56
|
+
context 'with field default' do
|
57
|
+
class AnotherStringMessage < ::Protobuf::Message
|
58
|
+
optional :string, :set_string, 1, :default => "default value this is"
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'returns the set default' do
|
62
|
+
expect(AnotherStringMessage.get_field('set_string').default).to eq "default value this is"
|
63
|
+
expect(AnotherStringMessage.new.set_string).to eq "default value this is"
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
context 'repeated field' do
|
69
|
+
class RepeatedStringMessage < ::Protobuf::Message
|
70
|
+
repeated :string, :repeated_string, 1
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'returns the set default' do
|
74
|
+
expect(RepeatedStringMessage.new.repeated_string).to eq []
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
45
78
|
|
46
79
|
end
|