cpp_bootstrap_unittest.cc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 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. // Author: kenton@google.com (Kenton Varda)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. //
  34. // This test insures that google/protobuf/descriptor.pb.{h,cc} match exactly
  35. // what would be generated by the protocol compiler. These files are not
  36. // generated automatically at build time because they are compiled into the
  37. // protocol compiler itself. So, if they were auto-generated, you'd have a
  38. // chicken-and-egg problem.
  39. //
  40. // If this test fails, run the script
  41. // "generate_descriptor_proto.sh" and add
  42. // descriptor.pb.{h,cc} to your changelist.
  43. #include <map>
  44. #include <google/protobuf/compiler/cpp/cpp_helpers.h>
  45. #include <google/protobuf/compiler/cpp/cpp_generator.h>
  46. #include <google/protobuf/compiler/importer.h>
  47. #include <google/protobuf/io/zero_copy_stream_impl.h>
  48. #include <google/protobuf/descriptor.h>
  49. #include <google/protobuf/stubs/substitute.h>
  50. #include <google/protobuf/stubs/map_util.h>
  51. #include <google/protobuf/stubs/stl_util.h>
  52. #include <google/protobuf/testing/file.h>
  53. #include <google/protobuf/testing/file.h>
  54. #include <google/protobuf/testing/googletest.h>
  55. #include <gtest/gtest.h>
  56. namespace google {
  57. namespace protobuf {
  58. namespace compiler {
  59. namespace cpp {
  60. namespace {
  61. class MockErrorCollector : public MultiFileErrorCollector {
  62. public:
  63. MockErrorCollector() {}
  64. ~MockErrorCollector() {}
  65. string text_;
  66. // implements ErrorCollector ---------------------------------------
  67. void AddError(const string& filename, int line, int column,
  68. const string& message) {
  69. strings::SubstituteAndAppend(&text_, "$0:$1:$2: $3\n",
  70. filename, line, column, message);
  71. }
  72. };
  73. class MockGeneratorContext : public GeneratorContext {
  74. public:
  75. MockGeneratorContext() {}
  76. ~MockGeneratorContext() {
  77. STLDeleteValues(&files_);
  78. }
  79. void ExpectFileMatches(const string& virtual_filename,
  80. const string& physical_filename) {
  81. string* expected_contents = FindPtrOrNull(files_, virtual_filename);
  82. ASSERT_TRUE(expected_contents != NULL)
  83. << "Generator failed to generate file: " << virtual_filename;
  84. string actual_contents;
  85. GOOGLE_CHECK_OK(
  86. File::GetContents(TestSourceDir() + "/" + physical_filename,
  87. &actual_contents, true));
  88. EXPECT_TRUE(actual_contents == *expected_contents)
  89. << physical_filename
  90. << " needs to be regenerated. Please run "
  91. "generate_descriptor_proto.sh. "
  92. "Then add this file to your CL.";
  93. }
  94. // implements GeneratorContext --------------------------------------
  95. virtual io::ZeroCopyOutputStream* Open(const string& filename) {
  96. string** map_slot = &files_[filename];
  97. delete *map_slot;
  98. *map_slot = new string;
  99. return new io::StringOutputStream(*map_slot);
  100. }
  101. private:
  102. std::map<string, string*> files_;
  103. };
  104. const char kDescriptorParameter[] = "dllexport_decl=LIBPROTOBUF_EXPORT";
  105. const char kPluginParameter[] = "dllexport_decl=LIBPROTOC_EXPORT";
  106. const char kNormalParameter[] = "";
  107. const char* test_protos[][2] = {
  108. {"google/protobuf/descriptor", kDescriptorParameter},
  109. {"google/protobuf/compiler/plugin", kPluginParameter},
  110. };
  111. TEST(BootstrapTest, GeneratedFilesMatch) {
  112. // We need a mapping from the actual file to virtual and actual path
  113. // of the data to compare to.
  114. std::map<string, string> vpath_map;
  115. std::map<string, string> rpath_map;
  116. rpath_map["third_party/protobuf/src/google/protobuf/test_messages_proto2"] =
  117. "net/proto2/z_generated_example/test_messages_proto2";
  118. rpath_map["third_party/protobuf/src/google/protobuf/test_messages_proto3"] =
  119. "net/proto2/z_generated_example/test_messages_proto3";
  120. rpath_map["google/protobuf/proto2_weak"] =
  121. "net/proto2/z_generated_example/proto2_weak";
  122. DiskSourceTree source_tree;
  123. source_tree.MapPath("", TestSourceDir());
  124. for (auto file_parameter : test_protos) {
  125. MockErrorCollector error_collector;
  126. Importer importer(&source_tree, &error_collector);
  127. const FileDescriptor* file =
  128. importer.Import(file_parameter[0] + string(".proto"));
  129. ASSERT_TRUE(file != nullptr)
  130. << "Can't import file " << file_parameter[0] + string(".proto") << "\n";
  131. EXPECT_EQ("", error_collector.text_);
  132. CppGenerator generator;
  133. MockGeneratorContext context;
  134. string error;
  135. ASSERT_TRUE(generator.Generate(file, file_parameter[1], &context, &error));
  136. string vpath =
  137. FindWithDefault(vpath_map, file_parameter[0], file_parameter[0]);
  138. string rpath =
  139. FindWithDefault(rpath_map, file_parameter[0], file_parameter[0]);
  140. context.ExpectFileMatches(vpath + ".pb.cc", rpath + ".pb.cc");
  141. context.ExpectFileMatches(vpath + ".pb.h", rpath + ".pb.h");
  142. }
  143. }
  144. } // namespace
  145. } // namespace cpp
  146. } // namespace compiler
  147. } // namespace protobuf
  148. } // namespace google