ruby_generator_unittest.cc 4.6 KB

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