From 724bed3901edaea09e94e68afe878f6e804d13ce Mon Sep 17 00:00:00 2001 From: jeffhuang Date: Wed, 6 May 2026 21:32:04 +0000 Subject: [PATCH] test: refine parser and flexbuffers oracles --- tests/flexbuffers_test.cpp | 15 ++++++++++++--- tests/parser_test.cpp | 8 ++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/tests/flexbuffers_test.cpp b/tests/flexbuffers_test.cpp index 6087a0affb..6c9a8a3446 100644 --- a/tests/flexbuffers_test.cpp +++ b/tests/flexbuffers_test.cpp @@ -290,7 +290,10 @@ 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); } @@ -298,7 +301,10 @@ void ParseFlexbuffersFromJsonWithNullTest() { 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); } @@ -306,7 +312,10 @@ void ParseFlexbuffersFromJsonWithNullTest() { 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()); diff --git a/tests/parser_test.cpp b/tests/parser_test.cpp index e03fd96234..086db4e9fb 100644 --- a/tests/parser_test.cpp +++ b/tests/parser_test.cpp @@ -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 }", @@ -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");