blob: 82bb342747c888cdaa4a602d74fa9d267c75ea9e [file] [log] [blame]
kenton@google.com5e744ff2009-12-18 04:51:42 +00001// Protocol Buffers - Google's data interchange format
2// Copyright 2008 Google Inc. All rights reserved.
Feng Xiaoe4288622014-10-01 16:26:23 -07003// https://developers.google.com/protocol-buffers/
kenton@google.com5e744ff2009-12-18 04:51:42 +00004//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
8//
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
14// distribution.
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.
18//
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.
30
31// Author: kenton@google.com (Kenton Varda)
32
33#include <google/protobuf/compiler/mock_code_generator.h>
34
Jisi Liu3b3c8ab2016-03-30 11:39:59 -070035#include <stdlib.h>
36#include <iostream>
jieluo@google.com4de8f552014-07-18 00:47:59 +000037#include <memory>
Feng Xiao6ef984a2014-11-10 17:34:54 -080038#ifndef _SHARED_PTR_H
39#include <google/protobuf/stubs/shared_ptr.h>
40#endif
Jisi Liu3b3c8ab2016-03-30 11:39:59 -070041#include <vector>
jieluo@google.com4de8f552014-07-18 00:47:59 +000042
Jisi Liu3b3c8ab2016-03-30 11:39:59 -070043#include <google/protobuf/stubs/logging.h>
44#include <google/protobuf/stubs/common.h>
kenton@google.com5e744ff2009-12-18 04:51:42 +000045#include <google/protobuf/testing/file.h>
Jisi Liu3b3c8ab2016-03-30 11:39:59 -070046#include <google/protobuf/testing/file.h>
47#include <google/protobuf/testing/file.h>
48#include <google/protobuf/io/printer.h>
49#include <google/protobuf/io/zero_copy_stream.h>
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +000050#include <google/protobuf/descriptor.pb.h>
kenton@google.com5e744ff2009-12-18 04:51:42 +000051#include <google/protobuf/descriptor.h>
kenton@google.com5e744ff2009-12-18 04:51:42 +000052#include <google/protobuf/stubs/strutil.h>
53#include <google/protobuf/stubs/substitute.h>
54#include <gtest/gtest.h>
kenton@google.com5e744ff2009-12-18 04:51:42 +000055
56namespace google {
57namespace protobuf {
58namespace compiler {
59
liujisi@google.com33165fe2010-11-02 13:14:58 +000060// Returns the list of the names of files in all_files in the form of a
61// comma-separated string.
62string CommaSeparatedList(const vector<const FileDescriptor*> all_files) {
63 vector<string> names;
64 for (int i = 0; i < all_files.size(); i++) {
65 names.push_back(all_files[i]->name());
66 }
jieluo@google.com4de8f552014-07-18 00:47:59 +000067 return Join(names, ",");
liujisi@google.com33165fe2010-11-02 13:14:58 +000068}
69
kenton@google.com5e744ff2009-12-18 04:51:42 +000070static const char* kFirstInsertionPointName = "first_mock_insertion_point";
71static const char* kSecondInsertionPointName = "second_mock_insertion_point";
72static const char* kFirstInsertionPoint =
73 "# @@protoc_insertion_point(first_mock_insertion_point) is here\n";
74static const char* kSecondInsertionPoint =
kenton@google.com5f121642009-12-23 07:03:06 +000075 " # @@protoc_insertion_point(second_mock_insertion_point) is here\n";
kenton@google.com5e744ff2009-12-18 04:51:42 +000076
77MockCodeGenerator::MockCodeGenerator(const string& name)
78 : name_(name) {}
79
80MockCodeGenerator::~MockCodeGenerator() {}
81
82void MockCodeGenerator::ExpectGenerated(
83 const string& name,
84 const string& parameter,
85 const string& insertions,
86 const string& file,
87 const string& first_message_name,
liujisi@google.com33165fe2010-11-02 13:14:58 +000088 const string& first_parsed_file_name,
kenton@google.com5e744ff2009-12-18 04:51:42 +000089 const string& output_directory) {
90 string content;
jieluo@google.com4de8f552014-07-18 00:47:59 +000091 GOOGLE_CHECK_OK(
92 File::GetContents(output_directory + "/" + GetOutputFileName(name, file),
93 &content, true));
kenton@google.com5e744ff2009-12-18 04:51:42 +000094
jieluo@google.com4de8f552014-07-18 00:47:59 +000095 vector<string> lines = Split(content, "\n", true);
kenton@google.com5e744ff2009-12-18 04:51:42 +000096
97 while (!lines.empty() && lines.back().empty()) {
98 lines.pop_back();
99 }
100 for (int i = 0; i < lines.size(); i++) {
101 lines[i] += "\n";
102 }
103
104 vector<string> insertion_list;
105 if (!insertions.empty()) {
106 SplitStringUsing(insertions, ",", &insertion_list);
107 }
108
jieluo@google.com4de8f552014-07-18 00:47:59 +0000109 EXPECT_EQ(lines.size(), 3 + insertion_list.size() * 2);
liujisi@google.com33165fe2010-11-02 13:14:58 +0000110 EXPECT_EQ(GetOutputFileContent(name, parameter, file,
111 first_parsed_file_name, first_message_name),
kenton@google.com5e744ff2009-12-18 04:51:42 +0000112 lines[0]);
113
114 EXPECT_EQ(kFirstInsertionPoint, lines[1 + insertion_list.size()]);
115 EXPECT_EQ(kSecondInsertionPoint, lines[2 + insertion_list.size() * 2]);
116
117 for (int i = 0; i < insertion_list.size(); i++) {
118 EXPECT_EQ(GetOutputFileContent(insertion_list[i], "first_insert",
liujisi@google.com33165fe2010-11-02 13:14:58 +0000119 file, file, first_message_name),
kenton@google.com5e744ff2009-12-18 04:51:42 +0000120 lines[1 + i]);
kenton@google.com5f121642009-12-23 07:03:06 +0000121 // Second insertion point is indented, so the inserted text should
122 // automatically be indented too.
123 EXPECT_EQ(" " + GetOutputFileContent(insertion_list[i], "second_insert",
liujisi@google.com33165fe2010-11-02 13:14:58 +0000124 file, file, first_message_name),
kenton@google.com5e744ff2009-12-18 04:51:42 +0000125 lines[2 + insertion_list.size() + i]);
126 }
127}
128
129bool MockCodeGenerator::Generate(
130 const FileDescriptor* file,
131 const string& parameter,
liujisi@google.com33165fe2010-11-02 13:14:58 +0000132 GeneratorContext* context,
kenton@google.com5e744ff2009-12-18 04:51:42 +0000133 string* error) const {
134 for (int i = 0; i < file->message_type_count(); i++) {
135 if (HasPrefixString(file->message_type(i)->name(), "MockCodeGenerator_")) {
136 string command = StripPrefixString(file->message_type(i)->name(),
137 "MockCodeGenerator_");
138 if (command == "Error") {
139 *error = "Saw message type MockCodeGenerator_Error.";
140 return false;
141 } else if (command == "Exit") {
Jisi Liu885b6122015-02-28 14:51:22 -0800142 std::cerr << "Saw message type MockCodeGenerator_Exit." << std::endl;
kenton@google.com5e744ff2009-12-18 04:51:42 +0000143 exit(123);
144 } else if (command == "Abort") {
Jisi Liu885b6122015-02-28 14:51:22 -0800145 std::cerr << "Saw message type MockCodeGenerator_Abort." << std::endl;
kenton@google.com5e744ff2009-12-18 04:51:42 +0000146 abort();
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000147 } else if (command == "HasSourceCodeInfo") {
148 FileDescriptorProto file_descriptor_proto;
149 file->CopySourceCodeInfoTo(&file_descriptor_proto);
150 bool has_source_code_info =
151 file_descriptor_proto.has_source_code_info() &&
152 file_descriptor_proto.source_code_info().location_size() > 0;
Jisi Liu885b6122015-02-28 14:51:22 -0800153 std::cerr << "Saw message type MockCodeGenerator_HasSourceCodeInfo: "
154 << has_source_code_info << "." << std::endl;
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000155 abort();
Feng Xiaoe841bac2015-12-11 17:09:20 -0800156 } else if (command == "HasJsonName") {
157 FieldDescriptorProto field_descriptor_proto;
158 file->message_type(i)->field(0)->CopyTo(&field_descriptor_proto);
159 std::cerr << "Saw json_name: "
160 << field_descriptor_proto.has_json_name() << std::endl;
161 abort();
kenton@google.com5e744ff2009-12-18 04:51:42 +0000162 } else {
163 GOOGLE_LOG(FATAL) << "Unknown MockCodeGenerator command: " << command;
164 }
165 }
166 }
167
168 if (HasPrefixString(parameter, "insert=")) {
169 vector<string> insert_into;
170 SplitStringUsing(StripPrefixString(parameter, "insert="),
171 ",", &insert_into);
172
173 for (int i = 0; i < insert_into.size(); i++) {
174 {
Feng Xiaof157a562014-11-14 11:50:31 -0800175 google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> output(context->OpenForInsert(
jieluo@google.com4de8f552014-07-18 00:47:59 +0000176 GetOutputFileName(insert_into[i], file), kFirstInsertionPointName));
kenton@google.com5e744ff2009-12-18 04:51:42 +0000177 io::Printer printer(output.get(), '$');
liujisi@google.com33165fe2010-11-02 13:14:58 +0000178 printer.PrintRaw(GetOutputFileContent(name_, "first_insert",
179 file, context));
kenton@google.com5e744ff2009-12-18 04:51:42 +0000180 if (printer.failed()) {
181 *error = "MockCodeGenerator detected write error.";
182 return false;
183 }
184 }
185
186 {
Feng Xiaof157a562014-11-14 11:50:31 -0800187 google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> output(
jieluo@google.com4de8f552014-07-18 00:47:59 +0000188 context->OpenForInsert(GetOutputFileName(insert_into[i], file),
189 kSecondInsertionPointName));
kenton@google.com5e744ff2009-12-18 04:51:42 +0000190 io::Printer printer(output.get(), '$');
liujisi@google.com33165fe2010-11-02 13:14:58 +0000191 printer.PrintRaw(GetOutputFileContent(name_, "second_insert",
192 file, context));
kenton@google.com5e744ff2009-12-18 04:51:42 +0000193 if (printer.failed()) {
194 *error = "MockCodeGenerator detected write error.";
195 return false;
196 }
197 }
198 }
199 } else {
Feng Xiaof157a562014-11-14 11:50:31 -0800200 google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> output(
liujisi@google.com33165fe2010-11-02 13:14:58 +0000201 context->Open(GetOutputFileName(name_, file)));
kenton@google.com5e744ff2009-12-18 04:51:42 +0000202
203 io::Printer printer(output.get(), '$');
liujisi@google.com33165fe2010-11-02 13:14:58 +0000204 printer.PrintRaw(GetOutputFileContent(name_, parameter,
205 file, context));
kenton@google.com5e744ff2009-12-18 04:51:42 +0000206 printer.PrintRaw(kFirstInsertionPoint);
207 printer.PrintRaw(kSecondInsertionPoint);
208
209 if (printer.failed()) {
210 *error = "MockCodeGenerator detected write error.";
211 return false;
212 }
213 }
214
215 return true;
216}
217
218string MockCodeGenerator::GetOutputFileName(const string& generator_name,
219 const FileDescriptor* file) {
220 return GetOutputFileName(generator_name, file->name());
221}
222
223string MockCodeGenerator::GetOutputFileName(const string& generator_name,
224 const string& file) {
225 return file + ".MockCodeGenerator." + generator_name;
226}
227
liujisi@google.com33165fe2010-11-02 13:14:58 +0000228string MockCodeGenerator::GetOutputFileContent(
229 const string& generator_name,
230 const string& parameter,
231 const FileDescriptor* file,
232 GeneratorContext *context) {
233 vector<const FileDescriptor*> all_files;
234 context->ListParsedFiles(&all_files);
kenton@google.com5e744ff2009-12-18 04:51:42 +0000235 return GetOutputFileContent(
236 generator_name, parameter, file->name(),
liujisi@google.com33165fe2010-11-02 13:14:58 +0000237 CommaSeparatedList(all_files),
kenton@google.com5e744ff2009-12-18 04:51:42 +0000238 file->message_type_count() > 0 ?
239 file->message_type(0)->name() : "(none)");
240}
241
242string MockCodeGenerator::GetOutputFileContent(
243 const string& generator_name,
244 const string& parameter,
245 const string& file,
liujisi@google.com33165fe2010-11-02 13:14:58 +0000246 const string& parsed_file_list,
kenton@google.com5e744ff2009-12-18 04:51:42 +0000247 const string& first_message_name) {
liujisi@google.com33165fe2010-11-02 13:14:58 +0000248 return strings::Substitute("$0: $1, $2, $3, $4\n",
249 generator_name, parameter, file,
250 first_message_name, parsed_file_list);
kenton@google.com5e744ff2009-12-18 04:51:42 +0000251}
252
253} // namespace compiler
254} // namespace protobuf
255} // namespace google