Improved coverage of JsonDeserializer

This commit is contained in:
Benoit Blanchon
2020-02-17 09:15:34 +01:00
parent 85499be855
commit 1902c0ec93
6 changed files with 234 additions and 22 deletions

View File

@ -15,6 +15,14 @@ TEST_CASE("Valid JSON strings value") {
TestCase testCases[] = {
{"\"hello world\"", "hello world"},
{"\'hello world\'", "hello world"},
{"'\"'", "\""},
{"'\\\\'", "\\"},
{"'\\/'", "/"},
{"'\\b'", "\b"},
{"'\\f'", "\f"},
{"'\\n'", "\n"},
{"'\\r'", "\r"},
{"'\\t'", "\t"},
{"\"1\\\"2\\\\3\\/4\\b5\\f6\\n7\\r8\\t9\"", "1\"2\\3/4\b5\f6\n7\r8\t9"},
{"'\\u0041'", "A"},
{"'\\u00e4'", "\xc3\xa4"}, // ä
@ -33,8 +41,8 @@ TEST_CASE("Valid JSON strings value") {
const TestCase& testCase = testCases[i];
CAPTURE(testCase.input);
DeserializationError err = deserializeJson(doc, testCase.input);
REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.as<std::string>() == testCase.expectedOutput);
CHECK(err == DeserializationError::Ok);
CHECK(doc.as<std::string>() == testCase.expectedOutput);
}
}
@ -54,7 +62,7 @@ TEST_CASE("Truncated JSON string") {
TEST_CASE("Invalid JSON string") {
const char* testCases[] = {"'\\u'", "'\\u000g'", "'\\u000'",
"'\\u000G'", "'\\u000/'", "\\x1234"};
"'\\u000G'", "'\\u000/'", "'\\x1234'"};
const size_t testCount = sizeof(testCases) / sizeof(testCases[0]);
DynamicJsonDocument doc(4096);
@ -67,9 +75,15 @@ TEST_CASE("Invalid JSON string") {
}
TEST_CASE("Not enough room to duplicate the string") {
DynamicJsonDocument doc(4);
DynamicJsonDocument doc(JSON_OBJECT_SIZE(0));
REQUIRE(deserializeJson(doc, "\"hello world!\"") ==
DeserializationError::NoMemory);
REQUIRE(doc.isNull() == true);
SECTION("Quoted string") {
REQUIRE(deserializeJson(doc, "{\"example\":1}") ==
DeserializationError::NoMemory);
}
SECTION("Non-quoted string") {
REQUIRE(deserializeJson(doc, "{example:1}") ==
DeserializationError::NoMemory);
}
}