JSON parsing & text generation is now enum-identifier aware.

When Parsing JSON, it will read enums either as int values, identifiers
specific to the enum type, or strings containing those identifiers.

When generating text, it will output enum identifiers by default
(this can be turned off in favor of integers, like before).

Change-Id: If28b0a1f8f27de79aff3e626f40c0c0b271c325a
Tested: on Windows and Linux
Bug: 16214968
This commit is contained in:
Wouter van Oortmerssen
2014-07-14 17:49:04 -07:00
parent 2811a3eac8
commit 3fb6a86d02
6 changed files with 65 additions and 37 deletions

View File

@@ -465,7 +465,8 @@ void ErrorTest() {
TestError("table X { Y:int; } root_type X; { Z:", "unknown field");
TestError("struct X { Y:int; Z:int; } table W { V:X; } root_type W; "
"{ V:{ Y:1 } }", "incomplete");
TestError("table X { Y:byte; } root_type X; { Y:U }", "valid enum");
TestError("enum E:byte { A } table X { Y:E; } root_type X; { Y:U }",
"unknown enum value");
TestError("table X { Y:byte; } root_type X; { Y:; }", "starting");
TestError("enum X:byte { Y } enum X {", "enum already");
TestError("enum X:float {}", "underlying");
@@ -482,7 +483,7 @@ void ErrorTest() {
}
// Additional parser testing not covered elsewhere.
void TokenTest() {
void ScientificTest() {
flatbuffers::Parser parser;
// Simple schema.
@@ -496,6 +497,15 @@ void TokenTest() {
TEST_EQ(fabs(root[1] - 3.14159) < 0.001, true);
}
void EnumStringsTest() {
flatbuffers::Parser parser;
TEST_EQ(parser.Parse("enum E:byte { A, B, C } table T { F:[E]; } root_type T;"
"{ F:[ A, B, \"C\" ] }"), true);
}
int main(int /*argc*/, const char * /*argv*/[]) {
// Run our various test suites:
@@ -510,7 +520,8 @@ int main(int /*argc*/, const char * /*argv*/[]) {
FuzzTest2();
ErrorTest();
TokenTest();
ScientificTest();
EnumStringsTest();
if (!testing_fails) {
TEST_OUTPUT_LINE("ALL TESTS PASSED");