Skip to content

Commit 23bb574

Browse files
vglavnyyaardappel
authored andcommitted
Add basic test for enum defaults (google#5280)
1 parent dd6daa7 commit 23bb574

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

tests/test.cpp

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,15 +1305,20 @@ void ErrorTest() {
13051305
TestError("enum X:bool { Y = true }", "must be integral");
13061306
}
13071307

1308-
template<typename T> T TestValue(const char *json, const char *type_name) {
1308+
template<typename T>
1309+
T TestValue(const char *json, const char *type_name,
1310+
const char *decls = nullptr) {
13091311
flatbuffers::Parser parser;
13101312
parser.builder_.ForceDefaults(true); // return defaults
13111313
auto check_default = json ? false : true;
13121314
if (check_default) { parser.opts.output_default_scalars_in_json = true; }
13131315
// Simple schema.
1314-
std::string schema =
1315-
"table X { Y:" + std::string(type_name) + "; } root_type X;";
1316-
TEST_EQ(parser.Parse(schema.c_str()), true);
1316+
std::string schema = std::string(decls ? decls : "") + "\n" +
1317+
"table X { Y:" + std::string(type_name) +
1318+
"; } root_type X;";
1319+
auto schema_done = parser.Parse(schema.c_str());
1320+
TEST_EQ_STR(parser.error_.c_str(), "");
1321+
TEST_EQ(schema_done, true);
13171322

13181323
auto done = parser.Parse(check_default ? "{}" : json);
13191324
TEST_EQ_STR(parser.error_.c_str(), "");
@@ -1439,6 +1444,24 @@ void EnumOutOfRangeTest() {
14391444
TestError("enum X:ulong { Y = 18446744073709551615 }", "constant does not fit");
14401445
}
14411446

1447+
void EnumValueTest() {
1448+
// json: "{ Y:0 }", schema: table X { Y : "E"}
1449+
// 0 in enum (V=0) E then Y=0 is valid.
1450+
TEST_EQ(TestValue<int>("{ Y:0 }", "E", "enum E:int { V }"), 0);
1451+
TEST_EQ(TestValue<int>("{ Y:V }", "E", "enum E:int { V }"), 0);
1452+
// A default value of Y is 0.
1453+
TEST_EQ(TestValue<int>("{ }", "E", "enum E:int { V }"), 0);
1454+
TEST_EQ(TestValue<int>("{ Y:5 }", "E=V", "enum E:int { V=5 }"), 5);
1455+
// Generate json with defaults and check.
1456+
TEST_EQ(TestValue<int>(nullptr, "E=V", "enum E:int { V=5 }"), 5);
1457+
// 5 in enum
1458+
TEST_EQ(TestValue<int>("{ Y:5 }", "E", "enum E:int { Z, V=5 }"), 5);
1459+
TEST_EQ(TestValue<int>("{ Y:5 }", "E=V", "enum E:int { Z, V=5 }"), 5);
1460+
// Generate json with defaults and check.
1461+
TEST_EQ(TestValue<int>(nullptr, "E", "enum E:int { Z, V=5 }"), 0);
1462+
TEST_EQ(TestValue<int>(nullptr, "E=V", "enum E:int { Z, V=5 }"), 5);
1463+
}
1464+
14421465
void IntegerOutOfRangeTest() {
14431466
TestError("table T { F:byte; } root_type T; { F:128 }",
14441467
"constant does not fit");
@@ -2622,6 +2645,7 @@ int FlatBufferTests() {
26222645

26232646
ErrorTest();
26242647
ValueTest();
2648+
EnumValueTest();
26252649
EnumStringsTest();
26262650
EnumNamesTest();
26272651
EnumOutOfRangeTest();

0 commit comments

Comments
 (0)