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
33 #include <google/protobuf/compiler/ruby/ruby_generator.h>
34 #include <google/protobuf/compiler/command_line_interface.h>
35 #include <google/protobuf/io/zero_copy_stream.h>
36 #include <google/protobuf/io/printer.h>
37
38 #include <google/protobuf/testing/googletest.h>
39 #include <gtest/gtest.h>
40 #include <google/protobuf/testing/file.h>
41
42 namespace google {
43 namespace protobuf {
44 namespace compiler {
45 namespace ruby {
46 namespace {
47
FindRubyTestDir()48 string FindRubyTestDir() {
49 return TestSourceDir() + "/google/protobuf/compiler/ruby";
50 }
51
52 // This test is a simple golden-file test over the output of the Ruby code
53 // generator. When we make changes to the Ruby extension and alter the Ruby code
54 // generator to use those changes, we should (i) manually test the output of the
55 // code generator with the extension, and (ii) update the golden output above.
56 // Some day, we may integrate build systems between protoc and the language
57 // extensions to the point where we can do this test in a more automated way.
58
TEST(RubyGeneratorTest,GeneratorTest)59 TEST(RubyGeneratorTest, GeneratorTest) {
60 string ruby_tests = FindRubyTestDir();
61
62 google::protobuf::compiler::CommandLineInterface cli;
63 cli.SetInputsAreProtoPathRelative(true);
64
65 ruby::Generator ruby_generator;
66 cli.RegisterGenerator("--ruby_out", &ruby_generator, "");
67
68 // Copy generated_code.proto to the temporary test directory.
69 string test_input;
70 GOOGLE_CHECK_OK(File::GetContents(
71 ruby_tests + "/ruby_generated_code.proto",
72 &test_input,
73 true));
74 GOOGLE_CHECK_OK(File::SetContents(
75 TestTempDir() + "/ruby_generated_code.proto",
76 test_input,
77 true));
78
79 // Invoke the proto compiler (we will be inside TestTempDir() at this point).
80 string ruby_out = "--ruby_out=" + TestTempDir();
81 string proto_path = "--proto_path=" + TestTempDir();
82 const char* argv[] = {
83 "protoc",
84 ruby_out.c_str(),
85 proto_path.c_str(),
86 "ruby_generated_code.proto",
87 };
88
89 EXPECT_EQ(0, cli.Run(4, argv));
90
91 // Load the generated output and compare to the expected result.
92 string output;
93 GOOGLE_CHECK_OK(File::GetContents(
94 TestTempDir() + "/ruby_generated_code_pb.rb",
95 &output,
96 true));
97 string expected_output;
98 GOOGLE_CHECK_OK(File::GetContents(
99 ruby_tests + "/ruby_generated_code_pb.rb",
100 &expected_output,
101 true));
102 EXPECT_EQ(expected_output, output);
103 }
104
105 } // namespace
106 } // namespace ruby
107 } // namespace compiler
108 } // namespace protobuf
109 } // namespace google
110