mock_code_generator.h 6.0 KB

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