protobuffy 3.6.0 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (209) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.rubocop.yml +67 -0
  4. data/.rubocop_todo.yml +145 -0
  5. data/.travis.yml +25 -5
  6. data/CHANGES.md +55 -0
  7. data/CONTRIBUTING.md +1 -1
  8. data/LICENSE.txt +17 -9
  9. data/README.md +13 -12
  10. data/Rakefile +15 -11
  11. data/bin/protoc-gen-ruby +8 -3
  12. data/bin/rpc_server +1 -0
  13. data/examples/lib/example/reverse-client.rb +2 -2
  14. data/install-protobuf.sh +28 -0
  15. data/lib/protobuf.rb +57 -53
  16. data/lib/protobuf/cli.rb +94 -74
  17. data/lib/protobuf/code_generator.rb +60 -9
  18. data/lib/protobuf/decoder.rb +19 -65
  19. data/lib/protobuf/deprecation.rb +117 -0
  20. data/lib/protobuf/descriptors/google/protobuf/compiler/plugin.pb.rb +11 -1
  21. data/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb +55 -3
  22. data/lib/protobuf/encoder.rb +13 -53
  23. data/lib/protobuf/enum.rb +58 -63
  24. data/lib/protobuf/field.rb +4 -4
  25. data/lib/protobuf/field/base_field.rb +101 -173
  26. data/lib/protobuf/field/bool_field.rb +17 -11
  27. data/lib/protobuf/field/bytes_field.rb +21 -35
  28. data/lib/protobuf/field/double_field.rb +0 -1
  29. data/lib/protobuf/field/enum_field.rb +23 -22
  30. data/lib/protobuf/field/field_array.rb +5 -4
  31. data/lib/protobuf/field/fixed32_field.rb +1 -1
  32. data/lib/protobuf/field/fixed64_field.rb +0 -1
  33. data/lib/protobuf/field/float_field.rb +4 -1
  34. data/lib/protobuf/field/int32_field.rb +0 -1
  35. data/lib/protobuf/field/int64_field.rb +0 -1
  36. data/lib/protobuf/field/integer_field.rb +0 -1
  37. data/lib/protobuf/field/message_field.rb +13 -28
  38. data/lib/protobuf/field/sfixed32_field.rb +0 -1
  39. data/lib/protobuf/field/sfixed64_field.rb +0 -1
  40. data/lib/protobuf/field/signed_integer_field.rb +0 -1
  41. data/lib/protobuf/field/sint32_field.rb +0 -1
  42. data/lib/protobuf/field/sint64_field.rb +0 -1
  43. data/lib/protobuf/field/string_field.rb +2 -4
  44. data/lib/protobuf/field/uint32_field.rb +0 -1
  45. data/lib/protobuf/field/uint64_field.rb +0 -1
  46. data/lib/protobuf/field/varint_field.rb +30 -13
  47. data/lib/protobuf/generators/base.rb +30 -16
  48. data/lib/protobuf/generators/enum_generator.rb +6 -9
  49. data/lib/protobuf/generators/extension_generator.rb +1 -2
  50. data/lib/protobuf/generators/field_generator.rb +25 -13
  51. data/lib/protobuf/generators/file_generator.rb +157 -35
  52. data/lib/protobuf/generators/group_generator.rb +22 -17
  53. data/lib/protobuf/generators/message_generator.rb +13 -14
  54. data/lib/protobuf/generators/option_generator.rb +17 -0
  55. data/lib/protobuf/generators/printable.rb +12 -13
  56. data/lib/protobuf/generators/service_generator.rb +2 -3
  57. data/lib/protobuf/http.rb +2 -2
  58. data/lib/protobuf/lifecycle.rb +20 -33
  59. data/lib/protobuf/logging.rb +39 -0
  60. data/lib/protobuf/message.rb +114 -47
  61. data/lib/protobuf/message/fields.rb +170 -88
  62. data/lib/protobuf/message/serialization.rb +19 -18
  63. data/lib/protobuf/optionable.rb +53 -6
  64. data/lib/protobuf/rpc/buffer.rb +18 -19
  65. data/lib/protobuf/rpc/client.rb +22 -50
  66. data/lib/protobuf/rpc/connectors/base.rb +177 -12
  67. data/lib/protobuf/rpc/connectors/http.rb +14 -9
  68. data/lib/protobuf/rpc/connectors/ping.rb +89 -0
  69. data/lib/protobuf/rpc/connectors/socket.rb +13 -8
  70. data/lib/protobuf/rpc/connectors/zmq.rb +178 -73
  71. data/lib/protobuf/rpc/dynamic_discovery.pb.rb +4 -1
  72. data/lib/protobuf/rpc/env.rb +12 -12
  73. data/lib/protobuf/rpc/error.rb +3 -3
  74. data/lib/protobuf/rpc/error/client_error.rb +4 -4
  75. data/lib/protobuf/rpc/error/server_error.rb +9 -9
  76. data/lib/protobuf/rpc/middleware/exception_handler.rb +6 -2
  77. data/lib/protobuf/rpc/middleware/logger.rb +8 -4
  78. data/lib/protobuf/rpc/middleware/request_decoder.rb +17 -21
  79. data/lib/protobuf/rpc/middleware/response_encoder.rb +22 -27
  80. data/lib/protobuf/rpc/middleware/statsd.rb +3 -3
  81. data/lib/protobuf/rpc/rpc.pb.rb +4 -1
  82. data/lib/protobuf/rpc/server.rb +1 -1
  83. data/lib/protobuf/rpc/servers/http/server.rb +19 -17
  84. data/lib/protobuf/rpc/servers/socket/server.rb +78 -70
  85. data/lib/protobuf/rpc/servers/socket/worker.rb +4 -4
  86. data/lib/protobuf/rpc/servers/socket_runner.rb +27 -15
  87. data/lib/protobuf/rpc/servers/zmq/broker.rb +70 -31
  88. data/lib/protobuf/rpc/servers/zmq/server.rb +55 -47
  89. data/lib/protobuf/rpc/servers/zmq/util.rb +14 -13
  90. data/lib/protobuf/rpc/servers/zmq/worker.rb +16 -16
  91. data/lib/protobuf/rpc/servers/zmq_runner.rb +26 -7
  92. data/lib/protobuf/rpc/service.rb +21 -27
  93. data/lib/protobuf/rpc/service_directory.rb +43 -27
  94. data/lib/protobuf/rpc/service_dispatcher.rb +9 -10
  95. data/lib/protobuf/rpc/service_filters.rb +32 -55
  96. data/lib/protobuf/rpc/stat.rb +4 -8
  97. data/lib/protobuf/socket.rb +1 -2
  98. data/lib/protobuf/tasks/compile.rake +3 -4
  99. data/lib/protobuf/varint.rb +9 -0
  100. data/lib/protobuf/varint_pure.rb +13 -0
  101. data/lib/protobuf/version.rb +1 -1
  102. data/lib/protobuf/zmq.rb +2 -2
  103. data/proto/google/protobuf/descriptor.proto +190 -31
  104. data/protobuffy.gemspec +30 -17
  105. data/spec/benchmark/tasks.rb +27 -19
  106. data/spec/bin/protoc-gen-ruby_spec.rb +11 -6
  107. data/spec/encoding/all_types_spec.rb +96 -84
  108. data/spec/encoding/extreme_values_spec.rb +0 -0
  109. data/spec/functional/class_inheritance_spec.rb +52 -0
  110. data/spec/functional/code_generator_spec.rb +38 -0
  111. data/spec/functional/socket_server_spec.rb +15 -15
  112. data/spec/functional/zmq_server_spec.rb +29 -27
  113. data/spec/lib/protobuf/cli_spec.rb +82 -67
  114. data/spec/lib/protobuf/code_generator_spec.rb +37 -10
  115. data/spec/lib/protobuf/enum_spec.rb +77 -46
  116. data/spec/lib/protobuf/field/bool_field_spec.rb +91 -0
  117. data/spec/lib/protobuf/field/double_field_spec.rb +9 -0
  118. data/spec/lib/protobuf/field/enum_field_spec.rb +26 -0
  119. data/spec/lib/protobuf/field/field_array_spec.rb +69 -0
  120. data/spec/lib/protobuf/field/fixed32_field_spec.rb +7 -0
  121. data/spec/lib/protobuf/field/fixed64_field_spec.rb +7 -0
  122. data/spec/lib/protobuf/field/float_field_spec.rb +90 -0
  123. data/spec/lib/protobuf/field/int32_field_spec.rb +114 -1
  124. data/spec/lib/protobuf/field/int64_field_spec.rb +7 -0
  125. data/spec/lib/protobuf/field/message_field_spec.rb +132 -0
  126. data/spec/lib/protobuf/field/sfixed32_field_spec.rb +9 -0
  127. data/spec/lib/protobuf/field/sfixed64_field_spec.rb +9 -0
  128. data/spec/lib/protobuf/field/sint32_field_spec.rb +9 -0
  129. data/spec/lib/protobuf/field/sint64_field_spec.rb +9 -0
  130. data/spec/lib/protobuf/field/string_field_spec.rb +44 -11
  131. data/spec/lib/protobuf/field/uint32_field_spec.rb +7 -0
  132. data/spec/lib/protobuf/field/uint64_field_spec.rb +7 -0
  133. data/spec/lib/protobuf/field_spec.rb +4 -6
  134. data/spec/lib/protobuf/generators/base_spec.rb +80 -13
  135. data/spec/lib/protobuf/generators/enum_generator_spec.rb +35 -21
  136. data/spec/lib/protobuf/generators/extension_generator_spec.rb +12 -13
  137. data/spec/lib/protobuf/generators/field_generator_spec.rb +73 -21
  138. data/spec/lib/protobuf/generators/file_generator_spec.rb +89 -6
  139. data/spec/lib/protobuf/generators/service_generator_spec.rb +25 -13
  140. data/spec/lib/protobuf/lifecycle_spec.rb +25 -20
  141. data/spec/lib/protobuf/message_spec.rb +578 -79
  142. data/spec/lib/protobuf/optionable_spec.rb +202 -26
  143. data/spec/lib/protobuf/rpc/client_spec.rb +16 -16
  144. data/spec/lib/protobuf/rpc/connectors/base_spec.rb +167 -13
  145. data/spec/lib/protobuf/rpc/connectors/connector_spec.rb +4 -5
  146. data/spec/lib/protobuf/rpc/connectors/http_spec.rb +13 -11
  147. data/spec/lib/protobuf/rpc/connectors/ping_spec.rb +69 -0
  148. data/spec/lib/protobuf/rpc/connectors/socket_spec.rb +6 -7
  149. data/spec/lib/protobuf/rpc/connectors/zmq_spec.rb +35 -52
  150. data/spec/lib/protobuf/rpc/middleware/exception_handler_spec.rb +10 -10
  151. data/spec/lib/protobuf/rpc/middleware/logger_spec.rb +11 -11
  152. data/spec/lib/protobuf/rpc/middleware/request_decoder_spec.rb +23 -23
  153. data/spec/lib/protobuf/rpc/middleware/response_encoder_spec.rb +11 -11
  154. data/spec/lib/protobuf/rpc/middleware/statsd_spec.rb +6 -6
  155. data/spec/lib/protobuf/rpc/servers/http/server_spec.rb +47 -44
  156. data/spec/lib/protobuf/rpc/servers/socket_server_spec.rb +6 -6
  157. data/spec/lib/protobuf/rpc/servers/zmq/server_spec.rb +12 -10
  158. data/spec/lib/protobuf/rpc/servers/zmq/util_spec.rb +11 -11
  159. data/spec/lib/protobuf/rpc/servers/zmq/worker_spec.rb +7 -7
  160. data/spec/lib/protobuf/rpc/service_directory_spec.rb +47 -49
  161. data/spec/lib/protobuf/rpc/service_dispatcher_spec.rb +8 -25
  162. data/spec/lib/protobuf/rpc/service_filters_spec.rb +102 -69
  163. data/spec/lib/protobuf/rpc/service_spec.rb +37 -36
  164. data/spec/lib/protobuf/rpc/stat_spec.rb +7 -9
  165. data/spec/lib/protobuf/varint_spec.rb +29 -0
  166. data/spec/lib/protobuf_spec.rb +55 -28
  167. data/spec/spec_helper.rb +12 -27
  168. data/spec/support/all.rb +0 -1
  169. data/spec/support/packed_field.rb +4 -3
  170. data/spec/support/{test → protos}/all_types.data.bin +0 -0
  171. data/spec/support/{test → protos}/all_types.data.txt +0 -0
  172. data/spec/support/{test → protos}/enum.pb.rb +8 -4
  173. data/spec/support/{test → protos}/enum.proto +4 -1
  174. data/spec/support/{test → protos}/extreme_values.data.bin +0 -0
  175. data/spec/support/protos/google_unittest.bin +0 -0
  176. data/spec/support/protos/google_unittest.pb.rb +798 -0
  177. data/spec/support/{test → protos}/google_unittest.proto +237 -66
  178. data/spec/support/protos/google_unittest_custom_options.bin +0 -0
  179. data/spec/support/protos/google_unittest_custom_options.pb.rb +268 -0
  180. data/spec/support/protos/google_unittest_custom_options.proto +424 -0
  181. data/spec/support/protos/google_unittest_import.pb.rb +55 -0
  182. data/spec/support/{test → protos}/google_unittest_import.proto +19 -10
  183. data/spec/support/protos/google_unittest_import_public.pb.rb +31 -0
  184. data/spec/support/{test → protos}/google_unittest_import_public.proto +8 -5
  185. data/spec/support/{test → protos}/multi_field_extensions.pb.rb +5 -2
  186. data/spec/support/{test → protos}/multi_field_extensions.proto +2 -0
  187. data/spec/support/{test → protos}/resource.pb.rb +47 -11
  188. data/spec/support/{test → protos}/resource.proto +24 -1
  189. data/spec/support/resource_service.rb +23 -0
  190. data/spec/support/server.rb +32 -61
  191. metadata +119 -59
  192. data/lib/protobuf/deprecator.rb +0 -42
  193. data/lib/protobuf/logger.rb +0 -93
  194. data/lib/protobuf/rpc/connector.rb +0 -21
  195. data/lib/protobuf/rpc/connectors/common.rb +0 -172
  196. data/spec/data/data.bin +0 -3
  197. data/spec/data/types.bin +0 -0
  198. data/spec/lib/protobuf/logger_spec.rb +0 -145
  199. data/spec/lib/protobuf/rpc/connector_spec.rb +0 -26
  200. data/spec/lib/protobuf/rpc/connectors/common_spec.rb +0 -170
  201. data/spec/support/test/defaults.pb.rb +0 -25
  202. data/spec/support/test/defaults.proto +0 -9
  203. data/spec/support/test/extended.pb.rb +0 -22
  204. data/spec/support/test/extended.proto +0 -10
  205. data/spec/support/test/google_unittest.pb.rb +0 -543
  206. data/spec/support/test/google_unittest_import.pb.rb +0 -37
  207. data/spec/support/test/google_unittest_import_public.pb.rb +0 -8
  208. data/spec/support/test/resource_service.rb +0 -26
  209. data/spec/support/tolerance_matcher.rb +0 -40
@@ -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
@@ -1,7 +1,8 @@
1
1
  require 'spec_helper'
2
2
  require 'protobuf/field'
3
+ require PROTOS_PATH.join('resource.pb')
3
4
 
4
- describe ::Protobuf::Field do
5
+ RSpec.describe ::Protobuf::Field do
5
6
 
6
7
  describe '.build' do
7
8
  pending
@@ -148,7 +149,6 @@ describe ::Protobuf::Field do
148
149
  end
149
150
  end
150
151
 
151
-
152
152
  context 'when type is a string field class or symbol' do
153
153
  it 'returns that class' do
154
154
  expected_field = ::Protobuf::Field::StringField
@@ -159,7 +159,6 @@ describe ::Protobuf::Field do
159
159
  end
160
160
  end
161
161
 
162
-
163
162
  context 'when type is a bytes field class or symbol' do
164
163
  it 'returns that class' do
165
164
  expected_field = ::Protobuf::Field::BytesField
@@ -170,7 +169,6 @@ describe ::Protobuf::Field do
170
169
  end
171
170
  end
172
171
 
173
-
174
172
  context 'when type is a bool field class or symbol' do
175
173
  it 'returns that class' do
176
174
  expected_field = ::Protobuf::Field::BoolField
@@ -183,9 +181,9 @@ describe ::Protobuf::Field do
183
181
 
184
182
  context 'when type is not mapped' do
185
183
  it 'raises an ArgumentError' do
186
- expect {
184
+ expect do
187
185
  subject.field_class("boom")
188
- }.to raise_error
186
+ end.to raise_error(ArgumentError)
189
187
  end
190
188
  end
191
189
 
@@ -3,15 +3,15 @@ require 'spec_helper'
3
3
  require 'protobuf/code_generator'
4
4
  require 'protobuf/generators/base'
5
5
 
6
- describe ::Protobuf::Generators::Base do
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') }
12
- subject { described_class.new(descriptor, 0, :namespace => [ :foo, :bar ]) }
13
- its(:type_namespace) { should eq [ :foo, :bar, 'Baz' ] }
14
- its(:fully_qualified_type_namespace) { should eq '.foo.bar.Baz' }
12
+ subject { described_class.new(descriptor, 0, :namespace => [:foo, :bar]) }
13
+ specify { expect(subject.type_namespace).to eq([:foo, :bar, 'Baz']) }
14
+ specify { expect(subject.fully_qualified_type_namespace).to eq('.foo.bar.Baz') }
15
15
  end
16
16
 
17
17
  describe '#run_once' do
@@ -67,21 +67,88 @@ describe ::Protobuf::Generators::Base do
67
67
  describe '#validate_tags' do
68
68
  context 'when tags are duplicated' do
69
69
  it 'fails with a GeneratorFatalError' do
70
- ::Protobuf::CodeGenerator.should_receive(:fatal)
71
- .with(/FooBar object has duplicate tags\. Expected 3 tags, but got 4/)
72
-
73
- described_class.validate_tags("FooBar", [1,2,2,3])
70
+ expect(::Protobuf::CodeGenerator).to receive(:fatal).with(/FooBar object has duplicate tags\. Expected 3 tags, but got 4/)
71
+ described_class.validate_tags("FooBar", [1, 2, 2, 3])
74
72
  end
75
73
  end
76
74
 
77
75
  context 'when tags are missing in the range' do
78
76
  it 'prints a warning' do
79
- ::Protobuf::CodeGenerator.should_receive(:warn)
80
- .with(/FooBar object should have 5 tags \(1\.\.5\), but found 4 tags/)
81
- described_class.validate_tags("FooBar", [1,2,4,5])
77
+ allow(ENV).to receive(:key?).and_call_original
78
+ allow(ENV).to receive(:key?).with("PB_NO_TAG_WARNINGS").and_return(false)
79
+ expect(::Protobuf::CodeGenerator).to receive(:print_tag_warning_suppress)
80
+ expect(::Protobuf::CodeGenerator).to receive(:warn).with(/FooBar object should have 5 tags \(1\.\.5\), but found 4 tags/)
81
+ described_class.validate_tags("FooBar", [1, 2, 4, 5])
82
82
  end
83
83
  end
84
84
  end
85
85
 
86
- end
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
87
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
154
+ end
@@ -2,67 +2,81 @@ require 'spec_helper'
2
2
 
3
3
  require 'protobuf/generators/enum_generator'
4
4
 
5
- describe ::Protobuf::Generators::EnumGenerator do
5
+ RSpec.describe ::Protobuf::Generators::EnumGenerator do
6
6
 
7
- let(:values) {
7
+ let(:values) do
8
8
  [
9
9
  { :name => 'FOO', :number => 1 },
10
10
  { :name => 'BAR', :number => 2 },
11
- { :name => 'BAZ', :number => 3 }
11
+ { :name => 'BAZ', :number => 3 },
12
12
  ]
13
- }
13
+ end
14
14
  let(:options) { nil }
15
- let(:enum_fields) { { :name => 'TestEnum',
16
- :value => values,
17
- :options => options } }
15
+ let(:enum_fields) do
16
+ {
17
+ :name => 'TestEnum',
18
+ :value => values,
19
+ :options => options,
20
+ }
21
+ end
18
22
 
19
23
  let(:enum) { ::Google::Protobuf::EnumDescriptorProto.new(enum_fields) }
20
24
 
21
25
  subject { described_class.new(enum) }
22
26
 
23
27
  describe '#compile' do
24
- let(:compiled) {
25
- %q{class TestEnum < ::Protobuf::Enum
28
+ let(:compiled) do
29
+ <<-RUBY
30
+ class TestEnum < ::Protobuf::Enum
26
31
  define :FOO, 1
27
32
  define :BAR, 2
28
33
  define :BAZ, 3
29
34
  end
30
35
 
31
- }
32
- }
36
+ RUBY
37
+ end
33
38
 
34
- it 'compiles the enum and it\'s field values' do
39
+ it 'compiles the enum and its field values' do
35
40
  subject.compile
36
- subject.to_s.should eq(compiled)
41
+ expect(subject.to_s).to eq(compiled)
37
42
  end
38
43
 
39
44
  context 'when allow_alias option is set' do
40
- let(:compiled) {
41
- %q{class TestEnum < ::Protobuf::Enum
42
- set_option :allow_alias
45
+ let(:compiled) do
46
+ <<-RUBY
47
+ class TestEnum < ::Protobuf::Enum
48
+ set_option :allow_alias, true
43
49
 
44
50
  define :FOO, 1
45
51
  define :BAR, 2
46
52
  define :BAZ, 3
47
53
  end
48
54
 
49
- }
50
- }
55
+ RUBY
56
+ end
51
57
 
52
58
  let(:options) { { :allow_alias => true } }
53
59
 
54
60
  it 'sets the allow_alias option' do
55
61
  subject.compile
56
- subject.to_s.should eq(compiled)
62
+ expect(subject.to_s).to eq(compiled)
57
63
  end
58
64
  end
59
65
  end
60
66
 
61
67
  describe '#build_value' do
62
68
  it 'returns a string identifying the given enum value' do
63
- subject.build_value(enum.value.first).should eq("define :FOO, 1")
69
+ expect(subject.build_value(enum.value.first)).to eq("define :FOO, 1")
70
+ end
71
+
72
+ context 'with PB_UPCASE_ENUMS set' do
73
+ before { allow(ENV).to receive(:key?).with('PB_UPCASE_ENUMS').and_return(true) }
74
+ let(:values) { [{ :name => 'boom', :number => 1 }] }
75
+
76
+ it 'returns a string with the given enum name in ALL CAPS' do
77
+ expect(subject.build_value(enum.value.first)).to eq("define :BOOM, 1")
78
+ end
64
79
  end
65
80
  end
66
81
 
67
82
  end
68
-
@@ -3,41 +3,40 @@ require 'spec_helper'
3
3
  require 'protobuf/code_generator'
4
4
  require 'protobuf/generators/extension_generator'
5
5
 
6
- describe ::Protobuf::Generators::ExtensionGenerator do
6
+ RSpec.describe ::Protobuf::Generators::ExtensionGenerator do
7
7
 
8
- let(:field_descriptors) {
8
+ let(:field_descriptors) do
9
9
  [
10
10
  double('field descriptor 1', :to_s => " field 1\n"),
11
11
  double('field descriptor 2', :to_s => " field 2\n"),
12
- double('field descriptor 3', :to_s => " field 3\n")
12
+ double('field descriptor 3', :to_s => " field 3\n"),
13
13
  ]
14
- }
14
+ end
15
15
  let(:message_type) { 'FooBar' }
16
16
 
17
17
  before do
18
- ::Protobuf::Generators::FieldGenerator.should_receive(:new).with(field_descriptors[0], 1).and_return(field_descriptors[0])
19
- ::Protobuf::Generators::FieldGenerator.should_receive(:new).with(field_descriptors[1], 1).and_return(field_descriptors[1])
20
- ::Protobuf::Generators::FieldGenerator.should_receive(:new).with(field_descriptors[2], 1).and_return(field_descriptors[2])
18
+ expect(::Protobuf::Generators::FieldGenerator).to receive(:new).with(field_descriptors[0], 1).and_return(field_descriptors[0])
19
+ expect(::Protobuf::Generators::FieldGenerator).to receive(:new).with(field_descriptors[1], 1).and_return(field_descriptors[1])
20
+ expect(::Protobuf::Generators::FieldGenerator).to receive(:new).with(field_descriptors[2], 1).and_return(field_descriptors[2])
21
21
  end
22
22
 
23
23
  subject { described_class.new(message_type, field_descriptors, 0) }
24
24
 
25
25
  describe '#compile' do
26
- let(:compiled) {
27
- %q{class FooBar < ::Protobuf::Message
26
+ let(:compiled) do
27
+ 'class FooBar < ::Protobuf::Message
28
28
  field 1
29
29
  field 2
30
30
  field 3
31
31
  end
32
32
 
33
- }
34
- }
33
+ '
34
+ end
35
35
 
36
36
  it 'compiles the a class with the extension fields' do
37
37
  subject.compile
38
- subject.to_s.should eq(compiled)
38
+ expect(subject.to_s).to eq(compiled)
39
39
  end
40
40
  end
41
41
 
42
42
  end
43
-
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  require 'protobuf/generators/field_generator'
4
4
 
5
- describe ::Protobuf::Generators::FieldGenerator do
5
+ RSpec.describe ::Protobuf::Generators::FieldGenerator do
6
6
 
7
7
  let(:label_enum) { :LABEL_OPTIONAL }
8
8
  let(:name) { 'foo_bar' }
@@ -13,47 +13,51 @@ describe ::Protobuf::Generators::FieldGenerator do
13
13
  let(:extendee) { nil }
14
14
  let(:field_options) { {} }
15
15
 
16
- let(:field_fields) { { :label => label_enum,
17
- :name => name,
18
- :number => number,
19
- :type => type_enum,
20
- :type_name => type_name,
21
- :default_value => default_value,
22
- :extendee => extendee,
23
- :options => field_options } }
16
+ let(:field_fields) do
17
+ {
18
+ :label => label_enum,
19
+ :name => name,
20
+ :number => number,
21
+ :type => type_enum,
22
+ :type_name => type_name,
23
+ :default_value => default_value,
24
+ :extendee => extendee,
25
+ :options => field_options,
26
+ }
27
+ end
24
28
 
25
29
  let(:field) { ::Google::Protobuf::FieldDescriptorProto.new(field_fields) }
26
30
 
27
31
  describe '#compile' do
28
32
  subject { described_class.new(field).to_s }
29
33
 
30
- it { should eq "optional :string, :foo_bar, 3\n" }
34
+ specify { expect(subject).to eq "optional :string, :foo_bar, 3\n" }
31
35
 
32
36
  context 'when the type is another message' do
33
37
  let(:type_enum) { :TYPE_MESSAGE }
34
38
  let(:type_name) { '.foo.bar.Baz' }
35
39
 
36
- it { should eq "optional ::Foo::Bar::Baz, :foo_bar, 3\n" }
40
+ specify { expect(subject).to eq "optional ::Foo::Bar::Baz, :foo_bar, 3\n" }
37
41
  end
38
42
 
39
43
  context 'when a default value is used' do
40
44
  let(:type_enum) { :TYPE_INT32 }
41
45
  let(:default_value) { '42' }
42
- it { should eq "optional :int32, :foo_bar, 3, :default => 42\n" }
46
+ specify { expect(subject).to eq "optional :int32, :foo_bar, 3, :default => 42\n" }
43
47
 
44
48
  context 'when type is an enum' do
45
49
  let(:type_enum) { :TYPE_ENUM }
46
50
  let(:type_name) { '.foo.bar.Baz' }
47
51
  let(:default_value) { 'QUUX' }
48
52
 
49
- it { should eq "optional ::Foo::Bar::Baz, :foo_bar, 3, :default => ::Foo::Bar::Baz::QUUX\n" }
53
+ specify { expect(subject).to eq "optional ::Foo::Bar::Baz, :foo_bar, 3, :default => ::Foo::Bar::Baz::QUUX\n" }
50
54
  end
51
55
 
52
56
  context 'when the type is a string' do
53
57
  let(:type_enum) { :TYPE_STRING }
54
58
  let(:default_value) { "a default \"string\"" }
55
59
 
56
- it { should eq %Q{optional :string, :foo_bar, 3, :default => "a default \"string\""\n} }
60
+ specify { expect(subject).to eq "optional :string, :foo_bar, 3, :default => \"a default \"string\"\"\n" }
57
61
  end
58
62
 
59
63
  context 'when float or double field type' do
@@ -61,17 +65,17 @@ describe ::Protobuf::Generators::FieldGenerator do
61
65
 
62
66
  context 'when the default value is "nan"' do
63
67
  let(:default_value) { 'nan' }
64
- it { should match(/::Float::NAN/) }
68
+ specify { expect(subject).to match(/::Float::NAN/) }
65
69
  end
66
70
 
67
71
  context 'when the default value is "inf"' do
68
72
  let(:default_value) { 'inf' }
69
- it { should match(/::Float::INFINITY/) }
73
+ specify { expect(subject).to match(/::Float::INFINITY/) }
70
74
  end
71
75
 
72
76
  context 'when the default value is "-inf"' do
73
77
  let(:default_value) { '-inf' }
74
- it { should match(/-::Float::INFINITY/) }
78
+ specify { expect(subject).to match(/-::Float::INFINITY/) }
75
79
  end
76
80
  end
77
81
  end
@@ -79,21 +83,69 @@ describe ::Protobuf::Generators::FieldGenerator do
79
83
  context 'when the field is an extension' do
80
84
  let(:extendee) { 'foo.bar.Baz' }
81
85
 
82
- it { should eq "optional :string, :foo_bar, 3, :extension => true\n" }
86
+ specify { expect(subject).to eq "optional :string, :foo_bar, 3, :extension => true\n" }
83
87
  end
84
88
 
85
89
  context 'when field is packed' do
86
90
  let(:field_options) { { :packed => true } }
87
91
 
88
- it { should eq "optional :string, :foo_bar, 3, :packed => true\n" }
92
+ specify { expect(subject).to eq "optional :string, :foo_bar, 3, :packed => true\n" }
89
93
  end
90
94
 
91
95
  context 'when field is deprecated' do
92
96
  let(:field_options) { { :deprecated => true } }
93
97
 
94
- it { should eq "optional :string, :foo_bar, 3, :deprecated => true\n" }
98
+ specify { expect(subject).to eq "optional :string, :foo_bar, 3, :deprecated => true\n" }
99
+ end
100
+
101
+ context 'when field uses a custom option that is an extension' do
102
+ class ::CustomFieldEnum < ::Protobuf::Enum
103
+ define :BOOM, 1
104
+ define :BAM, 2
105
+ end
106
+
107
+ class ::CustomFieldMessage < ::Protobuf::Message
108
+ optional :string, :foo, 1
109
+ end
110
+
111
+ class ::Google::Protobuf::FieldOptions < ::Protobuf::Message
112
+ optional :string, :custom_string_option, 22000, :extension => true
113
+ optional :bool, :custom_bool_option, 22001, :extension => true
114
+ optional :int32, :custom_int32_option, 22002, :extension => true
115
+ optional ::CustomFieldEnum, :custom_enum_option, 22003, :extension => true
116
+ optional ::CustomFieldMessage, :custom_message_option, 22004, :extension => true
117
+ end
118
+
119
+ describe 'option has a string value' do
120
+ let(:field_options) { { :custom_string_option => 'boom' } }
121
+
122
+ specify { expect(subject).to eq "optional :string, :foo_bar, 3, :custom_string_option => \"boom\"\n" }
123
+ end
124
+
125
+ describe 'option has a bool value' do
126
+ let(:field_options) { { :custom_bool_option => true } }
127
+
128
+ specify { expect(subject).to eq "optional :string, :foo_bar, 3, :custom_bool_option => true\n" }
129
+ end
130
+
131
+ describe 'option has a int32 value' do
132
+ let(:field_options) { { :custom_int32_option => 123 } }
133
+
134
+ specify { expect(subject).to eq "optional :string, :foo_bar, 3, :custom_int32_option => 123\n" }
135
+ end
136
+
137
+ describe 'option has a message value' do
138
+ let(:field_options) { { :custom_message_option => CustomFieldMessage.new(:foo => 'boom') } }
139
+
140
+ specify { expect(subject).to eq "optional :string, :foo_bar, 3, :custom_message_option => { :foo => \"boom\" }\n" }
141
+ end
142
+
143
+ describe 'option has a enum value' do
144
+ let(:field_options) { { :custom_enum_option => CustomFieldEnum::BAM } }
145
+
146
+ specify { expect(subject).to eq "optional :string, :foo_bar, 3, :custom_enum_option => ::CustomFieldEnum::BAM\n" }
147
+ end
95
148
  end
96
149
  end
97
150
 
98
151
  end
99
-