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 
38 
42 #include <gtest/gtest.h>
43 
44 namespace google {
45 namespace protobuf {
46 namespace compiler {
47 namespace cpp {
48 
49 namespace atu = annotation_test_util;
50 
51 namespace {
52 
53 class CppMetadataTest : public ::testing::Test {
54  public:
55  // Tries to capture a FileDescriptorProto, GeneratedCodeInfo, and output
56  // code from the previously added file with name `filename`. Returns true on
57  // success. If pb_h is non-null, expects a .pb.h and a .pb.h.meta (copied to
58  // pb_h and pb_h_info respecfively); similarly for proto_h and proto_h_info.
59  bool CaptureMetadata(const std::string& filename, FileDescriptorProto* file,
60  std::string* pb_h, GeneratedCodeInfo* pb_h_info,
61  std::string* proto_h, GeneratedCodeInfo* proto_h_info,
62  std::string* pb_cc) {
63  CommandLineInterface cli;
64  CppGenerator cpp_generator;
65  cli.RegisterGenerator("--cpp_out", &cpp_generator, "");
66  std::string cpp_out =
67  "--cpp_out=annotate_headers=true,"
68  "annotation_pragma_name=pragma_name,"
69  "annotation_guard_name=guard_name:" +
70  TestTempDir();
71 
72  const bool result = atu::RunProtoCompiler(filename, cpp_out, &cli, file);
73 
74  if (!result) {
75  return result;
76  }
77 
78  std::string output_base = TestTempDir() + "/" + StripProto(filename);
79 
80  if (pb_cc != NULL) {
82  File::GetContents(output_base + ".pb.cc", pb_cc, true));
83  }
84 
85  if (pb_h != NULL && pb_h_info != NULL) {
87  File::GetContents(output_base + ".pb.h", pb_h, true));
88  if (!atu::DecodeMetadata(output_base + ".pb.h.meta", pb_h_info)) {
89  return false;
90  }
91  }
92 
93  if (proto_h != NULL && proto_h_info != NULL) {
94  GOOGLE_CHECK_OK(File::GetContents(output_base + ".proto.h", proto_h,
95  true));
96  if (!atu::DecodeMetadata(output_base + ".proto.h.meta", proto_h_info)) {
97  return false;
98  }
99  }
100 
101  return true;
102  }
103 };
104 
105 const char kSmallTestFile[] =
106  "syntax = \"proto2\";\n"
107  "package foo;\n"
108  "enum Enum { VALUE = 0; }\n"
109  "message Message { }\n";
110 
111 TEST_F(CppMetadataTest, CapturesEnumNames) {
112  FileDescriptorProto file;
113  GeneratedCodeInfo info;
114  std::string pb_h;
115  atu::AddFile("test.proto", kSmallTestFile);
116  EXPECT_TRUE(
117  CaptureMetadata("test.proto", &file, &pb_h, &info, NULL, NULL, NULL));
118  EXPECT_EQ("Enum", file.enum_type(0).name());
119  std::vector<int> enum_path;
120  enum_path.push_back(FileDescriptorProto::kEnumTypeFieldNumber);
121  enum_path.push_back(0);
122  const GeneratedCodeInfo::Annotation* enum_annotation =
123  atu::FindAnnotationOnPath(info, "test.proto", enum_path);
124  EXPECT_TRUE(NULL != enum_annotation);
125  EXPECT_TRUE(atu::AnnotationMatchesSubstring(pb_h, enum_annotation, "Enum"));
126 }
127 
128 TEST_F(CppMetadataTest, AddsPragma) {
129  FileDescriptorProto file;
130  GeneratedCodeInfo info;
131  std::string pb_h;
132  atu::AddFile("test.proto", kSmallTestFile);
133  EXPECT_TRUE(
134  CaptureMetadata("test.proto", &file, &pb_h, &info, NULL, NULL, NULL));
135  EXPECT_TRUE(pb_h.find("#ifdef guard_name") != string::npos);
136  EXPECT_TRUE(pb_h.find("#pragma pragma_name \"test.pb.h.meta\"") !=
137  std::string::npos);
138 }
139 
140 TEST_F(CppMetadataTest, CapturesMessageNames) {
141  FileDescriptorProto file;
142  GeneratedCodeInfo info;
143  std::string pb_h;
144  atu::AddFile("test.proto", kSmallTestFile);
145  EXPECT_TRUE(
146  CaptureMetadata("test.proto", &file, &pb_h, &info, NULL, NULL, NULL));
147  EXPECT_EQ("Message", file.message_type(0).name());
148  std::vector<int> message_path;
149  message_path.push_back(FileDescriptorProto::kMessageTypeFieldNumber);
150  message_path.push_back(0);
151  const GeneratedCodeInfo::Annotation* message_annotation =
152  atu::FindAnnotationOnPath(info, "test.proto", message_path);
153  EXPECT_TRUE(NULL != message_annotation);
154  EXPECT_TRUE(
155  atu::AnnotationMatchesSubstring(pb_h, message_annotation, "Message"));
156 }
157 
158 } // namespace
159 } // namespace cpp
160 } // namespace compiler
161 } // namespace protobuf
162 } // namespace google
NULL
NULL
Definition: test_security_zap.cpp:405
google::protobuf::compiler::cpp::StripProto
std::string StripProto(const std::string &filename)
Definition: cpp_helpers.cc:474
gtest.h
FileDescriptorProto::kEnumTypeFieldNumber
@ kEnumTypeFieldNumber
Definition: descriptor.pb.h:643
EXPECT_EQ
#define EXPECT_EQ(val1, val2)
Definition: glog/src/googletest.h:155
google::protobuf::compiler::annotation_test_util::AddFile
void AddFile(const std::string &filename, const std::string &data)
Definition: annotation_test_util.cc:72
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
cpp_generator.h
testing::Test
Definition: gtest.h:415
cpp_helpers.h
FileDescriptorProto::enum_type
const PROTOBUF_NAMESPACE_ID::EnumDescriptorProto & enum_type(int index) const
Definition: descriptor.pb.h:6899
google::protobuf::File::GetContents
static bool GetContents(const string &name, string *output, bool)
Definition: file.h:83
GeneratedCodeInfo_Annotation
Definition: descriptor.pb.h:6147
annotation_test_util.h
EXPECT_TRUE
#define EXPECT_TRUE(cond)
Definition: glog/src/googletest.h:137
google::protobuf::compiler::annotation_test_util::RunProtoCompiler
bool RunProtoCompiler(const std::string &filename, const std::string &plugin_specific_args, CommandLineInterface *cli, FileDescriptorProto *file)
Definition: annotation_test_util.cc:77
cpp
Definition: third_party/googletest/googlemock/scripts/generator/cpp/__init__.py:1
FileDescriptorProto
Definition: descriptor.pb.h:501
FileDescriptorProto::message_type
const PROTOBUF_NAMESPACE_ID::DescriptorProto & message_type(int index) const
Definition: descriptor.pb.h:6869
FileDescriptorProto::kMessageTypeFieldNumber
@ kMessageTypeFieldNumber
Definition: descriptor.pb.h:642
command_line_interface.h
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: annotation_test_util.cc:124
google::protobuf::TEST_F
TEST_F(DynamicMessageTest, Descriptor)
Definition: dynamic_message_unittest.cc:126
GOOGLE_CHECK_OK
#define GOOGLE_CHECK_OK(A)
Definition: logging.h:155
googletest.h
google::protobuf::compiler::annotation_test_util::DecodeMetadata
bool DecodeMetadata(const std::string &path, GeneratedCodeInfo *info)
Definition: annotation_test_util.cc:95
file.h
descriptor.pb.h
google::protobuf::TestTempDir
string TestTempDir()
Definition: googletest.cc:189
GeneratedCodeInfo
Definition: descriptor.pb.h:6355
compiler
Definition: plugin.pb.cc:22
google
Definition: 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: annotation_test_util.cc:156


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:57