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