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