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
|
#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
|