ruby_generator_unittest.cc
Go to the documentation of this file.
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2014 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 #include <memory>
32 #include <list>
33 
38 
40 #include <gtest/gtest.h>
42 
43 namespace google {
44 namespace protobuf {
45 namespace compiler {
46 namespace ruby {
47 namespace {
48 
49 string FindRubyTestDir() {
50  return TestSourceDir() + "/google/protobuf/compiler/ruby";
51 }
52 
53 // This test is a simple golden-file test over the output of the Ruby code
54 // generator. When we make changes to the Ruby extension and alter the Ruby code
55 // generator to use those changes, we should (i) manually test the output of the
56 // code generator with the extension, and (ii) update the golden output above.
57 // Some day, we may integrate build systems between protoc and the language
58 // extensions to the point where we can do this test in a more automated way.
59 
60 void RubyTest(string proto_file) {
61  string ruby_tests = FindRubyTestDir();
62 
65 
66  ruby::Generator ruby_generator;
67  cli.RegisterGenerator("--ruby_out", &ruby_generator, "");
68 
69  // Copy generated_code.proto to the temporary test directory.
70  string test_input;
72  ruby_tests + proto_file + ".proto",
73  &test_input,
74  true));
76  TestTempDir() + proto_file + ".proto",
77  test_input,
78  true));
79 
80  // Invoke the proto compiler (we will be inside TestTempDir() at this point).
81  string ruby_out = "--ruby_out=" + TestTempDir();
82  string proto_path = "--proto_path=" + TestTempDir();
83  string proto_target = TestTempDir() + proto_file + ".proto";
84  const char* argv[] = {
85  "protoc",
86  ruby_out.c_str(),
87  proto_path.c_str(),
88  proto_target.c_str(),
89  };
90 
91  EXPECT_EQ(0, cli.Run(4, argv));
92 
93  // Load the generated output and compare to the expected result.
94  string output;
96  TestTempDir() + proto_file + "_pb.rb",
97  &output,
98  true));
99  string expected_output;
101  ruby_tests + proto_file + "_pb.rb",
102  &expected_output,
103  true));
104  EXPECT_EQ(expected_output, output);
105 }
106 
107 TEST(RubyGeneratorTest, Proto3GeneratorTest) {
108  RubyTest("/ruby_generated_code");
109 }
110 
111 TEST(RubyGeneratorTest, Proto2GeneratorTest) {
112  RubyTest("/ruby_generated_code_proto2");
113 }
114 
115 TEST(RubyGeneratorTest, Proto3ImplicitPackageTest) {
116  RubyTest("/ruby_generated_pkg_implicit");
117 }
118 
119 TEST(RubyGeneratorTest, Proto3ExplictPackageTest) {
120  RubyTest("/ruby_generated_pkg_explicit");
121 }
122 
123 TEST(RubyGeneratorTest, Proto3ExplictLegacyPackageTest) {
124  RubyTest("/ruby_generated_pkg_explicit_legacy");
125 }
126 
127 } // namespace
128 } // namespace ruby
129 } // namespace compiler
130 } // namespace protobuf
131 } // namespace google
zero_copy_stream.h
google::protobuf::File::SetContents
static bool SetContents(const string &name, const string &contents, bool)
Definition: file.h:93
google::protobuf::compiler::CommandLineInterface::RegisterGenerator
void RegisterGenerator(const std::string &flag_name, CodeGenerator *generator, const std::string &help_text)
Definition: command_line_interface.cc:797
gtest.h
google::protobuf::TestSourceDir
string TestSourceDir()
Definition: googletest.cc:73
EXPECT_EQ
#define EXPECT_EQ(val1, val2)
Definition: glog/src/googletest.h:155
google::protobuf::compiler::CommandLineInterface::SetInputsAreProtoPathRelative
void SetInputsAreProtoPathRelative(bool)
Definition: command_line_interface.h:197
google::protobuf::compiler::CommandLineInterface::Run
int Run(int argc, const char *const argv[])
Definition: command_line_interface.cc:823
google::protobuf::File::GetContentsAsText
static bool GetContentsAsText(const string &name, string *output, bool)
Definition: file.h:88
google::protobuf::compiler::CommandLineInterface
Definition: command_line_interface.h:109
google::protobuf::File::GetContents
static bool GetContents(const string &name, string *output, bool)
Definition: file.h:83
printer.h
google::protobuf::TEST
TEST(ArenaTest, ArenaConstructable)
Definition: arena_unittest.cc:156
ruby_generator.h
command_line_interface.h
GOOGLE_CHECK_OK
#define GOOGLE_CHECK_OK(A)
Definition: logging.h:155
googletest.h
file.h
output
const upb_json_parsermethod const upb_symtab upb_sink * output
Definition: ruby/ext/google/protobuf_c/upb.h:10503
google::protobuf::TestTempDir
string TestTempDir()
Definition: googletest.cc:189
compiler
Definition: plugin.pb.cc:22
google
Definition: data_proto2_to_proto3_util.h:11


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:58