annotation_test_util.cc 6.7 KB

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