annotation_test_util.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #ifndef GOOGLE_PROTOBUF_COMPILER_ANNOTATION_TEST_UTIL_H__
  31. #define GOOGLE_PROTOBUF_COMPILER_ANNOTATION_TEST_UTIL_H__
  32. #include <google/protobuf/descriptor.pb.h>
  33. #include <google/protobuf/testing/googletest.h>
  34. #include <gtest/gtest.h>
  35. // Utilities that assist in writing tests for generator annotations.
  36. // See java/internal/annotation_unittest.cc for an example.
  37. namespace google {
  38. namespace protobuf {
  39. namespace compiler {
  40. namespace annotation_test_util {
  41. // Struct that contains the file generated from a .proto file and its
  42. // GeneratedCodeInfo. For example, the Java generator will fill this struct
  43. // (for some 'foo.proto') with:
  44. // file_path = "Foo.java"
  45. // file_content = content of Foo.java
  46. // file_info = parsed content of Foo.java.pb.meta
  47. struct ExpectedOutput {
  48. string file_path;
  49. string file_content;
  50. GeneratedCodeInfo file_info;
  51. explicit ExpectedOutput(const string& file_path) : file_path(file_path) {}
  52. };
  53. // Creates a file with name `filename` and content `data` in temp test
  54. // directory.
  55. void AddFile(const string& filename, const string& data);
  56. // Runs proto compiler. Captures proto file structrue in FileDescriptorProto.
  57. // Files will be generated in TestTempDir() folder. Callers of this
  58. // function must read generated files themselves.
  59. //
  60. // filename: source .proto file used to generate code.
  61. // plugin_specific_args: command line arguments specific to current generator.
  62. // For Java, this value might be "--java_out=annotate_code:test_temp_dir"
  63. // cli: instance of command line interface to run generator. See Java's
  64. // annotation_unittest.cc for an example of how to initialize it.
  65. // file: output parameter, will be set to the descriptor of the proto file
  66. // specified in filename.
  67. bool RunProtoCompiler(const string& filename,
  68. const string& plugin_specific_args,
  69. CommandLineInterface* cli, FileDescriptorProto* file);
  70. bool DecodeMetadata(const string& path, GeneratedCodeInfo* info);
  71. // Finds all of the Annotations for a given source file and path.
  72. // See Location.path in http://google/protobuf/descriptor.proto for
  73. // explanation of what path vector is.
  74. void FindAnnotationsOnPath(
  75. const GeneratedCodeInfo& info, const string& source_file,
  76. const std::vector<int>& path,
  77. std::vector<const GeneratedCodeInfo::Annotation*>* annotations);
  78. // Finds the Annotation for a given source file and path (or returns null if it
  79. // couldn't). If there are several annotations for given path, returns the first
  80. // one. See Location.path in
  81. // http://google/protobuf/descriptor.proto for explanation of what path
  82. // vector is.
  83. const GeneratedCodeInfo::Annotation* FindAnnotationOnPath(
  84. const GeneratedCodeInfo& info, const string& source_file,
  85. const std::vector<int>& path);
  86. // Returns true if at least one of the provided annotations covers a given
  87. // substring in file_content.
  88. bool AtLeastOneAnnotationMatchesSubstring(
  89. const string& file_content,
  90. const std::vector<const GeneratedCodeInfo::Annotation*>& annotations,
  91. const string& expected_text);
  92. // Returns true if the provided annotation covers a given substring in
  93. // file_content.
  94. bool AnnotationMatchesSubstring(const string& file_content,
  95. const GeneratedCodeInfo::Annotation* annotation,
  96. const string& expected_text);
  97. } // namespace annotation_test_util
  98. } // namespace compiler
  99. } // namespace protobuf
  100. } // namespace google
  101. #endif // GOOGLE_PROTOBUF_COMPILER_ANNOTATION_TEST_UTIL_H__