Files
ArduinoJson/test/JsonParser/nestingLimit.cpp

50 lines
1.5 KiB
C++
Raw Normal View History

// ArduinoJson - arduinojson.org
2018-01-05 09:20:01 +01:00
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
2018-03-09 16:58:01 +01:00
JsonError tryParseArray(const char *json, uint8_t nestingLimit) {
DynamicJsonArray array;
return deserializeJson(array, json, nestingLimit);
}
2018-03-09 16:58:01 +01:00
JsonError tryParseObject(const char *json, uint8_t nestingLimit) {
DynamicJsonObject obj;
return deserializeJson(obj, json, nestingLimit);
}
TEST_CASE("JsonParser nestingLimit") {
SECTION("ParseArrayWithNestingLimit0") {
2018-03-09 16:58:01 +01:00
REQUIRE(tryParseArray("[]", 0) == JsonError::Ok);
REQUIRE(tryParseArray("[[]]", 0) == JsonError::TooDeep);
}
SECTION("ParseArrayWithNestingLimit1") {
2018-03-09 16:58:01 +01:00
REQUIRE(tryParseArray("[[]]", 1) == JsonError::Ok);
REQUIRE(tryParseArray("[[[]]]", 1) == JsonError::TooDeep);
}
SECTION("ParseArrayWithNestingLimit2") {
2018-03-09 16:58:01 +01:00
REQUIRE(tryParseArray("[[[]]]", 2) == JsonError::Ok);
REQUIRE(tryParseArray("[[[[]]]]", 2) == JsonError::TooDeep);
}
SECTION("ParseObjectWithNestingLimit0") {
2018-03-09 16:58:01 +01:00
REQUIRE(tryParseObject("{}", 0) == JsonError::Ok);
REQUIRE(tryParseObject("{\"key\":{}}", 0) == JsonError::TooDeep);
}
SECTION("ParseObjectWithNestingLimit1") {
2018-03-09 16:58:01 +01:00
REQUIRE(tryParseObject("{\"key\":{}}", 1) == JsonError::Ok);
REQUIRE(tryParseObject("{\"key\":{\"key\":{}}}", 1) == JsonError::TooDeep);
}
SECTION("ParseObjectWithNestingLimit2") {
2018-03-09 16:58:01 +01:00
REQUIRE(tryParseObject("{\"key\":{\"key\":{}}}", 2) == JsonError::Ok);
REQUIRE(tryParseObject("{\"key\":{\"key\":{\"key\":{}}}}", 2) ==
JsonError::TooDeep);
}
}