• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
34 #include <google/protobuf/compiler/ruby/ruby_generator.h>
35 #include <google/protobuf/compiler/command_line_interface.h>
36 #include <google/protobuf/io/zero_copy_stream.h>
37 #include <google/protobuf/io/printer.h>
38 
39 #include <google/protobuf/testing/googletest.h>
40 #include <gtest/gtest.h>
41 #include <google/protobuf/testing/file.h>
42 
43 namespace google {
44 namespace protobuf {
45 namespace compiler {
46 namespace ruby {
47 namespace {
48 
FindRubyTestDir()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 
RubyTest(string proto_file)60 void RubyTest(string proto_file) {
61   string ruby_tests = FindRubyTestDir();
62 
63   google::protobuf::compiler::CommandLineInterface cli;
64   cli.SetInputsAreProtoPathRelative(true);
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;
71   GOOGLE_CHECK_OK(File::GetContents(
72       ruby_tests + proto_file + ".proto",
73       &test_input,
74       true));
75   GOOGLE_CHECK_OK(File::SetContents(
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;
95   GOOGLE_CHECK_OK(File::GetContentsAsText(
96       TestTempDir() + proto_file + "_pb.rb",
97       &output,
98       true));
99   string expected_output;
100   GOOGLE_CHECK_OK(File::GetContentsAsText(
101       ruby_tests + proto_file + "_pb.rb",
102       &expected_output,
103       true));
104   EXPECT_EQ(expected_output, output);
105 }
106 
TEST(RubyGeneratorTest,Proto3GeneratorTest)107 TEST(RubyGeneratorTest, Proto3GeneratorTest) {
108   RubyTest("/ruby_generated_code");
109 }
110 
TEST(RubyGeneratorTest,Proto2GeneratorTest)111 TEST(RubyGeneratorTest, Proto2GeneratorTest) {
112     RubyTest("/ruby_generated_code_proto2");
113 }
114 
TEST(RubyGeneratorTest,Proto3ImplicitPackageTest)115 TEST(RubyGeneratorTest, Proto3ImplicitPackageTest) {
116     RubyTest("/ruby_generated_pkg_implicit");
117 }
118 
TEST(RubyGeneratorTest,Proto3ExplictPackageTest)119 TEST(RubyGeneratorTest, Proto3ExplictPackageTest) {
120     RubyTest("/ruby_generated_pkg_explicit");
121 }
122 
TEST(RubyGeneratorTest,Proto3ExplictLegacyPackageTest)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
132