protobuf-cucumber 3.10.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (204) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +21 -0
  3. data/.rubocop.yml +70 -0
  4. data/.rubocop_todo.yml +145 -0
  5. data/.travis.yml +40 -0
  6. data/.yardopts +5 -0
  7. data/CHANGES.md +344 -0
  8. data/CONTRIBUTING.md +16 -0
  9. data/Gemfile +3 -0
  10. data/LICENSE.txt +22 -0
  11. data/README.md +33 -0
  12. data/Rakefile +64 -0
  13. data/bin/protoc-gen-ruby +22 -0
  14. data/bin/rpc_server +5 -0
  15. data/install-protobuf.sh +28 -0
  16. data/lib/protobuf.rb +129 -0
  17. data/lib/protobuf/cli.rb +257 -0
  18. data/lib/protobuf/code_generator.rb +120 -0
  19. data/lib/protobuf/decoder.rb +28 -0
  20. data/lib/protobuf/deprecation.rb +117 -0
  21. data/lib/protobuf/descriptors.rb +3 -0
  22. data/lib/protobuf/descriptors/google/protobuf/compiler/plugin.pb.rb +62 -0
  23. data/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb +301 -0
  24. data/lib/protobuf/encoder.rb +11 -0
  25. data/lib/protobuf/enum.rb +365 -0
  26. data/lib/protobuf/exceptions.rb +9 -0
  27. data/lib/protobuf/field.rb +74 -0
  28. data/lib/protobuf/field/base_field.rb +380 -0
  29. data/lib/protobuf/field/base_field_object_definitions.rb +504 -0
  30. data/lib/protobuf/field/bool_field.rb +64 -0
  31. data/lib/protobuf/field/bytes_field.rb +78 -0
  32. data/lib/protobuf/field/double_field.rb +25 -0
  33. data/lib/protobuf/field/enum_field.rb +61 -0
  34. data/lib/protobuf/field/field_array.rb +104 -0
  35. data/lib/protobuf/field/field_hash.rb +122 -0
  36. data/lib/protobuf/field/fixed32_field.rb +25 -0
  37. data/lib/protobuf/field/fixed64_field.rb +28 -0
  38. data/lib/protobuf/field/float_field.rb +43 -0
  39. data/lib/protobuf/field/int32_field.rb +21 -0
  40. data/lib/protobuf/field/int64_field.rb +34 -0
  41. data/lib/protobuf/field/integer_field.rb +23 -0
  42. data/lib/protobuf/field/message_field.rb +51 -0
  43. data/lib/protobuf/field/sfixed32_field.rb +27 -0
  44. data/lib/protobuf/field/sfixed64_field.rb +28 -0
  45. data/lib/protobuf/field/signed_integer_field.rb +29 -0
  46. data/lib/protobuf/field/sint32_field.rb +21 -0
  47. data/lib/protobuf/field/sint64_field.rb +21 -0
  48. data/lib/protobuf/field/string_field.rb +51 -0
  49. data/lib/protobuf/field/uint32_field.rb +21 -0
  50. data/lib/protobuf/field/uint64_field.rb +21 -0
  51. data/lib/protobuf/field/varint_field.rb +77 -0
  52. data/lib/protobuf/generators/base.rb +85 -0
  53. data/lib/protobuf/generators/enum_generator.rb +39 -0
  54. data/lib/protobuf/generators/extension_generator.rb +27 -0
  55. data/lib/protobuf/generators/field_generator.rb +193 -0
  56. data/lib/protobuf/generators/file_generator.rb +262 -0
  57. data/lib/protobuf/generators/group_generator.rb +122 -0
  58. data/lib/protobuf/generators/message_generator.rb +104 -0
  59. data/lib/protobuf/generators/option_generator.rb +17 -0
  60. data/lib/protobuf/generators/printable.rb +160 -0
  61. data/lib/protobuf/generators/service_generator.rb +50 -0
  62. data/lib/protobuf/lifecycle.rb +33 -0
  63. data/lib/protobuf/logging.rb +39 -0
  64. data/lib/protobuf/message.rb +260 -0
  65. data/lib/protobuf/message/fields.rb +233 -0
  66. data/lib/protobuf/message/serialization.rb +85 -0
  67. data/lib/protobuf/optionable.rb +70 -0
  68. data/lib/protobuf/rpc/buffer.rb +78 -0
  69. data/lib/protobuf/rpc/client.rb +140 -0
  70. data/lib/protobuf/rpc/connectors/base.rb +221 -0
  71. data/lib/protobuf/rpc/connectors/ping.rb +89 -0
  72. data/lib/protobuf/rpc/connectors/socket.rb +78 -0
  73. data/lib/protobuf/rpc/connectors/zmq.rb +319 -0
  74. data/lib/protobuf/rpc/dynamic_discovery.pb.rb +50 -0
  75. data/lib/protobuf/rpc/env.rb +60 -0
  76. data/lib/protobuf/rpc/error.rb +28 -0
  77. data/lib/protobuf/rpc/error/client_error.rb +31 -0
  78. data/lib/protobuf/rpc/error/server_error.rb +43 -0
  79. data/lib/protobuf/rpc/middleware.rb +25 -0
  80. data/lib/protobuf/rpc/middleware/exception_handler.rb +40 -0
  81. data/lib/protobuf/rpc/middleware/logger.rb +95 -0
  82. data/lib/protobuf/rpc/middleware/request_decoder.rb +79 -0
  83. data/lib/protobuf/rpc/middleware/response_encoder.rb +83 -0
  84. data/lib/protobuf/rpc/middleware/runner.rb +18 -0
  85. data/lib/protobuf/rpc/rpc.pb.rb +64 -0
  86. data/lib/protobuf/rpc/rpc_method.rb +16 -0
  87. data/lib/protobuf/rpc/server.rb +39 -0
  88. data/lib/protobuf/rpc/servers/socket/server.rb +121 -0
  89. data/lib/protobuf/rpc/servers/socket/worker.rb +56 -0
  90. data/lib/protobuf/rpc/servers/socket_runner.rb +46 -0
  91. data/lib/protobuf/rpc/servers/zmq/broker.rb +194 -0
  92. data/lib/protobuf/rpc/servers/zmq/server.rb +321 -0
  93. data/lib/protobuf/rpc/servers/zmq/util.rb +48 -0
  94. data/lib/protobuf/rpc/servers/zmq/worker.rb +105 -0
  95. data/lib/protobuf/rpc/servers/zmq_runner.rb +70 -0
  96. data/lib/protobuf/rpc/service.rb +172 -0
  97. data/lib/protobuf/rpc/service_directory.rb +261 -0
  98. data/lib/protobuf/rpc/service_dispatcher.rb +45 -0
  99. data/lib/protobuf/rpc/service_filters.rb +250 -0
  100. data/lib/protobuf/rpc/stat.rb +119 -0
  101. data/lib/protobuf/socket.rb +21 -0
  102. data/lib/protobuf/tasks.rb +1 -0
  103. data/lib/protobuf/tasks/compile.rake +80 -0
  104. data/lib/protobuf/varint.rb +20 -0
  105. data/lib/protobuf/varint_pure.rb +31 -0
  106. data/lib/protobuf/version.rb +3 -0
  107. data/lib/protobuf/wire_type.rb +10 -0
  108. data/lib/protobuf/zmq.rb +21 -0
  109. data/profile.html +5103 -0
  110. data/proto/dynamic_discovery.proto +44 -0
  111. data/proto/google/protobuf/compiler/plugin.proto +147 -0
  112. data/proto/google/protobuf/descriptor.proto +779 -0
  113. data/proto/rpc.proto +69 -0
  114. data/protobuf-cucumber.gemspec +57 -0
  115. data/spec/benchmark/tasks.rb +143 -0
  116. data/spec/bin/protoc-gen-ruby_spec.rb +23 -0
  117. data/spec/encoding/all_types_spec.rb +103 -0
  118. data/spec/encoding/extreme_values_spec.rb +0 -0
  119. data/spec/functional/class_inheritance_spec.rb +52 -0
  120. data/spec/functional/code_generator_spec.rb +58 -0
  121. data/spec/functional/socket_server_spec.rb +59 -0
  122. data/spec/functional/zmq_server_spec.rb +105 -0
  123. data/spec/lib/protobuf/cli_spec.rb +317 -0
  124. data/spec/lib/protobuf/code_generator_spec.rb +87 -0
  125. data/spec/lib/protobuf/enum_spec.rb +307 -0
  126. data/spec/lib/protobuf/field/bool_field_spec.rb +91 -0
  127. data/spec/lib/protobuf/field/double_field_spec.rb +9 -0
  128. data/spec/lib/protobuf/field/enum_field_spec.rb +44 -0
  129. data/spec/lib/protobuf/field/field_array_spec.rb +105 -0
  130. data/spec/lib/protobuf/field/field_hash_spec.rb +168 -0
  131. data/spec/lib/protobuf/field/fixed32_field_spec.rb +7 -0
  132. data/spec/lib/protobuf/field/fixed64_field_spec.rb +7 -0
  133. data/spec/lib/protobuf/field/float_field_spec.rb +90 -0
  134. data/spec/lib/protobuf/field/int32_field_spec.rb +120 -0
  135. data/spec/lib/protobuf/field/int64_field_spec.rb +7 -0
  136. data/spec/lib/protobuf/field/message_field_spec.rb +132 -0
  137. data/spec/lib/protobuf/field/sfixed32_field_spec.rb +9 -0
  138. data/spec/lib/protobuf/field/sfixed64_field_spec.rb +9 -0
  139. data/spec/lib/protobuf/field/sint32_field_spec.rb +9 -0
  140. data/spec/lib/protobuf/field/sint64_field_spec.rb +9 -0
  141. data/spec/lib/protobuf/field/string_field_spec.rb +79 -0
  142. data/spec/lib/protobuf/field/uint32_field_spec.rb +7 -0
  143. data/spec/lib/protobuf/field/uint64_field_spec.rb +7 -0
  144. data/spec/lib/protobuf/field_spec.rb +192 -0
  145. data/spec/lib/protobuf/generators/base_spec.rb +154 -0
  146. data/spec/lib/protobuf/generators/enum_generator_spec.rb +82 -0
  147. data/spec/lib/protobuf/generators/extension_generator_spec.rb +42 -0
  148. data/spec/lib/protobuf/generators/field_generator_spec.rb +197 -0
  149. data/spec/lib/protobuf/generators/file_generator_spec.rb +119 -0
  150. data/spec/lib/protobuf/generators/message_generator_spec.rb +0 -0
  151. data/spec/lib/protobuf/generators/service_generator_spec.rb +99 -0
  152. data/spec/lib/protobuf/lifecycle_spec.rb +94 -0
  153. data/spec/lib/protobuf/message_spec.rb +944 -0
  154. data/spec/lib/protobuf/optionable_spec.rb +265 -0
  155. data/spec/lib/protobuf/rpc/client_spec.rb +66 -0
  156. data/spec/lib/protobuf/rpc/connectors/base_spec.rb +226 -0
  157. data/spec/lib/protobuf/rpc/connectors/ping_spec.rb +69 -0
  158. data/spec/lib/protobuf/rpc/connectors/socket_spec.rb +34 -0
  159. data/spec/lib/protobuf/rpc/connectors/zmq_spec.rb +110 -0
  160. data/spec/lib/protobuf/rpc/middleware/exception_handler_spec.rb +62 -0
  161. data/spec/lib/protobuf/rpc/middleware/logger_spec.rb +49 -0
  162. data/spec/lib/protobuf/rpc/middleware/request_decoder_spec.rb +115 -0
  163. data/spec/lib/protobuf/rpc/middleware/response_encoder_spec.rb +91 -0
  164. data/spec/lib/protobuf/rpc/servers/socket_server_spec.rb +38 -0
  165. data/spec/lib/protobuf/rpc/servers/zmq/server_spec.rb +43 -0
  166. data/spec/lib/protobuf/rpc/servers/zmq/util_spec.rb +55 -0
  167. data/spec/lib/protobuf/rpc/servers/zmq/worker_spec.rb +35 -0
  168. data/spec/lib/protobuf/rpc/service_directory_spec.rb +293 -0
  169. data/spec/lib/protobuf/rpc/service_dispatcher_spec.rb +35 -0
  170. data/spec/lib/protobuf/rpc/service_filters_spec.rb +517 -0
  171. data/spec/lib/protobuf/rpc/service_spec.rb +162 -0
  172. data/spec/lib/protobuf/rpc/stat_spec.rb +101 -0
  173. data/spec/lib/protobuf/varint_spec.rb +29 -0
  174. data/spec/lib/protobuf_spec.rb +105 -0
  175. data/spec/spec_helper.rb +42 -0
  176. data/spec/support/all.rb +6 -0
  177. data/spec/support/packed_field.rb +23 -0
  178. data/spec/support/protos/all_types.data.bin +0 -0
  179. data/spec/support/protos/all_types.data.txt +119 -0
  180. data/spec/support/protos/enum.pb.rb +63 -0
  181. data/spec/support/protos/enum.proto +37 -0
  182. data/spec/support/protos/extreme_values.data.bin +0 -0
  183. data/spec/support/protos/google_unittest.bin +0 -0
  184. data/spec/support/protos/google_unittest.pb.rb +798 -0
  185. data/spec/support/protos/google_unittest.proto +884 -0
  186. data/spec/support/protos/google_unittest_custom_options.bin +0 -0
  187. data/spec/support/protos/google_unittest_custom_options.pb.rb +361 -0
  188. data/spec/support/protos/google_unittest_custom_options.proto +424 -0
  189. data/spec/support/protos/google_unittest_import.pb.rb +55 -0
  190. data/spec/support/protos/google_unittest_import.proto +73 -0
  191. data/spec/support/protos/google_unittest_import_public.pb.rb +31 -0
  192. data/spec/support/protos/google_unittest_import_public.proto +41 -0
  193. data/spec/support/protos/map-test.bin +157 -0
  194. data/spec/support/protos/map-test.pb.rb +85 -0
  195. data/spec/support/protos/map-test.proto +68 -0
  196. data/spec/support/protos/multi_field_extensions.pb.rb +59 -0
  197. data/spec/support/protos/multi_field_extensions.proto +35 -0
  198. data/spec/support/protos/resource.pb.rb +172 -0
  199. data/spec/support/protos/resource.proto +137 -0
  200. data/spec/support/resource_service.rb +23 -0
  201. data/spec/support/server.rb +65 -0
  202. data/spec/support/test_app_file.rb +2 -0
  203. data/varint_prof.rb +82 -0
  204. metadata +579 -0
@@ -0,0 +1,154 @@
1
+ require 'spec_helper'
2
+
3
+ require 'protobuf/code_generator'
4
+ require 'protobuf/generators/base'
5
+
6
+ RSpec.describe ::Protobuf::Generators::Base do
7
+
8
+ subject(:generator) { described_class.new(double) }
9
+
10
+ context 'namespaces' do
11
+ let(:descriptor) { double(:name => '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
+ end
16
+
17
+ describe '#run_once' do
18
+ it 'protects the block from being entered more than once' do
19
+ foo = 0
20
+ bar = 0
21
+
22
+ test_run_once = lambda do
23
+ bar += 1
24
+ subject.run_once(:foo_test) do
25
+ foo += 1
26
+ end
27
+ end
28
+
29
+ 10.times { test_run_once.call }
30
+ expect(foo).to eq(1)
31
+ expect(bar).to eq(10)
32
+ end
33
+
34
+ it 'always returns the same object' do
35
+ rv = subject.run_once(:foo_test) do
36
+ "foo bar"
37
+ end
38
+ expect(rv).to eq("foo bar")
39
+
40
+ rv = subject.run_once(:foo_test) do
41
+ "baz quux"
42
+ end
43
+ expect(rv).to eq("foo bar")
44
+ end
45
+ end
46
+
47
+ describe '#to_s' do
48
+ before do
49
+ class ToStringTest < ::Protobuf::Generators::Base
50
+ def compile
51
+ run_once(:compile) do
52
+ puts "this is a test"
53
+ end
54
+ end
55
+ end
56
+ end
57
+
58
+ subject { ToStringTest.new(double) }
59
+
60
+ it 'compiles and returns the contents' do
61
+ 10.times do
62
+ expect(subject.to_s).to eq("this is a test\n")
63
+ end
64
+ end
65
+ end
66
+
67
+ describe '#validate_tags' do
68
+ context 'when tags are duplicated' do
69
+ it 'fails with a GeneratorFatalError' do
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])
72
+ end
73
+ end
74
+
75
+ context 'when tags are missing in the range' do
76
+ it 'prints a warning' do
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
+ end
83
+ end
84
+ end
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
154
+ end
@@ -0,0 +1,82 @@
1
+ require 'spec_helper'
2
+
3
+ require 'protobuf/generators/enum_generator'
4
+
5
+ RSpec.describe ::Protobuf::Generators::EnumGenerator do
6
+
7
+ let(:values) do
8
+ [
9
+ { :name => 'FOO', :number => 1 },
10
+ { :name => 'BAR', :number => 2 },
11
+ { :name => 'BAZ', :number => 3 },
12
+ ]
13
+ end
14
+ let(:options) { nil }
15
+ let(:enum_fields) do
16
+ {
17
+ :name => 'TestEnum',
18
+ :value => values,
19
+ :options => options,
20
+ }
21
+ end
22
+
23
+ let(:enum) { ::Google::Protobuf::EnumDescriptorProto.new(enum_fields) }
24
+
25
+ subject { described_class.new(enum) }
26
+
27
+ describe '#compile' do
28
+ let(:compiled) do
29
+ <<-RUBY
30
+ class TestEnum < ::Protobuf::Enum
31
+ define :FOO, 1
32
+ define :BAR, 2
33
+ define :BAZ, 3
34
+ end
35
+
36
+ RUBY
37
+ end
38
+
39
+ it 'compiles the enum and its field values' do
40
+ subject.compile
41
+ expect(subject.to_s).to eq(compiled)
42
+ end
43
+
44
+ context 'when allow_alias option is set' do
45
+ let(:compiled) do
46
+ <<-RUBY
47
+ class TestEnum < ::Protobuf::Enum
48
+ set_option :allow_alias, true
49
+
50
+ define :FOO, 1
51
+ define :BAR, 2
52
+ define :BAZ, 3
53
+ end
54
+
55
+ RUBY
56
+ end
57
+
58
+ let(:options) { { :allow_alias => true } }
59
+
60
+ it 'sets the allow_alias option' do
61
+ subject.compile
62
+ expect(subject.to_s).to eq(compiled)
63
+ end
64
+ end
65
+ end
66
+
67
+ describe '#build_value' do
68
+ it 'returns a string identifying the given enum value' do
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
79
+ end
80
+ end
81
+
82
+ end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ require 'protobuf/code_generator'
4
+ require 'protobuf/generators/extension_generator'
5
+
6
+ RSpec.describe ::Protobuf::Generators::ExtensionGenerator do
7
+
8
+ let(:field_descriptors) do
9
+ [
10
+ double('field descriptor 1', :to_s => " field 1\n"),
11
+ double('field descriptor 2', :to_s => " field 2\n"),
12
+ double('field descriptor 3', :to_s => " field 3\n"),
13
+ ]
14
+ end
15
+ let(:message_type) { 'FooBar' }
16
+
17
+ before do
18
+ expect(::Protobuf::Generators::FieldGenerator).to receive(:new).with(field_descriptors[0], nil, 1).and_return(field_descriptors[0])
19
+ expect(::Protobuf::Generators::FieldGenerator).to receive(:new).with(field_descriptors[1], nil, 1).and_return(field_descriptors[1])
20
+ expect(::Protobuf::Generators::FieldGenerator).to receive(:new).with(field_descriptors[2], nil, 1).and_return(field_descriptors[2])
21
+ end
22
+
23
+ subject { described_class.new(message_type, field_descriptors, 0) }
24
+
25
+ describe '#compile' do
26
+ let(:compiled) do
27
+ 'class FooBar < ::Protobuf::Message
28
+ field 1
29
+ field 2
30
+ field 3
31
+ end
32
+
33
+ '
34
+ end
35
+
36
+ it 'compiles the a class with the extension fields' do
37
+ subject.compile
38
+ expect(subject.to_s).to eq(compiled)
39
+ end
40
+ end
41
+
42
+ end
@@ -0,0 +1,197 @@
1
+ require 'spec_helper'
2
+
3
+ require 'protobuf/generators/field_generator'
4
+
5
+ RSpec.describe ::Protobuf::Generators::FieldGenerator do
6
+
7
+ let(:label_enum) { :LABEL_OPTIONAL }
8
+ let(:name) { 'foo_bar' }
9
+ let(:number) { 3 }
10
+ let(:type_enum) { :TYPE_STRING }
11
+ let(:type_name) { nil }
12
+ let(:default_value) { nil }
13
+ let(:extendee) { nil }
14
+ let(:field_options) { {} }
15
+
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
28
+ let(:field) { ::Google::Protobuf::FieldDescriptorProto.new(field_fields) }
29
+
30
+ let(:nested_types) { [] }
31
+ let(:owner_fields) do
32
+ {
33
+ :name => 'Baz',
34
+ :field => [field],
35
+ :nested_type => nested_types,
36
+ }
37
+ end
38
+ let(:owner_msg) { ::Google::Protobuf::DescriptorProto.new(owner_fields) }
39
+
40
+ describe '#compile' do
41
+ subject { described_class.new(field, owner_msg, 1).to_s }
42
+
43
+ specify { expect(subject).to eq " optional :string, :foo_bar, 3\n" }
44
+
45
+ context 'when the type is another message' do
46
+ let(:type_enum) { :TYPE_MESSAGE }
47
+ let(:type_name) { '.foo.bar.Baz' }
48
+
49
+ specify { expect(subject).to eq " optional ::Foo::Bar::Baz, :foo_bar, 3\n" }
50
+ end
51
+
52
+ context 'when a default value is used' do
53
+ let(:type_enum) { :TYPE_INT32 }
54
+ let(:default_value) { '42' }
55
+ specify { expect(subject).to eq " optional :int32, :foo_bar, 3, :default => 42\n" }
56
+
57
+ context 'when type is an enum' do
58
+ let(:type_enum) { :TYPE_ENUM }
59
+ let(:type_name) { '.foo.bar.Baz' }
60
+ let(:default_value) { 'QUUX' }
61
+
62
+ specify { expect(subject).to eq " optional ::Foo::Bar::Baz, :foo_bar, 3, :default => ::Foo::Bar::Baz::QUUX\n" }
63
+ end
64
+
65
+ context 'when type is an enum with lowercase default value with PB_UPCASE_ENUMS set' do
66
+ let(:type_enum) { :TYPE_ENUM }
67
+ let(:type_name) { '.foo.bar.Baz' }
68
+ let(:default_value) { 'quux' }
69
+ before { allow(ENV).to receive(:key?).with('PB_UPCASE_ENUMS').and_return(true) }
70
+
71
+ specify { expect(subject).to eq " optional ::Foo::Bar::Baz, :foo_bar, 3, :default => ::Foo::Bar::Baz::QUUX\n" }
72
+ end
73
+
74
+ context 'when the type is a string' do
75
+ let(:type_enum) { :TYPE_STRING }
76
+ let(:default_value) { "a default \"string\"" }
77
+
78
+ specify { expect(subject).to eq " optional :string, :foo_bar, 3, :default => \"a default \"string\"\"\n" }
79
+ end
80
+
81
+ context 'when float or double field type' do
82
+ let(:type_enum) { :TYPE_DOUBLE }
83
+
84
+ context 'when the default value is "nan"' do
85
+ let(:default_value) { 'nan' }
86
+ specify { expect(subject).to match(/::Float::NAN/) }
87
+ end
88
+
89
+ context 'when the default value is "inf"' do
90
+ let(:default_value) { 'inf' }
91
+ specify { expect(subject).to match(/::Float::INFINITY/) }
92
+ end
93
+
94
+ context 'when the default value is "-inf"' do
95
+ let(:default_value) { '-inf' }
96
+ specify { expect(subject).to match(/-::Float::INFINITY/) }
97
+ end
98
+ end
99
+ end
100
+
101
+ context 'when the field is an extension' do
102
+ let(:extendee) { 'foo.bar.Baz' }
103
+
104
+ specify { expect(subject).to eq " optional :string, :foo_bar, 3, :extension => true\n" }
105
+ end
106
+
107
+ context 'when field is packed' do
108
+ let(:field_options) { { :packed => true } }
109
+
110
+ specify { expect(subject).to eq " optional :string, :foo_bar, 3, :packed => true\n" }
111
+ end
112
+
113
+ context 'when field is a map' do
114
+ let(:type_enum) { :TYPE_MESSAGE }
115
+ let(:type_name) { '.foo.bar.Baz.FooBarEntry' }
116
+ let(:label_enum) { :LABEL_REPEATED }
117
+ let(:nested_types) do
118
+ [::Google::Protobuf::DescriptorProto.new(
119
+ :name => 'FooBarEntry',
120
+ :field => [
121
+ ::Google::Protobuf::FieldDescriptorProto.new(
122
+ :label => :LABEL_OPTIONAL,
123
+ :name => 'key',
124
+ :number => 1,
125
+ :type => :TYPE_STRING,
126
+ :type_name => nil),
127
+ ::Google::Protobuf::FieldDescriptorProto.new(
128
+ :label => :LABEL_OPTIONAL,
129
+ :name => 'value',
130
+ :number => 2,
131
+ :type => :TYPE_ENUM,
132
+ :type_name => '.foo.bar.SnafuState'),
133
+ ],
134
+ :options => ::Google::Protobuf::MessageOptions.new(:map_entry => true)),
135
+ ]
136
+ end
137
+
138
+ specify { expect(subject).to eq " map :string, ::Foo::Bar::SnafuState, :foo_bar, 3\n" }
139
+ end
140
+
141
+ context 'when field is deprecated' do
142
+ let(:field_options) { { :deprecated => true } }
143
+
144
+ specify { expect(subject).to eq " optional :string, :foo_bar, 3, :deprecated => true\n" }
145
+ end
146
+
147
+ context 'when field uses a custom option that is an extension' do
148
+ class ::CustomFieldEnum < ::Protobuf::Enum
149
+ define :BOOM, 1
150
+ define :BAM, 2
151
+ end
152
+
153
+ class ::CustomFieldMessage < ::Protobuf::Message
154
+ optional :string, :foo, 1
155
+ end
156
+
157
+ class ::Google::Protobuf::FieldOptions < ::Protobuf::Message
158
+ optional :string, :custom_string_option, 22000, :extension => true
159
+ optional :bool, :custom_bool_option, 22001, :extension => true
160
+ optional :int32, :custom_int32_option, 22002, :extension => true
161
+ optional ::CustomFieldEnum, :custom_enum_option, 22003, :extension => true
162
+ optional ::CustomFieldMessage, :custom_message_option, 22004, :extension => true
163
+ end
164
+
165
+ describe 'option has a string value' do
166
+ let(:field_options) { { :custom_string_option => 'boom' } }
167
+
168
+ specify { expect(subject).to eq " optional :string, :foo_bar, 3, :custom_string_option => \"boom\"\n" }
169
+ end
170
+
171
+ describe 'option has a bool value' do
172
+ let(:field_options) { { :custom_bool_option => true } }
173
+
174
+ specify { expect(subject).to eq " optional :string, :foo_bar, 3, :custom_bool_option => true\n" }
175
+ end
176
+
177
+ describe 'option has a int32 value' do
178
+ let(:field_options) { { :custom_int32_option => 123 } }
179
+
180
+ specify { expect(subject).to eq " optional :string, :foo_bar, 3, :custom_int32_option => 123\n" }
181
+ end
182
+
183
+ describe 'option has a message value' do
184
+ let(:field_options) { { :custom_message_option => CustomFieldMessage.new(:foo => 'boom') } }
185
+
186
+ specify { expect(subject).to eq " optional :string, :foo_bar, 3, :custom_message_option => { :foo => \"boom\" }\n" }
187
+ end
188
+
189
+ describe 'option has a enum value' do
190
+ let(:field_options) { { :custom_enum_option => CustomFieldEnum::BAM } }
191
+
192
+ specify { expect(subject).to eq " optional :string, :foo_bar, 3, :custom_enum_option => ::CustomFieldEnum::BAM\n" }
193
+ end
194
+ end
195
+ end
196
+
197
+ end