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,87 @@
1
+ require 'spec_helper'
2
+
3
+ require 'protobuf/code_generator'
4
+
5
+ RSpec.describe ::Protobuf::CodeGenerator do
6
+
7
+ # Some constants to shorten things up
8
+ DESCRIPTOR = ::Google::Protobuf
9
+ COMPILER = ::Google::Protobuf::Compiler
10
+
11
+ describe '#response_bytes' do
12
+ let(:input_file1) { DESCRIPTOR::FileDescriptorProto.new(:name => 'test/foo.proto') }
13
+ let(:input_file2) { DESCRIPTOR::FileDescriptorProto.new(:name => 'test/bar.proto') }
14
+
15
+ let(:output_file1) { COMPILER::CodeGeneratorResponse::File.new(:name => 'test/foo.pb.rb') }
16
+ let(:output_file2) { COMPILER::CodeGeneratorResponse::File.new(:name => 'test/bar.pb.rb') }
17
+
18
+ let(:file_generator1) { double('file generator 1', :generate_output_file => output_file1) }
19
+ let(:file_generator2) { double('file generator 2', :generate_output_file => output_file2) }
20
+
21
+ let(:request_bytes) do
22
+ COMPILER::CodeGeneratorRequest.encode(:proto_file => [input_file1, input_file2])
23
+ end
24
+
25
+ let(:expected_response_bytes) do
26
+ COMPILER::CodeGeneratorResponse.encode(:file => [output_file1, output_file2])
27
+ end
28
+
29
+ before do
30
+ expect(::Protobuf::Generators::FileGenerator).to receive(:new).with(input_file1).and_return(file_generator1)
31
+ expect(::Protobuf::Generators::FileGenerator).to receive(:new).with(input_file2).and_return(file_generator2)
32
+ end
33
+
34
+ it 'returns the serialized CodeGeneratorResponse which contains the generated file contents' do
35
+ generator = described_class.new(request_bytes)
36
+ expect(generator.response_bytes).to eq expected_response_bytes
37
+ end
38
+ end
39
+
40
+ describe '#eval_unknown_extensions' do
41
+ let(:input_file) do
42
+ DESCRIPTOR::FileDescriptorProto.new(
43
+ :name => 'test/boom.proto',
44
+ :package => 'test.pkg.code_generator_spec',
45
+ :extension => [{
46
+ :name => 'boom',
47
+ :number => 20100,
48
+ :label => Google::Protobuf::FieldDescriptorProto::Label::LABEL_OPTIONAL,
49
+ :type => Google::Protobuf::FieldDescriptorProto::Type::TYPE_STRING,
50
+ :extendee => '.google.protobuf.FieldOptions',
51
+ }],
52
+ )
53
+ end
54
+ let(:request_bytes) { COMPILER::CodeGeneratorRequest.encode(:proto_file => [input_file]) }
55
+
56
+ it 'evals files as they are generated' do
57
+ described_class.new(request_bytes).eval_unknown_extensions!
58
+ expect(Google::Protobuf::FieldOptions.extension_fields.map(&:fully_qualified_name)).to include(:'.test.pkg.code_generator_spec.boom')
59
+ expect(Google::Protobuf::FieldOptions.extension_fields.map(&:name)).to include(:boom)
60
+ added_extension = Google::Protobuf::FieldOptions.extension_fields.detect { |f| f.fully_qualified_name == :'.test.pkg.code_generator_spec.boom' }
61
+ expect(added_extension.name).to eq(:boom)
62
+ expect(added_extension.rule).to eq(:optional)
63
+ expect(added_extension.type_class).to eq(::Protobuf::Field::StringField)
64
+ expect(added_extension.tag).to eq(20100)
65
+ end
66
+ end
67
+
68
+ context 'class-level printing methods' do
69
+ describe '.fatal' do
70
+ it 'raises a CodeGeneratorFatalError error' do
71
+ expect do
72
+ described_class.fatal("something is wrong")
73
+ end.to raise_error(
74
+ ::Protobuf::CodeGenerator::CodeGeneratorFatalError,
75
+ "something is wrong",
76
+ )
77
+ end
78
+ end
79
+
80
+ describe '.warn' do
81
+ it 'prints a warning to stderr' do
82
+ expect(STDERR).to receive(:puts).with("[WARN] a warning")
83
+ described_class.warn("a warning")
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,307 @@
1
+ require 'spec_helper'
2
+ require PROTOS_PATH.join('enum.pb')
3
+
4
+ RSpec.describe Protobuf::Enum do
5
+
6
+ describe 'class dsl' do
7
+ let(:name) { :THREE }
8
+ let(:tag) { 3 }
9
+
10
+ before(:all) do
11
+ Test::EnumTestType.define(:MINUS_ONE, -1)
12
+ Test::EnumTestType.define(:THREE, 3)
13
+ end
14
+
15
+ before(:all) do
16
+ EnumAliasTest = ::Class.new(::Protobuf::Enum) do
17
+ set_option :allow_alias
18
+ define :FOO, 1
19
+ define :BAR, 1
20
+ define :BAZ, 2
21
+ end
22
+ end
23
+
24
+ describe '.==' do
25
+ it 'is true for identical values' do
26
+ expect(Test::EnumTestType::THREE).to eq(Test::EnumTestType::THREE)
27
+ end
28
+
29
+ it 'is false for different values in the same enum' do
30
+ expect(Test::EnumTestType::TWO).to_not eq(Test::EnumTestType::THREE)
31
+ end
32
+
33
+ it 'is false for values from different enums' do
34
+ expect(Test::EnumTestType::THREE).to_not eq(Test::AliasedEnum::THREE)
35
+ end
36
+ end
37
+
38
+ describe '.aliases_allowed?' do
39
+ it 'is false when the option is not set' do
40
+ expect(Test::EnumTestType.aliases_allowed?).to be false
41
+ end
42
+ end
43
+
44
+ describe '.define' do
45
+ it 'defines a constant enum on the parent class' do
46
+ expect(Test::EnumTestType.constants).to include(name)
47
+ expect(Test::EnumTestType::THREE).to be_a(Protobuf::Enum)
48
+ end
49
+
50
+ context 'when enum allows aliases' do
51
+ before(:all) do
52
+ DefineEnumAlias = ::Class.new(::Protobuf::Enum) do
53
+ set_option :allow_alias
54
+ end
55
+ end
56
+
57
+ it 'allows defining enums with the same tag number' do
58
+ expect do
59
+ DefineEnumAlias.define(:FOO, 1)
60
+ DefineEnumAlias.define(:BAR, 1)
61
+ end.not_to raise_error
62
+ end
63
+ end
64
+ end
65
+
66
+ describe '.enums' do
67
+ it 'provides an array of defined Enums' do
68
+ expect(Test::EnumTestType.enums).to eq(
69
+ [
70
+ Test::EnumTestType::ZERO,
71
+ Test::EnumTestType::ONE,
72
+ Test::EnumTestType::TWO,
73
+ Test::EnumTestType::MINUS_ONE,
74
+ Test::EnumTestType::THREE,
75
+ ],
76
+ )
77
+ end
78
+
79
+ context 'when enum allows aliases' do
80
+ it 'treats aliased enums as valid' do
81
+ expect(EnumAliasTest.enums).to eq(
82
+ [
83
+ EnumAliasTest::FOO,
84
+ EnumAliasTest::BAR,
85
+ EnumAliasTest::BAZ,
86
+ ],
87
+ )
88
+ end
89
+ end
90
+ end
91
+
92
+ describe '.enums_for_tag' do
93
+ it 'returns an array of Enums for the given tag, if any' do
94
+ expect(EnumAliasTest.enums_for_tag(nil)).to eq([])
95
+ expect(EnumAliasTest.enums_for_tag(1)).to eq([EnumAliasTest::FOO, EnumAliasTest::BAR])
96
+ expect(EnumAliasTest.enums_for_tag(2)).to eq([EnumAliasTest::BAZ])
97
+ expect(EnumAliasTest.enums_for_tag(3)).to eq([])
98
+ end
99
+ end
100
+
101
+ describe '.fetch' do
102
+ context 'when candidate is an Enum' do
103
+ it 'responds with the Enum' do
104
+ expect(Test::EnumTestType.fetch(Test::EnumTestType::THREE)).to eq(Test::EnumTestType::THREE)
105
+ end
106
+ end
107
+
108
+ context 'when candidate can be coerced to a symbol' do
109
+ it 'fetches based on the symbol name' do
110
+ expect(Test::EnumTestType.fetch("ONE")).to eq(Test::EnumTestType::ONE)
111
+ expect(Test::EnumTestType.fetch(:ONE)).to eq(Test::EnumTestType::ONE)
112
+ end
113
+ end
114
+
115
+ context 'when candidate can be coerced to an integer' do
116
+ it 'fetches based on the integer tag' do
117
+ expect(Test::EnumTestType.fetch(3.0)).to eq(Test::EnumTestType::THREE)
118
+ expect(Test::EnumTestType.fetch(3)).to eq(Test::EnumTestType::THREE)
119
+ end
120
+
121
+ context 'when enum allows aliases' do
122
+ it 'fetches the first defined Enum' do
123
+ expect(EnumAliasTest.fetch(1)).to eq(EnumAliasTest::FOO)
124
+ end
125
+ end
126
+ end
127
+
128
+ context 'when candidate is not an applicable type' do
129
+ it 'returns a nil' do
130
+ expect(Test::EnumTestType.fetch(EnumAliasTest::FOO)).to be_nil
131
+ expect(Test::EnumTestType.fetch(Test::Resource.new)).to be_nil
132
+ expect(Test::EnumTestType.fetch(nil)).to be_nil
133
+ expect(Test::EnumTestType.fetch(false)).to be_nil
134
+ expect(Test::EnumTestType.fetch(-10)).to be_nil
135
+ end
136
+ end
137
+ end
138
+
139
+ describe '.enum_for_tag' do
140
+ it 'gets the Enum corresponding to the given tag' do
141
+ expect(Test::EnumTestType.enum_for_tag(tag)).to eq(Test::EnumTestType.const_get(name))
142
+ expect(Test::EnumTestType.enum_for_tag(-5)).to be_nil
143
+ expect(Test::EnumTestType.enum_for_tag(nil)).to be_nil
144
+ end
145
+ end
146
+
147
+ describe '.name_for_tag' do
148
+ it 'get the name of the enum given the enum' do
149
+ expect(Test::EnumTestType.name_for_tag(::Test::EnumTestType::THREE)).to eq(name)
150
+ end
151
+
152
+ it 'gets the name of the enum corresponding to the given tag' do
153
+ expect(Test::EnumTestType.name_for_tag(tag)).to eq(name)
154
+ end
155
+
156
+ it 'gets the name when the tag is coercable to an int' do
157
+ expect(Test::EnumTestType.name_for_tag("3")).to eq(name)
158
+ end
159
+
160
+ it 'returns nil when tag does not correspond to a name' do
161
+ expect(Test::EnumTestType.name_for_tag(12345)).to be_nil
162
+ end
163
+
164
+ context 'when given name is nil' do
165
+ it 'returns a nil' do
166
+ expect(Test::EnumTestType.name_for_tag(nil)).to be_nil
167
+ end
168
+ end
169
+
170
+ context 'when enum allows aliases' do
171
+ it 'returns the first defined name for the given tag' do
172
+ expect(EnumAliasTest.name_for_tag(1)).to eq(:FOO)
173
+ end
174
+ end
175
+ end
176
+
177
+ describe '.to_json' do
178
+ it 'renders the enum value' do
179
+ expect(Test::EnumTestType::ONE.to_json).to eq("1")
180
+ expect({ :value => Test::EnumTestType::ONE }.to_json).to eq(%({"value":1}))
181
+ # JSON.dump passes arguments to the to_json method which broke in the 3.8.3 release.
182
+ expect(JSON.dump(:value => Test::EnumTestType::ONE)).to eq(%({"value":1}))
183
+ end
184
+ end
185
+
186
+ describe '.valid_tag?' do
187
+ context 'when tag is defined' do
188
+ specify { expect(Test::EnumTestType.valid_tag?(tag)).to be true }
189
+ end
190
+
191
+ context 'when tag is not defined' do
192
+ specify { expect(Test::EnumTestType.valid_tag?(300)).to be false }
193
+ end
194
+
195
+ context 'is true for aliased enums' do
196
+ specify { expect(EnumAliasTest.valid_tag?(1)).to be true }
197
+ end
198
+ end
199
+
200
+ describe '.enum_for_name' do
201
+ it 'gets the Enum corresponding to the given name' do
202
+ expect(Test::EnumTestType.enum_for_name(name)).to eq(Test::EnumTestType::THREE)
203
+ end
204
+ end
205
+
206
+ describe '.values' do
207
+ around do |example|
208
+ # this method is deprecated
209
+ ::Protobuf.deprecator.silence(&example)
210
+ end
211
+
212
+ it 'provides a hash of defined Enums' do
213
+ expect(Test::EnumTestType.values).to eq(
214
+ :MINUS_ONE => Test::EnumTestType::MINUS_ONE,
215
+ :ZERO => Test::EnumTestType::ZERO,
216
+ :ONE => Test::EnumTestType::ONE,
217
+ :TWO => Test::EnumTestType::TWO,
218
+ :THREE => Test::EnumTestType::THREE,
219
+ )
220
+ end
221
+
222
+ it 'contains aliased Enums' do
223
+ expect(EnumAliasTest.values).to eq(
224
+ :FOO => EnumAliasTest::FOO,
225
+ :BAR => EnumAliasTest::BAR,
226
+ :BAZ => EnumAliasTest::BAZ,
227
+ )
228
+ end
229
+ end
230
+
231
+ describe '.all_tags' do
232
+ it 'provides a unique array of defined tags' do
233
+ expect(Test::EnumTestType.all_tags).to include(1, 2, -1, 3)
234
+ expect(EnumAliasTest.all_tags).to include(1, 2)
235
+ end
236
+ end
237
+ end
238
+
239
+ subject { Test::EnumTestType::ONE }
240
+ specify { expect(subject.class).to eq(1.class) }
241
+ specify { expect(subject.parent_class).to eq(Test::EnumTestType) }
242
+ specify { expect(subject.name).to eq(:ONE) }
243
+ specify { expect(subject.tag).to eq(1) }
244
+
245
+ context 'deprecated' do
246
+ around do |example|
247
+ # this method is deprecated
248
+ ::Protobuf.deprecator.silence(&example)
249
+ end
250
+
251
+ specify { expect(subject.value).to eq(1) }
252
+ end
253
+
254
+ specify { expect(subject.to_hash_value).to eq(1) }
255
+ specify { expect(subject.to_s).to eq("1") }
256
+ specify { expect(subject.inspect).to eq('#<Protobuf::Enum(Test::EnumTestType)::ONE=1>') }
257
+ specify { expect(subject.to_s(:tag)).to eq("1") }
258
+ specify { expect(subject.to_s(:name)).to eq("ONE") }
259
+
260
+ it "can be used as the index to an array" do
261
+ array = [0, 1, 2, 3]
262
+ expect(array[::Test::EnumTestType::ONE]).to eq(1)
263
+ end
264
+
265
+ describe '#try' do
266
+ specify { expect(subject.try(:parent_class)).to eq(subject.parent_class) }
267
+ specify { expect(subject.try(:class)).to eq(subject.class) }
268
+ specify { expect(subject.try(:name)).to eq(subject.name) }
269
+ specify { expect(subject.try(:tag)).to eq(subject.tag) }
270
+
271
+ context 'deprecated' do
272
+ around do |example|
273
+ # this method is deprecated
274
+ ::Protobuf.deprecator.silence(&example)
275
+ end
276
+
277
+ specify { expect(subject.try(:value)).to eq(subject.value) }
278
+ end
279
+
280
+ specify { expect(subject.try(:to_i)).to eq(subject.to_i) }
281
+ specify { expect(subject.try(:to_int)).to eq(subject.to_int) }
282
+ specify { subject.try { |yielded| expect(yielded).to eq(subject) } }
283
+ end
284
+
285
+ describe '#eql?' do
286
+ it "is equal to itself" do
287
+ expect(::Test::EnumTestType::ZERO.eql?(::Test::EnumTestType::ZERO)).to be(true)
288
+ end
289
+
290
+ it "is equal to it's tag" do
291
+ expect(::Test::EnumTestType::ZERO.eql?(::Test::EnumTestType::ZERO.tag)).to be(true)
292
+ end
293
+
294
+ it "is not equal to it's name" do
295
+ expect(::Test::EnumTestType::ZERO.eql?(::Test::EnumTestType::ZERO.name)).to be(false)
296
+ end
297
+ end
298
+
299
+ context 'when coercing from enum' do
300
+ subject { Test::StatusType::PENDING }
301
+ it { is_expected.to eq(0) }
302
+ end
303
+
304
+ context 'when coercing from integer' do
305
+ specify { expect(0).to eq(Test::StatusType::PENDING) }
306
+ end
307
+ end
@@ -0,0 +1,91 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Protobuf::Field::BoolField do
4
+
5
+ it_behaves_like :packable_field, described_class do
6
+ let(:value) { [true, false] }
7
+ end
8
+
9
+ class SomeBoolMessage < ::Protobuf::Message
10
+ optional :bool, :some_bool, 1
11
+ required :bool, :required_bool, 2
12
+ end
13
+
14
+ let(:instance) { SomeBoolMessage.new }
15
+
16
+ describe 'setting and getting field' do
17
+ subject { instance.some_bool = value; instance.some_bool }
18
+
19
+ [true, false].each do |val|
20
+ context "when set with #{val}" do
21
+ let(:value) { val }
22
+
23
+ it 'is readable as a bool' do
24
+ expect(subject).to eq(val)
25
+ end
26
+ end
27
+ end
28
+
29
+ [['true', true], ['false', false]].each do |val, expected|
30
+ context "when set with a string of #{val}" do
31
+ let(:value) { val }
32
+
33
+ it 'is readable as a bool' do
34
+ expect(subject).to eq(expected)
35
+ end
36
+ end
37
+ end
38
+
39
+ context 'when set with a non-bool string' do
40
+ let(:value) { "aaaa" }
41
+
42
+ it 'throws an error' do
43
+ expect { subject }.to raise_error(TypeError)
44
+ end
45
+ end
46
+
47
+ context 'when set with something that is not a bool' do
48
+ let(:value) { [1, 2, 3] }
49
+
50
+ it 'throws an error' do
51
+ expect { subject }.to raise_error(TypeError)
52
+ end
53
+ end
54
+ end
55
+
56
+ it 'defines ? method' do
57
+ instance.required_bool = false
58
+ expect(instance.required_bool?).to be(false)
59
+ end
60
+
61
+ describe '#default_value' do
62
+ context 'optional and required fields' do
63
+ it 'returns the class default' do
64
+ expect(SomeBoolMessage.get_field('some_bool').default).to be nil
65
+ expect(::Protobuf::Field::BoolField.default).to be false
66
+ expect(instance.some_bool).to be false
67
+ end
68
+
69
+ context 'with field default' do
70
+ class AnotherBoolMessage < ::Protobuf::Message
71
+ optional :bool, :set_bool, 1, :default => true
72
+ end
73
+
74
+ it 'returns the set default' do
75
+ expect(AnotherBoolMessage.get_field('set_bool').default).to be true
76
+ expect(AnotherBoolMessage.new.set_bool).to be true
77
+ end
78
+ end
79
+ end
80
+
81
+ context 'repeated field' do
82
+ class RepeatedBoolMessage < ::Protobuf::Message
83
+ repeated :bool, :repeated_bool, 1
84
+ end
85
+
86
+ it 'returns the set default' do
87
+ expect(RepeatedBoolMessage.new.repeated_bool).to eq []
88
+ end
89
+ end
90
+ end
91
+ end