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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
|
#include "proto_test.h"
#include "flatbuffers/code_generator.h"
#include "idl_gen_fbs.h"
#include "test_assert.h"
namespace flatbuffers {
namespace tests {
void RunTest(const flatbuffers::IDLOptions &opts, const std::string &proto_path,
const std::string &proto_file, const std::string &golden_file,
const std::string import_proto_file) {
const char *include_directories[] = { proto_path.c_str(), nullptr };
// Parse proto.
flatbuffers::Parser parser(opts);
TEST_EQ(parser.Parse(proto_file.c_str(), include_directories), true);
// Generate fbs.
std::unique_ptr<CodeGenerator> fbs_generator = NewFBSCodeGenerator(true);
std::string fbs;
TEST_EQ(fbs_generator->GenerateCodeString(parser, "test", fbs),
CodeGenerator::Status::OK);
// Ensure generated file is parsable.
flatbuffers::Parser parser2;
if (!import_proto_file.empty()) {
// Generate fbs from import.proto
flatbuffers::Parser import_parser(opts);
TEST_EQ(import_parser.Parse(import_proto_file.c_str(), include_directories),
true);
std::string import_fbs;
TEST_EQ(
fbs_generator->GenerateCodeString(import_parser, "test", import_fbs),
CodeGenerator::Status::OK);
// auto import_fbs = flatbuffers::GenerateFBS(import_parser, "test", true);
// Since `imported.fbs` isn't in the filesystem AbsolutePath can't figure it
// out by itself. We manually construct it so Parser works.
std::string imported_fbs = flatbuffers::PosixPath(
flatbuffers::AbsolutePath(proto_path) + "/imported.fbs");
TEST_EQ(parser2.Parse(import_fbs.c_str(), include_directories,
imported_fbs.c_str()),
true);
}
TEST_EQ(parser2.Parse(fbs.c_str(), nullptr), true);
TEST_EQ_STR(fbs.c_str(), golden_file.c_str());
}
void proto_test(const std::string &proto_path, const std::string &proto_file) {
flatbuffers::IDLOptions opts;
opts.include_dependence_headers = false;
opts.proto_mode = true;
opts.proto_id_gap_action = IDLOptions::ProtoIdGapAction::NO_OP;
// load the .proto and the golden file from disk
std::string golden_file;
TEST_EQ(flatbuffers::LoadFile((proto_path + "test.golden.fbs").c_str(), false,
&golden_file),
true);
RunTest(opts, proto_path, proto_file, golden_file);
}
void proto_test_id(const std::string &proto_path,
const std::string &proto_file) {
flatbuffers::IDLOptions opts;
opts.include_dependence_headers = false;
opts.proto_mode = true;
opts.keep_proto_id = true;
opts.proto_id_gap_action = IDLOptions::ProtoIdGapAction::NO_OP;
// load the .proto and the golden file from disk
std::string golden_file;
TEST_EQ(flatbuffers::LoadFile((proto_path + "test_id.golden.fbs").c_str(),
false, &golden_file),
true);
RunTest(opts, proto_path, proto_file, golden_file);
}
void proto_test_union(const std::string &proto_path,
const std::string &proto_file) {
// Parse proto with --oneof-union option.
flatbuffers::IDLOptions opts;
opts.include_dependence_headers = false;
opts.proto_mode = true;
opts.proto_oneof_union = true;
opts.proto_id_gap_action = IDLOptions::ProtoIdGapAction::NO_OP;
std::string golden_file;
TEST_EQ(flatbuffers::LoadFile((proto_path + "test_union.golden.fbs").c_str(),
false, &golden_file),
true);
RunTest(opts, proto_path, proto_file, golden_file);
}
void proto_test_union_id(const std::string &proto_path,
const std::string &proto_file) {
// Parse proto with --oneof-union option.
flatbuffers::IDLOptions opts;
opts.include_dependence_headers = false;
opts.proto_mode = true;
opts.proto_oneof_union = true;
opts.keep_proto_id = true;
opts.proto_id_gap_action = IDLOptions::ProtoIdGapAction::NO_OP;
std::string golden_file;
TEST_EQ(
flatbuffers::LoadFile((proto_path + "test_union_id.golden.fbs").c_str(),
false, &golden_file),
true);
RunTest(opts, proto_path, proto_file, golden_file);
}
void proto_test_union_suffix(const std::string &proto_path,
const std::string &proto_file) {
flatbuffers::IDLOptions opts;
opts.include_dependence_headers = false;
opts.proto_mode = true;
opts.proto_namespace_suffix = "test_namespace_suffix";
opts.proto_oneof_union = true;
opts.proto_id_gap_action = IDLOptions::ProtoIdGapAction::NO_OP;
std::string golden_file;
TEST_EQ(flatbuffers::LoadFile(
(proto_path + "test_union_suffix.golden.fbs").c_str(), false,
&golden_file),
true);
RunTest(opts, proto_path, proto_file, golden_file);
}
void proto_test_union_suffix_id(const std::string &proto_path,
const std::string &proto_file) {
flatbuffers::IDLOptions opts;
opts.include_dependence_headers = false;
opts.proto_mode = true;
opts.proto_namespace_suffix = "test_namespace_suffix";
opts.proto_oneof_union = true;
opts.keep_proto_id = true;
opts.proto_id_gap_action = IDLOptions::ProtoIdGapAction::NO_OP;
std::string golden_file;
TEST_EQ(flatbuffers::LoadFile(
(proto_path + "test_union_suffix_id.golden.fbs").c_str(), false,
&golden_file),
true);
RunTest(opts, proto_path, proto_file, golden_file);
}
void proto_test_include(const std::string &proto_path,
const std::string &proto_file,
const std::string &import_proto_file) {
flatbuffers::IDLOptions opts;
opts.include_dependence_headers = true;
opts.proto_mode = true;
opts.proto_id_gap_action = IDLOptions::ProtoIdGapAction::NO_OP;
std::string golden_file;
TEST_EQ(
flatbuffers::LoadFile((proto_path + "test_include.golden.fbs").c_str(),
false, &golden_file),
true);
RunTest(opts, proto_path, proto_file, golden_file, import_proto_file);
}
void proto_test_include_id(const std::string &proto_path,
const std::string &proto_file,
const std::string &import_proto_file) {
flatbuffers::IDLOptions opts;
opts.include_dependence_headers = true;
opts.proto_mode = true;
opts.keep_proto_id = true;
opts.proto_id_gap_action = IDLOptions::ProtoIdGapAction::NO_OP;
std::string golden_file;
TEST_EQ(
flatbuffers::LoadFile((proto_path + "test_include_id.golden.fbs").c_str(),
false, &golden_file),
true);
RunTest(opts, proto_path, proto_file, golden_file, import_proto_file);
}
void proto_test_include_union(const std::string &proto_path,
const std::string &proto_file,
const std::string &import_proto_file) {
flatbuffers::IDLOptions opts;
opts.include_dependence_headers = true;
opts.proto_mode = true;
opts.proto_oneof_union = true;
opts.proto_id_gap_action = IDLOptions::ProtoIdGapAction::NO_OP;
std::string golden_file;
TEST_EQ(flatbuffers::LoadFile(
(proto_path + "test_union_include.golden.fbs").c_str(), false,
&golden_file),
true);
RunTest(opts, proto_path, proto_file, golden_file, import_proto_file);
}
void proto_test_include_union_id(const std::string &proto_path,
const std::string &proto_file,
const std::string &import_proto_file) {
flatbuffers::IDLOptions opts;
opts.include_dependence_headers = true;
opts.proto_mode = true;
opts.proto_oneof_union = true;
opts.keep_proto_id = true;
opts.proto_id_gap_action = IDLOptions::ProtoIdGapAction::NO_OP;
std::string golden_file;
TEST_EQ(flatbuffers::LoadFile(
(proto_path + "test_union_include_id.golden.fbs").c_str(), false,
&golden_file),
true);
RunTest(opts, proto_path, proto_file, golden_file, import_proto_file);
}
void ParseCorruptedProto(const std::string &proto_path) {
const char *include_directories[] = { proto_path.c_str(), nullptr };
flatbuffers::IDLOptions opts;
opts.include_dependence_headers = true;
opts.proto_mode = true;
opts.proto_oneof_union = true;
std::string proto_file;
std::unique_ptr<CodeGenerator> fbs_generator = NewFBSCodeGenerator(true);
// Parse proto with non positive id.
{
flatbuffers::Parser parser(opts);
TEST_EQ(
flatbuffers::LoadFile((proto_path + "non-positive-id.proto").c_str(),
false, &proto_file),
true);
TEST_EQ(parser.Parse(proto_file.c_str(), include_directories), true);
TEST_NE(fbs_generator->GenerateCode(parser, "temp.fbs", "test"),
CodeGenerator::Status::OK);
}
// Parse proto with twice id.
{
flatbuffers::Parser parser(opts);
TEST_EQ(flatbuffers::LoadFile((proto_path + "twice-id.proto").c_str(),
false, &proto_file),
true);
TEST_EQ(parser.Parse(proto_file.c_str(), include_directories), true);
TEST_NE(fbs_generator->GenerateCode(parser, "temp.fbs", "test"),
CodeGenerator::Status::OK);
}
// Parse proto with using reserved id.
{
flatbuffers::Parser parser(opts);
TEST_EQ(flatbuffers::LoadFile((proto_path + "twice-id.proto").c_str(),
false, &proto_file),
true);
TEST_EQ(parser.Parse(proto_file.c_str(), include_directories), true);
TEST_NE(fbs_generator->GenerateCode(parser, "temp.fbs", "test"),
CodeGenerator::Status::OK);
}
// Parse proto with error on gap.
{
opts.proto_id_gap_action = IDLOptions::ProtoIdGapAction::ERROR;
flatbuffers::Parser parser(opts);
TEST_EQ(flatbuffers::LoadFile((proto_path + "test.proto").c_str(), false,
&proto_file),
true);
TEST_EQ(parser.Parse(proto_file.c_str(), include_directories), true);
TEST_NE(fbs_generator->GenerateCode(parser, "temp.fbs", "test"),
CodeGenerator::Status::OK);
}
}
// Parse a .proto schema, output as .fbs
void ParseProtoTest(const std::string &tests_data_path) {
auto proto_path = tests_data_path + "prototest/";
std::string proto_file;
TEST_EQ(
flatbuffers::LoadFile((tests_data_path + "prototest/test.proto").c_str(),
false, &proto_file),
true);
std::string import_proto_file;
TEST_EQ(flatbuffers::LoadFile(
(tests_data_path + "prototest/imported.proto").c_str(), false,
&import_proto_file),
true);
proto_test(proto_path, proto_file);
proto_test_union(proto_path, proto_file);
proto_test_union_suffix(proto_path, proto_file);
proto_test_include(proto_path, proto_file, import_proto_file);
proto_test_include_union(proto_path, proto_file, import_proto_file);
proto_test_id(proto_path, proto_file);
proto_test_union_id(proto_path, proto_file);
proto_test_union_suffix_id(proto_path, proto_file);
proto_test_include_id(proto_path, proto_file, import_proto_file);
proto_test_include_union_id(proto_path, proto_file, import_proto_file);
ParseCorruptedProto(proto_path);
}
void ParseProtoBufAsciiTest() {
// We can put the parser in a mode where it will accept JSON that looks more
// like Protobuf ASCII, for users that have data in that format.
// This uses no "" for field names (which we already support by default,
// omits `,`, `:` before `{` and a couple of other features.
flatbuffers::Parser parser;
parser.opts.protobuf_ascii_alike = true;
TEST_EQ(
parser.Parse("table S { B:int; } table T { A:[int]; C:S; } root_type T;"),
true);
TEST_EQ(parser.Parse("{ A [1 2] C { B:2 }}"), true);
// Similarly, in text output, it should omit these.
std::string text;
auto err =
flatbuffers::GenText(
parser, parser.builder_.GetBufferPointer(), &text);
TEST_NULL(err);
TEST_EQ_STR(text.c_str(),
"{\n A [\n 1\n 2\n ]\n C {\n B: 2\n }\n}\n");
}
} // namespace tests
} // namespace flatbuffers
|