Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions tests/flexbuffers_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,23 +290,32 @@ void ParseFlexbuffersFromJsonWithNullTest() {
char json[] = "{\"opt_field\": 123 }";
flatbuffers::Parser parser;
flexbuffers::Builder flexbuild;
parser.ParseFlexBuffer(json, nullptr, &flexbuild);
TEST_EQ(parser.ParseFlexBuffer(json, nullptr, &flexbuild), true);
TEST_EQ(flexbuffers::VerifyBuffer(flexbuild.GetBuffer().data(),
flexbuild.GetBuffer().size(), nullptr),
true);
auto root = flexbuffers::GetRoot(flexbuild.GetBuffer());
TEST_EQ(root.AsMap()["opt_field"].AsInt64(), 123);
}
{
char json[] = "{\"opt_field\": 123.4 }";
flatbuffers::Parser parser;
flexbuffers::Builder flexbuild;
parser.ParseFlexBuffer(json, nullptr, &flexbuild);
TEST_EQ(parser.ParseFlexBuffer(json, nullptr, &flexbuild), true);
TEST_EQ(flexbuffers::VerifyBuffer(flexbuild.GetBuffer().data(),
flexbuild.GetBuffer().size(), nullptr),
true);
auto root = flexbuffers::GetRoot(flexbuild.GetBuffer());
TEST_EQ(root.AsMap()["opt_field"].AsDouble(), 123.4);
}
{
char json[] = "{\"opt_field\": null }";
flatbuffers::Parser parser;
flexbuffers::Builder flexbuild;
parser.ParseFlexBuffer(json, nullptr, &flexbuild);
TEST_EQ(parser.ParseFlexBuffer(json, nullptr, &flexbuild), true);
TEST_EQ(flexbuffers::VerifyBuffer(flexbuild.GetBuffer().data(),
flexbuild.GetBuffer().size(), nullptr),
true);
auto root = flexbuffers::GetRoot(flexbuild.GetBuffer());
TEST_ASSERT(!root.AsMap().IsTheEmptyMap());
TEST_ASSERT(root.AsMap()["opt_field"].IsNull());
Expand Down
8 changes: 8 additions & 0 deletions tests/parser_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ void ErrorTest() {
TestError("table Y {} table X { Y:int; }", "same as table");
TestError("struct X { Y:string; }", "only scalar");
TestError("struct X { a:uint = 42; }", "default values");
TestError("table X { Y:int (required); }",
"only non-scalar fields in tables may be 'required'");
TestError("struct X { Y:int (required); }",
"struct fields are always required");
TestError("enum Y:byte { Z = 1 } table X { y:Y; }", "not part of enum");
TestError("struct X { Y:int (deprecated); }", "deprecate");
TestError("union Z { X } table X { Y:Z; } root_type X; { Y: {}, A:1 }",
Expand Down Expand Up @@ -107,6 +111,10 @@ void ErrorTest() {
TestError("union Z { X } struct X { Y:int; }", "only tables");
TestError("table X { Y:[int]; YLength:int; }", "clash");
TestError("table X { Y:byte; } root_type X; { Y:1, Y:2 }", "more than once");
TestError("table X { Y:string (required); } root_type X; { }",
"required field is missing: Y in X");
TestError("table Y { } table X { Z:Y (required); } root_type X; { }",
"required field is missing: Z in X");
// float to integer conversion is forbidden
TestError("table X { Y:int; } root_type X; { Y:1.0 }", "float");
TestError("table X { Y:bool; } root_type X; { Y:1.0 }", "float");
Expand Down