protobuf/src/google/protobuf/compiler/cpp/metadata_test.cc
Go to the documentation of this file.
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 
31 #include <memory>
32 
33 #include <google/protobuf/testing/file.h>
34 #include <google/protobuf/testing/file.h>
35 #include <google/protobuf/compiler/cpp/cpp_helpers.h>
36 #include <google/protobuf/compiler/cpp/cpp_generator.h>
37 #include <google/protobuf/compiler/annotation_test_util.h>
38 #include <google/protobuf/compiler/command_line_interface.h>
39 #include <google/protobuf/descriptor.pb.h>
40 #include <google/protobuf/testing/googletest.h>
41 #include <gtest/gtest.h>
42 
43 namespace google {
44 namespace protobuf {
45 namespace compiler {
46 namespace cpp {
47 
48 namespace atu = annotation_test_util;
49 
50 namespace {
51 
52 class CppMetadataTest : public ::testing::Test {
53  public:
54  // Tries to capture a FileDescriptorProto, GeneratedCodeInfo, and output
55  // code from the previously added file with name `filename`. Returns true on
56  // success. If pb_h is non-null, expects a .pb.h and a .pb.h.meta (copied to
57  // pb_h and pb_h_info respecfively); similarly for proto_h and proto_h_info.
58  bool CaptureMetadata(const std::string& filename, FileDescriptorProto* file,
59  std::string* pb_h, GeneratedCodeInfo* pb_h_info,
60  std::string* proto_h, GeneratedCodeInfo* proto_h_info,
61  std::string* pb_cc) {
62  CommandLineInterface cli;
63  CppGenerator cpp_generator;
64  cli.RegisterGenerator("--cpp_out", &cpp_generator, "");
65  std::string cpp_out =
66  "--cpp_out=annotate_headers=true,"
67  "annotation_pragma_name=pragma_name,"
68  "annotation_guard_name=guard_name:" +
69  TestTempDir();
70 
71  const bool result = atu::RunProtoCompiler(filename, cpp_out, &cli, file);
72 
73  if (!result) {
74  return result;
75  }
76 
77  std::string output_base = TestTempDir() + "/" + StripProto(filename);
78 
79  if (pb_cc != NULL) {
81  File::GetContents(output_base + ".pb.cc", pb_cc, true));
82  }
83 
84  if (pb_h != NULL && pb_h_info != NULL) {
86  File::GetContents(output_base + ".pb.h", pb_h, true));
87  if (!atu::DecodeMetadata(output_base + ".pb.h.meta", pb_h_info)) {
88  return false;
89  }
90  }
91 
92  if (proto_h != NULL && proto_h_info != NULL) {
93  GOOGLE_CHECK_OK(File::GetContents(output_base + ".proto.h", proto_h,
94  true));
95  if (!atu::DecodeMetadata(output_base + ".proto.h.meta", proto_h_info)) {
96  return false;
97  }
98  }
99 
100  return true;
101  }
102 };
103 
104 const char kSmallTestFile[] =
105  "syntax = \"proto2\";\n"
106  "package foo;\n"
107  "enum Enum { VALUE = 0; }\n"
108  "message Message { }\n";
109 
110 TEST_F(CppMetadataTest, CapturesEnumNames) {
112  GeneratedCodeInfo info;
113  std::string pb_h;
114  atu::AddFile("test.proto", kSmallTestFile);
115  EXPECT_TRUE(
116  CaptureMetadata("test.proto", &file, &pb_h, &info, NULL, NULL, NULL));
117  EXPECT_EQ("Enum", file.enum_type(0).name());
118  std::vector<int> enum_path;
119  enum_path.push_back(FileDescriptorProto::kEnumTypeFieldNumber);
120  enum_path.push_back(0);
121  const GeneratedCodeInfo::Annotation* enum_annotation =
122  atu::FindAnnotationOnPath(info, "test.proto", enum_path);
123  EXPECT_TRUE(NULL != enum_annotation);
124  EXPECT_TRUE(atu::AnnotationMatchesSubstring(pb_h, enum_annotation, "Enum"));
125 }
126 
127 TEST_F(CppMetadataTest, AddsPragma) {
129  GeneratedCodeInfo info;
130  std::string pb_h;
131  atu::AddFile("test.proto", kSmallTestFile);
132  EXPECT_TRUE(
133  CaptureMetadata("test.proto", &file, &pb_h, &info, NULL, NULL, NULL));
134  EXPECT_TRUE(pb_h.find("#ifdef guard_name") != std::string::npos);
135  EXPECT_TRUE(pb_h.find("#pragma pragma_name \"test.pb.h.meta\"") !=
136  std::string::npos);
137 }
138 
139 TEST_F(CppMetadataTest, CapturesMessageNames) {
141  GeneratedCodeInfo info;
142  std::string pb_h;
143  atu::AddFile("test.proto", kSmallTestFile);
144  EXPECT_TRUE(
145  CaptureMetadata("test.proto", &file, &pb_h, &info, NULL, NULL, NULL));
146  EXPECT_EQ("Message", file.message_type(0).name());
147  std::vector<int> message_path;
148  message_path.push_back(FileDescriptorProto::kMessageTypeFieldNumber);
149  message_path.push_back(0);
150  const GeneratedCodeInfo::Annotation* message_annotation =
151  atu::FindAnnotationOnPath(info, "test.proto", message_path);
152  EXPECT_TRUE(NULL != message_annotation);
153  EXPECT_TRUE(
154  atu::AnnotationMatchesSubstring(pb_h, message_annotation, "Message"));
155 }
156 
157 } // namespace
158 } // namespace cpp
159 } // namespace compiler
160 } // namespace protobuf
161 } // namespace google
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
filename
const char * filename
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:135
file
const grpc_generator::File * file
Definition: python_private_generator.h:38
google::protobuf::compiler::cpp::StripProto
std::string StripProto(const std::string &filename)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_helpers.cc:480
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
google::protobuf::compiler::annotation_test_util::AddFile
void AddFile(const std::string &filename, const std::string &data)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/annotation_test_util.cc:72
file
Definition: bloaty/third_party/zlib/examples/gzappend.c:170
google::protobuf
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:12
FileDescriptorProto::kMessageTypeFieldNumber
@ kMessageTypeFieldNumber
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.pb.h:635
testing::Test
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:402
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
FileDescriptorProto::kEnumTypeFieldNumber
@ kEnumTypeFieldNumber
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.pb.h:636
google::protobuf::File::GetContents
static bool GetContents(const string &name, string *output, bool)
Definition: bloaty/third_party/protobuf/src/google/protobuf/testing/file.h:83
GeneratedCodeInfo_Annotation
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.pb.h:6853
google::protobuf::compiler::annotation_test_util::RunProtoCompiler
bool RunProtoCompiler(const std::string &filename, const std::string &plugin_specific_args, CommandLineInterface *cli, FileDescriptorProto *file)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/annotation_test_util.cc:77
cpp
Definition: third_party/bloaty/third_party/googletest/googlemock/scripts/generator/cpp/__init__.py:1
FileDescriptorProto
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.pb.h:501
google::protobuf::compiler::annotation_test_util::FindAnnotationOnPath
const GeneratedCodeInfo::Annotation * FindAnnotationOnPath(const GeneratedCodeInfo &info, const std::string &source_file, const std::vector< int > &path)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/annotation_test_util.cc:124
google::protobuf::TEST_F
TEST_F(DynamicMessageTest, Descriptor)
Definition: bloaty/third_party/protobuf/src/google/protobuf/dynamic_message_unittest.cc:126
google::protobuf::compiler::annotation_test_util::DecodeMetadata
bool DecodeMetadata(const std::string &path, GeneratedCodeInfo *info)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/annotation_test_util.cc:95
EXPECT_TRUE
#define EXPECT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1967
google::protobuf::TestTempDir
string TestTempDir()
Definition: bloaty/third_party/protobuf/src/google/protobuf/testing/googletest.cc:189
file::name
char * name
Definition: bloaty/third_party/zlib/examples/gzappend.c:176
GeneratedCodeInfo
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.pb.h:7087
compiler
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/plugin.pb.cc:21
google
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:11
google::protobuf::compiler::annotation_test_util::AnnotationMatchesSubstring
bool AnnotationMatchesSubstring(const std::string &file_content, const GeneratedCodeInfo::Annotation *annotation, const std::string &expected_text)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/annotation_test_util.cc:156
GOOGLE_CHECK_OK
#define GOOGLE_CHECK_OK(A)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/logging.h:155


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:29