mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-07 05:47:36 +00:00
Refactor test.cpp (#7487)
* Add timing command to cmakelist * Start to split test.cpp. Move flexbuffers tests * Move monster related tests to own file * Move parser related tests to own file * Move json related tests to own file * Move proto related tests to own file * moved more functions to parser test * moved more functions to parser test2 * move monster extra tests to monster test * move evolution tests to own file * move reflection tests to own file * move util tests to own file * rehomed various tests * move optional scalars test to own file: * rehome more tests * move fuzz tests. Got rid of global test_data_path * fixes for CI failures * add bazel files
This commit is contained in:
204
tests/proto_test.cpp
Normal file
204
tests/proto_test.cpp
Normal file
@@ -0,0 +1,204 @@
|
||||
#include "proto_test.h"
|
||||
|
||||
#include "flatbuffers/idl.h"
|
||||
#include "test_assert.h"
|
||||
|
||||
namespace flatbuffers {
|
||||
namespace tests {
|
||||
|
||||
// Parse a .proto schema, output as .fbs
|
||||
void ParseProtoTest(const std::string &tests_data_path) {
|
||||
// load the .proto and the golden file from disk
|
||||
std::string protofile;
|
||||
std::string goldenfile;
|
||||
std::string goldenunionfile;
|
||||
TEST_EQ(
|
||||
flatbuffers::LoadFile((tests_data_path + "prototest/test.proto").c_str(),
|
||||
false, &protofile),
|
||||
true);
|
||||
TEST_EQ(
|
||||
flatbuffers::LoadFile((tests_data_path + "prototest/test.golden").c_str(),
|
||||
false, &goldenfile),
|
||||
true);
|
||||
TEST_EQ(flatbuffers::LoadFile(
|
||||
(tests_data_path + "prototest/test_union.golden").c_str(), false,
|
||||
&goldenunionfile),
|
||||
true);
|
||||
|
||||
flatbuffers::IDLOptions opts;
|
||||
opts.include_dependence_headers = false;
|
||||
opts.proto_mode = true;
|
||||
|
||||
// Parse proto.
|
||||
flatbuffers::Parser parser(opts);
|
||||
auto protopath = tests_data_path + "prototest/";
|
||||
const char *include_directories[] = { protopath.c_str(), nullptr };
|
||||
TEST_EQ(parser.Parse(protofile.c_str(), include_directories), true);
|
||||
|
||||
// Generate fbs.
|
||||
auto fbs = flatbuffers::GenerateFBS(parser, "test");
|
||||
|
||||
// Ensure generated file is parsable.
|
||||
flatbuffers::Parser parser2;
|
||||
TEST_EQ(parser2.Parse(fbs.c_str(), nullptr), true);
|
||||
TEST_EQ_STR(fbs.c_str(), goldenfile.c_str());
|
||||
|
||||
// Parse proto with --oneof-union option.
|
||||
opts.proto_oneof_union = true;
|
||||
flatbuffers::Parser parser3(opts);
|
||||
TEST_EQ(parser3.Parse(protofile.c_str(), include_directories), true);
|
||||
|
||||
// Generate fbs.
|
||||
auto fbs_union = flatbuffers::GenerateFBS(parser3, "test");
|
||||
|
||||
// Ensure generated file is parsable.
|
||||
flatbuffers::Parser parser4;
|
||||
TEST_EQ(parser4.Parse(fbs_union.c_str(), nullptr), true);
|
||||
TEST_EQ_STR(fbs_union.c_str(), goldenunionfile.c_str());
|
||||
}
|
||||
|
||||
// Parse a .proto schema, output as .fbs
|
||||
void ParseProtoTestWithSuffix(const std::string &tests_data_path) {
|
||||
// load the .proto and the golden file from disk
|
||||
std::string protofile;
|
||||
std::string goldenfile;
|
||||
std::string goldenunionfile;
|
||||
TEST_EQ(
|
||||
flatbuffers::LoadFile((tests_data_path + "prototest/test.proto").c_str(),
|
||||
false, &protofile),
|
||||
true);
|
||||
TEST_EQ(flatbuffers::LoadFile(
|
||||
(tests_data_path + "prototest/test_suffix.golden").c_str(), false,
|
||||
&goldenfile),
|
||||
true);
|
||||
TEST_EQ(flatbuffers::LoadFile(
|
||||
(tests_data_path + "prototest/test_union_suffix.golden").c_str(),
|
||||
false, &goldenunionfile),
|
||||
true);
|
||||
|
||||
flatbuffers::IDLOptions opts;
|
||||
opts.include_dependence_headers = false;
|
||||
opts.proto_mode = true;
|
||||
opts.proto_namespace_suffix = "test_namespace_suffix";
|
||||
|
||||
// Parse proto.
|
||||
flatbuffers::Parser parser(opts);
|
||||
auto protopath = tests_data_path + "prototest/";
|
||||
const char *include_directories[] = { protopath.c_str(), nullptr };
|
||||
TEST_EQ(parser.Parse(protofile.c_str(), include_directories), true);
|
||||
|
||||
// Generate fbs.
|
||||
auto fbs = flatbuffers::GenerateFBS(parser, "test");
|
||||
|
||||
// Ensure generated file is parsable.
|
||||
flatbuffers::Parser parser2;
|
||||
TEST_EQ(parser2.Parse(fbs.c_str(), nullptr), true);
|
||||
TEST_EQ_STR(fbs.c_str(), goldenfile.c_str());
|
||||
|
||||
// Parse proto with --oneof-union option.
|
||||
opts.proto_oneof_union = true;
|
||||
flatbuffers::Parser parser3(opts);
|
||||
TEST_EQ(parser3.Parse(protofile.c_str(), include_directories), true);
|
||||
|
||||
// Generate fbs.
|
||||
auto fbs_union = flatbuffers::GenerateFBS(parser3, "test");
|
||||
|
||||
// Ensure generated file is parsable.
|
||||
flatbuffers::Parser parser4;
|
||||
TEST_EQ(parser4.Parse(fbs_union.c_str(), nullptr), true);
|
||||
TEST_EQ_STR(fbs_union.c_str(), goldenunionfile.c_str());
|
||||
}
|
||||
|
||||
// Parse a .proto schema, output as .fbs
|
||||
void ParseProtoTestWithIncludes(const std::string &tests_data_path) {
|
||||
// load the .proto and the golden file from disk
|
||||
std::string protofile;
|
||||
std::string goldenfile;
|
||||
std::string goldenunionfile;
|
||||
std::string importprotofile;
|
||||
TEST_EQ(
|
||||
flatbuffers::LoadFile((tests_data_path + "prototest/test.proto").c_str(),
|
||||
false, &protofile),
|
||||
true);
|
||||
TEST_EQ(flatbuffers::LoadFile(
|
||||
(tests_data_path + "prototest/imported.proto").c_str(), false,
|
||||
&importprotofile),
|
||||
true);
|
||||
TEST_EQ(flatbuffers::LoadFile(
|
||||
(tests_data_path + "prototest/test_include.golden").c_str(),
|
||||
false, &goldenfile),
|
||||
true);
|
||||
TEST_EQ(flatbuffers::LoadFile(
|
||||
(tests_data_path + "prototest/test_union_include.golden").c_str(),
|
||||
false, &goldenunionfile),
|
||||
true);
|
||||
|
||||
flatbuffers::IDLOptions opts;
|
||||
opts.include_dependence_headers = true;
|
||||
opts.proto_mode = true;
|
||||
|
||||
// Parse proto.
|
||||
flatbuffers::Parser parser(opts);
|
||||
auto protopath = tests_data_path + "prototest/";
|
||||
const char *include_directories[] = { protopath.c_str(), nullptr };
|
||||
TEST_EQ(parser.Parse(protofile.c_str(), include_directories), true);
|
||||
|
||||
// Generate fbs.
|
||||
auto fbs = flatbuffers::GenerateFBS(parser, "test");
|
||||
|
||||
// Generate fbs from import.proto
|
||||
flatbuffers::Parser import_parser(opts);
|
||||
TEST_EQ(import_parser.Parse(importprotofile.c_str(), include_directories),
|
||||
true);
|
||||
auto import_fbs = flatbuffers::GenerateFBS(import_parser, "test");
|
||||
|
||||
// Ensure generated file is parsable.
|
||||
flatbuffers::Parser parser2;
|
||||
// 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(protopath) + "/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(), goldenfile.c_str());
|
||||
|
||||
// Parse proto with --oneof-union option.
|
||||
opts.proto_oneof_union = true;
|
||||
flatbuffers::Parser parser3(opts);
|
||||
TEST_EQ(parser3.Parse(protofile.c_str(), include_directories), true);
|
||||
|
||||
// Generate fbs.
|
||||
auto fbs_union = flatbuffers::GenerateFBS(parser3, "test");
|
||||
|
||||
// Ensure generated file is parsable.
|
||||
flatbuffers::Parser parser4;
|
||||
TEST_EQ(parser4.Parse(import_fbs.c_str(), nullptr, imported_fbs.c_str()),
|
||||
true);
|
||||
TEST_EQ(parser4.Parse(fbs_union.c_str(), nullptr), true);
|
||||
TEST_EQ_STR(fbs_union.c_str(), goldenunionfile.c_str());
|
||||
}
|
||||
|
||||
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 ok = flatbuffers::GenerateText(
|
||||
parser, parser.builder_.GetBufferPointer(), &text);
|
||||
TEST_EQ(ok, true);
|
||||
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
|
||||
Reference in New Issue
Block a user