blob: b7c6ddd31961288758e1249013250a33a44f3483 [file] [log] [blame]
Yu Shan10812112018-09-07 16:45:52 -07001// 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.
40namespace google {
41namespace protobuf {
42namespace compiler {
43namespace 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
51struct ExpectedOutput {
Armelle Lainefbc68102022-11-06 19:03:46 +000052 std::string file_path;
53 std::string file_content;
Yu Shan10812112018-09-07 16:45:52 -070054 GeneratedCodeInfo file_info;
Armelle Lainefbc68102022-11-06 19:03:46 +000055 explicit ExpectedOutput(const std::string& file_path)
56 : file_path(file_path) {}
Yu Shan10812112018-09-07 16:45:52 -070057};
58
59// Creates a file with name `filename` and content `data` in temp test
60// directory.
Armelle Lainefbc68102022-11-06 19:03:46 +000061void AddFile(const std::string& filename, const std::string& data);
Yu Shan10812112018-09-07 16:45:52 -070062
Armelle Lainefbc68102022-11-06 19:03:46 +000063// Runs proto compiler. Captures proto file structure in FileDescriptorProto.
Yu Shan10812112018-09-07 16:45:52 -070064// 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 Lainefbc68102022-11-06 19:03:46 +000074bool RunProtoCompiler(const std::string& filename,
75 const std::string& plugin_specific_args,
Yu Shan10812112018-09-07 16:45:52 -070076 CommandLineInterface* cli, FileDescriptorProto* file);
77
Armelle Lainefbc68102022-11-06 19:03:46 +000078bool DecodeMetadata(const std::string& path, GeneratedCodeInfo* info);
Yu Shan10812112018-09-07 16:45:52 -070079
80// Finds all of the Annotations for a given source file and path.
Armelle Lainefbc68102022-11-06 19:03:46 +000081// See Location.path in https://github.com/protocolbuffers/protobuf/blob/main/src/google/protobuf/descriptor.proto for
Yu Shan10812112018-09-07 16:45:52 -070082// explanation of what path vector is.
83void FindAnnotationsOnPath(
Armelle Lainefbc68102022-11-06 19:03:46 +000084 const GeneratedCodeInfo& info, const std::string& source_file,
Yu Shan10812112018-09-07 16:45:52 -070085 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 Lainefbc68102022-11-06 19:03:46 +000091// https://github.com/protocolbuffers/protobuf/blob/main/src/google/protobuf/descriptor.proto for explanation of what path
Yu Shan10812112018-09-07 16:45:52 -070092// vector is.
93const GeneratedCodeInfo::Annotation* FindAnnotationOnPath(
Armelle Lainefbc68102022-11-06 19:03:46 +000094 const GeneratedCodeInfo& info, const std::string& source_file,
Yu Shan10812112018-09-07 16:45:52 -070095 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.
99bool AtLeastOneAnnotationMatchesSubstring(
Armelle Lainefbc68102022-11-06 19:03:46 +0000100 const std::string& file_content,
Yu Shan10812112018-09-07 16:45:52 -0700101 const std::vector<const GeneratedCodeInfo::Annotation*>& annotations,
Armelle Lainefbc68102022-11-06 19:03:46 +0000102 const std::string& expected_text);
Yu Shan10812112018-09-07 16:45:52 -0700103
104// Returns true if the provided annotation covers a given substring in
105// file_content.
Armelle Lainefbc68102022-11-06 19:03:46 +0000106bool AnnotationMatchesSubstring(const std::string& file_content,
Yu Shan10812112018-09-07 16:45:52 -0700107 const GeneratedCodeInfo::Annotation* annotation,
Armelle Lainefbc68102022-11-06 19:03:46 +0000108 const std::string& expected_text);
Yu Shan10812112018-09-07 16:45:52 -0700109
110} // namespace annotation_test_util
111} // namespace compiler
112} // namespace protobuf
Yu Shan10812112018-09-07 16:45:52 -0700113} // namespace google
Armelle Lainefbc68102022-11-06 19:03:46 +0000114
Yu Shan10812112018-09-07 16:45:52 -0700115#endif // GOOGLE_PROTOBUF_COMPILER_ANNOTATION_TEST_UTIL_H__