| OLD | NEW |
| 1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
| 2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
| 3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
| 4 // | 4 // |
| 5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
| 6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
| 7 // met: | 7 // met: |
| 8 // | 8 // |
| 9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
| 10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 #include <google/protobuf/stubs/shared_ptr.h> | 39 #include <google/protobuf/stubs/shared_ptr.h> |
| 40 #endif | 40 #endif |
| 41 | 41 |
| 42 #include <google/protobuf/compiler/python/python_generator.h> | 42 #include <google/protobuf/compiler/python/python_generator.h> |
| 43 #include <google/protobuf/compiler/command_line_interface.h> | 43 #include <google/protobuf/compiler/command_line_interface.h> |
| 44 #include <google/protobuf/io/zero_copy_stream.h> | 44 #include <google/protobuf/io/zero_copy_stream.h> |
| 45 #include <google/protobuf/io/printer.h> | 45 #include <google/protobuf/io/printer.h> |
| 46 | 46 |
| 47 #include <google/protobuf/testing/file.h> | 47 #include <google/protobuf/testing/file.h> |
| 48 #include <google/protobuf/testing/file.h> | 48 #include <google/protobuf/testing/file.h> |
| 49 #include <google/protobuf/stubs/strutil.h> |
| 49 #include <google/protobuf/testing/googletest.h> | 50 #include <google/protobuf/testing/googletest.h> |
| 50 #include <gtest/gtest.h> | 51 #include <gtest/gtest.h> |
| 51 | 52 |
| 52 namespace google { | 53 namespace google { |
| 53 namespace protobuf { | 54 namespace protobuf { |
| 54 namespace compiler { | 55 namespace compiler { |
| 55 namespace python { | 56 namespace python { |
| 56 namespace { | 57 namespace { |
| 57 | 58 |
| 58 class TestGenerator : public CodeGenerator { | 59 class TestGenerator : public CodeGenerator { |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 "protoc", | 109 "protoc", |
| 109 proto_path.c_str(), | 110 proto_path.c_str(), |
| 110 python_out.c_str(), | 111 python_out.c_str(), |
| 111 test_out.c_str(), | 112 test_out.c_str(), |
| 112 "test.proto" | 113 "test.proto" |
| 113 }; | 114 }; |
| 114 | 115 |
| 115 EXPECT_EQ(0, cli.Run(5, argv)); | 116 EXPECT_EQ(0, cli.Run(5, argv)); |
| 116 } | 117 } |
| 117 | 118 |
| 119 // This test verifies that the generated Python output uses regular imports (as |
| 120 // opposed to importlib) in the usual case where the .proto file paths do not |
| 121 // not contain any Python keywords. |
| 122 TEST(PythonPluginTest, ImportTest) { |
| 123 // Create files test1.proto and test2.proto with the former importing the |
| 124 // latter. |
| 125 GOOGLE_CHECK_OK(File::SetContents(TestTempDir() + "/test1.proto", |
| 126 "syntax = \"proto3\";\n" |
| 127 "package foo;\n" |
| 128 "import \"test2.proto\";" |
| 129 "message Message1 {\n" |
| 130 " Message2 message_2 = 1;\n" |
| 131 "}\n", |
| 132 true)); |
| 133 GOOGLE_CHECK_OK(File::SetContents(TestTempDir() + "/test2.proto", |
| 134 "syntax = \"proto3\";\n" |
| 135 "package foo;\n" |
| 136 "message Message2 {}\n", |
| 137 true)); |
| 138 |
| 139 google::protobuf::compiler::CommandLineInterface cli; |
| 140 cli.SetInputsAreProtoPathRelative(true); |
| 141 python::Generator python_generator; |
| 142 cli.RegisterGenerator("--python_out", &python_generator, ""); |
| 143 string proto_path = "-I" + TestTempDir(); |
| 144 string python_out = "--python_out=" + TestTempDir(); |
| 145 const char* argv[] = {"protoc", proto_path.c_str(), "-I.", python_out.c_str(), |
| 146 "test1.proto"}; |
| 147 ASSERT_EQ(0, cli.Run(5, argv)); |
| 148 |
| 149 // Loop over the lines of the generated code and verify that we find an |
| 150 // ordinary Python import but do not find the string "importlib". |
| 151 string output; |
| 152 GOOGLE_CHECK_OK(File::GetContents(TestTempDir() + "/test1_pb2.py", &output, |
| 153 true)); |
| 154 std::vector<string> lines = Split(output, "\n"); |
| 155 string expected_import = "import test2_pb2"; |
| 156 bool found_expected_import = false; |
| 157 for (int i = 0; i < lines.size(); ++i) { |
| 158 if (lines[i].find(expected_import) != string::npos) { |
| 159 found_expected_import = true; |
| 160 } |
| 161 EXPECT_EQ(string::npos, lines[i].find("importlib")); |
| 162 } |
| 163 EXPECT_TRUE(found_expected_import); |
| 164 } |
| 165 |
| 118 } // namespace | 166 } // namespace |
| 119 } // namespace python | 167 } // namespace python |
| 120 } // namespace compiler | 168 } // namespace compiler |
| 121 } // namespace protobuf | 169 } // namespace protobuf |
| 122 } // namespace google | 170 } // namespace google |
| OLD | NEW |