56 class TestGenerator :
public CodeGenerator {
62 const std::string& parameter, GeneratorContext* context,
64 TryInsert(
"test_pb2.py",
"imports", context);
65 TryInsert(
"test_pb2.py",
"module_scope", context);
66 TryInsert(
"test_pb2.py",
"class_scope:foo.Bar", context);
67 TryInsert(
"test_pb2.py",
"class_scope:foo.Bar.Baz", context);
73 GeneratorContext* context)
const {
74 std::unique_ptr<io::ZeroCopyOutputStream>
output(
75 context->OpenForInsert(filename, insertion_point));
76 io::Printer printer(
output.get(),
'$');
77 printer.Print(
"// inserted $name$\n",
"name", insertion_point);
84 TEST(PythonPluginTest, PluginTest) {
86 "syntax = \"proto2\";\n"
93 compiler::CommandLineInterface cli;
94 cli.SetInputsAreProtoPathRelative(
true);
96 python::Generator python_generator;
97 TestGenerator test_generator;
98 cli.RegisterGenerator(
"--python_out", &python_generator,
"");
99 cli.RegisterGenerator(
"--test_out", &test_generator,
"");
105 const char* argv[] = {
"protoc", proto_path.c_str(), python_out.c_str(),
106 test_out.c_str(),
"test.proto"};
114 TEST(PythonPluginTest, ImportTest) {
118 "syntax = \"proto3\";\n"
120 "import \"test2.proto\";"
121 "message Message1 {\n"
122 " Message2 message_2 = 1;\n"
126 "syntax = \"proto3\";\n"
128 "message Message2 {}\n",
131 compiler::CommandLineInterface cli;
132 cli.SetInputsAreProtoPathRelative(
true);
133 python::Generator python_generator;
134 cli.RegisterGenerator(
"--python_out", &python_generator,
"");
137 const char* argv[] = {
"protoc", proto_path.c_str(),
"-I.", python_out.c_str(),
146 std::vector<std::string> lines =
Split(
output,
"\n");
148 bool found_expected_import =
false;
149 for (
int i = 0;
i < lines.size(); ++
i) {
150 if (lines[
i].find(expected_import) != std::string::npos) {
151 found_expected_import =
true;
153 EXPECT_EQ(std::string::npos, lines[
i].find(
"importlib"));