/
generator_unittest.cc
187 lines (157 loc) · 4.92 KB
/
generator_unittest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google Inc. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#include "google/protobuf/compiler/cpp/generator.h"
#include <memory>
#include "google/protobuf/descriptor.pb.h"
#include <gtest/gtest.h>
#include "google/protobuf/compiler/command_line_interface_tester.h"
#include "google/protobuf/cpp_features.pb.h"
namespace google {
namespace protobuf {
namespace compiler {
namespace cpp {
namespace {
class CppGeneratorTest : public CommandLineInterfaceTester {
protected:
CppGeneratorTest() {
RegisterGenerator("--cpp_out", "--cpp_opt",
std::make_unique<CppGenerator>(), "C++ test generator");
// Generate built-in protos.
CreateTempFile(
"google/protobuf/descriptor.proto",
google::protobuf::DescriptorProto::descriptor()->file()->DebugString());
CreateTempFile("google/protobuf/cpp_features.proto",
pb::CppFeatures::descriptor()->file()->DebugString());
}
};
TEST_F(CppGeneratorTest, Basic) {
CreateTempFile("foo.proto",
R"schema(
syntax = "proto2";
message Foo {
optional int32 bar = 1;
})schema");
RunProtoc(
"protocol_compiler --proto_path=$tmpdir --cpp_out=$tmpdir foo.proto");
ExpectNoErrors();
}
TEST_F(CppGeneratorTest, BasicError) {
CreateTempFile("foo.proto",
R"schema(
syntax = "proto2";
message Foo {
int32 bar = 1;
})schema");
RunProtoc(
"protocol_compiler --proto_path=$tmpdir --cpp_out=$tmpdir foo.proto");
ExpectErrorSubstring(
"foo.proto:4:7: Expected \"required\", \"optional\", or \"repeated\"");
}
TEST_F(CppGeneratorTest, LegacyClosedEnumOnNonEnumField) {
CreateTempFile("foo.proto",
R"schema(
edition = "2023";
import "google/protobuf/cpp_features.proto";
message Foo {
int32 bar = 1 [features.(pb.cpp).legacy_closed_enum = true];
})schema");
RunProtoc(
"protocol_compiler --proto_path=$tmpdir --cpp_out=$tmpdir "
"--experimental_editions foo.proto");
ExpectErrorSubstring(
"Field Foo.bar specifies the legacy_closed_enum feature but has non-enum "
"type.");
}
TEST_F(CppGeneratorTest, LegacyClosedEnum) {
CreateTempFile("foo.proto",
R"schema(
edition = "2023";
import "google/protobuf/cpp_features.proto";
enum TestEnum {
TEST_ENUM_UNKNOWN = 0;
}
message Foo {
TestEnum bar = 1 [features.(pb.cpp).legacy_closed_enum = true];
})schema");
RunProtoc(
"protocol_compiler --proto_path=$tmpdir --cpp_out=$tmpdir "
"--experimental_editions foo.proto");
ExpectNoErrors();
}
TEST_F(CppGeneratorTest, LegacyClosedEnumInherited) {
CreateTempFile("foo.proto",
R"schema(
edition = "2023";
import "google/protobuf/cpp_features.proto";
option features.(pb.cpp).legacy_closed_enum = true;
enum TestEnum {
TEST_ENUM_UNKNOWN = 0;
}
message Foo {
TestEnum bar = 1;
int32 baz = 2;
})schema");
RunProtoc(
"protocol_compiler --proto_path=$tmpdir --cpp_out=$tmpdir "
"--experimental_editions foo.proto");
ExpectNoErrors();
}
TEST_F(CppGeneratorTest, LegacyClosedEnumImplicit) {
CreateTempFile("foo.proto", R"schema(
edition = "2023";
import "google/protobuf/cpp_features.proto";
option features.(pb.cpp).legacy_closed_enum = true;
enum TestEnum {
TEST_ENUM_UNKNOWN = 0;
}
message Foo {
TestEnum bar = 1 [features.field_presence = IMPLICIT];
int32 baz = 2;
}
)schema");
RunProtoc(
"protocol_compiler --proto_path=$tmpdir --cpp_out=$tmpdir "
"--experimental_editions foo.proto");
ExpectErrorSubstring(
"Field Foo.bar has a closed enum type with implicit presence.");
}
TEST_F(CppGeneratorTest, CtypeOnNoneStringFieldTest) {
CreateTempFile("foo.proto",
R"schema(
edition = "2023";
message Foo {
int32 bar = 1 [ctype=STRING];
})schema");
RunProtoc(
"protocol_compiler --proto_path=$tmpdir --cpp_out=$tmpdir "
"--experimental_editions foo.proto");
ExpectErrorSubstring(
"Field Foo.bar specifies ctype, but is not "
"a string nor bytes field.");
}
TEST_F(CppGeneratorTest, CtypeOnExtensionTest) {
CreateTempFile("foo.proto",
R"schema(
edition = "2023";
message Foo {
extensions 1 to max;
}
extend Foo {
bytes bar = 1 [ctype=CORD];
})schema");
RunProtoc(
"protocol_compiler --proto_path=$tmpdir --cpp_out=$tmpdir "
"--experimental_editions foo.proto");
ExpectErrorSubstring(
"Extension bar specifies ctype=CORD which is "
"not supported for extensions.");
}
} // namespace
} // namespace cpp
} // namespace compiler
} // namespace protobuf
} // namespace google