| 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 |
// Author: [email protected] (Kenton Varda) |
| 32 |
|
| 33 |
#ifndef GOOGLE_PROTOBUF_COMPILER_MOCK_CODE_GENERATOR_H__ |
| 34 |
#define GOOGLE_PROTOBUF_COMPILER_MOCK_CODE_GENERATOR_H__ |
| 35 |
|
| 36 |
#include <cstdint> |
| 37 |
#include <string> |
| 38 |
|
| 39 |
#include "absl/strings/string_view.h" |
| 40 |
#include "google/protobuf/compiler/code_generator.h" |
| 41 |
|
| 42 |
namespace google { |
| 43 |
namespace protobuf { |
| 44 |
class FileDescriptor; |
| 45 |
} // namespace protobuf |
| 46 |
} // namespace google |
| 47 |
|
| 48 |
namespace google { |
| 49 |
namespace protobuf { |
| 50 |
namespace compiler { |
| 51 |
|
| 52 |
// A mock CodeGenerator, used by command_line_interface_unittest. This is in |
| 53 |
// its own file so that it can be used both directly and as a plugin. |
| 54 |
// |
| 55 |
// Generate() produces some output which can be checked by ExpectCalled(). The |
| 56 |
// generator can run in a different process (e.g. a plugin). |
| 57 |
// |
| 58 |
// If the parameter is "insert=NAMES", the MockCodeGenerator will insert lines |
| 59 |
// into the files generated by other MockCodeGenerators instead of creating |
| 60 |
// its own file. NAMES is a comma-separated list of the names of those other |
| 61 |
// MockCodeGenerators. If the parameter is "insert_endlines=NAMES", the |
| 62 |
// MockCodeGenerator will insert data guaranteed to contain more than one |
| 63 |
// endline into the files generated by NAMES. |
| 64 |
// |
| 65 |
// MockCodeGenerator will also modify its behavior slightly if the input file |
| 66 |
// contains a message type with one of the following names: |
| 67 |
// MockCodeGenerator_Error: Causes Generate() to return false and set the |
| 68 |
// error message to "Saw message type MockCodeGenerator_Error." |
| 69 |
// MockCodeGenerator_Exit: Generate() prints "Saw message type |
| 70 |
// MockCodeGenerator_Exit." to stderr and then calls exit(123). |
| 71 |
// MockCodeGenerator_Abort: Generate() prints "Saw message type |
| 72 |
// MockCodeGenerator_Abort." to stderr and then calls abort(). |
| 73 |
// MockCodeGenerator_HasSourceCodeInfo: Causes Generate() to abort after |
| 74 |
// printing "Saw message type MockCodeGenerator_HasSourceCodeInfo: FOO." to |
| 75 |
// stderr, where FOO is "1" if the supplied FileDescriptorProto has source |
| 76 |
// code info, and "0" otherwise. |
| 77 |
// MockCodeGenerator_Annotate: Generate() will add annotations to its output |
| 78 |
// that can later be verified with CheckGeneratedAnnotations. |
| 79 |
class MockCodeGenerator : public CodeGenerator { |
| 80 |
public: |
| 81 |
explicit MockCodeGenerator(absl::string_view name); |
| 82 |
~MockCodeGenerator() override; |
| 83 |
|
| 84 |
// Expect (via gTest) that a MockCodeGenerator with the given name was called |
| 85 |
// with the given parameters by inspecting the output location. |
| 86 |
// |
| 87 |
// |insertions| is a comma-separated list of names of MockCodeGenerators which |
| 88 |
// should have inserted lines into this file. |
| 89 |
// |parsed_file_list| is a comma-separated list of names of the files |
| 90 |
// that are being compiled together in this run. |
| 91 |
static void ExpectGenerated(absl::string_view name, |
| 92 |
absl::string_view parameter, |
| 93 |
absl::string_view insertions, |
| 94 |
absl::string_view file, |
| 95 |
absl::string_view first_message_name, |
| 96 |
absl::string_view first_parsed_file_name, |
| 97 |
absl::string_view output_directory); |
| 98 |
|
| 99 |
// Checks that the correct text ranges were annotated by the |
| 100 |
// MockCodeGenerator_Annotate directive. |
| 101 |
static void CheckGeneratedAnnotations(absl::string_view name, |
| 102 |
absl::string_view file, |
| 103 |
absl::string_view output_directory); |
| 104 |
|
| 105 |
// Get the name of the file which would be written by the given generator. |
| 106 |
static std::string GetOutputFileName(absl::string_view generator_name, |
| 107 |
const FileDescriptor* file); |
| 108 |
static std::string GetOutputFileName(absl::string_view generator_name, |
| 109 |
absl::string_view file); |
| 110 |
|
| 111 |
// implements CodeGenerator ---------------------------------------- |
| 112 |
|
| 113 |
bool Generate(const FileDescriptor* file, const std::string& parameter, |
| 114 |
GeneratorContext* context, std::string* error) const override; |
| 115 |
|
| 116 |
uint64_t GetSupportedFeatures() const override; |
| 117 |
void SuppressFeatures(uint64_t features); |
| 118 |
|
| 119 |
private: |
| 120 |
std::string name_; |
| 121 |
|
| 122 |
// Mark this mutable so that our test plugin can modify it during the Generate |
| 123 |
// call via generator flags. |
| 124 |
mutable uint64_t suppressed_features_ = 0; |
| 125 |
|
| 126 |
static std::string GetOutputFileContent(absl::string_view generator_name, |
| 127 |
absl::string_view parameter, |
| 128 |
const FileDescriptor* file, |
| 129 |
GeneratorContext* context); |
| 130 |
static std::string GetOutputFileContent(absl::string_view generator_name, |
| 131 |
absl::string_view parameter, |
| 132 |
absl::string_view file, |
| 133 |
absl::string_view parsed_file_list, |
| 134 |
absl::string_view first_message_name); |
| 135 |
}; |
| 136 |
|
| 137 |
} // namespace compiler |
| 138 |
} // namespace protobuf |
| 139 |
} // namespace google |
| 140 |
|
| 141 |
#endif // GOOGLE_PROTOBUF_COMPILER_MOCK_CODE_GENERATOR_H__ |