@@ -1305,15 +1305,20 @@ void ErrorTest() {
1305
1305
TestError (" enum X:bool { Y = true }" , " must be integral" );
1306
1306
}
1307
1307
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 ) {
1309
1311
flatbuffers::Parser parser;
1310
1312
parser.builder_ .ForceDefaults (true ); // return defaults
1311
1313
auto check_default = json ? false : true ;
1312
1314
if (check_default) { parser.opts .output_default_scalars_in_json = true ; }
1313
1315
// 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 );
1317
1322
1318
1323
auto done = parser.Parse (check_default ? " {}" : json);
1319
1324
TEST_EQ_STR (parser.error_ .c_str (), " " );
@@ -1439,6 +1444,24 @@ void EnumOutOfRangeTest() {
1439
1444
TestError (" enum X:ulong { Y = 18446744073709551615 }" , " constant does not fit" );
1440
1445
}
1441
1446
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
+
1442
1465
void IntegerOutOfRangeTest () {
1443
1466
TestError (" table T { F:byte; } root_type T; { F:128 }" ,
1444
1467
" constant does not fit" );
@@ -2622,6 +2645,7 @@ int FlatBufferTests() {
2622
2645
2623
2646
ErrorTest ();
2624
2647
ValueTest ();
2648
+ EnumValueTest ();
2625
2649
EnumStringsTest ();
2626
2650
EnumNamesTest ();
2627
2651
EnumOutOfRangeTest ();
0 commit comments