protobuf 3.7.0.pre2 → 3.7.0.pre3

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.
Files changed (114) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +6 -1
  3. data/.rubocop_todo.yml +7 -1
  4. data/.travis.yml +8 -1
  5. data/CHANGES.md +25 -1
  6. data/bin/protoc-gen-ruby +2 -2
  7. data/lib/protobuf/cli.rb +29 -17
  8. data/lib/protobuf/code_generator.rb +49 -1
  9. data/lib/protobuf/descriptors/google/protobuf/compiler/plugin.pb.rb +9 -1
  10. data/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb +14 -1
  11. data/lib/protobuf/encoder.rb +2 -2
  12. data/lib/protobuf/enum.rb +3 -3
  13. data/lib/protobuf/field/base_field.rb +27 -19
  14. data/lib/protobuf/field/bool_field.rb +10 -8
  15. data/lib/protobuf/field/bytes_field.rb +14 -6
  16. data/lib/protobuf/field/float_field.rb +2 -0
  17. data/lib/protobuf/field/string_field.rb +10 -0
  18. data/lib/protobuf/field/varint_field.rb +12 -2
  19. data/lib/protobuf/generators/base.rb +29 -14
  20. data/lib/protobuf/generators/enum_generator.rb +4 -7
  21. data/lib/protobuf/generators/field_generator.rb +17 -4
  22. data/lib/protobuf/generators/file_generator.rb +121 -10
  23. data/lib/protobuf/generators/group_generator.rb +9 -3
  24. data/lib/protobuf/generators/message_generator.rb +8 -2
  25. data/lib/protobuf/generators/option_generator.rb +17 -0
  26. data/lib/protobuf/generators/printable.rb +2 -2
  27. data/lib/protobuf/generators/service_generator.rb +27 -3
  28. data/lib/protobuf/lifecycle.rb +1 -1
  29. data/lib/protobuf/message/fields.rb +13 -15
  30. data/lib/protobuf/message/serialization.rb +9 -9
  31. data/lib/protobuf/message.rb +23 -29
  32. data/lib/protobuf/optionable.rb +10 -10
  33. data/lib/protobuf/rpc/buffer.rb +7 -6
  34. data/lib/protobuf/rpc/client.rb +2 -30
  35. data/lib/protobuf/rpc/connectors/base.rb +168 -6
  36. data/lib/protobuf/rpc/connectors/ping.rb +2 -2
  37. data/lib/protobuf/rpc/connectors/socket.rb +6 -1
  38. data/lib/protobuf/rpc/connectors/zmq.rb +1 -2
  39. data/lib/protobuf/rpc/dynamic_discovery.pb.rb +2 -1
  40. data/lib/protobuf/rpc/error.rb +2 -2
  41. data/lib/protobuf/rpc/middleware/exception_handler.rb +4 -0
  42. data/lib/protobuf/rpc/middleware/logger.rb +4 -0
  43. data/lib/protobuf/rpc/middleware/request_decoder.rb +11 -16
  44. data/lib/protobuf/rpc/middleware/response_encoder.rb +18 -23
  45. data/lib/protobuf/rpc/rpc.pb.rb +2 -1
  46. data/lib/protobuf/rpc/rpc_method.rb +16 -0
  47. data/lib/protobuf/rpc/servers/socket/server.rb +4 -4
  48. data/lib/protobuf/rpc/servers/socket_runner.rb +8 -0
  49. data/lib/protobuf/rpc/servers/zmq/broker.rb +7 -6
  50. data/lib/protobuf/rpc/servers/zmq/server.rb +8 -7
  51. data/lib/protobuf/rpc/servers/zmq/util.rb +6 -6
  52. data/lib/protobuf/rpc/servers/zmq/worker.rb +7 -6
  53. data/lib/protobuf/rpc/servers/zmq_runner.rb +8 -0
  54. data/lib/protobuf/rpc/service.rb +6 -15
  55. data/lib/protobuf/rpc/service_directory.rb +1 -1
  56. data/lib/protobuf/rpc/service_dispatcher.rb +5 -6
  57. data/lib/protobuf/rpc/service_filters.rb +8 -30
  58. data/lib/protobuf/socket.rb +2 -2
  59. data/lib/protobuf/version.rb +1 -1
  60. data/lib/protobuf/zmq.rb +2 -2
  61. data/lib/protobuf.rb +12 -27
  62. data/protobuf.gemspec +5 -3
  63. data/spec/benchmark/tasks.rb +1 -0
  64. data/spec/functional/code_generator_spec.rb +38 -0
  65. data/spec/lib/protobuf/cli_spec.rb +19 -10
  66. data/spec/lib/protobuf/code_generator_spec.rb +28 -0
  67. data/spec/lib/protobuf/enum_spec.rb +6 -2
  68. data/spec/lib/protobuf/field/bool_field_spec.rb +4 -0
  69. data/spec/lib/protobuf/field/double_field_spec.rb +9 -0
  70. data/spec/lib/protobuf/field/fixed32_field_spec.rb +7 -0
  71. data/spec/lib/protobuf/field/fixed64_field_spec.rb +7 -0
  72. data/spec/lib/protobuf/field/float_field_spec.rb +5 -1
  73. data/spec/lib/protobuf/field/int64_field_spec.rb +7 -0
  74. data/spec/lib/protobuf/field/message_field_spec.rb +53 -0
  75. data/spec/lib/protobuf/field/sfixed32_field_spec.rb +9 -0
  76. data/spec/lib/protobuf/field/sfixed64_field_spec.rb +9 -0
  77. data/spec/lib/protobuf/field/sint32_field_spec.rb +9 -0
  78. data/spec/lib/protobuf/field/sint64_field_spec.rb +9 -0
  79. data/spec/lib/protobuf/field/uint32_field_spec.rb +7 -0
  80. data/spec/lib/protobuf/field/uint64_field_spec.rb +7 -0
  81. data/spec/lib/protobuf/generators/base_spec.rb +69 -1
  82. data/spec/lib/protobuf/generators/enum_generator_spec.rb +1 -1
  83. data/spec/lib/protobuf/generators/field_generator_spec.rb +58 -0
  84. data/spec/lib/protobuf/generators/file_generator_spec.rb +47 -0
  85. data/spec/lib/protobuf/generators/service_generator_spec.rb +58 -14
  86. data/spec/lib/protobuf/message_spec.rb +2 -2
  87. data/spec/lib/protobuf/optionable_spec.rb +96 -0
  88. data/spec/lib/protobuf/rpc/connectors/base_spec.rb +151 -0
  89. data/spec/lib/protobuf/rpc/connectors/ping_spec.rb +3 -3
  90. data/spec/lib/protobuf/rpc/connectors/socket_spec.rb +0 -2
  91. data/spec/lib/protobuf/rpc/middleware/logger_spec.rb +2 -2
  92. data/spec/lib/protobuf/rpc/middleware/request_decoder_spec.rb +4 -4
  93. data/spec/lib/protobuf/rpc/middleware/response_encoder_spec.rb +2 -2
  94. data/spec/lib/protobuf/rpc/service_dispatcher_spec.rb +1 -18
  95. data/spec/lib/protobuf/rpc/service_filters_spec.rb +2 -2
  96. data/spec/lib/protobuf/varint_spec.rb +1 -1
  97. data/spec/lib/protobuf_spec.rb +13 -16
  98. data/spec/support/packed_field.rb +3 -2
  99. data/spec/support/protos/enum.pb.rb +2 -1
  100. data/spec/support/protos/enum.proto +1 -0
  101. data/spec/support/protos/google_unittest.pb.rb +69 -58
  102. data/spec/support/protos/google_unittest_custom_options.bin +0 -0
  103. data/spec/support/protos/google_unittest_custom_options.pb.rb +361 -0
  104. data/spec/support/protos/google_unittest_custom_options.proto +424 -0
  105. data/spec/support/protos/google_unittest_import.pb.rb +8 -0
  106. data/spec/support/protos/google_unittest_import_public.pb.rb +6 -0
  107. data/spec/support/protos/resource.pb.rb +54 -2
  108. data/spec/support/protos/resource.proto +42 -2
  109. data/spec/support/server.rb +1 -1
  110. metadata +39 -16
  111. data/lib/protobuf/rpc/connector.rb +0 -19
  112. data/lib/protobuf/rpc/connectors/common.rb +0 -176
  113. data/spec/lib/protobuf/rpc/connector_spec.rb +0 -26
  114. data/spec/lib/protobuf/rpc/connectors/common_spec.rb +0 -170
@@ -1,9 +1,18 @@
1
1
  require 'spec_helper'
2
2
 
3
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
+
4
11
  let(:field_message) do
5
12
  Class.new(::Protobuf::Message) do
6
13
  optional :int32, :field, 1
14
+ repeated :int64, :repeated_field, 2
15
+ optional InnerMessage, :message_field, 3
7
16
  end
8
17
  end
9
18
 
@@ -14,6 +23,7 @@ RSpec.describe Protobuf::Field::MessageField do
14
23
  end
15
24
 
16
25
  before do
26
+ stub_const('InnerMessage', inner_message)
17
27
  stub_const('FieldMessage', field_message)
18
28
  stub_const('Message', message)
19
29
  end
@@ -76,4 +86,47 @@ RSpec.describe Protobuf::Field::MessageField do
76
86
  end
77
87
  end
78
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
79
132
  end
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Protobuf::Field::Sfixed32Field do
4
+
5
+ it_behaves_like :packable_field, described_class do
6
+ let(:value) { [-1, 0, 1] }
7
+ end
8
+
9
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Protobuf::Field::Sfixed64Field do
4
+
5
+ it_behaves_like :packable_field, described_class do
6
+ let(:value) { [-1, 0, 1] }
7
+ end
8
+
9
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Protobuf::Field::Sint32Field do
4
+
5
+ it_behaves_like :packable_field, described_class do
6
+ let(:value) { [-1, 0, 1] }
7
+ end
8
+
9
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Protobuf::Field::Sint64Field do
4
+
5
+ it_behaves_like :packable_field, described_class do
6
+ let(:value) { [-1, 0, 1] }
7
+ end
8
+
9
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Protobuf::Field::Uint32Field 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::Uint64Field do
4
+
5
+ it_behaves_like :packable_field, described_class
6
+
7
+ end
@@ -5,7 +5,7 @@ require 'protobuf/generators/base'
5
5
 
6
6
  RSpec.describe ::Protobuf::Generators::Base do
7
7
 
8
- subject { described_class.new(double) }
8
+ subject(:generator) { described_class.new(double) }
9
9
 
10
10
  context 'namespaces' do
11
11
  let(:descriptor) { double(:name => 'Baz') }
@@ -83,4 +83,72 @@ RSpec.describe ::Protobuf::Generators::Base do
83
83
  end
84
84
  end
85
85
 
86
+ describe '#serialize_value' do
87
+ before do
88
+ stub_const("MyEnum", Class.new(::Protobuf::Enum) do
89
+ define :FOO, 1
90
+ define :BOO, 2
91
+ end)
92
+ stub_const("MyMessage1", Class.new(Protobuf::Message) do
93
+ optional :string, :foo, 1
94
+ end)
95
+ stub_const("MyMessage2", Class.new(Protobuf::Message) do
96
+ optional :string, :foo, 1
97
+ optional MyMessage1, :bar, 2
98
+ optional :int32, :boom, 3
99
+ optional MyEnum, :goat, 4
100
+ optional :bool, :bam, 5
101
+ optional :float, :fire, 6
102
+ end)
103
+ stub_const("MyMessage3", Class.new(Protobuf::Message) do
104
+ optional :string, :foo, 1
105
+ repeated MyMessage2, :bar, 2
106
+ optional :int32, :boom, 3
107
+ optional MyEnum, :goat, 4
108
+ optional :bool, :bam, 5
109
+ optional :float, :fire, 6
110
+ end)
111
+ end
112
+
113
+ it 'serializes messages' do
114
+ output_string = <<-STRING
115
+ { :foo => "space",
116
+ :bar => [{
117
+ :foo => "station",
118
+ :bar => { :foo => "orbit" },
119
+ :boom => 123,
120
+ :goat => ::MyEnum::FOO,
121
+ :bam => false,
122
+ :fire => 3.5 }],
123
+ :boom => 456,
124
+ :goat => ::MyEnum::BOO,
125
+ :bam => true, :fire => 1.2 }
126
+ STRING
127
+
128
+ output_string.lstrip!
129
+ output_string.rstrip!
130
+ output_string.delete!("\n")
131
+ output_string.squeeze!(" ")
132
+ expect(generator.serialize_value(MyMessage3.new(
133
+ :foo => 'space',
134
+ :bar => [MyMessage2.new(
135
+ :foo => 'station',
136
+ :bar => MyMessage1.new(:foo => 'orbit'),
137
+ :boom => 123,
138
+ :goat => MyEnum::FOO,
139
+ :bam => false,
140
+ :fire => 3.5,
141
+ )],
142
+ :boom => 456,
143
+ :goat => MyEnum::BOO,
144
+ :bam => true,
145
+ :fire => 1.2,
146
+ ))).to eq(output_string)
147
+ end
148
+
149
+ it 'serializes enums' do
150
+ expect(generator.serialize_value(MyEnum::FOO)).to eq("::MyEnum::FOO")
151
+ expect(generator.serialize_value(MyEnum::BOO)).to eq("::MyEnum::BOO")
152
+ end
153
+ end
86
154
  end
@@ -45,7 +45,7 @@ end
45
45
  let(:compiled) do
46
46
  <<-RUBY
47
47
  class TestEnum < ::Protobuf::Enum
48
- set_option :allow_alias
48
+ set_option :allow_alias, true
49
49
 
50
50
  define :FOO, 1
51
51
  define :BAR, 2
@@ -53,6 +53,15 @@ RSpec.describe ::Protobuf::Generators::FieldGenerator do
53
53
  specify { expect(subject).to eq "optional ::Foo::Bar::Baz, :foo_bar, 3, :default => ::Foo::Bar::Baz::QUUX\n" }
54
54
  end
55
55
 
56
+ context 'when type is an enum with lowercase default value with PB_UPCASE_ENUMS set' do
57
+ let(:type_enum) { :TYPE_ENUM }
58
+ let(:type_name) { '.foo.bar.Baz' }
59
+ let(:default_value) { 'quux' }
60
+ before { allow(ENV).to receive(:key?).with('PB_UPCASE_ENUMS').and_return(true) }
61
+
62
+ specify { expect(subject).to eq "optional ::Foo::Bar::Baz, :foo_bar, 3, :default => ::Foo::Bar::Baz::QUUX\n" }
63
+ end
64
+
56
65
  context 'when the type is a string' do
57
66
  let(:type_enum) { :TYPE_STRING }
58
67
  let(:default_value) { "a default \"string\"" }
@@ -97,6 +106,55 @@ RSpec.describe ::Protobuf::Generators::FieldGenerator do
97
106
 
98
107
  specify { expect(subject).to eq "optional :string, :foo_bar, 3, :deprecated => true\n" }
99
108
  end
109
+
110
+ context 'when field uses a custom option that is an extension' do
111
+ class ::CustomFieldEnum < ::Protobuf::Enum
112
+ define :BOOM, 1
113
+ define :BAM, 2
114
+ end
115
+
116
+ class ::CustomFieldMessage < ::Protobuf::Message
117
+ optional :string, :foo, 1
118
+ end
119
+
120
+ class ::Google::Protobuf::FieldOptions < ::Protobuf::Message
121
+ optional :string, :custom_string_option, 22000, :extension => true
122
+ optional :bool, :custom_bool_option, 22001, :extension => true
123
+ optional :int32, :custom_int32_option, 22002, :extension => true
124
+ optional ::CustomFieldEnum, :custom_enum_option, 22003, :extension => true
125
+ optional ::CustomFieldMessage, :custom_message_option, 22004, :extension => true
126
+ end
127
+
128
+ describe 'option has a string value' do
129
+ let(:field_options) { { :custom_string_option => 'boom' } }
130
+
131
+ specify { expect(subject).to eq "optional :string, :foo_bar, 3, :custom_string_option => \"boom\"\n" }
132
+ end
133
+
134
+ describe 'option has a bool value' do
135
+ let(:field_options) { { :custom_bool_option => true } }
136
+
137
+ specify { expect(subject).to eq "optional :string, :foo_bar, 3, :custom_bool_option => true\n" }
138
+ end
139
+
140
+ describe 'option has a int32 value' do
141
+ let(:field_options) { { :custom_int32_option => 123 } }
142
+
143
+ specify { expect(subject).to eq "optional :string, :foo_bar, 3, :custom_int32_option => 123\n" }
144
+ end
145
+
146
+ describe 'option has a message value' do
147
+ let(:field_options) { { :custom_message_option => CustomFieldMessage.new(:foo => 'boom') } }
148
+
149
+ specify { expect(subject).to eq "optional :string, :foo_bar, 3, :custom_message_option => { :foo => \"boom\" }\n" }
150
+ end
151
+
152
+ describe 'option has a enum value' do
153
+ let(:field_options) { { :custom_enum_option => CustomFieldEnum::BAM } }
154
+
155
+ specify { expect(subject).to eq "optional :string, :foo_bar, 3, :custom_enum_option => ::CustomFieldEnum::BAM\n" }
156
+ end
157
+ end
100
158
  end
101
159
 
102
160
  end
@@ -61,5 +61,52 @@ end
61
61
 
62
62
  EOF
63
63
  end
64
+
65
+ context 'with extended messages' do
66
+ let(:descriptor_fields) do
67
+ base_descriptor_fields.merge(
68
+ :package => 'test.pkg.file_generator_spec',
69
+ :extension => [{
70
+ :name => 'boom',
71
+ :number => 20_000,
72
+ :label => Google::Protobuf::FieldDescriptorProto::Label::LABEL_OPTIONAL,
73
+ :type => Google::Protobuf::FieldDescriptorProto::Type::TYPE_STRING,
74
+ :extendee => '.google.protobuf.FieldOptions',
75
+ }],
76
+ )
77
+ end
78
+
79
+ it 'generates the file contents that include the namespaced extension name' do
80
+ subject.compile
81
+ expect(subject.to_s).to eq <<EOF
82
+ # encoding: utf-8
83
+
84
+ ##
85
+ # This file is auto-generated. DO NOT EDIT!
86
+ #
87
+ require 'protobuf'
88
+
89
+ module Test
90
+ module Pkg
91
+ module File_generator_spec
92
+ ::Protobuf::Optionable.inject(self) { ::Google::Protobuf::FileOptions }
93
+
94
+ ##
95
+ # Extended Message Fields
96
+ #
97
+ class ::Google::Protobuf::FieldOptions < ::Protobuf::Message
98
+ optional :string, :".test.pkg.file_generator_spec.boom", 20000, :extension => true
99
+ end
100
+
101
+ end
102
+
103
+ end
104
+
105
+ end
106
+
107
+ EOF
108
+ end
109
+ end
110
+
64
111
  end
65
112
  end
@@ -4,10 +4,38 @@ require 'protobuf/generators/service_generator'
4
4
 
5
5
  RSpec.describe ::Protobuf::Generators::ServiceGenerator do
6
6
 
7
+ class ::CustomMethodEnum < ::Protobuf::Enum
8
+ define :BOOM, 1
9
+ define :BAM, 2
10
+ end
11
+
12
+ class ::CustomMethodMessage < ::Protobuf::Message
13
+ optional :string, :foo, 1
14
+ end
15
+
16
+ class ::Google::Protobuf::MethodOptions < ::Protobuf::Message
17
+ optional :string, :custom_string_option, 22000, :extension => true
18
+ optional :bool, :custom_bool_option, 22001, :extension => true
19
+ optional :int32, :custom_int32_option, 22002, :extension => true
20
+ optional ::CustomMethodEnum, :custom_enum_option, 22003, :extension => true
21
+ optional ::CustomMethodMessage, :custom_message_option, 22004, :extension => true
22
+ end
23
+
7
24
  let(:methods) do
8
25
  [
9
26
  { :name => 'Search', :input_type => 'FooRequest', :output_type => 'FooResponse' },
10
- { :name => 'FooBar', :input_type => '.foo.Request', :output_type => '.bar.Response' },
27
+ {
28
+ :name => 'FooBar',
29
+ :input_type => '.foo.Request',
30
+ :output_type => '.bar.Response',
31
+ :options => {
32
+ :custom_string_option => 'boom',
33
+ :custom_bool_option => true,
34
+ :custom_int32_option => 123,
35
+ :custom_message_option => CustomMethodMessage.new(:foo => 'bam'),
36
+ :custom_enum_option => CustomMethodEnum::BAM,
37
+ },
38
+ },
11
39
  ]
12
40
  end
13
41
  let(:service_fields) do
@@ -19,37 +47,53 @@ RSpec.describe ::Protobuf::Generators::ServiceGenerator do
19
47
 
20
48
  let(:service) { ::Google::Protobuf::ServiceDescriptorProto.new(service_fields) }
21
49
 
22
- subject { described_class.new(service) }
50
+ subject(:service_generator) { described_class.new(service) }
23
51
 
24
52
  describe '#compile' do
25
53
  let(:compiled) do
26
- 'class TestService < ::Protobuf::Rpc::Service
54
+ <<EOF
55
+ class TestService < ::Protobuf::Rpc::Service
27
56
  rpc :search, FooRequest, FooResponse
28
- rpc :foo_bar, ::Foo::Request, ::Bar::Response
57
+ rpc :foo_bar, ::Foo::Request, ::Bar::Response do
58
+ set_option :custom_string_option, "boom"
59
+ set_option :custom_bool_option, true
60
+ set_option :custom_int32_option, 123
61
+ set_option :custom_enum_option, ::CustomMethodEnum::BAM
62
+ set_option :custom_message_option, { :foo => "bam" }
63
+ end
29
64
  end
30
65
 
31
- '
66
+ EOF
32
67
  end
33
68
 
34
- it 'compiles the service and it\'s rpc methods' do
69
+ it "compiles the service and it's rpc methods" do
35
70
  subject.compile
36
71
  expect(subject.to_s).to eq(compiled)
37
72
  end
73
+
74
+ context 'with PB_USE_RAW_RPC_NAMES in the environemnt' do
75
+ let(:compiled) do
76
+ <<EOF
77
+ class TestService < ::Protobuf::Rpc::Service
78
+ rpc :Search, FooRequest, FooResponse
79
+ rpc :FooBar, ::Foo::Request, ::Bar::Response do
80
+ set_option :custom_string_option, "boom"
81
+ set_option :custom_bool_option, true
82
+ set_option :custom_int32_option, 123
83
+ set_option :custom_enum_option, ::CustomMethodEnum::BAM
84
+ set_option :custom_message_option, { :foo => "bam" }
38
85
  end
86
+ end
39
87
 
40
- describe '#build_method' do
41
- it 'returns a string identifying the given method descriptor' do
42
- expect(subject.build_method(service.method.first)).to eq("rpc :search, FooRequest, FooResponse")
43
- end
88
+ EOF
89
+ end
44
90
 
45
- context 'with PB_USE_RAW_RPC_NAMES in the environemnt' do
46
91
  before { allow(ENV).to receive(:key?).with('PB_USE_RAW_RPC_NAMES').and_return(true) }
47
92
 
48
93
  it 'uses the raw RPC name and does not underscore it' do
49
- expect(subject.build_method(service.method.first)).to eq("rpc :Search, FooRequest, FooResponse")
50
- expect(subject.build_method(service.method.last)).to eq("rpc :FooBar, ::Foo::Request, ::Bar::Response")
94
+ subject.compile
95
+ expect(subject.to_s).to eq(compiled)
51
96
  end
52
97
  end
53
98
  end
54
-
55
99
  end
@@ -142,12 +142,12 @@ RSpec.describe Protobuf::Message do
142
142
  describe '#initialize' do
143
143
  it "defaults to the first value listed in the enum's type definition" do
144
144
  test_enum = Test::EnumTestMessage.new
145
- expect(test_enum.non_default_enum).to eq(1)
145
+ expect(test_enum.non_default_enum).to eq(Test::EnumTestType.enums.first)
146
146
  end
147
147
 
148
148
  it "defaults to a a value with a name" do
149
149
  test_enum = Test::EnumTestMessage.new
150
- expect(test_enum.non_default_enum.name).to eq(:ONE)
150
+ expect(test_enum.non_default_enum.name).to eq(Test::EnumTestType.enums.first.name)
151
151
  end
152
152
 
153
153
  it "exposes the enum getter raw value through ! method" do
@@ -1,6 +1,7 @@
1
1
  require 'spec_helper'
2
2
  require 'protobuf/optionable'
3
3
  require 'protobuf/field/message_field'
4
+ require PROTOS_PATH.join('resource.pb')
4
5
 
5
6
  RSpec.describe 'Optionable' do
6
7
  describe '.{get,get!}_option' do
@@ -166,4 +167,99 @@ RSpec.describe 'Optionable' do
166
167
  end
167
168
  end
168
169
  end
170
+
171
+ describe 'getting options from generated code' do
172
+ context 'file options' do
173
+ it 'gets base options' do
174
+ expect(::Test.get_option!(:cc_generic_services)).to eq(true)
175
+ end
176
+
177
+ it 'gets unset options' do
178
+ expect(::Test.get_option!(:java_multiple_files)).to eq(nil)
179
+ expect(::Test.get_option(:java_multiple_files)).to eq(false)
180
+ end
181
+
182
+ it 'gets custom options' do
183
+ expect(::Test.get_option!(:".test.file_option")).to eq(9876543210)
184
+ end
185
+ end
186
+
187
+ context 'field options' do
188
+ subject { ::Test::Resource.fields[0] }
189
+
190
+ it 'gets base options' do
191
+ expect(subject.get_option!(:ctype))
192
+ .to eq(::Google::Protobuf::FieldOptions::CType::CORD)
193
+ end
194
+
195
+ it 'gets unset options' do
196
+ expect(subject.get_option!(:lazy)).to eq(nil)
197
+ expect(subject.get_option(:lazy)).to eq(false)
198
+ end
199
+
200
+ it 'gets custom options' do
201
+ expect(subject.get_option!(:".test.field_option")).to eq(8765432109)
202
+ end
203
+ end
204
+
205
+ context 'enum options' do
206
+ subject { ::Test::StatusType }
207
+
208
+ it 'gets base options' do
209
+ expect(subject.get_option!(:allow_alias)).to eq(true)
210
+ end
211
+
212
+ it 'gets unset options' do
213
+ expect(subject.get_option!(:deprecated)).to eq(nil)
214
+ expect(subject.get_option(:deprecated)).to eq(false)
215
+ end
216
+
217
+ it 'gets custom options' do
218
+ expect(subject.get_option!(:".test.enum_option")).to eq(-789)
219
+ end
220
+ end
221
+
222
+ context 'message options' do
223
+ subject { ::Test::Resource }
224
+
225
+ it 'gets base options' do
226
+ expect(subject.get_option!(:map_entry)).to eq(false)
227
+ end
228
+
229
+ it 'gets unset options' do
230
+ expect(subject.get_option!(:deprecated)).to eq(nil)
231
+ expect(subject.get_option(:deprecated)).to eq(false)
232
+ end
233
+
234
+ it 'gets custom options' do
235
+ expect(subject.get_option!(:".test.message_option")).to eq(-56)
236
+ end
237
+ end
238
+
239
+ context 'service options' do
240
+ subject { ::Test::ResourceService }
241
+
242
+ it 'gets unset options' do
243
+ expect(subject.get_option!(:deprecated)).to eq(nil)
244
+ expect(subject.get_option(:deprecated)).to eq(false)
245
+ end
246
+
247
+ it 'gets custom options' do
248
+ expect(subject.get_option!(:".test.service_option")).to eq(-9876543210)
249
+ end
250
+ end
251
+
252
+ context 'method options' do
253
+ subject { ::Test::ResourceService.rpcs[:find] }
254
+
255
+ it 'gets unset options' do
256
+ expect(subject.get_option!(:deprecated)).to eq(nil)
257
+ expect(subject.get_option(:deprecated)).to eq(false)
258
+ end
259
+
260
+ it 'gets custom options' do
261
+ expect(subject.get_option!(:".test.method_option")).to eq(2)
262
+ end
263
+ end
264
+ end
169
265
  end