mock_code_generator.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 google
  40. namespace google {
  41. namespace protobuf {
  42. namespace compiler {
  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. // MockCodeGenerator_Annotate: Generate() will add annotations to its output
  67. // that can later be verified with CheckGeneratedAnnotations.
  68. class MockCodeGenerator : public CodeGenerator {
  69. public:
  70. MockCodeGenerator(const std::string& name);
  71. virtual ~MockCodeGenerator();
  72. // Expect (via gTest) that a MockCodeGenerator with the given name was called
  73. // with the given parameters by inspecting the output location.
  74. //
  75. // |insertions| is a comma-separated list of names of MockCodeGenerators which
  76. // should have inserted lines into this file.
  77. // |parsed_file_list| is a comma-separated list of names of the files
  78. // that are being compiled together in this run.
  79. static void ExpectGenerated(const std::string& name,
  80. const std::string& parameter,
  81. const std::string& insertions,
  82. const std::string& file,
  83. const std::string& first_message_name,
  84. const std::string& parsed_file_list,
  85. const std::string& output_directory);
  86. // Checks that the correct text ranges were annotated by the
  87. // MockCodeGenerator_Annotate directive.
  88. static void CheckGeneratedAnnotations(const std::string& name,
  89. const std::string& file,
  90. const std::string& output_directory);
  91. // Get the name of the file which would be written by the given generator.
  92. static std::string GetOutputFileName(const std::string& generator_name,
  93. const FileDescriptor* file);
  94. static std::string GetOutputFileName(const std::string& generator_name,
  95. const std::string& file);
  96. // implements CodeGenerator ----------------------------------------
  97. bool Generate(const FileDescriptor* file, const std::string& parameter,
  98. GeneratorContext* context, std::string* error) const override;
  99. uint64_t GetSupportedFeatures() const override;
  100. void SuppressFeatures(uint64 features);
  101. private:
  102. std::string name_;
  103. uint64 suppressed_features_ = 0;
  104. static std::string GetOutputFileContent(const std::string& generator_name,
  105. const std::string& parameter,
  106. const FileDescriptor* file,
  107. GeneratorContext* context);
  108. static std::string GetOutputFileContent(
  109. const std::string& generator_name, const std::string& parameter,
  110. const std::string& file, const std::string& parsed_file_list,
  111. const std::string& first_message_name);
  112. };
  113. } // namespace compiler
  114. } // namespace protobuf
  115. } // namespace google
  116. #endif // GOOGLE_PROTOBUF_COMPILER_MOCK_CODE_GENERATOR_H__