|
9 | 9 |
|
10 | 10 | #include <memory>
|
11 | 11 | #include <string>
|
| 12 | +#include <utility> |
12 | 13 | #include <vector>
|
13 | 14 |
|
14 | 15 | #include "google/protobuf/testing/file.h"
|
15 |
| -#include "google/protobuf/testing/file.h" |
16 |
| -#include "google/protobuf/compiler/command_line_interface.h" |
17 |
| -#include "google/protobuf/compiler/python/generator.h" |
18 | 16 | #include <gtest/gtest.h>
|
19 | 17 | #include "absl/log/absl_check.h"
|
| 18 | +#include "absl/strings/str_cat.h" |
20 | 19 | #include "absl/strings/str_split.h"
|
| 20 | +#include "absl/strings/substitute.h" |
| 21 | +#include "google/protobuf/compiler/code_generator.h" |
| 22 | +#include "google/protobuf/compiler/command_line_interface_tester.h" |
| 23 | +#include "google/protobuf/compiler/cpp/generator.h" |
| 24 | +#include "google/protobuf/compiler/python/generator.h" |
| 25 | +#include "google/protobuf/cpp_features.pb.h" |
21 | 26 | #include "google/protobuf/io/printer.h"
|
22 | 27 | #include "google/protobuf/io/zero_copy_stream.h"
|
23 | 28 |
|
@@ -100,6 +105,58 @@ TEST(PythonPluginTest, ImportTest) {
|
100 | 105 | EXPECT_TRUE(found_expected_import);
|
101 | 106 | }
|
102 | 107 |
|
| 108 | +class PythonGeneratorTest : public CommandLineInterfaceTester, |
| 109 | + public testing::WithParamInterface<bool> { |
| 110 | + protected: |
| 111 | + PythonGeneratorTest() { |
| 112 | + auto generator = std::make_unique<Generator>(); |
| 113 | + generator->set_opensource_runtime(GetParam()); |
| 114 | + RegisterGenerator("--python_out", "--python_opt", std::move(generator), |
| 115 | + "Python test generator"); |
| 116 | + |
| 117 | + // Generate built-in protos. |
| 118 | + CreateTempFile( |
| 119 | + google::protobuf::DescriptorProto::descriptor()->file()->name(), |
| 120 | + google::protobuf::DescriptorProto::descriptor()->file()->DebugString()); |
| 121 | + } |
| 122 | +}; |
| 123 | + |
| 124 | +TEST_P(PythonGeneratorTest, PythonWithCppFeatures) { |
| 125 | + // Test that the presence of C++ features does not break Python generation. |
| 126 | + RegisterGenerator("--cpp_out", "--cpp_opt", |
| 127 | + std::make_unique<cpp::CppGenerator>(), |
| 128 | + "C++ test generator"); |
| 129 | + CreateTempFile("google/protobuf/cpp_features.proto", |
| 130 | + pb::CppFeatures::descriptor()->file()->DebugString()); |
| 131 | + CreateTempFile("foo.proto", |
| 132 | + R"schema( |
| 133 | + edition = "2023"; |
| 134 | +
|
| 135 | + import "google/protobuf/cpp_features.proto"; |
| 136 | +
|
| 137 | + package foo; |
| 138 | + |
| 139 | + enum Bar { |
| 140 | + AAA = 0; |
| 141 | + BBB = 1; |
| 142 | + } |
| 143 | +
|
| 144 | + message Foo { |
| 145 | + Bar bar_enum = 1 [features.(pb.cpp).legacy_closed_enum = true]; |
| 146 | + })schema"); |
| 147 | + |
| 148 | + RunProtoc(absl::Substitute( |
| 149 | + "protocol_compiler --proto_path=$$tmpdir --cpp_out=$$tmpdir " |
| 150 | + "--python_out=$$tmpdir foo.proto $0 " |
| 151 | + "google/protobuf/cpp_features.proto", |
| 152 | + google::protobuf::DescriptorProto::descriptor()->file()->name())); |
| 153 | + |
| 154 | + ExpectNoErrors(); |
| 155 | +} |
| 156 | + |
| 157 | +INSTANTIATE_TEST_SUITE_P(PythonGeneratorTest, PythonGeneratorTest, |
| 158 | + testing::Bool()); |
| 159 | + |
103 | 160 | } // namespace
|
104 | 161 | } // namespace python
|
105 | 162 | } // namespace compiler
|
|
0 commit comments