protobuf/src/google/protobuf/compiler/annotation_test_util.h
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 #ifndef GOOGLE_PROTOBUF_COMPILER_ANNOTATION_TEST_UTIL_H__
32 #define GOOGLE_PROTOBUF_COMPILER_ANNOTATION_TEST_UTIL_H__
33 
34 #include <google/protobuf/descriptor.pb.h>
35 #include <google/protobuf/testing/googletest.h>
36 #include <gtest/gtest.h>
37 
38 // Utilities that assist in writing tests for generator annotations.
39 // See java/internal/annotation_unittest.cc for an example.
40 namespace google {
41 namespace protobuf {
42 namespace compiler {
43 namespace annotation_test_util {
44 
45 // Struct that contains the file generated from a .proto file and its
46 // GeneratedCodeInfo. For example, the Java generator will fill this struct
47 // (for some 'foo.proto') with:
48 // file_path = "Foo.java"
49 // file_content = content of Foo.java
50 // file_info = parsed content of Foo.java.pb.meta
51 struct ExpectedOutput {
56  : file_path(file_path) {}
57 };
58 
59 // Creates a file with name `filename` and content `data` in temp test
60 // directory.
61 void AddFile(const std::string& filename, const std::string& data);
62 
63 // Runs proto compiler. Captures proto file structure in FileDescriptorProto.
64 // Files will be generated in TestTempDir() folder. Callers of this
65 // function must read generated files themselves.
66 //
67 // filename: source .proto file used to generate code.
68 // plugin_specific_args: command line arguments specific to current generator.
69 // For Java, this value might be "--java_out=annotate_code:test_temp_dir"
70 // cli: instance of command line interface to run generator. See Java's
71 // annotation_unittest.cc for an example of how to initialize it.
72 // file: output parameter, will be set to the descriptor of the proto file
73 // specified in filename.
75  const std::string& plugin_specific_args,
77 
79 
80 // Finds all of the Annotations for a given source file and path.
81 // See Location.path in https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/descriptor.proto for
82 // explanation of what path vector is.
84  const GeneratedCodeInfo& info, const std::string& source_file,
85  const std::vector<int>& path,
86  std::vector<const GeneratedCodeInfo::Annotation*>* annotations);
87 
88 // Finds the Annotation for a given source file and path (or returns null if it
89 // couldn't). If there are several annotations for given path, returns the first
90 // one. See Location.path in
91 // https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/descriptor.proto for explanation of what path
92 // vector is.
94  const GeneratedCodeInfo& info, const std::string& source_file,
95  const std::vector<int>& path);
96 
97 // Returns true if at least one of the provided annotations covers a given
98 // substring in file_content.
100  const std::string& file_content,
101  const std::vector<const GeneratedCodeInfo::Annotation*>& annotations,
102  const std::string& expected_text);
103 
104 // Returns true if the provided annotation covers a given substring in
105 // file_content.
106 bool AnnotationMatchesSubstring(const std::string& file_content,
107  const GeneratedCodeInfo::Annotation* annotation,
108  const std::string& expected_text);
109 
110 } // namespace annotation_test_util
111 } // namespace compiler
112 } // namespace protobuf
113 } // namespace google
114 
115 #endif // GOOGLE_PROTOBUF_COMPILER_ANNOTATION_TEST_UTIL_H__
filename
const char * filename
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:135
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
check_documentation.path
path
Definition: check_documentation.py:57
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: bloaty/third_party/protobuf/src/google/protobuf/compiler/annotation_test_util.cc:102
google::protobuf::compiler::annotation_test_util::ExpectedOutput::ExpectedOutput
ExpectedOutput(const std::string &file_path)
Definition: protobuf/src/google/protobuf/compiler/annotation_test_util.h:55
google::protobuf::compiler::CommandLineInterface
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/command_line_interface.h:109
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: bloaty/third_party/protobuf/src/google/protobuf/compiler/annotation_test_util.cc:135
google::protobuf::compiler::annotation_test_util::ExpectedOutput::file_info
GeneratedCodeInfo file_info
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/annotation_test_util.h:54
GeneratedCodeInfo_Annotation
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.pb.h:6853
data
char data[kBufferLength]
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1006
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
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::compiler::annotation_test_util::ExpectedOutput::file_path
std::string file_path
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/annotation_test_util.h:52
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
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::protobuf::compiler::annotation_test_util::ExpectedOutput::file_content
std::string file_content
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/annotation_test_util.h:53


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:41