• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 {
52   std::string file_path;
53   std::string file_content;
54   GeneratedCodeInfo file_info;
ExpectedOutputExpectedOutput55   explicit ExpectedOutput(const std::string& file_path)
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 structrue 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.
74 bool RunProtoCompiler(const std::string& filename,
75                       const std::string& plugin_specific_args,
76                       CommandLineInterface* cli, FileDescriptorProto* file);
77 
78 bool DecodeMetadata(const std::string& path, GeneratedCodeInfo* info);
79 
80 // Finds all of the Annotations for a given source file and path.
81 // See Location.path in http://google3/net/proto2/proto/descriptor.proto for
82 // explanation of what path vector is.
83 void FindAnnotationsOnPath(
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 // http://google3/net/proto2/proto/descriptor.proto for explanation of what path
92 // vector is.
93 const GeneratedCodeInfo::Annotation* FindAnnotationOnPath(
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.
99 bool AtLeastOneAnnotationMatchesSubstring(
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__
116