metadata_test.cc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. #include <memory>
  31. #include <google/protobuf/compiler/cpp/cpp_helpers.h>
  32. #include <google/protobuf/compiler/cpp/cpp_generator.h>
  33. #include <google/protobuf/compiler/annotation_test_util.h>
  34. #include <google/protobuf/compiler/command_line_interface.h>
  35. #include <google/protobuf/descriptor.pb.h>
  36. #include <google/protobuf/testing/file.h>
  37. #include <google/protobuf/testing/file.h>
  38. #include <google/protobuf/testing/googletest.h>
  39. #include <gtest/gtest.h>
  40. namespace google {
  41. namespace atu = ::google::protobuf::compiler::annotation_test_util;
  42. namespace protobuf {
  43. namespace compiler {
  44. namespace cpp {
  45. namespace {
  46. class CppMetadataTest : public ::testing::Test {
  47. public:
  48. // Tries to capture a FileDescriptorProto, GeneratedCodeInfo, and output
  49. // code from the previously added file with name `filename`. Returns true on
  50. // success. If pb_h is non-null, expects a .pb.h and a .pb.h.meta (copied to
  51. // pb_h and pb_h_info respecfively); similarly for proto_h and proto_h_info.
  52. bool CaptureMetadata(const string& filename, FileDescriptorProto* file,
  53. string* pb_h, GeneratedCodeInfo* pb_h_info,
  54. string* proto_h, GeneratedCodeInfo* proto_h_info,
  55. string* pb_cc) {
  56. google::protobuf::compiler::CommandLineInterface cli;
  57. CppGenerator cpp_generator;
  58. cli.RegisterGenerator("--cpp_out", &cpp_generator, "");
  59. string cpp_out =
  60. "--cpp_out=annotate_headers=true,"
  61. "annotation_pragma_name=pragma_name,"
  62. "annotation_guard_name=guard_name:" +
  63. TestTempDir();
  64. const bool result =
  65. atu::RunProtoCompiler(filename, cpp_out, &cli, file);
  66. if (!result) {
  67. return result;
  68. }
  69. string output_base = TestTempDir() + "/" + StripProto(filename);
  70. if (pb_cc != NULL) {
  71. GOOGLE_CHECK_OK(
  72. File::GetContents(output_base + ".pb.cc", pb_cc, true));
  73. }
  74. if (pb_h != NULL && pb_h_info != NULL) {
  75. GOOGLE_CHECK_OK(
  76. File::GetContents(output_base + ".pb.h", pb_h, true));
  77. if (!atu::DecodeMetadata(output_base + ".pb.h.meta", pb_h_info)) {
  78. return false;
  79. }
  80. }
  81. if (proto_h != NULL && proto_h_info != NULL) {
  82. GOOGLE_CHECK_OK(File::GetContents(output_base + ".proto.h", proto_h,
  83. true));
  84. if (!atu::DecodeMetadata(output_base + ".proto.h.meta", proto_h_info)) {
  85. return false;
  86. }
  87. }
  88. return true;
  89. }
  90. };
  91. const char kSmallTestFile[] =
  92. "syntax = \"proto2\";\n"
  93. "package foo;\n"
  94. "enum Enum { VALUE = 0; }\n"
  95. "message Message { }\n";
  96. TEST_F(CppMetadataTest, CapturesEnumNames) {
  97. FileDescriptorProto file;
  98. GeneratedCodeInfo info;
  99. string pb_h;
  100. atu::AddFile("test.proto", kSmallTestFile);
  101. EXPECT_TRUE(
  102. CaptureMetadata("test.proto", &file, &pb_h, &info, NULL, NULL, NULL));
  103. EXPECT_EQ("Enum", file.enum_type(0).name());
  104. std::vector<int> enum_path;
  105. enum_path.push_back(FileDescriptorProto::kEnumTypeFieldNumber);
  106. enum_path.push_back(0);
  107. const GeneratedCodeInfo::Annotation* enum_annotation =
  108. atu::FindAnnotationOnPath(info, "test.proto", enum_path);
  109. EXPECT_TRUE(NULL != enum_annotation);
  110. EXPECT_TRUE(atu::AnnotationMatchesSubstring(pb_h, enum_annotation, "Enum"));
  111. }
  112. TEST_F(CppMetadataTest, AddsPragma) {
  113. FileDescriptorProto file;
  114. GeneratedCodeInfo info;
  115. string pb_h;
  116. atu::AddFile("test.proto", kSmallTestFile);
  117. EXPECT_TRUE(
  118. CaptureMetadata("test.proto", &file, &pb_h, &info, NULL, NULL, NULL));
  119. EXPECT_TRUE(pb_h.find("#ifdef guard_name") != string::npos);
  120. EXPECT_TRUE(pb_h.find("#pragma pragma_name \"test.pb.h.meta\"") !=
  121. string::npos);
  122. }
  123. TEST_F(CppMetadataTest, CapturesMessageNames) {
  124. FileDescriptorProto file;
  125. GeneratedCodeInfo info;
  126. string pb_h;
  127. atu::AddFile("test.proto", kSmallTestFile);
  128. EXPECT_TRUE(
  129. CaptureMetadata("test.proto", &file, &pb_h, &info, NULL, NULL, NULL));
  130. EXPECT_EQ("Message", file.message_type(0).name());
  131. std::vector<int> message_path;
  132. message_path.push_back(FileDescriptorProto::kMessageTypeFieldNumber);
  133. message_path.push_back(0);
  134. const GeneratedCodeInfo::Annotation* message_annotation =
  135. atu::FindAnnotationOnPath(info, "test.proto", message_path);
  136. EXPECT_TRUE(NULL != message_annotation);
  137. EXPECT_TRUE(
  138. atu::AnnotationMatchesSubstring(pb_h, message_annotation, "Message"));
  139. }
  140. } // namespace
  141. } // namespace cpp
  142. } // namespace compiler
  143. } // namespace protobuf
  144. } // namespace google