cpp_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 // Author: kenton@google.com (Kenton Varda)
32 // Based on original Protocol Buffers design by
33 // Sanjay Ghemawat, Jeff Dean, and others.
34 //
35 // This test insures that net/proto2/proto/descriptor.pb.{h,cc} match exactly
36 // what would be generated by the protocol compiler. These files are not
37 // generated automatically at build time because they are compiled into the
38 // protocol compiler itself. So, if they were auto-generated, you'd have a
39 // chicken-and-egg problem.
40 //
41 // If this test fails, run the script
42 // "generate_descriptor_proto.sh" and add
43 // descriptor.pb.{h,cc} to your changelist.
44 
45 #include <map>
46 
57 
61 #include <gtest/gtest.h>
62 
63 namespace google {
64 namespace protobuf {
65 namespace compiler {
66 namespace cpp {
67 
68 namespace {
69 
70 class MockErrorCollector : public MultiFileErrorCollector {
71  public:
72  MockErrorCollector() {}
73  ~MockErrorCollector() {}
74 
76 
77  // implements ErrorCollector ---------------------------------------
78  void AddError(const std::string& filename, int line, int column,
79  const std::string& message) {
80  strings::SubstituteAndAppend(&text_, "$0:$1:$2: $3\n", filename, line,
81  column, message);
82  }
83 };
84 
85 class MockGeneratorContext : public GeneratorContext {
86  public:
87  MockGeneratorContext() {}
88  ~MockGeneratorContext() { STLDeleteValues(&files_); }
89 
90  void ExpectFileMatches(const std::string& virtual_filename,
91  const std::string& physical_filename) {
92  std::string* expected_contents =
93  FindPtrOrNull(files_, virtual_filename);
94  ASSERT_TRUE(expected_contents != NULL)
95  << "Generator failed to generate file: " << virtual_filename;
96 
97  std::string actual_contents;
99  File::GetContents(TestUtil::TestSourceDir() + "/" + physical_filename,
100  &actual_contents, true))
101  << physical_filename;
102  CleanStringLineEndings(&actual_contents, false);
103 
104 #ifdef WRITE_FILES // Define to debug mismatched files.
105  GOOGLE_CHECK_OK(File::SetContents("/tmp/expected.cc", *expected_contents,
106  true));
108  File::SetContents("/tmp/actual.cc", actual_contents, true));
109 #endif
110 
111  ASSERT_EQ(*expected_contents, actual_contents)
112  << physical_filename
113  << " needs to be regenerated. Please run "
114  "generate_descriptor_proto.sh. "
115  "Then add this file to your CL.";
116  }
117 
118  // implements GeneratorContext --------------------------------------
119 
120  virtual io::ZeroCopyOutputStream* Open(const std::string& filename) {
121  std::string** map_slot = &files_[filename];
122  delete *map_slot;
123  *map_slot = new std::string;
124 
125  return new io::StringOutputStream(*map_slot);
126  }
127 
128  private:
129  std::map<std::string, std::string*> files_;
130 };
131 
132 const char kDescriptorParameter[] = "dllexport_decl=PROTOBUF_EXPORT";
133 const char kPluginParameter[] = "dllexport_decl=PROTOC_EXPORT";
134 
135 
136 const char* test_protos[][2] = {
137  {"google/protobuf/descriptor", kDescriptorParameter},
138  {"google/protobuf/compiler/plugin", kPluginParameter},
139 };
140 
141 TEST(BootstrapTest, GeneratedFilesMatch) {
142  // We need a mapping from the actual file to virtual and actual path
143  // of the data to compare to.
144  std::map<std::string, std::string> vpath_map;
145  std::map<std::string, std::string> rpath_map;
146  rpath_map
147  ["third_party/protobuf_legacy_opensource/src/google/protobuf/"
148  "test_messages_proto2"] =
149  "net/proto2/z_generated_example/test_messages_proto2";
150  rpath_map
151  ["third_party/protobuf_legacy_opensource/src/google/protobuf/"
152  "test_messages_proto3"] =
153  "net/proto2/z_generated_example/test_messages_proto3";
154  rpath_map["net/proto2/internal/proto2_weak"] =
155  "net/proto2/z_generated_example/proto2_weak";
156 
157  DiskSourceTree source_tree;
158  source_tree.MapPath("", TestUtil::TestSourceDir());
159 
160  for (auto file_parameter : test_protos) {
161  MockErrorCollector error_collector;
162  Importer importer(&source_tree, &error_collector);
163  const FileDescriptor* file =
164  importer.Import(file_parameter[0] + std::string(".proto"));
165  ASSERT_TRUE(file != nullptr)
166  << "Can't import file " << file_parameter[0] + string(".proto") << "\n";
167  EXPECT_EQ("", error_collector.text_);
168  CppGenerator generator;
169  MockGeneratorContext context;
170 #ifdef GOOGLE_PROTOBUF_RUNTIME_INCLUDE_BASE
171  generator.set_opensource_runtime(true);
172  generator.set_runtime_include_base(GOOGLE_PROTOBUF_RUNTIME_INCLUDE_BASE);
173 #endif
175  ASSERT_TRUE(generator.Generate(file, file_parameter[1], &context, &error));
176 
177  std::string vpath =
178  FindWithDefault(vpath_map, file_parameter[0], file_parameter[0]);
179  std::string rpath =
180  FindWithDefault(rpath_map, file_parameter[0], file_parameter[0]);
181  context.ExpectFileMatches(vpath + ".pb.cc", rpath + ".pb.cc");
182  context.ExpectFileMatches(vpath + ".pb.h", rpath + ".pb.h");
183  }
184 }
185 
186 } // namespace
187 
188 } // namespace cpp
189 } // namespace compiler
190 } // namespace protobuf
191 } // namespace google
google::protobuf::FindWithDefault
const Collection::value_type::second_type & FindWithDefault(const Collection &collection, const typename Collection::value_type::first_type &key, const typename Collection::value_type::second_type &value)
Definition: map_util.h:123
google::protobuf::File::SetContents
static bool SetContents(const string &name, const string &contents, bool)
Definition: file.h:93
map_util.h
zero_copy_stream_impl.h
NULL
NULL
Definition: test_security_zap.cpp:405
gtest.h
google::protobuf::FindPtrOrNull
Collection::value_type::second_type FindPtrOrNull(const Collection &collection, const typename Collection::value_type::first_type &key)
Definition: map_util.h:166
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
cpp_generator.h
text_
std::string text_
Definition: cpp_bootstrap_unittest.cc:75
error
Definition: cJSON.c:88
test_util2.h
ASSERT_EQ
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:2082
cpp_helpers.h
strutil.h
google::protobuf::File::GetContents
static bool GetContents(const string &name, string *output, bool)
Definition: file.h:83
files_
std::map< std::string, std::string * > files_
Definition: cpp_bootstrap_unittest.cc:129
google::protobuf::TEST
TEST(ArenaTest, ArenaConstructable)
Definition: arena_unittest.cc:156
cpp
Definition: third_party/googletest/googlemock/scripts/generator/cpp/__init__.py:1
ASSERT_TRUE
#define ASSERT_TRUE(condition)
Definition: gtest.h:1995
stl_util.h
GOOGLE_CHECK_OK
#define GOOGLE_CHECK_OK(A)
Definition: logging.h:155
googletest.h
google::protobuf::CleanStringLineEndings
void CleanStringLineEndings(const string &src, string *dst, bool auto_end_last_line)
Definition: strutil.cc:2341
descriptor.h
substitute.h
google::protobuf::compiler::MultiFileErrorCollector
Definition: importer.h:194
file.h
google::protobuf::STLDeleteValues
void STLDeleteValues(T *v)
Definition: stl_util.h:110
google::protobuf::TestUtil::TestSourceDir
std::string TestSourceDir()
Definition: test_util2.h:62
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


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