annotation_test_util.cc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. #include <google/protobuf/compiler/annotation_test_util.h>
  31. #include <memory>
  32. #include <google/protobuf/compiler/code_generator.h>
  33. #include <google/protobuf/compiler/command_line_interface.h>
  34. #include <google/protobuf/io/printer.h>
  35. #include <google/protobuf/io/zero_copy_stream.h>
  36. #include <google/protobuf/io/zero_copy_stream_impl_lite.h>
  37. #include <google/protobuf/descriptor.pb.h>
  38. #include <google/protobuf/testing/file.h>
  39. #include <google/protobuf/testing/file.h>
  40. #include <google/protobuf/testing/googletest.h>
  41. #include <gtest/gtest.h>
  42. namespace google {
  43. namespace protobuf {
  44. namespace compiler {
  45. namespace annotation_test_util {
  46. namespace {
  47. // A CodeGenerator that captures the FileDescriptor it's passed as a
  48. // FileDescriptorProto.
  49. class DescriptorCapturingGenerator : public CodeGenerator {
  50. public:
  51. // Does not own file; file must outlive the Generator.
  52. explicit DescriptorCapturingGenerator(FileDescriptorProto* file)
  53. : file_(file) {}
  54. virtual bool Generate(const FileDescriptor* file, const string& parameter,
  55. GeneratorContext* context, string* error) const {
  56. file->CopyTo(file_);
  57. return true;
  58. }
  59. private:
  60. FileDescriptorProto* file_;
  61. };
  62. } // namespace
  63. void AddFile(const string& filename, const string& data) {
  64. GOOGLE_CHECK_OK(File::SetContents(TestTempDir() + "/" + filename, data,
  65. true));
  66. }
  67. bool RunProtoCompiler(const string& filename,
  68. const string& plugin_specific_args,
  69. CommandLineInterface* cli, FileDescriptorProto* file) {
  70. cli->SetInputsAreProtoPathRelative(true);
  71. DescriptorCapturingGenerator capturing_generator(file);
  72. cli->RegisterGenerator("--capture_out", &capturing_generator, "");
  73. string proto_path = "-I" + TestTempDir();
  74. string capture_out = "--capture_out=" + TestTempDir();
  75. const char* argv[] = {"protoc", proto_path.c_str(),
  76. plugin_specific_args.c_str(), capture_out.c_str(),
  77. filename.c_str()};
  78. return cli->Run(5, argv) == 0;
  79. }
  80. bool DecodeMetadata(const string& path, GeneratedCodeInfo* info) {
  81. string data;
  82. GOOGLE_CHECK_OK(File::GetContents(path, &data, true));
  83. io::ArrayInputStream input(data.data(), data.size());
  84. return info->ParseFromZeroCopyStream(&input);
  85. }
  86. void FindAnnotationsOnPath(
  87. const GeneratedCodeInfo& info, const string& source_file,
  88. const std::vector<int>& path,
  89. std::vector<const GeneratedCodeInfo::Annotation*>* annotations) {
  90. for (int i = 0; i < info.annotation_size(); ++i) {
  91. const GeneratedCodeInfo::Annotation* annotation = &info.annotation(i);
  92. if (annotation->source_file() != source_file ||
  93. annotation->path_size() != path.size()) {
  94. continue;
  95. }
  96. int node = 0;
  97. for (; node < path.size(); ++node) {
  98. if (annotation->path(node) != path[node]) {
  99. break;
  100. }
  101. }
  102. if (node == path.size()) {
  103. annotations->push_back(annotation);
  104. }
  105. }
  106. }
  107. const GeneratedCodeInfo::Annotation* FindAnnotationOnPath(
  108. const GeneratedCodeInfo& info, const string& source_file,
  109. const std::vector<int>& path) {
  110. std::vector<const GeneratedCodeInfo::Annotation*> annotations;
  111. FindAnnotationsOnPath(info, source_file, path, &annotations);
  112. if (annotations.empty()) {
  113. return NULL;
  114. }
  115. return annotations[0];
  116. }
  117. bool AtLeastOneAnnotationMatchesSubstring(
  118. const string& file_content,
  119. const std::vector<const GeneratedCodeInfo::Annotation*>& annotations,
  120. const string& expected_text) {
  121. for (std::vector<const GeneratedCodeInfo::Annotation*>::const_iterator
  122. i = annotations.begin(),
  123. e = annotations.end();
  124. i != e; ++i) {
  125. const GeneratedCodeInfo::Annotation* annotation = *i;
  126. uint32 begin = annotation->begin();
  127. uint32 end = annotation->end();
  128. if (end < begin || end > file_content.size()) {
  129. return false;
  130. }
  131. if (file_content.substr(begin, end - begin) == expected_text) {
  132. return true;
  133. }
  134. }
  135. return false;
  136. }
  137. bool AnnotationMatchesSubstring(const string& file_content,
  138. const GeneratedCodeInfo::Annotation* annotation,
  139. const string& expected_text) {
  140. std::vector<const GeneratedCodeInfo::Annotation*> annotations;
  141. annotations.push_back(annotation);
  142. return AtLeastOneAnnotationMatchesSubstring(file_content, annotations,
  143. expected_text);
  144. }
  145. } // namespace annotation_test_util
  146. } // namespace compiler
  147. } // namespace protobuf
  148. } // namespace google