1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // http://code.google.com/p/protobuf/
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
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
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.
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.
31 // Author: kenton@google.com (Kenton Varda)
33 #include <google/protobuf/compiler/mock_code_generator.h>
35 #include <google/protobuf/testing/file.h>
36 #include <google/protobuf/descriptor.pb.h>
37 #include <google/protobuf/descriptor.h>
38 #include <google/protobuf/io/zero_copy_stream.h>
39 #include <google/protobuf/io/printer.h>
40 #include <google/protobuf/stubs/strutil.h>
41 #include <google/protobuf/stubs/substitute.h>
42 #include <gtest/gtest.h>
43 #include <google/protobuf/stubs/stl_util.h>
49 // Returns the list of the names of files in all_files in the form of a
50 // comma-separated string.
51 string CommaSeparatedList(const vector<const FileDescriptor*> all_files) {
53 for (int i = 0; i < all_files.size(); i++) {
54 names.push_back(all_files[i]->name());
56 return JoinStrings(names, ",");
59 static const char* kFirstInsertionPointName = "first_mock_insertion_point";
60 static const char* kSecondInsertionPointName = "second_mock_insertion_point";
61 static const char* kFirstInsertionPoint =
62 "# @@protoc_insertion_point(first_mock_insertion_point) is here\n";
63 static const char* kSecondInsertionPoint =
64 " # @@protoc_insertion_point(second_mock_insertion_point) is here\n";
66 MockCodeGenerator::MockCodeGenerator(const string& name)
69 MockCodeGenerator::~MockCodeGenerator() {}
71 void MockCodeGenerator::ExpectGenerated(
73 const string& parameter,
74 const string& insertions,
76 const string& first_message_name,
77 const string& first_parsed_file_name,
78 const string& output_directory) {
80 ASSERT_TRUE(File::ReadFileToString(
81 output_directory + "/" + GetOutputFileName(name, file), &content));
84 SplitStringUsing(content, "\n", &lines);
86 while (!lines.empty() && lines.back().empty()) {
89 for (int i = 0; i < lines.size(); i++) {
93 vector<string> insertion_list;
94 if (!insertions.empty()) {
95 SplitStringUsing(insertions, ",", &insertion_list);
98 ASSERT_EQ(lines.size(), 3 + insertion_list.size() * 2);
99 EXPECT_EQ(GetOutputFileContent(name, parameter, file,
100 first_parsed_file_name, first_message_name),
103 EXPECT_EQ(kFirstInsertionPoint, lines[1 + insertion_list.size()]);
104 EXPECT_EQ(kSecondInsertionPoint, lines[2 + insertion_list.size() * 2]);
106 for (int i = 0; i < insertion_list.size(); i++) {
107 EXPECT_EQ(GetOutputFileContent(insertion_list[i], "first_insert",
108 file, file, first_message_name),
110 // Second insertion point is indented, so the inserted text should
111 // automatically be indented too.
112 EXPECT_EQ(" " + GetOutputFileContent(insertion_list[i], "second_insert",
113 file, file, first_message_name),
114 lines[2 + insertion_list.size() + i]);
118 bool MockCodeGenerator::Generate(
119 const FileDescriptor* file,
120 const string& parameter,
121 GeneratorContext* context,
122 string* error) const {
123 for (int i = 0; i < file->message_type_count(); i++) {
124 if (HasPrefixString(file->message_type(i)->name(), "MockCodeGenerator_")) {
125 string command = StripPrefixString(file->message_type(i)->name(),
126 "MockCodeGenerator_");
127 if (command == "Error") {
128 *error = "Saw message type MockCodeGenerator_Error.";
130 } else if (command == "Exit") {
131 cerr << "Saw message type MockCodeGenerator_Exit." << endl;
133 } else if (command == "Abort") {
134 cerr << "Saw message type MockCodeGenerator_Abort." << endl;
136 } else if (command == "HasSourceCodeInfo") {
137 FileDescriptorProto file_descriptor_proto;
138 file->CopySourceCodeInfoTo(&file_descriptor_proto);
139 bool has_source_code_info =
140 file_descriptor_proto.has_source_code_info() &&
141 file_descriptor_proto.source_code_info().location_size() > 0;
142 cerr << "Saw message type MockCodeGenerator_HasSourceCodeInfo: "
143 << has_source_code_info << "." << endl;
146 GOOGLE_LOG(FATAL) << "Unknown MockCodeGenerator command: " << command;
151 if (HasPrefixString(parameter, "insert=")) {
152 vector<string> insert_into;
153 SplitStringUsing(StripPrefixString(parameter, "insert="),
156 for (int i = 0; i < insert_into.size(); i++) {
158 scoped_ptr<io::ZeroCopyOutputStream> output(
159 context->OpenForInsert(
160 GetOutputFileName(insert_into[i], file),
161 kFirstInsertionPointName));
162 io::Printer printer(output.get(), '$');
163 printer.PrintRaw(GetOutputFileContent(name_, "first_insert",
165 if (printer.failed()) {
166 *error = "MockCodeGenerator detected write error.";
172 scoped_ptr<io::ZeroCopyOutputStream> output(
173 context->OpenForInsert(
174 GetOutputFileName(insert_into[i], file),
175 kSecondInsertionPointName));
176 io::Printer printer(output.get(), '$');
177 printer.PrintRaw(GetOutputFileContent(name_, "second_insert",
179 if (printer.failed()) {
180 *error = "MockCodeGenerator detected write error.";
186 scoped_ptr<io::ZeroCopyOutputStream> output(
187 context->Open(GetOutputFileName(name_, file)));
189 io::Printer printer(output.get(), '$');
190 printer.PrintRaw(GetOutputFileContent(name_, parameter,
192 printer.PrintRaw(kFirstInsertionPoint);
193 printer.PrintRaw(kSecondInsertionPoint);
195 if (printer.failed()) {
196 *error = "MockCodeGenerator detected write error.";
204 string MockCodeGenerator::GetOutputFileName(const string& generator_name,
205 const FileDescriptor* file) {
206 return GetOutputFileName(generator_name, file->name());
209 string MockCodeGenerator::GetOutputFileName(const string& generator_name,
210 const string& file) {
211 return file + ".MockCodeGenerator." + generator_name;
214 string MockCodeGenerator::GetOutputFileContent(
215 const string& generator_name,
216 const string& parameter,
217 const FileDescriptor* file,
218 GeneratorContext *context) {
219 vector<const FileDescriptor*> all_files;
220 context->ListParsedFiles(&all_files);
221 return GetOutputFileContent(
222 generator_name, parameter, file->name(),
223 CommaSeparatedList(all_files),
224 file->message_type_count() > 0 ?
225 file->message_type(0)->name() : "(none)");
228 string MockCodeGenerator::GetOutputFileContent(
229 const string& generator_name,
230 const string& parameter,
232 const string& parsed_file_list,
233 const string& first_message_name) {
234 return strings::Substitute("$0: $1, $2, $3, $4\n",
235 generator_name, parameter, file,
236 first_message_name, parsed_file_list);
239 } // namespace compiler
240 } // namespace protobuf
241 } // namespace google