• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2014 Google Inc.  All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7 
8 #include "google/protobuf/compiler/ruby/ruby_generator.h"
9 
10 #include <list>
11 #include <memory>
12 
13 #include "google/protobuf/testing/file.h"
14 #include "google/protobuf/testing/googletest.h"
15 #include <gtest/gtest.h>
16 #include "google/protobuf/compiler/command_line_interface.h"
17 #include "google/protobuf/io/printer.h"
18 #include "google/protobuf/io/zero_copy_stream.h"
19 
20 namespace google {
21 namespace protobuf {
22 namespace compiler {
23 namespace ruby {
24 namespace {
25 
FindRubyTestDir()26 std::string FindRubyTestDir() {
27   return absl::StrCat(TestSourceDir(), "/google/protobuf/compiler/ruby");
28 }
29 
30 // This test is a simple golden-file test over the output of the Ruby code
31 // generator. When we make changes to the Ruby extension and alter the Ruby code
32 // generator to use those changes, we should (i) manually test the output of the
33 // code generator with the extension, and (ii) update the golden output above.
34 // Some day, we may integrate build systems between protoc and the language
35 // extensions to the point where we can do this test in a more automated way.
36 
RubyTest(std::string proto_file,std::string import_proto_file="")37 void RubyTest(std::string proto_file, std::string import_proto_file = "") {
38   std::string ruby_tests = FindRubyTestDir();
39 
40   google::protobuf::compiler::CommandLineInterface cli;
41   cli.SetInputsAreProtoPathRelative(true);
42 
43   ruby::Generator ruby_generator;
44   cli.RegisterGenerator("--ruby_out", &ruby_generator, "");
45 
46   // Copy generated_code.proto to the temporary test directory.
47   std::string test_input;
48   ABSL_CHECK_OK(File::GetContents(
49       absl::StrCat(ruby_tests, proto_file, ".proto"), &test_input, true));
50   ABSL_CHECK_OK(File::SetContents(
51       absl::StrCat(TestTempDir(), proto_file, ".proto"), test_input, true));
52 
53   // Copy generated_code_import.proto to the temporary test directory.
54   std::string test_import;
55   if (!import_proto_file.empty()) {
56     ABSL_CHECK_OK(
57         File::GetContents(absl::StrCat(ruby_tests, import_proto_file, ".proto"),
58                           &test_import, true));
59     ABSL_CHECK_OK(File::SetContents(
60         absl::StrCat(TestTempDir(), import_proto_file, ".proto"), test_import,
61         true));
62   }
63 
64   // Invoke the proto compiler (we will be inside TestTempDir() at this point).
65   std::string ruby_out = absl::StrCat("--ruby_out=", TestTempDir());
66   std::string proto_path = absl::StrCat("--proto_path=", TestTempDir());
67 
68   std::string proto_target = absl::StrCat(TestTempDir(), proto_file, ".proto");
69   const char* argv[] = {
70     "protoc",
71     ruby_out.c_str(),
72     proto_path.c_str(),
73     proto_target.c_str(),
74   };
75 
76   EXPECT_EQ(0, cli.Run(4, argv));
77 
78   // Load the generated output and compare to the expected result.
79   std::string output;
80   ABSL_CHECK_OK(File::GetContentsAsText(
81       absl::StrCat(TestTempDir(), proto_file, "_pb.rb"), &output, true));
82   std::string expected_output;
83   ABSL_CHECK_OK(File::GetContentsAsText(
84       absl::StrCat(ruby_tests, proto_file, "_pb.rb"), &expected_output, true));
85   EXPECT_EQ(expected_output, output);
86 }
87 
TEST(RubyGeneratorTest,Proto3GeneratorTest)88 TEST(RubyGeneratorTest, Proto3GeneratorTest) {
89   RubyTest("/ruby_generated_code", "/ruby_generated_code_proto2_import");
90 }
91 
TEST(RubyGeneratorTest,Proto2GeneratorTest)92 TEST(RubyGeneratorTest, Proto2GeneratorTest) {
93     RubyTest("/ruby_generated_code_proto2", "/ruby_generated_code_proto2_import");
94 }
95 
TEST(RubyGeneratorTest,Proto3ImplicitPackageTest)96 TEST(RubyGeneratorTest, Proto3ImplicitPackageTest) {
97     RubyTest("/ruby_generated_pkg_implicit");
98 }
99 
TEST(RubyGeneratorTest,Proto3ExplicitPackageTest)100 TEST(RubyGeneratorTest, Proto3ExplicitPackageTest) {
101   RubyTest("/ruby_generated_pkg_explicit");
102 }
103 
TEST(RubyGeneratorTest,Proto3ExplicitLegacyPackageTest)104 TEST(RubyGeneratorTest, Proto3ExplicitLegacyPackageTest) {
105   RubyTest("/ruby_generated_pkg_explicit_legacy");
106 }
107 
108 }  // namespace
109 }  // namespace ruby
110 }  // namespace compiler
111 }  // namespace protobuf
112 }  // namespace google
113