protobuf-core 3.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (110) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +10 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +18 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE.txt +21 -0
  7. data/README.md +23 -0
  8. data/Rakefile +5 -0
  9. data/bin/protoc-gen-ruby +16 -0
  10. data/lib/protobuf.rb +27 -0
  11. data/lib/protobuf/code_generator.rb +44 -0
  12. data/lib/protobuf/core.rb +2 -0
  13. data/lib/protobuf/core/version.rb +5 -0
  14. data/lib/protobuf/decoder.rb +73 -0
  15. data/lib/protobuf/deprecation.rb +112 -0
  16. data/lib/protobuf/descriptors.rb +3 -0
  17. data/lib/protobuf/descriptors/google/protobuf/compiler/plugin.pb.rb +54 -0
  18. data/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb +251 -0
  19. data/lib/protobuf/encoder.rb +67 -0
  20. data/lib/protobuf/enum.rb +303 -0
  21. data/lib/protobuf/exceptions.rb +9 -0
  22. data/lib/protobuf/field.rb +74 -0
  23. data/lib/protobuf/field/base_field.rb +267 -0
  24. data/lib/protobuf/field/bool_field.rb +59 -0
  25. data/lib/protobuf/field/bytes_field.rb +82 -0
  26. data/lib/protobuf/field/double_field.rb +25 -0
  27. data/lib/protobuf/field/enum_field.rb +68 -0
  28. data/lib/protobuf/field/field_array.rb +87 -0
  29. data/lib/protobuf/field/fixed32_field.rb +25 -0
  30. data/lib/protobuf/field/fixed64_field.rb +28 -0
  31. data/lib/protobuf/field/float_field.rb +41 -0
  32. data/lib/protobuf/field/int32_field.rb +21 -0
  33. data/lib/protobuf/field/int64_field.rb +21 -0
  34. data/lib/protobuf/field/integer_field.rb +23 -0
  35. data/lib/protobuf/field/message_field.rb +65 -0
  36. data/lib/protobuf/field/sfixed32_field.rb +27 -0
  37. data/lib/protobuf/field/sfixed64_field.rb +28 -0
  38. data/lib/protobuf/field/signed_integer_field.rb +29 -0
  39. data/lib/protobuf/field/sint32_field.rb +21 -0
  40. data/lib/protobuf/field/sint64_field.rb +21 -0
  41. data/lib/protobuf/field/string_field.rb +34 -0
  42. data/lib/protobuf/field/uint32_field.rb +21 -0
  43. data/lib/protobuf/field/uint64_field.rb +21 -0
  44. data/lib/protobuf/field/varint_field.rb +73 -0
  45. data/lib/protobuf/generators/base.rb +70 -0
  46. data/lib/protobuf/generators/enum_generator.rb +41 -0
  47. data/lib/protobuf/generators/extension_generator.rb +27 -0
  48. data/lib/protobuf/generators/field_generator.rb +131 -0
  49. data/lib/protobuf/generators/file_generator.rb +132 -0
  50. data/lib/protobuf/generators/group_generator.rb +105 -0
  51. data/lib/protobuf/generators/message_generator.rb +98 -0
  52. data/lib/protobuf/generators/printable.rb +160 -0
  53. data/lib/protobuf/logging.rb +39 -0
  54. data/lib/protobuf/message.rb +193 -0
  55. data/lib/protobuf/message/fields.rb +133 -0
  56. data/lib/protobuf/message/serialization.rb +89 -0
  57. data/lib/protobuf/optionable.rb +23 -0
  58. data/lib/protobuf/wire_type.rb +10 -0
  59. data/proto/dynamic_discovery.proto +44 -0
  60. data/proto/google/protobuf/compiler/plugin.proto +147 -0
  61. data/proto/google/protobuf/descriptor.proto +620 -0
  62. data/proto/rpc.proto +62 -0
  63. data/protobuf-core.gemspec +31 -0
  64. data/spec/bin/protoc-gen-ruby_spec.rb +23 -0
  65. data/spec/data/data.bin +3 -0
  66. data/spec/data/types.bin +0 -0
  67. data/spec/encoding/all_types_spec.rb +105 -0
  68. data/spec/encoding/extreme_values_spec.rb +0 -0
  69. data/spec/functional/class_inheritance_spec.rb +52 -0
  70. data/spec/functional/compile_and_require_spec.rb +29 -0
  71. data/spec/lib/protobuf/base_spec.rb +84 -0
  72. data/spec/lib/protobuf/code_generator_spec.rb +60 -0
  73. data/spec/lib/protobuf/enum_generator_spec.rb +73 -0
  74. data/spec/lib/protobuf/enum_spec.rb +265 -0
  75. data/spec/lib/protobuf/extension_generator_spec.rb +42 -0
  76. data/spec/lib/protobuf/field/bool_field_spec.rb +51 -0
  77. data/spec/lib/protobuf/field/field_array_spec.rb +69 -0
  78. data/spec/lib/protobuf/field/float_field_spec.rb +55 -0
  79. data/spec/lib/protobuf/field/int32_field_spec.rb +90 -0
  80. data/spec/lib/protobuf/field/string_field_spec.rb +45 -0
  81. data/spec/lib/protobuf/field_generator_spec.rb +102 -0
  82. data/spec/lib/protobuf/field_spec.rb +191 -0
  83. data/spec/lib/protobuf/file_generator_spec.rb +32 -0
  84. data/spec/lib/protobuf/message_generator_spec.rb +0 -0
  85. data/spec/lib/protobuf/message_spec.rb +526 -0
  86. data/spec/lib/protobuf/optionable_spec.rb +46 -0
  87. data/spec/lib/protobuf_spec.rb +45 -0
  88. data/spec/spec_helper.rb +9 -0
  89. data/spec/support/packed_field.rb +22 -0
  90. data/spec/support/test/all_types.data.bin +0 -0
  91. data/spec/support/test/all_types.data.txt +119 -0
  92. data/spec/support/test/bacon.proto +14 -0
  93. data/spec/support/test/defaults.pb.rb +27 -0
  94. data/spec/support/test/defaults.proto +9 -0
  95. data/spec/support/test/enum.pb.rb +61 -0
  96. data/spec/support/test/enum.proto +34 -0
  97. data/spec/support/test/extended.pb.rb +24 -0
  98. data/spec/support/test/extended.proto +10 -0
  99. data/spec/support/test/extreme_values.data.bin +0 -0
  100. data/spec/support/test/google_unittest.pb.rb +530 -0
  101. data/spec/support/test/google_unittest.proto +713 -0
  102. data/spec/support/test/google_unittest_import.pb.rb +39 -0
  103. data/spec/support/test/google_unittest_import.proto +64 -0
  104. data/spec/support/test/google_unittest_import_public.pb.rb +10 -0
  105. data/spec/support/test/google_unittest_import_public.proto +38 -0
  106. data/spec/support/test/multi_field_extensions.pb.rb +58 -0
  107. data/spec/support/test/multi_field_extensions.proto +33 -0
  108. data/spec/support/test/resource.pb.rb +106 -0
  109. data/spec/support/test/resource.proto +94 -0
  110. metadata +306 -0
@@ -0,0 +1,62 @@
1
+ // Copyright (c) 2009 Shardul Deo
2
+ //
3
+ // Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ // of this software and associated documentation files (the "Software"), to deal
5
+ // in the Software without restriction, including without limitation the rights
6
+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ // copies of the Software, and to permit persons to whom the Software is
8
+ // furnished to do so, subject to the following conditions:
9
+ //
10
+ // The above copyright notice and this permission notice shall be included in
11
+ // all copies or substantial portions of the Software.
12
+ //
13
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ // THE SOFTWARE.
20
+
21
+ // Authors: Shardul Deo, BJ Neilsen
22
+ //
23
+ // Protobufs needed for socket rpcs.
24
+
25
+ package protobuf.socketrpc;
26
+
27
+ message Request
28
+ {
29
+ required string service_name = 1; // Fully- qualified Service class name
30
+ required string method_name = 2; // Service method to invoke
31
+ optional bytes request_proto = 3; // Serialized request bytes
32
+ optional string caller = 4; // Calling hostname or address
33
+ }
34
+
35
+ message Response
36
+ {
37
+ optional bytes response_proto = 1; // Serialized response
38
+ optional string error = 2; // Error message, if any
39
+ optional bool callback = 3 [default = false]; // Was callback invoked (not sure what this is for)
40
+ optional ErrorReason error_reason = 4; // Error Reason
41
+ }
42
+
43
+ // Possible error reasons
44
+ // The server-side errors are returned in the response from the server.
45
+ // The client-side errors are returned by the client-side code when it doesn't
46
+ // have a response from the server.
47
+ enum ErrorReason
48
+ {
49
+ // Server-side errors
50
+ BAD_REQUEST_DATA = 0; // Server received bad request data
51
+ BAD_REQUEST_PROTO = 1; // Server received bad request proto
52
+ SERVICE_NOT_FOUND = 2; // Service not found on server
53
+ METHOD_NOT_FOUND = 3; // Method not found on server
54
+ RPC_ERROR = 4; // Rpc threw exception on server
55
+ RPC_FAILED = 5; // Rpc failed on server
56
+
57
+ // Client-side errors (these are returned by the client-side code)
58
+ INVALID_REQUEST_PROTO = 6; // Rpc was called with invalid request proto
59
+ BAD_RESPONSE_PROTO = 7; // Server returned a bad response proto
60
+ UNKNOWN_HOST = 8; // Could not find supplied host
61
+ IO_ERROR = 9; // I/O error while communicating with server
62
+ }
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'protobuf/core/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "protobuf-core"
8
+ spec.version = Protobuf::Core::VERSION
9
+ spec.authors = ["BJ Neilsen", "Brandon Dewitt", "Devin Christensen", "Adam Hutchison", "Michael Ries"]
10
+ spec.email = ["bj.neilsen+protobuf@gmail.com", "brandonsdewitt+protobuf@gmail.com", "quixoten@gmail.com", "liveh2o@gmail.com", "michael@riesd.com"]
11
+
12
+ spec.summary = "Google Protocol Buffers serialization and compiler implementation for Ruby."
13
+ spec.description = spec.summary
14
+ spec.homepage = "https://github.com/ruby-protobuf/protobuf-core"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ spec.bindir = "bin"
20
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_runtime_dependency "activesupport", ">= 3.2"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.9"
26
+ spec.add_development_dependency 'pry'
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", ">= 3.2"
29
+ spec.add_development_dependency 'ruby-prof' if RUBY_ENGINE.to_sym == :ruby
30
+ spec.add_development_dependency "timecop"
31
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ require 'protobuf/code_generator'
4
+
5
+ RSpec.describe 'protoc-gen-ruby' do
6
+ let(:binpath) { ::File.expand_path('../../../bin/protoc-gen-ruby', __FILE__) }
7
+ let(:package) { 'test' }
8
+ let(:request_bytes) do
9
+ ::Google::Protobuf::Compiler::CodeGeneratorRequest.encode(
10
+ :proto_file => [{ :package => package }],
11
+ )
12
+ end
13
+
14
+ it 'reads the serialized request bytes and outputs serialized response bytes' do
15
+ ::IO.popen(binpath, 'w+') do |pipe|
16
+ pipe.write(request_bytes)
17
+ pipe.close_write # needed so we can implicitly read until EOF
18
+ response_bytes = pipe.read
19
+ response = ::Google::Protobuf::Compiler::CodeGeneratorResponse.decode(response_bytes)
20
+ expect(response.file.first.content).to include("module #{package.titleize}")
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+
2
+ John Doe� jdoe@example.com"
3
+ 555-4321
Binary file
@@ -0,0 +1,105 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe ::Protobuf do
4
+ it "correctly encodes all types" do
5
+ message = GoogleUnittest::TestAllTypes.new(
6
+ :optional_int32 => 101,
7
+ :optional_int64 => 102,
8
+ :optional_uint32 => 103,
9
+ :optional_uint64 => 104,
10
+ :optional_sint32 => 105,
11
+ :optional_sint64 => 106,
12
+ :optional_fixed32 => 107,
13
+ :optional_fixed64 => 108,
14
+ :optional_sfixed32 => 109,
15
+ :optional_sfixed64 => 110,
16
+ :optional_float => 111,
17
+ :optional_double => 112,
18
+ :optional_bool => true,
19
+ :optional_string => "115",
20
+ :optional_bytes => "116",
21
+ :optional_nested_message => GoogleUnittest::TestAllTypes::NestedMessage.new(:bb => 118),
22
+ :optional_foreign_message => GoogleUnittest::ForeignMessage.new(:c => 119),
23
+ :optional_import_message => GoogleUnittestImport::ImportMessage.new(:d => 120),
24
+ :optional_nested_enum => GoogleUnittest::TestAllTypes::NestedEnum::BAZ,
25
+ :optional_foreign_enum => GoogleUnittest::ForeignEnum::FOREIGN_BAZ,
26
+ :optional_import_enum => GoogleUnittestImport::ImportEnum::IMPORT_BAZ,
27
+ :optional_string_piece => "124",
28
+ :optional_cord => "125",
29
+ :optional_public_import_message => GoogleUnittestImport::PublicImportMessage.new(:e => 126),
30
+ :optional_lazy_message => GoogleUnittest::TestAllTypes::NestedMessage.new(:bb => 127),
31
+ :repeated_int32 => [201, 301],
32
+ :repeated_int64 => [202, 302],
33
+ :repeated_uint32 => [203, 303],
34
+ :repeated_uint64 => [204, 304],
35
+ :repeated_sint32 => [205, 305],
36
+ :repeated_sint64 => [206, 306],
37
+ :repeated_fixed32 => [207, 307],
38
+ :repeated_fixed64 => [208, 308],
39
+ :repeated_sfixed32 => [209, 309],
40
+ :repeated_sfixed64 => [210, 310],
41
+ :repeated_float => [211, 311],
42
+ :repeated_double => [212, 312],
43
+ :repeated_bool => [true, false],
44
+ :repeated_string => ["215", "315"],
45
+ :repeated_bytes => ["216", "316"],
46
+ :repeated_nested_message => [
47
+ ::GoogleUnittest::TestAllTypes::NestedMessage.new(:bb => 218),
48
+ ::GoogleUnittest::TestAllTypes::NestedMessage.new(:bb => 318),
49
+ ],
50
+ :repeated_foreign_message => [
51
+ ::GoogleUnittest::ForeignMessage.new(:c => 219),
52
+ ::GoogleUnittest::ForeignMessage.new(:c => 319),
53
+ ],
54
+ :repeated_import_message => [
55
+ ::GoogleUnittestImport::ImportMessage.new(:d => 220),
56
+ ::GoogleUnittestImport::ImportMessage.new(:d => 320),
57
+ ],
58
+ :repeated_nested_enum => [
59
+ ::GoogleUnittest::TestAllTypes::NestedEnum::BAR,
60
+ ::GoogleUnittest::TestAllTypes::NestedEnum::BAZ,
61
+ ],
62
+ :repeated_foreign_enum => [
63
+ ::GoogleUnittest::ForeignEnum::FOREIGN_BAR,
64
+ ::GoogleUnittest::ForeignEnum::FOREIGN_BAZ,
65
+ ],
66
+ :repeated_import_enum => [
67
+ ::GoogleUnittestImport::ImportEnum::IMPORT_BAR,
68
+ ::GoogleUnittestImport::ImportEnum::IMPORT_BAZ,
69
+ ],
70
+ :repeated_string_piece => ["224", "324"],
71
+ :repeated_cord => ["225", "325"],
72
+ :repeated_lazy_message => [
73
+ ::GoogleUnittest::TestAllTypes::NestedMessage.new(:bb => 227),
74
+ ::GoogleUnittest::TestAllTypes::NestedMessage.new(:bb => 327),
75
+ ],
76
+ :default_int32 => 401,
77
+ :default_int64 => 402,
78
+ :default_uint32 => 403,
79
+ :default_uint64 => 404,
80
+ :default_sint32 => 405,
81
+ :default_sint64 => 406,
82
+ :default_fixed32 => 407,
83
+ :default_fixed64 => 408,
84
+ :default_sfixed32 => 409,
85
+ :default_sfixed64 => 410,
86
+ :default_float => 411,
87
+ :default_double => 412,
88
+ :default_bool => false,
89
+ :default_string => "415",
90
+ :default_bytes => "416",
91
+ :default_nested_enum => ::GoogleUnittest::TestAllTypes::NestedEnum::FOO,
92
+ :default_foreign_enum => ::GoogleUnittest::ForeignEnum::FOREIGN_FOO,
93
+ :default_import_enum => ::GoogleUnittestImport::ImportEnum::IMPORT_FOO,
94
+ :default_string_piece => "424",
95
+ :default_cord => "425",
96
+ )
97
+
98
+ data_file_path = File.expand_path('../../support/test/all_types.data.bin', __FILE__)
99
+ data = File.open(data_file_path, 'rb') do |file|
100
+ file.read
101
+ end
102
+
103
+ expect(data).to eq(message.serialize_to_string)
104
+ end
105
+ end
@@ -0,0 +1,52 @@
1
+ require 'spec_helper'
2
+ require 'support/test/resource.pb'
3
+
4
+ RSpec.describe 'works through class inheritance' do
5
+ module Corp
6
+ module Protobuf
7
+ class Error < ::Protobuf::Message
8
+ required :string, :foo, 1
9
+ end
10
+ end
11
+ end
12
+ module Corp
13
+ class ErrorHandler < Corp::Protobuf::Error
14
+ end
15
+ end
16
+
17
+ let(:args) { { :foo => 'bar' } }
18
+ let(:parent_class) { Corp::Protobuf::Error }
19
+ let(:inherited_class) { Corp::ErrorHandler }
20
+
21
+ specify '#encode' do
22
+ expected_result = "\n\x03bar"
23
+ expected_result.force_encoding(Encoding::BINARY)
24
+ expect(parent_class.new(args).encode).to eq(expected_result)
25
+ expect(inherited_class.new(args).encode).to eq(expected_result)
26
+ end
27
+
28
+ specify '#to_hash' do
29
+ expect(parent_class.new(args).to_hash).to eq(args)
30
+ expect(inherited_class.new(args).to_hash).to eq(args)
31
+ end
32
+
33
+ specify '#to_json' do
34
+ expect(parent_class.new(args).to_json).to eq(args.to_json)
35
+ expect(inherited_class.new(args).to_json).to eq(args.to_json)
36
+ end
37
+
38
+ specify '.encode' do
39
+ expected_result = "\n\x03bar"
40
+ expected_result.force_encoding(Encoding::BINARY)
41
+ expect(parent_class.encode(args)).to eq(expected_result)
42
+ expect(inherited_class.encode(args)).to eq(expected_result)
43
+ end
44
+
45
+ specify '.decode' do
46
+ raw_value = "\n\x03bar"
47
+ raw_value.force_encoding(Encoding::BINARY)
48
+ expect(parent_class.decode(raw_value).to_hash).to eq(args)
49
+ expect(inherited_class.decode(raw_value).to_hash).to eq(args)
50
+ end
51
+
52
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe 'can compile and load a class' do
4
+ before(:context) do
5
+ proto_path = File.expand_path('../../support/test/bacon.proto', __FILE__)
6
+ ruby_path = proto_path.sub('.proto','.pb.rb')
7
+ proto_load_path = File.dirname(proto_path)
8
+ File.unlink(ruby_path) if File.file?(ruby_path)
9
+ `protoc --plugin=./bin/protoc-gen-ruby --ruby_out=#{proto_load_path} -I #{proto_load_path} #{proto_path}`
10
+ require(ruby_path)
11
+ end
12
+
13
+ after(:context) do
14
+ Object.send(:remove_const, :Bacon) if defined?(Bacon)
15
+ end
16
+
17
+ it 'compiles a Bacon class' do
18
+ expect(defined?(Bacon)).to eq 'constant'
19
+ end
20
+
21
+ it 'has getters and setters' do
22
+ bacon = Bacon.new
23
+ bacon.deliciousness = 10
24
+ bacon.texture = Texture::CRUNCHY
25
+
26
+ expect(bacon.deliciousness).to eq 10
27
+ expect(bacon.texture).to eq Texture::CRUNCHY
28
+ end
29
+ end
@@ -0,0 +1,84 @@
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 { 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
+ expect(::Protobuf::CodeGenerator).to receive(:print_tag_warning_suppress)
78
+ expect(::Protobuf::CodeGenerator).to receive(:warn).with(/FooBar object should have 5 tags \(1\.\.5\), but found 4 tags/)
79
+ described_class.validate_tags("FooBar", [1, 2, 4, 5])
80
+ end
81
+ end
82
+ end
83
+
84
+ end
@@ -0,0 +1,60 @@
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
+ context 'class-level printing methods' do
41
+ describe '.fatal' do
42
+ it 'raises a CodeGeneratorFatalError error' do
43
+ expect do
44
+ described_class.fatal("something is wrong")
45
+ end.to raise_error(
46
+ ::Protobuf::CodeGenerator::CodeGeneratorFatalError,
47
+ "something is wrong",
48
+ )
49
+ end
50
+ end
51
+
52
+ describe '.warn' do
53
+ it 'prints a warning to stderr' do
54
+ expect(STDERR).to receive(:puts).with("[WARN] a warning")
55
+ described_class.warn("a warning")
56
+ end
57
+ end
58
+ end
59
+
60
+ end