OLD | NEW |
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 // https://developers.google.com/protocol-buffers/ | 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. |
(...skipping 22 matching lines...) Expand all Loading... |
33 #include <google/protobuf/compiler/mock_code_generator.h> | 33 #include <google/protobuf/compiler/mock_code_generator.h> |
34 | 34 |
35 #include <stdlib.h> | 35 #include <stdlib.h> |
36 #include <iostream> | 36 #include <iostream> |
37 #include <memory> | 37 #include <memory> |
38 #ifndef _SHARED_PTR_H | 38 #ifndef _SHARED_PTR_H |
39 #include <google/protobuf/stubs/shared_ptr.h> | 39 #include <google/protobuf/stubs/shared_ptr.h> |
40 #endif | 40 #endif |
41 #include <vector> | 41 #include <vector> |
42 | 42 |
| 43 #include <google/protobuf/compiler/plugin.pb.h> |
43 #include <google/protobuf/stubs/logging.h> | 44 #include <google/protobuf/stubs/logging.h> |
44 #include <google/protobuf/stubs/common.h> | 45 #include <google/protobuf/stubs/common.h> |
45 #include <google/protobuf/testing/file.h> | 46 #include <google/protobuf/testing/file.h> |
46 #include <google/protobuf/testing/file.h> | 47 #include <google/protobuf/testing/file.h> |
47 #include <google/protobuf/testing/file.h> | 48 #include <google/protobuf/testing/file.h> |
48 #include <google/protobuf/io/printer.h> | 49 #include <google/protobuf/io/printer.h> |
49 #include <google/protobuf/io/zero_copy_stream.h> | 50 #include <google/protobuf/io/zero_copy_stream.h> |
50 #include <google/protobuf/descriptor.pb.h> | 51 #include <google/protobuf/descriptor.pb.h> |
51 #include <google/protobuf/descriptor.h> | 52 #include <google/protobuf/descriptor.h> |
52 #include <google/protobuf/stubs/strutil.h> | 53 #include <google/protobuf/stubs/strutil.h> |
53 #include <google/protobuf/stubs/substitute.h> | 54 #include <google/protobuf/stubs/substitute.h> |
54 #include <gtest/gtest.h> | 55 #include <gtest/gtest.h> |
55 | 56 |
56 namespace google { | 57 namespace google { |
57 namespace protobuf { | 58 namespace protobuf { |
58 namespace compiler { | 59 namespace compiler { |
59 | 60 |
60 // Returns the list of the names of files in all_files in the form of a | 61 // Returns the list of the names of files in all_files in the form of a |
61 // comma-separated string. | 62 // comma-separated string. |
62 string CommaSeparatedList(const vector<const FileDescriptor*> all_files) { | 63 string CommaSeparatedList(const std::vector<const FileDescriptor*> all_files) { |
63 vector<string> names; | 64 std::vector<string> names; |
64 for (int i = 0; i < all_files.size(); i++) { | 65 for (size_t i = 0; i < all_files.size(); i++) { |
65 names.push_back(all_files[i]->name()); | 66 names.push_back(all_files[i]->name()); |
66 } | 67 } |
67 return Join(names, ","); | 68 return Join(names, ","); |
68 } | 69 } |
69 | 70 |
70 static const char* kFirstInsertionPointName = "first_mock_insertion_point"; | 71 static const char* kFirstInsertionPointName = "first_mock_insertion_point"; |
71 static const char* kSecondInsertionPointName = "second_mock_insertion_point"; | 72 static const char* kSecondInsertionPointName = "second_mock_insertion_point"; |
72 static const char* kFirstInsertionPoint = | 73 static const char* kFirstInsertionPoint = |
73 "# @@protoc_insertion_point(first_mock_insertion_point) is here\n"; | 74 "# @@protoc_insertion_point(first_mock_insertion_point) is here\n"; |
74 static const char* kSecondInsertionPoint = | 75 static const char* kSecondInsertionPoint = |
(...skipping 10 matching lines...) Expand all Loading... |
85 const string& insertions, | 86 const string& insertions, |
86 const string& file, | 87 const string& file, |
87 const string& first_message_name, | 88 const string& first_message_name, |
88 const string& first_parsed_file_name, | 89 const string& first_parsed_file_name, |
89 const string& output_directory) { | 90 const string& output_directory) { |
90 string content; | 91 string content; |
91 GOOGLE_CHECK_OK( | 92 GOOGLE_CHECK_OK( |
92 File::GetContents(output_directory + "/" + GetOutputFileName(name, file), | 93 File::GetContents(output_directory + "/" + GetOutputFileName(name, file), |
93 &content, true)); | 94 &content, true)); |
94 | 95 |
95 vector<string> lines = Split(content, "\n", true); | 96 std::vector<string> lines = |
| 97 Split(content, "\n", true); |
96 | 98 |
97 while (!lines.empty() && lines.back().empty()) { | 99 while (!lines.empty() && lines.back().empty()) { |
98 lines.pop_back(); | 100 lines.pop_back(); |
99 } | 101 } |
100 for (int i = 0; i < lines.size(); i++) { | 102 for (size_t i = 0; i < lines.size(); i++) { |
101 lines[i] += "\n"; | 103 lines[i] += "\n"; |
102 } | 104 } |
103 | 105 |
104 vector<string> insertion_list; | 106 std::vector<string> insertion_list; |
105 if (!insertions.empty()) { | 107 if (!insertions.empty()) { |
106 SplitStringUsing(insertions, ",", &insertion_list); | 108 SplitStringUsing(insertions, ",", &insertion_list); |
107 } | 109 } |
108 | 110 |
109 EXPECT_EQ(lines.size(), 3 + insertion_list.size() * 2); | 111 EXPECT_EQ(lines.size(), 3 + insertion_list.size() * 2); |
110 EXPECT_EQ(GetOutputFileContent(name, parameter, file, | 112 EXPECT_EQ(GetOutputFileContent(name, parameter, file, |
111 first_parsed_file_name, first_message_name), | 113 first_parsed_file_name, first_message_name), |
112 lines[0]); | 114 lines[0]); |
113 | 115 |
114 EXPECT_EQ(kFirstInsertionPoint, lines[1 + insertion_list.size()]); | 116 EXPECT_EQ(kFirstInsertionPoint, lines[1 + insertion_list.size()]); |
115 EXPECT_EQ(kSecondInsertionPoint, lines[2 + insertion_list.size() * 2]); | 117 EXPECT_EQ(kSecondInsertionPoint, lines[2 + insertion_list.size() * 2]); |
116 | 118 |
117 for (int i = 0; i < insertion_list.size(); i++) { | 119 for (size_t i = 0; i < insertion_list.size(); i++) { |
118 EXPECT_EQ(GetOutputFileContent(insertion_list[i], "first_insert", | 120 EXPECT_EQ(GetOutputFileContent(insertion_list[i], "first_insert", |
119 file, file, first_message_name), | 121 file, file, first_message_name), |
120 lines[1 + i]); | 122 lines[1 + i]); |
121 // Second insertion point is indented, so the inserted text should | 123 // Second insertion point is indented, so the inserted text should |
122 // automatically be indented too. | 124 // automatically be indented too. |
123 EXPECT_EQ(" " + GetOutputFileContent(insertion_list[i], "second_insert", | 125 EXPECT_EQ(" " + GetOutputFileContent(insertion_list[i], "second_insert", |
124 file, file, first_message_name), | 126 file, file, first_message_name), |
125 lines[2 + insertion_list.size() + i]); | 127 lines[2 + insertion_list.size() + i]); |
126 } | 128 } |
127 } | 129 } |
(...skipping 24 matching lines...) Expand all Loading... |
152 file_descriptor_proto.source_code_info().location_size() > 0; | 154 file_descriptor_proto.source_code_info().location_size() > 0; |
153 std::cerr << "Saw message type MockCodeGenerator_HasSourceCodeInfo: " | 155 std::cerr << "Saw message type MockCodeGenerator_HasSourceCodeInfo: " |
154 << has_source_code_info << "." << std::endl; | 156 << has_source_code_info << "." << std::endl; |
155 abort(); | 157 abort(); |
156 } else if (command == "HasJsonName") { | 158 } else if (command == "HasJsonName") { |
157 FieldDescriptorProto field_descriptor_proto; | 159 FieldDescriptorProto field_descriptor_proto; |
158 file->message_type(i)->field(0)->CopyTo(&field_descriptor_proto); | 160 file->message_type(i)->field(0)->CopyTo(&field_descriptor_proto); |
159 std::cerr << "Saw json_name: " | 161 std::cerr << "Saw json_name: " |
160 << field_descriptor_proto.has_json_name() << std::endl; | 162 << field_descriptor_proto.has_json_name() << std::endl; |
161 abort(); | 163 abort(); |
| 164 } else if (command == "ShowVersionNumber") { |
| 165 Version compiler_version; |
| 166 context->GetCompilerVersion(&compiler_version); |
| 167 std::cerr << "Saw compiler_version: " |
| 168 << compiler_version.major() * 1000000 + |
| 169 compiler_version.minor() * 1000 + |
| 170 compiler_version.patch() |
| 171 << " " << compiler_version.suffix() << std::endl; |
| 172 abort(); |
162 } else { | 173 } else { |
163 GOOGLE_LOG(FATAL) << "Unknown MockCodeGenerator command: " << command; | 174 GOOGLE_LOG(FATAL) << "Unknown MockCodeGenerator command: " << command; |
164 } | 175 } |
165 } | 176 } |
166 } | 177 } |
167 | 178 |
168 if (HasPrefixString(parameter, "insert=")) { | 179 if (HasPrefixString(parameter, "insert=")) { |
169 vector<string> insert_into; | 180 std::vector<string> insert_into; |
170 SplitStringUsing(StripPrefixString(parameter, "insert="), | 181 SplitStringUsing(StripPrefixString(parameter, "insert="), |
171 ",", &insert_into); | 182 ",", &insert_into); |
172 | 183 |
173 for (int i = 0; i < insert_into.size(); i++) { | 184 for (size_t i = 0; i < insert_into.size(); i++) { |
174 { | 185 { |
175 google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> output(context->O
penForInsert( | 186 google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> output(context->O
penForInsert( |
176 GetOutputFileName(insert_into[i], file), kFirstInsertionPointName)); | 187 GetOutputFileName(insert_into[i], file), kFirstInsertionPointName)); |
177 io::Printer printer(output.get(), '$'); | 188 io::Printer printer(output.get(), '$'); |
178 printer.PrintRaw(GetOutputFileContent(name_, "first_insert", | 189 printer.PrintRaw(GetOutputFileContent(name_, "first_insert", |
179 file, context)); | 190 file, context)); |
180 if (printer.failed()) { | 191 if (printer.failed()) { |
181 *error = "MockCodeGenerator detected write error."; | 192 *error = "MockCodeGenerator detected write error."; |
182 return false; | 193 return false; |
183 } | 194 } |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
223 string MockCodeGenerator::GetOutputFileName(const string& generator_name, | 234 string MockCodeGenerator::GetOutputFileName(const string& generator_name, |
224 const string& file) { | 235 const string& file) { |
225 return file + ".MockCodeGenerator." + generator_name; | 236 return file + ".MockCodeGenerator." + generator_name; |
226 } | 237 } |
227 | 238 |
228 string MockCodeGenerator::GetOutputFileContent( | 239 string MockCodeGenerator::GetOutputFileContent( |
229 const string& generator_name, | 240 const string& generator_name, |
230 const string& parameter, | 241 const string& parameter, |
231 const FileDescriptor* file, | 242 const FileDescriptor* file, |
232 GeneratorContext *context) { | 243 GeneratorContext *context) { |
233 vector<const FileDescriptor*> all_files; | 244 std::vector<const FileDescriptor*> all_files; |
234 context->ListParsedFiles(&all_files); | 245 context->ListParsedFiles(&all_files); |
235 return GetOutputFileContent( | 246 return GetOutputFileContent( |
236 generator_name, parameter, file->name(), | 247 generator_name, parameter, file->name(), |
237 CommaSeparatedList(all_files), | 248 CommaSeparatedList(all_files), |
238 file->message_type_count() > 0 ? | 249 file->message_type_count() > 0 ? |
239 file->message_type(0)->name() : "(none)"); | 250 file->message_type(0)->name() : "(none)"); |
240 } | 251 } |
241 | 252 |
242 string MockCodeGenerator::GetOutputFileContent( | 253 string MockCodeGenerator::GetOutputFileContent( |
243 const string& generator_name, | 254 const string& generator_name, |
244 const string& parameter, | 255 const string& parameter, |
245 const string& file, | 256 const string& file, |
246 const string& parsed_file_list, | 257 const string& parsed_file_list, |
247 const string& first_message_name) { | 258 const string& first_message_name) { |
248 return strings::Substitute("$0: $1, $2, $3, $4\n", | 259 return strings::Substitute("$0: $1, $2, $3, $4\n", |
249 generator_name, parameter, file, | 260 generator_name, parameter, file, |
250 first_message_name, parsed_file_list); | 261 first_message_name, parsed_file_list); |
251 } | 262 } |
252 | 263 |
253 } // namespace compiler | 264 } // namespace compiler |
254 } // namespace protobuf | 265 } // namespace protobuf |
255 } // namespace google | 266 } // namespace google |
OLD | NEW |