1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
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 // TODO(kenton): Share code with the versions of this test in other languages?
34 // It seemed like parameterizing it would add more complexity than it is
35 // worth.
36
37 #include <memory>
38 #ifndef _SHARED_PTR_H
39 #include <google/protobuf/stubs/shared_ptr.h>
40 #endif
41
42 #include <google/protobuf/compiler/cpp/cpp_generator.h>
43 #include <google/protobuf/compiler/command_line_interface.h>
44 #include <google/protobuf/io/zero_copy_stream.h>
45 #include <google/protobuf/io/printer.h>
46
47 #include <google/protobuf/testing/file.h>
48 #include <google/protobuf/testing/file.h>
49 #include <google/protobuf/testing/googletest.h>
50 #include <gtest/gtest.h>
51
52 namespace google {
53 namespace protobuf {
54 namespace compiler {
55 namespace cpp {
56 namespace {
57
58 class TestGenerator : public CodeGenerator {
59 public:
TestGenerator()60 TestGenerator() {}
~TestGenerator()61 ~TestGenerator() {}
62
Generate(const FileDescriptor * file,const string & parameter,GeneratorContext * context,string * error) const63 virtual bool Generate(const FileDescriptor* file,
64 const string& parameter,
65 GeneratorContext* context,
66 string* error) const {
67 TryInsert("test.pb.h", "includes", context);
68 TryInsert("test.pb.h", "namespace_scope", context);
69 TryInsert("test.pb.h", "global_scope", context);
70 TryInsert("test.pb.h", "class_scope:foo.Bar", context);
71 TryInsert("test.pb.h", "class_scope:foo.Bar.Baz", context);
72
73 TryInsert("test.pb.cc", "includes", context);
74 TryInsert("test.pb.cc", "namespace_scope", context);
75 TryInsert("test.pb.cc", "global_scope", context);
76
77 // Check field accessors for an optional int32:
78 TryInsert("test.pb.h", "field_get:foo.Bar.optInt", context);
79 TryInsert("test.pb.h", "field_set:foo.Bar.optInt", context);
80
81 // Check field accessors for a repeated int32:
82 TryInsert("test.pb.h", "field_get:foo.Bar.repeatedInt", context);
83 TryInsert("test.pb.h", "field_set:foo.Bar.repeatedInt", context);
84
85 // Check field accessors for a required string:
86 TryInsert("test.pb.h", "field_get:foo.Bar.requiredString", context);
87 TryInsert("test.pb.h", "field_set:foo.Bar.requiredString", context);
88 TryInsert("test.pb.h", "field_set_char:foo.Bar.requiredString", context);
89 TryInsert("test.pb.h", "field_set_pointer:foo.Bar.requiredString", context);
90 TryInsert("test.pb.h", "field_mutable:foo.Bar.requiredString", context);
91 TryInsert("test.pb.h", "field_set_allocated:foo.Bar.requiredString",
92 context);
93 TryInsert("test.pb.h", "field_set_char:foo.Bar.requiredString", context);
94 TryInsert("test.pb.h", "field_set_pointer:foo.Bar.requiredString", context);
95
96 // Check field accessors for a repeated string:
97 TryInsert("test.pb.h", "field_get:foo.Bar.repeatedString", context);
98 TryInsert("test.pb.h", "field_set:foo.Bar.repeatedString", context);
99 TryInsert("test.pb.h", "field_set_char:foo.Bar.repeatedString", context);
100 TryInsert("test.pb.h", "field_set_pointer:foo.Bar.repeatedString", context);
101 TryInsert("test.pb.h", "field_mutable:foo.Bar.repeatedString", context);
102 TryInsert("test.pb.h", "field_set_char:foo.Bar.repeatedString", context);
103 TryInsert("test.pb.h", "field_set_pointer:foo.Bar.repeatedString", context);
104
105 // Check field accessors for an int inside oneof{}:
106 TryInsert("test.pb.h", "field_get:foo.Bar.oneOfInt", context);
107 TryInsert("test.pb.h", "field_set:foo.Bar.oneOfInt", context);
108
109 // Check field accessors for a string inside oneof{}:
110 TryInsert("test.pb.h", "field_get:foo.Bar.oneOfString", context);
111 TryInsert("test.pb.h", "field_set:foo.Bar.oneOfString", context);
112 TryInsert("test.pb.h", "field_set_char:foo.Bar.oneOfString", context);
113 TryInsert("test.pb.h", "field_set_pointer:foo.Bar.oneOfString", context);
114 TryInsert("test.pb.h", "field_mutable:foo.Bar.oneOfString", context);
115 TryInsert("test.pb.h", "field_set_allocated:foo.Bar.oneOfString", context);
116 TryInsert("test.pb.h", "field_set_char:foo.Bar.oneOfString", context);
117 TryInsert("test.pb.h", "field_set_pointer:foo.Bar.oneOfString", context);
118
119 // Check field accessors for an optional message:
120 TryInsert("test.pb.h", "field_get:foo.Bar.optMessage", context);
121 TryInsert("test.pb.h", "field_mutable:foo.Bar.optMessage", context);
122 TryInsert("test.pb.h", "field_set_allocated:foo.Bar.optMessage", context);
123
124 // Check field accessors for a repeated message:
125 TryInsert("test.pb.h", "field_add:foo.Bar.repeatedMessage", context);
126 TryInsert("test.pb.h", "field_get:foo.Bar.repeatedMessage", context);
127 TryInsert("test.pb.h", "field_list:foo.Bar.repeatedMessage", context);
128 TryInsert("test.pb.h", "field_mutable:foo.Bar.repeatedMessage", context);
129 TryInsert("test.pb.h", "field_mutable_list:foo.Bar.repeatedMessage",
130 context);
131
132 // Check field accessors for a message inside oneof{}:
133 TryInsert("test.pb.h", "field_get:foo.Bar.oneOfMessage", context);
134 TryInsert("test.pb.h", "field_mutable:foo.Bar.oneOfMessage", context);
135 TryInsert("test.pb.h", "field_set_allocated:foo.Bar.oneOfMessage", context);
136
137 // Check field accessors for an optional enum:
138 TryInsert("test.pb.h", "field_get:foo.Bar.optEnum", context);
139 TryInsert("test.pb.h", "field_set:foo.Bar.optEnum", context);
140
141 // Check field accessors for a repeated enum:
142 TryInsert("test.pb.h", "field_get:foo.Bar.repeatedEnum", context);
143 TryInsert("test.pb.h", "field_set:foo.Bar.repeatedEnum", context);
144 TryInsert("test.pb.h", "field_add:foo.Bar.repeatedEnum", context);
145 TryInsert("test.pb.h", "field_list:foo.Bar.repeatedEnum", context);
146 TryInsert("test.pb.h", "field_mutable_list:foo.Bar.repeatedEnum", context);
147
148 // Check field accessors for an enum inside oneof{}:
149 TryInsert("test.pb.h", "field_get:foo.Bar.oneOfEnum", context);
150 TryInsert("test.pb.h", "field_set:foo.Bar.oneOfEnum", context);
151
152 // Check field accessors for a required cord:
153 TryInsert("test.pb.h", "field_get:foo.Bar.requiredCord", context);
154 TryInsert("test.pb.h", "field_set:foo.Bar.requiredCord", context);
155 TryInsert("test.pb.h", "field_mutable:foo.Bar.requiredCord", context);
156
157 // Check field accessors for a repeated cord:
158 TryInsert("test.pb.h", "field_get:foo.Bar.repeatedCord", context);
159 TryInsert("test.pb.h", "field_set:foo.Bar.repeatedCord", context);
160 TryInsert("test.pb.h", "field_add:foo.Bar.repeatedCord", context);
161 TryInsert("test.pb.h", "field_list:foo.Bar.repeatedCord", context);
162 TryInsert("test.pb.h", "field_mutable:foo.Bar.repeatedCord", context);
163 TryInsert("test.pb.h", "field_mutable_list:foo.Bar.repeatedCord", context);
164
165 // Check field accessors for a cord inside oneof{}:
166 TryInsert("test.pb.h", "field_get:foo.Bar.oneOfCord", context);
167 TryInsert("test.pb.h", "field_set:foo.Bar.oneOfCord", context);
168 TryInsert("test.pb.h", "field_mutable:foo.Bar.oneOfCord", context);
169
170 return true;
171 }
172
TryInsert(const string & filename,const string & insertion_point,GeneratorContext * context) const173 void TryInsert(const string& filename, const string& insertion_point,
174 GeneratorContext* context) const {
175 google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> output(
176 context->OpenForInsert(filename, insertion_point));
177 io::Printer printer(output.get(), '$');
178 printer.Print("// inserted $name$\n", "name", insertion_point);
179 }
180 };
181
182 // This test verifies that all the expected insertion points exist. It does
183 // not verify that they are correctly-placed; that would require actually
184 // compiling the output which is a bit more than I care to do for this test.
TEST(CppPluginTest,PluginTest)185 TEST(CppPluginTest, PluginTest) {
186 GOOGLE_CHECK_OK(File::SetContents(TestTempDir() + "/test.proto",
187 "syntax = \"proto2\";\n"
188 "package foo;\n"
189 "\n"
190 "enum Thud { VALUE = 0; }\n"
191 "\n"
192 "message Bar {\n"
193 " message Baz {}\n"
194 " optional int32 optInt = 1;\n"
195 " repeated int32 repeatedInt = 2;\n"
196 "\n"
197 " required string requiredString = 3;\n"
198 " repeated string repeatedString = 4;\n"
199 "\n"
200 " optional Baz optMessage = 6;\n"
201 " repeated Baz repeatedMessage = 7;\n"
202 "\n"
203 " optional Thud optEnum = 8;\n"
204 " repeated Thud repeatedEnum = 9;\n"
205 "\n"
206 " required string requiredCord = 10 [\n"
207 " ctype = CORD\n"
208 " ];\n"
209 " repeated string repeatedCord = 11 [\n"
210 " ctype = CORD\n"
211 " ];\n"
212 "\n"
213 " oneof Qux {\n"
214 " int64 oneOfInt = 20;\n"
215 " string oneOfString = 21;\n"
216 " Baz oneOfMessage = 22;\n"
217 " Thud oneOfEnum = 23;"
218 " string oneOfCord = 24 [\n"
219 " ctype = CORD\n"
220 " ];\n"
221 " }\n"
222 "}\n",
223 true));
224
225 google::protobuf::compiler::CommandLineInterface cli;
226 cli.SetInputsAreProtoPathRelative(true);
227
228 CppGenerator cpp_generator;
229 TestGenerator test_generator;
230 cli.RegisterGenerator("--cpp_out", &cpp_generator, "");
231 cli.RegisterGenerator("--test_out", &test_generator, "");
232
233 string proto_path = "-I" + TestTempDir();
234 string cpp_out = "--cpp_out=" + TestTempDir();
235 string test_out = "--test_out=" + TestTempDir();
236
237 const char* argv[] = {
238 "protoc",
239 proto_path.c_str(),
240 cpp_out.c_str(),
241 test_out.c_str(),
242 "test.proto"
243 };
244
245 EXPECT_EQ(0, cli.Run(5, argv));
246 }
247
248 } // namespace
249 } // namespace cpp
250 } // namespace compiler
251 } // namespace protobuf
252 } // namespace google
253