csharp_bootstrap_unittest.cc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. // This test insures that
  31. // csharp/src/Google.Protobuf/Reflection/Descriptor.cs match exactly
  32. // what would be generated by the protocol compiler. The file is not
  33. // generated automatically at build time.
  34. //
  35. // If this test fails, run the script
  36. // "generate_descriptor_proto.sh" and add the changed files under
  37. // csharp/src/ to your changelist.
  38. #include <map>
  39. #include <google/protobuf/compiler/csharp/csharp_generator.h>
  40. #include <google/protobuf/compiler/importer.h>
  41. #include <google/protobuf/descriptor.h>
  42. #include <google/protobuf/io/zero_copy_stream_impl.h>
  43. #include <google/protobuf/stubs/map_util.h>
  44. #include <google/protobuf/stubs/stl_util.h>
  45. #include <google/protobuf/stubs/strutil.h>
  46. #include <google/protobuf/stubs/substitute.h>
  47. #include <google/protobuf/testing/file.h>
  48. #include <google/protobuf/testing/file.h>
  49. #include <google/protobuf/testing/googletest.h>
  50. #include <gtest/gtest.h>
  51. namespace google {
  52. namespace protobuf {
  53. namespace compiler {
  54. namespace csharp {
  55. namespace {
  56. class MockErrorCollector : public MultiFileErrorCollector {
  57. public:
  58. MockErrorCollector() {}
  59. ~MockErrorCollector() {}
  60. string text_;
  61. // implements ErrorCollector ---------------------------------------
  62. void AddError(const string& filename, int line, int column,
  63. const string& message) {
  64. strings::SubstituteAndAppend(&text_, "$0:$1:$2: $3\n",
  65. filename, line, column, message);
  66. }
  67. };
  68. class MockGeneratorContext : public GeneratorContext {
  69. public:
  70. MockGeneratorContext() {}
  71. ~MockGeneratorContext() {
  72. STLDeleteValues(&files_);
  73. }
  74. void ExpectFileMatches(const string& virtual_filename,
  75. const string& physical_filename) {
  76. string* expected_contents = FindPtrOrNull(files_, virtual_filename);
  77. ASSERT_TRUE(expected_contents != NULL)
  78. << "Generator failed to generate file: " << virtual_filename;
  79. string actual_contents;
  80. GOOGLE_CHECK_OK(
  81. File::GetContents(TestSourceDir() + "/" + physical_filename,
  82. &actual_contents, true))
  83. << "Unable to get " << physical_filename;
  84. EXPECT_TRUE(actual_contents == *expected_contents)
  85. << physical_filename << " needs to be regenerated. Please run "
  86. "generate_descriptor_proto.sh. Then add this file "
  87. "to your CL.";
  88. }
  89. // implements GeneratorContext --------------------------------------
  90. virtual io::ZeroCopyOutputStream* Open(const string& filename) {
  91. string** map_slot = &files_[filename];
  92. delete *map_slot;
  93. *map_slot = new string;
  94. return new io::StringOutputStream(*map_slot);
  95. }
  96. private:
  97. std::map<string, string*> files_;
  98. };
  99. class GenerateAndTest {
  100. public:
  101. GenerateAndTest() {}
  102. void Run(const FileDescriptor* proto_file, string file1, string file2) {
  103. ASSERT_TRUE(proto_file != NULL) << TestSourceDir();
  104. ASSERT_TRUE(generator_.Generate(proto_file, parameter_,
  105. &context_, &error_));
  106. context_.ExpectFileMatches(file1, file2);
  107. }
  108. void SetParameter(string parameter) {
  109. parameter_ = parameter;
  110. }
  111. private:
  112. Generator generator_;
  113. MockGeneratorContext context_;
  114. string error_;
  115. string parameter_;
  116. };
  117. TEST(CsharpBootstrapTest, GeneratedCsharpDescriptorMatches) {
  118. // Skip this whole test if the csharp directory doesn't exist (i.e., a C++11
  119. // only distribution).
  120. string descriptor_file_name =
  121. "../csharp/src/Google.Protobuf/Reflection/Descriptor.cs";
  122. if (!File::Exists(TestSourceDir() + "/" + descriptor_file_name)) {
  123. return;
  124. }
  125. MockErrorCollector error_collector;
  126. DiskSourceTree source_tree;
  127. Importer importer(&source_tree, &error_collector);
  128. GenerateAndTest generate_test;
  129. generate_test.SetParameter("base_namespace=Google.Protobuf");
  130. source_tree.MapPath("", TestSourceDir());
  131. generate_test.Run(importer.Import("google/protobuf/descriptor.proto"),
  132. "Reflection/Descriptor.cs",
  133. "../csharp/src/Google.Protobuf/Reflection/Descriptor.cs");
  134. generate_test.Run(importer.Import("google/protobuf/any.proto"),
  135. "WellKnownTypes/Any.cs",
  136. "../csharp/src/Google.Protobuf/WellKnownTypes/Any.cs");
  137. generate_test.Run(importer.Import("google/protobuf/api.proto"),
  138. "WellKnownTypes/Api.cs",
  139. "../csharp/src/Google.Protobuf/WellKnownTypes/Api.cs");
  140. generate_test.Run(importer.Import("google/protobuf/duration.proto"),
  141. "WellKnownTypes/Duration.cs",
  142. "../csharp/src/Google.Protobuf/WellKnownTypes/Duration.cs");
  143. generate_test.Run(importer.Import("google/protobuf/empty.proto"),
  144. "WellKnownTypes/Empty.cs",
  145. "../csharp/src/Google.Protobuf/WellKnownTypes/Empty.cs");
  146. generate_test.Run(importer.Import("google/protobuf/field_mask.proto"),
  147. "WellKnownTypes/FieldMask.cs",
  148. "../csharp/src/Google.Protobuf/WellKnownTypes/FieldMask.cs");
  149. generate_test.Run(importer.Import("google/protobuf/source_context.proto"),
  150. "WellKnownTypes/SourceContext.cs",
  151. "../csharp/src/Google.Protobuf/WellKnownTypes/SourceContext.cs");
  152. generate_test.Run(importer.Import("google/protobuf/struct.proto"),
  153. "WellKnownTypes/Struct.cs",
  154. "../csharp/src/Google.Protobuf/WellKnownTypes/Struct.cs");
  155. generate_test.Run(importer.Import("google/protobuf/timestamp.proto"),
  156. "WellKnownTypes/Timestamp.cs",
  157. "../csharp/src/Google.Protobuf/WellKnownTypes/Timestamp.cs");
  158. generate_test.Run(importer.Import("google/protobuf/type.proto"),
  159. "WellKnownTypes/Type.cs",
  160. "../csharp/src/Google.Protobuf/WellKnownTypes/Type.cs");
  161. generate_test.Run(importer.Import("google/protobuf/wrappers.proto"),
  162. "WellKnownTypes/Wrappers.cs",
  163. "../csharp/src/Google.Protobuf/WellKnownTypes/Wrappers.cs");
  164. generate_test.SetParameter("");
  165. source_tree.MapPath("", TestSourceDir() + "/../conformance");
  166. generate_test.Run(importer.Import("conformance.proto"),
  167. "Conformance.cs",
  168. "../csharp/src/Google.Protobuf.Conformance/Conformance.cs");
  169. EXPECT_EQ("", error_collector.text_);
  170. }
  171. } // namespace
  172. } // namespace csharp
  173. } // namespace compiler
  174. } // namespace protobuf
  175. } // namespace google