csharp_bootstrap_unittest.cc
Go to the documentation of this file.
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 
50 
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:
65  MockErrorCollector() {}
66  ~MockErrorCollector() {}
67 
68  string text_;
69 
70  // implements ErrorCollector ---------------------------------------
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:
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;
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 
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:
112  GenerateAndTest() {}
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  }
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 
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
context_
MockGeneratorContext context_
Definition: csharp_bootstrap_unittest.cc:125
map_util.h
zero_copy_stream_impl.h
NULL
NULL
Definition: test_security_zap.cpp:405
text_
string text_
Definition: csharp_bootstrap_unittest.cc:68
gtest.h
google::protobuf::TestSourceDir
string TestSourceDir()
Definition: googletest.cc:73
FileDescriptor
Definition: ruby/ext/google/protobuf_c/protobuf.h:125
EXPECT_EQ
#define EXPECT_EQ(val1, val2)
Definition: glog/src/googletest.h:155
importer.h
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
google::protobuf::File::GetContentsAsText
static bool GetContentsAsText(const string &name, string *output, bool)
Definition: file.h:88
strutil.h
generator_
Generator generator_
Definition: csharp_bootstrap_unittest.cc:124
parameter_
string parameter_
Definition: csharp_bootstrap_unittest.cc:127
google::protobuf::TEST
TEST(ArenaTest, ArenaConstructable)
Definition: arena_unittest.cc:156
EXPECT_TRUE
#define EXPECT_TRUE(cond)
Definition: glog/src/googletest.h:137
ASSERT_TRUE
#define ASSERT_TRUE(condition)
Definition: gtest.h:1995
csharp_generator.h
error_
string error_
Definition: csharp_bootstrap_unittest.cc:126
googletest-break-on-failure-unittest.Run
def Run(command)
Definition: googletest-break-on-failure-unittest.py:76
stl_util.h
GOOGLE_CHECK_OK
#define GOOGLE_CHECK_OK(A)
Definition: logging.h:155
googletest.h
descriptor.h
files_
std::map< std::string, std::unique_ptr< std::string > > files_
Definition: csharp_bootstrap_unittest.cc:107
substitute.h
google::protobuf::compiler::MultiFileErrorCollector
Definition: importer.h:194
file.h
it
MapIter it
Definition: php/ext/google/protobuf/map.c:205
compiler
Definition: plugin.pb.cc:22
google
Definition: data_proto2_to_proto3_util.h:11
message
GLenum GLuint GLenum GLsizei const GLchar * message
Definition: glcorearb.h:2695
google::protobuf::strings::SubstituteAndAppend
void SubstituteAndAppend(string *output, const char *format, const SubstituteArg &arg0, const SubstituteArg &arg1, const SubstituteArg &arg2, const SubstituteArg &arg3, const SubstituteArg &arg4, const SubstituteArg &arg5, const SubstituteArg &arg6, const SubstituteArg &arg7, const SubstituteArg &arg8, const SubstituteArg &arg9)
Definition: substitute.cc:68
google::protobuf::File::Exists
static bool Exists(const string &name)
Definition: file.cc:69


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:49