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