annotation_test_util.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 
32 
33 #include <memory>
40 
44 #include <gtest/gtest.h>
45 
46 namespace google {
47 namespace protobuf {
48 namespace compiler {
49 namespace annotation_test_util {
50 namespace {
51 
52 // A CodeGenerator that captures the FileDescriptor it's passed as a
53 // FileDescriptorProto.
54 class DescriptorCapturingGenerator : public CodeGenerator {
55  public:
56  // Does not own file; file must outlive the Generator.
57  explicit DescriptorCapturingGenerator(FileDescriptorProto* file)
58  : file_(file) {}
59 
60  virtual bool Generate(const FileDescriptor* file,
61  const std::string& parameter, GeneratorContext* context,
62  std::string* error) const {
63  file->CopyTo(file_);
64  return true;
65  }
66 
67  private:
69 };
70 } // namespace
71 
72 void AddFile(const std::string& filename, const std::string& data) {
74  true));
75 }
76 
77 bool RunProtoCompiler(const std::string& filename,
78  const std::string& plugin_specific_args,
81 
82  DescriptorCapturingGenerator capturing_generator(file);
83  cli->RegisterGenerator("--capture_out", &capturing_generator, "");
84 
85  std::string proto_path = "-I" + TestTempDir();
86  std::string capture_out = "--capture_out=" + TestTempDir();
87 
88  const char* argv[] = {"protoc", proto_path.c_str(),
89  plugin_specific_args.c_str(), capture_out.c_str(),
90  filename.c_str()};
91 
92  return cli->Run(5, argv) == 0;
93 }
94 
98  io::ArrayInputStream input(data.data(), data.size());
99  return info->ParseFromZeroCopyStream(&input);
100 }
101 
103  const GeneratedCodeInfo& info, const std::string& source_file,
104  const std::vector<int>& path,
105  std::vector<const GeneratedCodeInfo::Annotation*>* annotations) {
106  for (int i = 0; i < info.annotation_size(); ++i) {
107  const GeneratedCodeInfo::Annotation* annotation = &info.annotation(i);
108  if (annotation->source_file() != source_file ||
109  annotation->path_size() != path.size()) {
110  continue;
111  }
112  int node = 0;
113  for (; node < path.size(); ++node) {
114  if (annotation->path(node) != path[node]) {
115  break;
116  }
117  }
118  if (node == path.size()) {
119  annotations->push_back(annotation);
120  }
121  }
122 }
123 
125  const GeneratedCodeInfo& info, const std::string& source_file,
126  const std::vector<int>& path) {
127  std::vector<const GeneratedCodeInfo::Annotation*> annotations;
128  FindAnnotationsOnPath(info, source_file, path, &annotations);
129  if (annotations.empty()) {
130  return NULL;
131  }
132  return annotations[0];
133 }
134 
136  const std::string& file_content,
137  const std::vector<const GeneratedCodeInfo::Annotation*>& annotations,
138  const std::string& expected_text) {
139  for (std::vector<const GeneratedCodeInfo::Annotation*>::const_iterator
140  i = annotations.begin(),
141  e = annotations.end();
142  i != e; ++i) {
143  const GeneratedCodeInfo::Annotation* annotation = *i;
144  uint32 begin = annotation->begin();
145  uint32 end = annotation->end();
146  if (end < begin || end > file_content.size()) {
147  return false;
148  }
149  if (file_content.substr(begin, end - begin) == expected_text) {
150  return true;
151  }
152  }
153  return false;
154 }
155 
156 bool AnnotationMatchesSubstring(const std::string& file_content,
157  const GeneratedCodeInfo::Annotation* annotation,
158  const std::string& expected_text) {
159  std::vector<const GeneratedCodeInfo::Annotation*> annotations;
160  annotations.push_back(annotation);
161  return AtLeastOneAnnotationMatchesSubstring(file_content, annotations,
162  expected_text);
163 }
164 } // namespace annotation_test_util
165 } // namespace compiler
166 } // namespace protobuf
167 } // namespace google
zero_copy_stream.h
google::protobuf::File::SetContents
static bool SetContents(const string &name, const string &contents, bool)
Definition: file.h:93
google::protobuf::compiler::CommandLineInterface::RegisterGenerator
void RegisterGenerator(const std::string &flag_name, CodeGenerator *generator, const std::string &help_text)
Definition: command_line_interface.cc:797
end
GLuint GLuint end
Definition: glcorearb.h:2858
google::protobuf::compiler::CodeGenerator
Definition: code_generator.h:68
NULL
NULL
Definition: test_security_zap.cpp:405
input
std::string input
Definition: tokenizer_unittest.cc:197
gtest.h
GeneratedCodeInfo_Annotation::begin
::PROTOBUF_NAMESPACE_ID::int32 begin() const
Definition: descriptor.pb.h:11852
google::protobuf::uint32
uint32_t uint32
Definition: protobuf/src/google/protobuf/stubs/port.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
GeneratedCodeInfo_Annotation::source_file
const std::string & source_file() const
Definition: descriptor.pb.h:11772
google::protobuf::compiler::annotation_test_util::FindAnnotationsOnPath
void FindAnnotationsOnPath(const GeneratedCodeInfo &info, const std::string &source_file, const std::vector< int > &path, std::vector< const GeneratedCodeInfo::Annotation * > *annotations)
Definition: annotation_test_util.cc:102
google::protobuf::compiler::CommandLineInterface::SetInputsAreProtoPathRelative
void SetInputsAreProtoPathRelative(bool)
Definition: command_line_interface.h:197
google::protobuf::compiler::CommandLineInterface::Run
int Run(int argc, const char *const argv[])
Definition: command_line_interface.cc:823
GeneratedCodeInfo::annotation_size
int annotation_size() const
Definition: descriptor.pb.h:11885
error
Definition: cJSON.c:88
google::protobuf::compiler::CommandLineInterface
Definition: command_line_interface.h:109
begin
static size_t begin(const upb_table *t)
Definition: php/ext/google/protobuf/upb.c:4898
GeneratedCodeInfo::annotation
const PROTOBUF_NAMESPACE_ID::GeneratedCodeInfo_Annotation & annotation(int index) const
Definition: descriptor.pb.h:11900
google::protobuf::compiler::annotation_test_util::AtLeastOneAnnotationMatchesSubstring
bool AtLeastOneAnnotationMatchesSubstring(const std::string &file_content, const std::vector< const GeneratedCodeInfo::Annotation * > &annotations, const std::string &expected_text)
Definition: annotation_test_util.cc:135
google::protobuf::File::GetContents
static bool GetContents(const string &name, string *output, bool)
Definition: file.h:83
path
GLsizei const GLchar ** path
Definition: glcorearb.h:3658
GeneratedCodeInfo_Annotation::path
::PROTOBUF_NAMESPACE_ID::int32 path(int index) const
Definition: descriptor.pb.h:11741
GeneratedCodeInfo_Annotation
Definition: descriptor.pb.h:6147
printer.h
google::protobuf::io::ArrayInputStream
Definition: zero_copy_stream_impl_lite.h:65
GeneratedCodeInfo_Annotation::path_size
int path_size() const
Definition: descriptor.pb.h:11735
annotation_test_util.h
code_generator.h
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
FileDescriptorProto
Definition: descriptor.pb.h:501
google::protobuf::FileDescriptor::CopyTo
void CopyTo(FileDescriptorProto *proto) const
Definition: src/google/protobuf/descriptor.cc:2010
i
int i
Definition: gmock-matchers_test.cc:764
zero_copy_stream_impl_lite.h
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_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
google::protobuf::FileDescriptor
Definition: src/google/protobuf/descriptor.h:1320
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: glcorearb.h:2879
file.h
GeneratedCodeInfo_Annotation::end
::PROTOBUF_NAMESPACE_ID::int32 end() const
Definition: descriptor.pb.h:11870
descriptor.pb.h
google::protobuf::TestTempDir
string TestTempDir()
Definition: googletest.cc:189
GeneratedCodeInfo
Definition: descriptor.pb.h:6355
google::protobuf::compiler::GeneratorContext
Definition: code_generator.h:119
file_
FileDescriptorProto * file_
Definition: annotation_test_util.cc:68
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:47