| 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 // 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 Loading... |
| 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 Loading... |
| 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; |
| 149 abort(); |
| 150 } else if (command == "HasJsonName") { |
| 151 FieldDescriptorProto field_descriptor_proto; |
| 152 file->message_type(i)->field(0)->CopyTo(&field_descriptor_proto); |
| 153 std::cerr << "Saw json_name: " |
| 154 << field_descriptor_proto.has_json_name() << std::endl; |
| 144 abort(); | 155 abort(); |
| 145 } else { | 156 } else { |
| 146 GOOGLE_LOG(FATAL) << "Unknown MockCodeGenerator command: " << command; | 157 GOOGLE_LOG(FATAL) << "Unknown MockCodeGenerator command: " << command; |
| 147 } | 158 } |
| 148 } | 159 } |
| 149 } | 160 } |
| 150 | 161 |
| 151 if (HasPrefixString(parameter, "insert=")) { | 162 if (HasPrefixString(parameter, "insert=")) { |
| 152 vector<string> insert_into; | 163 vector<string> insert_into; |
| 153 SplitStringUsing(StripPrefixString(parameter, "insert="), | 164 SplitStringUsing(StripPrefixString(parameter, "insert="), |
| 154 ",", &insert_into); | 165 ",", &insert_into); |
| 155 | 166 |
| 156 for (int i = 0; i < insert_into.size(); i++) { | 167 for (int i = 0; i < insert_into.size(); i++) { |
| 157 { | 168 { |
| 158 scoped_ptr<io::ZeroCopyOutputStream> output( | 169 google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> output(context->O
penForInsert( |
| 159 context->OpenForInsert( | 170 GetOutputFileName(insert_into[i], file), kFirstInsertionPointName)); |
| 160 GetOutputFileName(insert_into[i], file), | |
| 161 kFirstInsertionPointName)); | |
| 162 io::Printer printer(output.get(), '$'); | 171 io::Printer printer(output.get(), '$'); |
| 163 printer.PrintRaw(GetOutputFileContent(name_, "first_insert", | 172 printer.PrintRaw(GetOutputFileContent(name_, "first_insert", |
| 164 file, context)); | 173 file, context)); |
| 165 if (printer.failed()) { | 174 if (printer.failed()) { |
| 166 *error = "MockCodeGenerator detected write error."; | 175 *error = "MockCodeGenerator detected write error."; |
| 167 return false; | 176 return false; |
| 168 } | 177 } |
| 169 } | 178 } |
| 170 | 179 |
| 171 { | 180 { |
| 172 scoped_ptr<io::ZeroCopyOutputStream> output( | 181 google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> output( |
| 173 context->OpenForInsert( | 182 context->OpenForInsert(GetOutputFileName(insert_into[i], file), |
| 174 GetOutputFileName(insert_into[i], file), | 183 kSecondInsertionPointName)); |
| 175 kSecondInsertionPointName)); | |
| 176 io::Printer printer(output.get(), '$'); | 184 io::Printer printer(output.get(), '$'); |
| 177 printer.PrintRaw(GetOutputFileContent(name_, "second_insert", | 185 printer.PrintRaw(GetOutputFileContent(name_, "second_insert", |
| 178 file, context)); | 186 file, context)); |
| 179 if (printer.failed()) { | 187 if (printer.failed()) { |
| 180 *error = "MockCodeGenerator detected write error."; | 188 *error = "MockCodeGenerator detected write error."; |
| 181 return false; | 189 return false; |
| 182 } | 190 } |
| 183 } | 191 } |
| 184 } | 192 } |
| 185 } else { | 193 } else { |
| 186 scoped_ptr<io::ZeroCopyOutputStream> output( | 194 google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> output( |
| 187 context->Open(GetOutputFileName(name_, file))); | 195 context->Open(GetOutputFileName(name_, file))); |
| 188 | 196 |
| 189 io::Printer printer(output.get(), '$'); | 197 io::Printer printer(output.get(), '$'); |
| 190 printer.PrintRaw(GetOutputFileContent(name_, parameter, | 198 printer.PrintRaw(GetOutputFileContent(name_, parameter, |
| 191 file, context)); | 199 file, context)); |
| 192 printer.PrintRaw(kFirstInsertionPoint); | 200 printer.PrintRaw(kFirstInsertionPoint); |
| 193 printer.PrintRaw(kSecondInsertionPoint); | 201 printer.PrintRaw(kSecondInsertionPoint); |
| 194 | 202 |
| 195 if (printer.failed()) { | 203 if (printer.failed()) { |
| 196 *error = "MockCodeGenerator detected write error."; | 204 *error = "MockCodeGenerator detected write error."; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 const string& parsed_file_list, | 240 const string& parsed_file_list, |
| 233 const string& first_message_name) { | 241 const string& first_message_name) { |
| 234 return strings::Substitute("$0: $1, $2, $3, $4\n", | 242 return strings::Substitute("$0: $1, $2, $3, $4\n", |
| 235 generator_name, parameter, file, | 243 generator_name, parameter, file, |
| 236 first_message_name, parsed_file_list); | 244 first_message_name, parsed_file_list); |
| 237 } | 245 } |
| 238 | 246 |
| 239 } // namespace compiler | 247 } // namespace compiler |
| 240 } // namespace protobuf | 248 } // namespace protobuf |
| 241 } // namespace google | 249 } // namespace google |
| OLD | NEW |