|
4 | 4 | require 'protobuf/code_generator'
|
5 | 5 |
|
6 | 6 | RSpec.describe 'code generation' do
|
7 |
| - it "generates code for google's unittest.proto" do |
8 |
| - bytes = IO.read(PROTOS_PATH.join('google_unittest.bin'), :mode => 'rb') |
9 |
| - |
10 |
| - expected_files = |
11 |
| - ["google_unittest_import_public.pb.rb", "google_unittest_import.pb.rb", "google_unittest.pb.rb"] |
12 |
| - |
13 |
| - expected_file_descriptors = expected_files.map do |file_name| |
14 |
| - file_content = File.open(PROTOS_PATH.join(file_name), "r:UTF-8", &:read) |
15 |
| - ::Google::Protobuf::Compiler::CodeGeneratorResponse::File.new( |
16 |
| - :name => "protos/" + file_name, :content => file_content) |
17 |
| - end |
18 |
| - |
19 |
| - expected_output = |
20 |
| - ::Google::Protobuf::Compiler::CodeGeneratorResponse.encode(:file => expected_file_descriptors) |
21 |
| - |
22 |
| - code_generator = ::Protobuf::CodeGenerator.new(bytes) |
23 |
| - code_generator.eval_unknown_extensions! |
24 |
| - expect(code_generator.response_bytes).to eq(expected_output) |
| 7 | + before(:all) do |
| 8 | + require PROTOS_PATH.join('google_unittest.pb') unless defined?(::Protobuf_unittest::ForeignEnum) |
| 9 | + require PROTOS_PATH.join('map-test.pb') unless defined?(::Foo::Bar) |
| 10 | + require PROTOS_PATH.join('google_unittest_custom_options.pb') unless defined?(::Protobuf_unittest::MethodOpt1) |
25 | 11 | end
|
26 | 12 |
|
27 |
| - it "generates code for map types" do |
28 |
| - input_descriptor = ::Google::Protobuf::FileDescriptorSet.decode( |
29 |
| - IO.read(PROTOS_PATH.join('map-test.bin'), :mode => 'rb')) |
30 |
| - request = ::Google::Protobuf::Compiler::CodeGeneratorRequest.new(:file_to_generate => ['map-test.proto'], |
31 |
| - :proto_file => input_descriptor.file) |
32 |
| - |
33 |
| - file_name = "map-test.pb.rb" |
34 |
| - file_content = File.open(PROTOS_PATH.join(file_name), "r:UTF-8", &:read) |
35 |
| - expected_file_output = |
36 |
| - ::Google::Protobuf::Compiler::CodeGeneratorResponse::File.new( |
37 |
| - :name => file_name, :content => file_content) |
38 |
| - |
39 |
| - expected_response = |
40 |
| - ::Google::Protobuf::Compiler::CodeGeneratorResponse.encode(:file => [expected_file_output]) |
41 |
| - |
42 |
| - code_generator = ::Protobuf::CodeGenerator.new(request.encode) |
43 |
| - code_generator.eval_unknown_extensions! |
44 |
| - expect(code_generator.response_bytes).to eq(expected_response) |
| 13 | + it "generates code for google's unittest.proto" do |
| 14 | + attrs = { |
| 15 | + optional_int32: 1, |
| 16 | + repeated_string: %w[boom], |
| 17 | + optional_nested_message: { bb: 10 }, |
| 18 | + optional_nested_enum: ::Protobuf_unittest::TestAllTypes::NestedEnum::BAZ, |
| 19 | + optional_foreign_message: { c: 12 }, |
| 20 | + repeated_import_message: [{ d: 13 }], |
| 21 | + } |
| 22 | + bytes = ::Protobuf_unittest::TestAllTypes.new(attrs).encode |
| 23 | + message = ::Protobuf_unittest::TestAllTypes.decode(bytes) |
| 24 | + expect(message.to_hash).to eq(attrs) |
| 25 | + expect(message.default_int32).to eq(41) |
| 26 | + expect(::Protobuf_unittest::TestAllTypes::FULLY_QUALIFIED_NAME).to eq('protobuf_unittest.TestAllTypes') |
| 27 | + descriptor_file = ::Protobuf_unittest.descriptor_set.file.first |
| 28 | + expect(descriptor_file.name).to eq('protos/google_unittest.proto') |
| 29 | + expect(descriptor_file.package).to eq('protobuf_unittest') |
45 | 30 | end
|
46 | 31 |
|
47 |
| - it "generates code (including service stubs) with custom field and method options" do |
48 |
| - expected_unittest_custom_options = |
49 |
| - File.open(PROTOS_PATH.join('google_unittest_custom_options.pb.rb'), "r:UTF-8", &:read) |
| 32 | + it "generates code for google's unittest.proto extensions" do |
| 33 | + attrs = { optional_import_message_extension: { d: 14 } } |
| 34 | + bytes = ::Protobuf_unittest::TestAllExtensions.new(attrs).encode |
| 35 | + message = ::Protobuf_unittest::TestAllExtensions.decode(bytes) |
| 36 | + expect(message.to_hash).to eq(attrs) |
| 37 | + expect(message.default_int64_extension).to eq(42) |
| 38 | + ::Protobuf_unittest::TestService.send(:set_option, '.protobuf_unittest.service_opt1', 10) |
| 39 | + expect(::Protobuf_unittest::TestService.get_option!('.protobuf_unittest.service_opt1')).to eq(10) |
| 40 | + rpc = ::Protobuf_unittest::TestService.rpcs[:foo] |
| 41 | + rpc.send(:set_option, '.protobuf_unittest.method_opt1', :METHODOPT1_VAL2) |
| 42 | + expect(rpc.get_option!('.protobuf_unittest.method_opt1')).to eq(:METHODOPT1_VAL2) |
| 43 | + end |
50 | 44 |
|
51 |
| - bytes = IO.read(PROTOS_PATH.join('google_unittest_custom_options.bin'), :mode => 'rb') |
52 |
| - code_generator = ::Protobuf::CodeGenerator.new(bytes) |
53 |
| - code_generator.eval_unknown_extensions! |
54 |
| - response = ::Google::Protobuf::Compiler::CodeGeneratorResponse.decode(code_generator.response_bytes) |
55 |
| - expect(response.file.find { |f| f.name == 'protos/google_unittest_custom_options.pb.rb' }.content) |
56 |
| - .to eq(expected_unittest_custom_options) |
| 45 | + it "generates code for map types" do |
| 46 | + bar = ::Foo::Bar.new(int32_to_baz: { 4 => { looks_like_map: { 'goat' => 'sheep' } } }) |
| 47 | + expect(bar.int32_to_baz[4].looks_like_map).to eq('goat' => 'sheep') |
57 | 48 | end
|
58 | 49 | end
|
0 commit comments