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