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