Yu Shan | 1081211 | 2018-09-07 16:45:52 -0700 | [diff] [blame] | 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 { |
Armelle Laine | fbc6810 | 2022-11-06 19:03:46 +0000 | [diff] [blame] | 52 | std::string file_path; |
| 53 | std::string file_content; |
Yu Shan | 1081211 | 2018-09-07 16:45:52 -0700 | [diff] [blame] | 54 | GeneratedCodeInfo file_info; |
Armelle Laine | fbc6810 | 2022-11-06 19:03:46 +0000 | [diff] [blame] | 55 | explicit ExpectedOutput(const std::string& file_path) |
| 56 | : file_path(file_path) {} |
Yu Shan | 1081211 | 2018-09-07 16:45:52 -0700 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | // Creates a file with name `filename` and content `data` in temp test |
| 60 | // directory. |
Armelle Laine | fbc6810 | 2022-11-06 19:03:46 +0000 | [diff] [blame] | 61 | void AddFile(const std::string& filename, const std::string& data); |
Yu Shan | 1081211 | 2018-09-07 16:45:52 -0700 | [diff] [blame] | 62 | |
Armelle Laine | fbc6810 | 2022-11-06 19:03:46 +0000 | [diff] [blame] | 63 | // Runs proto compiler. Captures proto file structure in FileDescriptorProto. |
Yu Shan | 1081211 | 2018-09-07 16:45:52 -0700 | [diff] [blame] | 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. |
Armelle Laine | fbc6810 | 2022-11-06 19:03:46 +0000 | [diff] [blame] | 74 | bool RunProtoCompiler(const std::string& filename, |
| 75 | const std::string& plugin_specific_args, |
Yu Shan | 1081211 | 2018-09-07 16:45:52 -0700 | [diff] [blame] | 76 | CommandLineInterface* cli, FileDescriptorProto* file); |
| 77 | |
Armelle Laine | fbc6810 | 2022-11-06 19:03:46 +0000 | [diff] [blame] | 78 | bool DecodeMetadata(const std::string& path, GeneratedCodeInfo* info); |
Yu Shan | 1081211 | 2018-09-07 16:45:52 -0700 | [diff] [blame] | 79 | |
| 80 | // Finds all of the Annotations for a given source file and path. |
Armelle Laine | fbc6810 | 2022-11-06 19:03:46 +0000 | [diff] [blame] | 81 | // See Location.path in https://github.com/protocolbuffers/protobuf/blob/main/src/google/protobuf/descriptor.proto for |
Yu Shan | 1081211 | 2018-09-07 16:45:52 -0700 | [diff] [blame] | 82 | // explanation of what path vector is. |
| 83 | void FindAnnotationsOnPath( |
Armelle Laine | fbc6810 | 2022-11-06 19:03:46 +0000 | [diff] [blame] | 84 | const GeneratedCodeInfo& info, const std::string& source_file, |
Yu Shan | 1081211 | 2018-09-07 16:45:52 -0700 | [diff] [blame] | 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 |
Armelle Laine | fbc6810 | 2022-11-06 19:03:46 +0000 | [diff] [blame] | 91 | // https://github.com/protocolbuffers/protobuf/blob/main/src/google/protobuf/descriptor.proto for explanation of what path |
Yu Shan | 1081211 | 2018-09-07 16:45:52 -0700 | [diff] [blame] | 92 | // vector is. |
| 93 | const GeneratedCodeInfo::Annotation* FindAnnotationOnPath( |
Armelle Laine | fbc6810 | 2022-11-06 19:03:46 +0000 | [diff] [blame] | 94 | const GeneratedCodeInfo& info, const std::string& source_file, |
Yu Shan | 1081211 | 2018-09-07 16:45:52 -0700 | [diff] [blame] | 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( |
Armelle Laine | fbc6810 | 2022-11-06 19:03:46 +0000 | [diff] [blame] | 100 | const std::string& file_content, |
Yu Shan | 1081211 | 2018-09-07 16:45:52 -0700 | [diff] [blame] | 101 | const std::vector<const GeneratedCodeInfo::Annotation*>& annotations, |
Armelle Laine | fbc6810 | 2022-11-06 19:03:46 +0000 | [diff] [blame] | 102 | const std::string& expected_text); |
Yu Shan | 1081211 | 2018-09-07 16:45:52 -0700 | [diff] [blame] | 103 | |
| 104 | // Returns true if the provided annotation covers a given substring in |
| 105 | // file_content. |
Armelle Laine | fbc6810 | 2022-11-06 19:03:46 +0000 | [diff] [blame] | 106 | bool AnnotationMatchesSubstring(const std::string& file_content, |
Yu Shan | 1081211 | 2018-09-07 16:45:52 -0700 | [diff] [blame] | 107 | const GeneratedCodeInfo::Annotation* annotation, |
Armelle Laine | fbc6810 | 2022-11-06 19:03:46 +0000 | [diff] [blame] | 108 | const std::string& expected_text); |
Yu Shan | 1081211 | 2018-09-07 16:45:52 -0700 | [diff] [blame] | 109 | |
| 110 | } // namespace annotation_test_util |
| 111 | } // namespace compiler |
| 112 | } // namespace protobuf |
Yu Shan | 1081211 | 2018-09-07 16:45:52 -0700 | [diff] [blame] | 113 | } // namespace google |
Armelle Laine | fbc6810 | 2022-11-06 19:03:46 +0000 | [diff] [blame] | 114 | |
Yu Shan | 1081211 | 2018-09-07 16:45:52 -0700 | [diff] [blame] | 115 | #endif // GOOGLE_PROTOBUF_COMPILER_ANNOTATION_TEST_UTIL_H__ |