Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(46)

Side by Side Diff: third_party/protobuf/src/google/protobuf/compiler/mock_code_generator.cc

Issue 1291903002: Pull new version of protobuf sources. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Protocol Buffers - Google's data interchange format 1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved. 2 // Copyright 2008 Google Inc. All rights reserved.
3 // http://code.google.com/p/protobuf/ 3 // https://developers.google.com/protocol-buffers/
4 // 4 //
5 // Redistribution and use in source and binary forms, with or without 5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are 6 // modification, are permitted provided that the following conditions are
7 // met: 7 // met:
8 // 8 //
9 // * Redistributions of source code must retain the above copyright 9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer. 10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above 11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer 12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the 13 // in the documentation and/or other materials provided with the
(...skipping 11 matching lines...) Expand all
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 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. 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 30
31 // Author: kenton@google.com (Kenton Varda) 31 // Author: kenton@google.com (Kenton Varda)
32 32
33 #include <google/protobuf/compiler/mock_code_generator.h> 33 #include <google/protobuf/compiler/mock_code_generator.h>
34 34
35 #include <memory>
36 #ifndef _SHARED_PTR_H
37 #include <google/protobuf/stubs/shared_ptr.h>
38 #endif
39
35 #include <google/protobuf/testing/file.h> 40 #include <google/protobuf/testing/file.h>
36 #include <google/protobuf/descriptor.pb.h> 41 #include <google/protobuf/descriptor.pb.h>
37 #include <google/protobuf/descriptor.h> 42 #include <google/protobuf/descriptor.h>
38 #include <google/protobuf/io/zero_copy_stream.h> 43 #include <google/protobuf/io/zero_copy_stream.h>
39 #include <google/protobuf/io/printer.h> 44 #include <google/protobuf/io/printer.h>
40 #include <google/protobuf/stubs/strutil.h> 45 #include <google/protobuf/stubs/strutil.h>
41 #include <google/protobuf/stubs/substitute.h> 46 #include <google/protobuf/stubs/substitute.h>
42 #include <gtest/gtest.h> 47 #include <gtest/gtest.h>
43 #include <google/protobuf/stubs/stl_util.h> 48 #include <google/protobuf/stubs/stl_util.h>
44 49
45 namespace google { 50 namespace google {
46 namespace protobuf { 51 namespace protobuf {
47 namespace compiler { 52 namespace compiler {
48 53
49 // Returns the list of the names of files in all_files in the form of a 54 // Returns the list of the names of files in all_files in the form of a
50 // comma-separated string. 55 // comma-separated string.
51 string CommaSeparatedList(const vector<const FileDescriptor*> all_files) { 56 string CommaSeparatedList(const vector<const FileDescriptor*> all_files) {
52 vector<string> names; 57 vector<string> names;
53 for (int i = 0; i < all_files.size(); i++) { 58 for (int i = 0; i < all_files.size(); i++) {
54 names.push_back(all_files[i]->name()); 59 names.push_back(all_files[i]->name());
55 } 60 }
56 return JoinStrings(names, ","); 61 return Join(names, ",");
57 } 62 }
58 63
59 static const char* kFirstInsertionPointName = "first_mock_insertion_point"; 64 static const char* kFirstInsertionPointName = "first_mock_insertion_point";
60 static const char* kSecondInsertionPointName = "second_mock_insertion_point"; 65 static const char* kSecondInsertionPointName = "second_mock_insertion_point";
61 static const char* kFirstInsertionPoint = 66 static const char* kFirstInsertionPoint =
62 "# @@protoc_insertion_point(first_mock_insertion_point) is here\n"; 67 "# @@protoc_insertion_point(first_mock_insertion_point) is here\n";
63 static const char* kSecondInsertionPoint = 68 static const char* kSecondInsertionPoint =
64 " # @@protoc_insertion_point(second_mock_insertion_point) is here\n"; 69 " # @@protoc_insertion_point(second_mock_insertion_point) is here\n";
65 70
66 MockCodeGenerator::MockCodeGenerator(const string& name) 71 MockCodeGenerator::MockCodeGenerator(const string& name)
67 : name_(name) {} 72 : name_(name) {}
68 73
69 MockCodeGenerator::~MockCodeGenerator() {} 74 MockCodeGenerator::~MockCodeGenerator() {}
70 75
71 void MockCodeGenerator::ExpectGenerated( 76 void MockCodeGenerator::ExpectGenerated(
72 const string& name, 77 const string& name,
73 const string& parameter, 78 const string& parameter,
74 const string& insertions, 79 const string& insertions,
75 const string& file, 80 const string& file,
76 const string& first_message_name, 81 const string& first_message_name,
77 const string& first_parsed_file_name, 82 const string& first_parsed_file_name,
78 const string& output_directory) { 83 const string& output_directory) {
79 string content; 84 string content;
80 ASSERT_TRUE(File::ReadFileToString( 85 GOOGLE_CHECK_OK(
81 output_directory + "/" + GetOutputFileName(name, file), &content)); 86 File::GetContents(output_directory + "/" + GetOutputFileName(name, file),
87 &content, true));
82 88
83 vector<string> lines; 89 vector<string> lines = Split(content, "\n", true);
84 SplitStringUsing(content, "\n", &lines);
85 90
86 while (!lines.empty() && lines.back().empty()) { 91 while (!lines.empty() && lines.back().empty()) {
87 lines.pop_back(); 92 lines.pop_back();
88 } 93 }
89 for (int i = 0; i < lines.size(); i++) { 94 for (int i = 0; i < lines.size(); i++) {
90 lines[i] += "\n"; 95 lines[i] += "\n";
91 } 96 }
92 97
93 vector<string> insertion_list; 98 vector<string> insertion_list;
94 if (!insertions.empty()) { 99 if (!insertions.empty()) {
95 SplitStringUsing(insertions, ",", &insertion_list); 100 SplitStringUsing(insertions, ",", &insertion_list);
96 } 101 }
97 102
98 ASSERT_EQ(lines.size(), 3 + insertion_list.size() * 2); 103 EXPECT_EQ(lines.size(), 3 + insertion_list.size() * 2);
99 EXPECT_EQ(GetOutputFileContent(name, parameter, file, 104 EXPECT_EQ(GetOutputFileContent(name, parameter, file,
100 first_parsed_file_name, first_message_name), 105 first_parsed_file_name, first_message_name),
101 lines[0]); 106 lines[0]);
102 107
103 EXPECT_EQ(kFirstInsertionPoint, lines[1 + insertion_list.size()]); 108 EXPECT_EQ(kFirstInsertionPoint, lines[1 + insertion_list.size()]);
104 EXPECT_EQ(kSecondInsertionPoint, lines[2 + insertion_list.size() * 2]); 109 EXPECT_EQ(kSecondInsertionPoint, lines[2 + insertion_list.size() * 2]);
105 110
106 for (int i = 0; i < insertion_list.size(); i++) { 111 for (int i = 0; i < insertion_list.size(); i++) {
107 EXPECT_EQ(GetOutputFileContent(insertion_list[i], "first_insert", 112 EXPECT_EQ(GetOutputFileContent(insertion_list[i], "first_insert",
108 file, file, first_message_name), 113 file, file, first_message_name),
(...skipping 12 matching lines...) Expand all
121 GeneratorContext* context, 126 GeneratorContext* context,
122 string* error) const { 127 string* error) const {
123 for (int i = 0; i < file->message_type_count(); i++) { 128 for (int i = 0; i < file->message_type_count(); i++) {
124 if (HasPrefixString(file->message_type(i)->name(), "MockCodeGenerator_")) { 129 if (HasPrefixString(file->message_type(i)->name(), "MockCodeGenerator_")) {
125 string command = StripPrefixString(file->message_type(i)->name(), 130 string command = StripPrefixString(file->message_type(i)->name(),
126 "MockCodeGenerator_"); 131 "MockCodeGenerator_");
127 if (command == "Error") { 132 if (command == "Error") {
128 *error = "Saw message type MockCodeGenerator_Error."; 133 *error = "Saw message type MockCodeGenerator_Error.";
129 return false; 134 return false;
130 } else if (command == "Exit") { 135 } else if (command == "Exit") {
131 cerr << "Saw message type MockCodeGenerator_Exit." << endl; 136 std::cerr << "Saw message type MockCodeGenerator_Exit." << std::endl;
132 exit(123); 137 exit(123);
133 } else if (command == "Abort") { 138 } else if (command == "Abort") {
134 cerr << "Saw message type MockCodeGenerator_Abort." << endl; 139 std::cerr << "Saw message type MockCodeGenerator_Abort." << std::endl;
135 abort(); 140 abort();
136 } else if (command == "HasSourceCodeInfo") { 141 } else if (command == "HasSourceCodeInfo") {
137 FileDescriptorProto file_descriptor_proto; 142 FileDescriptorProto file_descriptor_proto;
138 file->CopySourceCodeInfoTo(&file_descriptor_proto); 143 file->CopySourceCodeInfoTo(&file_descriptor_proto);
139 bool has_source_code_info = 144 bool has_source_code_info =
140 file_descriptor_proto.has_source_code_info() && 145 file_descriptor_proto.has_source_code_info() &&
141 file_descriptor_proto.source_code_info().location_size() > 0; 146 file_descriptor_proto.source_code_info().location_size() > 0;
142 cerr << "Saw message type MockCodeGenerator_HasSourceCodeInfo: " 147 std::cerr << "Saw message type MockCodeGenerator_HasSourceCodeInfo: "
143 << has_source_code_info << "." << endl; 148 << has_source_code_info << "." << std::endl;
144 abort(); 149 abort();
145 } else { 150 } else {
146 GOOGLE_LOG(FATAL) << "Unknown MockCodeGenerator command: " << command; 151 GOOGLE_LOG(FATAL) << "Unknown MockCodeGenerator command: " << command;
147 } 152 }
148 } 153 }
149 } 154 }
150 155
151 if (HasPrefixString(parameter, "insert=")) { 156 if (HasPrefixString(parameter, "insert=")) {
152 vector<string> insert_into; 157 vector<string> insert_into;
153 SplitStringUsing(StripPrefixString(parameter, "insert="), 158 SplitStringUsing(StripPrefixString(parameter, "insert="),
154 ",", &insert_into); 159 ",", &insert_into);
155 160
156 for (int i = 0; i < insert_into.size(); i++) { 161 for (int i = 0; i < insert_into.size(); i++) {
157 { 162 {
158 scoped_ptr<io::ZeroCopyOutputStream> output( 163 google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> output(context->O penForInsert(
159 context->OpenForInsert( 164 GetOutputFileName(insert_into[i], file), kFirstInsertionPointName));
160 GetOutputFileName(insert_into[i], file),
161 kFirstInsertionPointName));
162 io::Printer printer(output.get(), '$'); 165 io::Printer printer(output.get(), '$');
163 printer.PrintRaw(GetOutputFileContent(name_, "first_insert", 166 printer.PrintRaw(GetOutputFileContent(name_, "first_insert",
164 file, context)); 167 file, context));
165 if (printer.failed()) { 168 if (printer.failed()) {
166 *error = "MockCodeGenerator detected write error."; 169 *error = "MockCodeGenerator detected write error.";
167 return false; 170 return false;
168 } 171 }
169 } 172 }
170 173
171 { 174 {
172 scoped_ptr<io::ZeroCopyOutputStream> output( 175 google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> output(
173 context->OpenForInsert( 176 context->OpenForInsert(GetOutputFileName(insert_into[i], file),
174 GetOutputFileName(insert_into[i], file), 177 kSecondInsertionPointName));
175 kSecondInsertionPointName));
176 io::Printer printer(output.get(), '$'); 178 io::Printer printer(output.get(), '$');
177 printer.PrintRaw(GetOutputFileContent(name_, "second_insert", 179 printer.PrintRaw(GetOutputFileContent(name_, "second_insert",
178 file, context)); 180 file, context));
179 if (printer.failed()) { 181 if (printer.failed()) {
180 *error = "MockCodeGenerator detected write error."; 182 *error = "MockCodeGenerator detected write error.";
181 return false; 183 return false;
182 } 184 }
183 } 185 }
184 } 186 }
185 } else { 187 } else {
186 scoped_ptr<io::ZeroCopyOutputStream> output( 188 google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> output(
187 context->Open(GetOutputFileName(name_, file))); 189 context->Open(GetOutputFileName(name_, file)));
188 190
189 io::Printer printer(output.get(), '$'); 191 io::Printer printer(output.get(), '$');
190 printer.PrintRaw(GetOutputFileContent(name_, parameter, 192 printer.PrintRaw(GetOutputFileContent(name_, parameter,
191 file, context)); 193 file, context));
192 printer.PrintRaw(kFirstInsertionPoint); 194 printer.PrintRaw(kFirstInsertionPoint);
193 printer.PrintRaw(kSecondInsertionPoint); 195 printer.PrintRaw(kSecondInsertionPoint);
194 196
195 if (printer.failed()) { 197 if (printer.failed()) {
196 *error = "MockCodeGenerator detected write error."; 198 *error = "MockCodeGenerator detected write error.";
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
232 const string& parsed_file_list, 234 const string& parsed_file_list,
233 const string& first_message_name) { 235 const string& first_message_name) {
234 return strings::Substitute("$0: $1, $2, $3, $4\n", 236 return strings::Substitute("$0: $1, $2, $3, $4\n",
235 generator_name, parameter, file, 237 generator_name, parameter, file,
236 first_message_name, parsed_file_list); 238 first_message_name, parsed_file_list);
237 } 239 }
238 240
239 } // namespace compiler 241 } // namespace compiler
240 } // namespace protobuf 242 } // namespace protobuf
241 } // namespace google 243 } // namespace google
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698