forked from bblanchon/ArduinoJson
Don't use JsonBuffer to create or parse objects and arrays.
* Added DynamicJsonArray and StaticJsonArray * Added DynamicJsonObject and StaticJsonObject * Added DynamicJsonVariant and StaticJsonVariant * Added deserializeJson() * Removed JsonBuffer::parseArray(), parseObject() and parse() * Removed JsonBuffer::createArray() and createObject()
This commit is contained in:
15
test/JsonParser/CMakeLists.txt
Normal file
15
test/JsonParser/CMakeLists.txt
Normal file
@ -0,0 +1,15 @@
|
||||
# ArduinoJson - arduinojson.org
|
||||
# Copyright Benoit Blanchon 2014-2018
|
||||
# MIT License
|
||||
|
||||
add_executable(JsonParserTests
|
||||
JsonArray.cpp
|
||||
JsonObject.cpp
|
||||
JsonVariant.cpp
|
||||
nestingLimit.cpp
|
||||
StaticJsonArray.cpp
|
||||
StaticJsonObject.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(JsonParserTests catch)
|
||||
add_test(JsonParser JsonParserTests)
|
346
test/JsonParser/JsonArray.cpp
Normal file
346
test/JsonParser/JsonArray.cpp
Normal file
@ -0,0 +1,346 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("deserializeJson(JsonArray&)") {
|
||||
DynamicJsonArray arr;
|
||||
|
||||
SECTION("EmptyArray") {
|
||||
bool success = deserializeJson(arr, "[]");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(0 == arr.size());
|
||||
}
|
||||
|
||||
SECTION("MissingOpeningBracket") {
|
||||
bool success = deserializeJson(arr, "]");
|
||||
REQUIRE_FALSE(success == true);
|
||||
}
|
||||
|
||||
SECTION("ArrayWithNoEnd") {
|
||||
bool success = deserializeJson(arr, "[");
|
||||
REQUIRE_FALSE(success == true);
|
||||
}
|
||||
|
||||
SECTION("EmptyArrayWithLeadingSpaces") {
|
||||
bool success = deserializeJson(arr, " []");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(0 == arr.size());
|
||||
}
|
||||
|
||||
SECTION("Garbage") {
|
||||
bool success = deserializeJson(arr, "%*$£¤");
|
||||
|
||||
REQUIRE_FALSE(success == true);
|
||||
}
|
||||
|
||||
SECTION("OneInteger") {
|
||||
bool success = deserializeJson(arr, "[42]");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(1 == arr.size());
|
||||
REQUIRE(arr[0] == 42);
|
||||
}
|
||||
|
||||
SECTION("OneIntegerWithSpacesBefore") {
|
||||
bool success = deserializeJson(arr, "[ \t\r\n42]");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(1 == arr.size());
|
||||
REQUIRE(arr[0] == 42);
|
||||
}
|
||||
|
||||
SECTION("OneIntegerWithSpaceAfter") {
|
||||
bool success = deserializeJson(arr, "[42 \t\r\n]");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(1 == arr.size());
|
||||
REQUIRE(arr[0] == 42);
|
||||
}
|
||||
|
||||
SECTION("TwoIntegers") {
|
||||
bool success = deserializeJson(arr, "[42,84]");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(2 == arr.size());
|
||||
REQUIRE(arr[0] == 42);
|
||||
REQUIRE(arr[1] == 84);
|
||||
}
|
||||
|
||||
SECTION("TwoDoubles") {
|
||||
bool success = deserializeJson(arr, "[4.2,1e2]");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(2 == arr.size());
|
||||
REQUIRE(arr[0] == 4.2);
|
||||
REQUIRE(arr[1] == 1e2);
|
||||
}
|
||||
|
||||
SECTION("UnsignedLong") {
|
||||
bool success = deserializeJson(arr, "[4294967295]");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(1 == arr.size());
|
||||
REQUIRE(arr[0] == 4294967295UL);
|
||||
}
|
||||
|
||||
SECTION("TwoBooleans") {
|
||||
bool success = deserializeJson(arr, "[true,false]");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(2 == arr.size());
|
||||
REQUIRE(arr[0] == true);
|
||||
REQUIRE(arr[1] == false);
|
||||
}
|
||||
|
||||
SECTION("TwoNulls") {
|
||||
bool success = deserializeJson(arr, "[null,null]");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(2 == arr.size());
|
||||
REQUIRE(arr[0].as<char *>() == 0);
|
||||
REQUIRE(arr[1].as<char *>() == 0);
|
||||
}
|
||||
|
||||
SECTION("TwoStringsDoubleQuotes") {
|
||||
bool success = deserializeJson(arr, "[ \"hello\" , \"world\" ]");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(2 == arr.size());
|
||||
REQUIRE(arr[0] == "hello");
|
||||
REQUIRE(arr[1] == "world");
|
||||
}
|
||||
|
||||
SECTION("TwoStringsSingleQuotes") {
|
||||
bool success = deserializeJson(arr, "[ 'hello' , 'world' ]");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(2 == arr.size());
|
||||
REQUIRE(arr[0] == "hello");
|
||||
REQUIRE(arr[1] == "world");
|
||||
}
|
||||
|
||||
SECTION("TwoStringsNoQuotes") {
|
||||
bool success = deserializeJson(arr, "[ hello , world ]");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(2 == arr.size());
|
||||
REQUIRE(arr[0] == "hello");
|
||||
REQUIRE(arr[1] == "world");
|
||||
}
|
||||
|
||||
SECTION("EmptyStringsDoubleQuotes") {
|
||||
bool success = deserializeJson(arr, "[\"\",\"\"]");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(2 == arr.size());
|
||||
REQUIRE(arr[0] == "");
|
||||
REQUIRE(arr[1] == "");
|
||||
}
|
||||
|
||||
SECTION("EmptyStringSingleQuotes") {
|
||||
bool success = deserializeJson(arr, "[\'\',\'\']");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(2 == arr.size());
|
||||
REQUIRE(arr[0] == "");
|
||||
REQUIRE(arr[1] == "");
|
||||
}
|
||||
|
||||
SECTION("EmptyStringNoQuotes") {
|
||||
bool success = deserializeJson(arr, "[,]");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(2 == arr.size());
|
||||
REQUIRE(arr[0] == "");
|
||||
REQUIRE(arr[1] == "");
|
||||
}
|
||||
|
||||
SECTION("ClosingDoubleQuoteMissing") {
|
||||
bool success = deserializeJson(arr, "[\"]");
|
||||
|
||||
REQUIRE_FALSE(success == true);
|
||||
}
|
||||
|
||||
SECTION("ClosingSignleQuoteMissing") {
|
||||
bool success = deserializeJson(arr, "[\']");
|
||||
|
||||
REQUIRE_FALSE(success == true);
|
||||
}
|
||||
|
||||
SECTION("StringWithEscapedChars") {
|
||||
bool success =
|
||||
deserializeJson(arr, "[\"1\\\"2\\\\3\\/4\\b5\\f6\\n7\\r8\\t9\"]");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(1 == arr.size());
|
||||
REQUIRE(arr[0] == "1\"2\\3/4\b5\f6\n7\r8\t9");
|
||||
}
|
||||
|
||||
SECTION("StringWithUnterminatedEscapeSequence") {
|
||||
bool success = deserializeJson(arr, "\"\\\0\"", 4);
|
||||
REQUIRE_FALSE(success == true);
|
||||
}
|
||||
|
||||
SECTION("CCommentBeforeOpeningBracket") {
|
||||
bool success = deserializeJson(arr, "/*COMMENT*/ [\"hello\"]");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(1 == arr.size());
|
||||
REQUIRE(arr[0] == "hello");
|
||||
}
|
||||
|
||||
SECTION("CCommentAfterOpeningBracket") {
|
||||
bool success = deserializeJson(arr, "[/*COMMENT*/ \"hello\"]");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(1 == arr.size());
|
||||
REQUIRE(arr[0] == "hello");
|
||||
}
|
||||
|
||||
SECTION("CCommentBeforeClosingBracket") {
|
||||
bool success = deserializeJson(arr, "[\"hello\"/*COMMENT*/]");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(1 == arr.size());
|
||||
REQUIRE(arr[0] == "hello");
|
||||
}
|
||||
|
||||
SECTION("CCommentAfterClosingBracket") {
|
||||
bool success = deserializeJson(arr, "[\"hello\"]/*COMMENT*/");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(1 == arr.size());
|
||||
REQUIRE(arr[0] == "hello");
|
||||
}
|
||||
|
||||
SECTION("CCommentBeforeComma") {
|
||||
bool success = deserializeJson(arr, "[\"hello\"/*COMMENT*/,\"world\"]");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(2 == arr.size());
|
||||
REQUIRE(arr[0] == "hello");
|
||||
REQUIRE(arr[1] == "world");
|
||||
}
|
||||
|
||||
SECTION("CCommentAfterComma") {
|
||||
bool success = deserializeJson(arr, "[\"hello\",/*COMMENT*/ \"world\"]");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(2 == arr.size());
|
||||
REQUIRE(arr[0] == "hello");
|
||||
REQUIRE(arr[1] == "world");
|
||||
}
|
||||
|
||||
SECTION("CppCommentBeforeOpeningBracket") {
|
||||
bool success = deserializeJson(arr, "//COMMENT\n\t[\"hello\"]");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(1 == arr.size());
|
||||
REQUIRE(arr[0] == "hello");
|
||||
}
|
||||
|
||||
SECTION("CppCommentAfterOpeningBracket") {
|
||||
bool success = deserializeJson(arr, "[//COMMENT\n\"hello\"]");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(1 == arr.size());
|
||||
REQUIRE(arr[0] == "hello");
|
||||
}
|
||||
|
||||
SECTION("CppCommentBeforeClosingBracket") {
|
||||
bool success = deserializeJson(arr, "[\"hello\"//COMMENT\r\n]");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(1 == arr.size());
|
||||
REQUIRE(arr[0] == "hello");
|
||||
}
|
||||
|
||||
SECTION("CppCommentAfterClosingBracket") {
|
||||
bool success = deserializeJson(arr, "[\"hello\"]//COMMENT\n");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(1 == arr.size());
|
||||
REQUIRE(arr[0] == "hello");
|
||||
}
|
||||
|
||||
SECTION("CppCommentBeforeComma") {
|
||||
bool success = deserializeJson(arr, "[\"hello\"//COMMENT\n,\"world\"]");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(2 == arr.size());
|
||||
REQUIRE(arr[0] == "hello");
|
||||
REQUIRE(arr[1] == "world");
|
||||
}
|
||||
|
||||
SECTION("CppCommentAfterComma") {
|
||||
bool success = deserializeJson(arr, "[\"hello\",//COMMENT\n\"world\"]");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(2 == arr.size());
|
||||
REQUIRE(arr[0] == "hello");
|
||||
REQUIRE(arr[1] == "world");
|
||||
}
|
||||
|
||||
SECTION("InvalidCppComment") {
|
||||
bool success = deserializeJson(arr, "[/COMMENT\n]");
|
||||
REQUIRE_FALSE(success == true);
|
||||
}
|
||||
|
||||
SECTION("InvalidComment") {
|
||||
bool success = deserializeJson(arr, "[/*/\n]");
|
||||
REQUIRE_FALSE(success == true);
|
||||
}
|
||||
|
||||
SECTION("UnfinishedCComment") {
|
||||
bool success = deserializeJson(arr, "[/*COMMENT]");
|
||||
REQUIRE_FALSE(success == true);
|
||||
}
|
||||
|
||||
SECTION("EndsInCppComment") {
|
||||
bool success = deserializeJson(arr, "[//COMMENT");
|
||||
REQUIRE_FALSE(success == true);
|
||||
}
|
||||
|
||||
SECTION("AfterClosingStar") {
|
||||
bool success = deserializeJson(arr, "[/*COMMENT*");
|
||||
REQUIRE_FALSE(success == true);
|
||||
}
|
||||
|
||||
SECTION("DeeplyNested") {
|
||||
bool success = deserializeJson(
|
||||
arr, "[[[[[[[[[[[[[[[[[[[\"Not too deep\"]]]]]]]]]]]]]]]]]]]");
|
||||
REQUIRE(success == true);
|
||||
}
|
||||
|
||||
SECTION("ObjectNestedInArray") {
|
||||
char jsonString[] =
|
||||
" [ { \"a\" : 1 , \"b\" : 2 } , { \"c\" : 3 , \"d\" : 4 } ] ";
|
||||
|
||||
bool success = deserializeJson(arr, jsonString);
|
||||
|
||||
JsonObject &object1 = arr[0];
|
||||
const JsonObject &object2 = arr[1];
|
||||
JsonObject &object3 = arr[2];
|
||||
|
||||
REQUIRE(true == success);
|
||||
|
||||
REQUIRE(true == object1.success());
|
||||
REQUIRE(true == object2.success());
|
||||
REQUIRE(false == object3.success());
|
||||
|
||||
REQUIRE(2 == object1.size());
|
||||
REQUIRE(2 == object2.size());
|
||||
REQUIRE(0 == object3.size());
|
||||
|
||||
REQUIRE(1 == object1["a"].as<int>());
|
||||
REQUIRE(2 == object1["b"].as<int>());
|
||||
REQUIRE(3 == object2["c"].as<int>());
|
||||
REQUIRE(4 == object2["d"].as<int>());
|
||||
REQUIRE(0 == object3["e"].as<int>());
|
||||
}
|
||||
}
|
197
test/JsonParser/JsonObject.cpp
Normal file
197
test/JsonParser/JsonObject.cpp
Normal file
@ -0,0 +1,197 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("deserializeJson(JsonObject&)") {
|
||||
DynamicJsonObject obj;
|
||||
|
||||
SECTION("An empty object") {
|
||||
bool success = deserializeJson(obj, "{}");
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(obj.size() == 0);
|
||||
}
|
||||
|
||||
SECTION("Quotes") {
|
||||
SECTION("Double quotes") {
|
||||
bool success = deserializeJson(obj, "{\"key\":\"value\"}");
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(obj.size() == 1);
|
||||
REQUIRE(obj["key"] == "value");
|
||||
}
|
||||
|
||||
SECTION("Single quotes") {
|
||||
bool success = deserializeJson(obj, "{'key':'value'}");
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(obj.size() == 1);
|
||||
REQUIRE(obj["key"] == "value");
|
||||
}
|
||||
|
||||
SECTION("No quotes") {
|
||||
bool success = deserializeJson(obj, "{key:value}");
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(obj.size() == 1);
|
||||
REQUIRE(obj["key"] == "value");
|
||||
}
|
||||
|
||||
SECTION("No quotes, allow underscore in key") {
|
||||
bool success = deserializeJson(obj, "{_k_e_y_:42}");
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(obj.size() == 1);
|
||||
REQUIRE(obj["_k_e_y_"] == 42);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("Spaces") {
|
||||
SECTION("Before the key") {
|
||||
bool success = deserializeJson(obj, "{ \"key\":\"value\"}");
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(obj.size() == 1);
|
||||
REQUIRE(obj["key"] == "value");
|
||||
}
|
||||
|
||||
SECTION("After the key") {
|
||||
bool success = deserializeJson(obj, "{\"key\" :\"value\"}");
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(obj.size() == 1);
|
||||
REQUIRE(obj["key"] == "value");
|
||||
}
|
||||
|
||||
SECTION("Before the value") {
|
||||
bool success = deserializeJson(obj, "{\"key\": \"value\"}");
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(obj.size() == 1);
|
||||
REQUIRE(obj["key"] == "value");
|
||||
}
|
||||
|
||||
SECTION("After the value") {
|
||||
bool success = deserializeJson(obj, "{\"key\":\"value\" }");
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(obj.size() == 1);
|
||||
REQUIRE(obj["key"] == "value");
|
||||
}
|
||||
|
||||
SECTION("Before the colon") {
|
||||
bool success =
|
||||
deserializeJson(obj, "{\"key1\":\"value1\" ,\"key2\":\"value2\"}");
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(obj.size() == 2);
|
||||
REQUIRE(obj["key1"] == "value1");
|
||||
REQUIRE(obj["key2"] == "value2");
|
||||
}
|
||||
|
||||
SECTION("After the colon") {
|
||||
bool success =
|
||||
deserializeJson(obj, "{\"key1\":\"value1\" ,\"key2\":\"value2\"}");
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(obj.size() == 2);
|
||||
REQUIRE(obj["key1"] == "value1");
|
||||
REQUIRE(obj["key2"] == "value2");
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("Values types") {
|
||||
SECTION("String") {
|
||||
bool success =
|
||||
deserializeJson(obj, "{\"key1\":\"value1\",\"key2\":\"value2\"}");
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(obj.size() == 2);
|
||||
REQUIRE(obj["key1"] == "value1");
|
||||
REQUIRE(obj["key2"] == "value2");
|
||||
}
|
||||
|
||||
SECTION("Integer") {
|
||||
bool success = deserializeJson(obj, "{\"key1\":42,\"key2\":-42}");
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(obj.size() == 2);
|
||||
REQUIRE(obj["key1"] == 42);
|
||||
REQUIRE(obj["key2"] == -42);
|
||||
}
|
||||
|
||||
SECTION("Double") {
|
||||
bool success = deserializeJson(obj, "{\"key1\":12.345,\"key2\":-7E89}");
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(obj.size() == 2);
|
||||
REQUIRE(obj["key1"] == 12.345);
|
||||
REQUIRE(obj["key2"] == -7E89);
|
||||
}
|
||||
|
||||
SECTION("Booleans") {
|
||||
bool success = deserializeJson(obj, "{\"key1\":true,\"key2\":false}");
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(obj.size() == 2);
|
||||
REQUIRE(obj["key1"] == true);
|
||||
REQUIRE(obj["key2"] == false);
|
||||
}
|
||||
|
||||
SECTION("Null") {
|
||||
bool success = deserializeJson(obj, "{\"key1\":null,\"key2\":null}");
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(obj.size() == 2);
|
||||
REQUIRE(obj["key1"].as<char *>() == 0);
|
||||
REQUIRE(obj["key2"].as<char *>() == 0);
|
||||
}
|
||||
|
||||
SECTION("Array") {
|
||||
char jsonString[] = " { \"ab\" : [ 1 , 2 ] , \"cd\" : [ 3 , 4 ] } ";
|
||||
|
||||
bool success = deserializeJson(obj, jsonString);
|
||||
|
||||
JsonArray &array1 = obj["ab"];
|
||||
const JsonArray &array2 = obj["cd"];
|
||||
JsonArray &array3 = obj["ef"];
|
||||
|
||||
REQUIRE(true == success);
|
||||
|
||||
REQUIRE(true == array1.success());
|
||||
REQUIRE(true == array2.success());
|
||||
REQUIRE(false == array3.success());
|
||||
|
||||
REQUIRE(2 == array1.size());
|
||||
REQUIRE(2 == array2.size());
|
||||
REQUIRE(0 == array3.size());
|
||||
|
||||
REQUIRE(1 == array1[0].as<int>());
|
||||
REQUIRE(2 == array1[1].as<int>());
|
||||
|
||||
REQUIRE(3 == array2[0].as<int>());
|
||||
REQUIRE(4 == array2[1].as<int>());
|
||||
|
||||
REQUIRE(0 == array3[0].as<int>());
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("Misc") {
|
||||
SECTION("The opening brace is missing") {
|
||||
bool success = deserializeJson(obj, "}");
|
||||
REQUIRE(success == false);
|
||||
}
|
||||
|
||||
SECTION("The closing brace is missing") {
|
||||
bool success = deserializeJson(obj, "{");
|
||||
REQUIRE(success == false);
|
||||
}
|
||||
|
||||
SECTION("A quoted key without value") {
|
||||
bool success = deserializeJson(obj, "{\"key\"}");
|
||||
REQUIRE(success == false);
|
||||
}
|
||||
|
||||
SECTION("A non-quoted key without value") {
|
||||
bool success = deserializeJson(obj, "{key}");
|
||||
REQUIRE(success == false);
|
||||
}
|
||||
|
||||
SECTION("A dangling comma") {
|
||||
bool success = deserializeJson(obj, "{\"key1\":\"value1\",}");
|
||||
REQUIRE(success == false);
|
||||
}
|
||||
|
||||
SECTION("null as a key") {
|
||||
bool success = deserializeJson(obj, "null:\"value\"}");
|
||||
REQUIRE(success == false);
|
||||
}
|
||||
}
|
||||
}
|
90
test/JsonParser/JsonVariant.cpp
Normal file
90
test/JsonParser/JsonVariant.cpp
Normal file
@ -0,0 +1,90 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
using namespace Catch::Matchers;
|
||||
|
||||
TEST_CASE("deserializeJson(JsonVariant&)") {
|
||||
DynamicJsonVariant variant;
|
||||
|
||||
SECTION("EmptyObject") {
|
||||
bool success = deserializeJson(variant, "{}");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(variant.is<JsonObject>());
|
||||
}
|
||||
|
||||
SECTION("EmptyArray") {
|
||||
bool success = deserializeJson(variant, "[]");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(variant.is<JsonArray>());
|
||||
}
|
||||
|
||||
SECTION("Integer") {
|
||||
bool success = deserializeJson(variant, "-42");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(variant.is<int>());
|
||||
REQUIRE_FALSE(variant.is<bool>());
|
||||
REQUIRE(variant == -42);
|
||||
}
|
||||
|
||||
SECTION("Double") {
|
||||
bool success = deserializeJson(variant, "-1.23e+4");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE_FALSE(variant.is<int>());
|
||||
REQUIRE(variant.is<double>());
|
||||
REQUIRE(variant.as<double>() == Approx(-1.23e+4));
|
||||
}
|
||||
|
||||
SECTION("Double quoted string") {
|
||||
bool success = deserializeJson(variant, "\"hello world\"");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(variant.is<char*>());
|
||||
REQUIRE_THAT(variant.as<char*>(), Equals("hello world"));
|
||||
}
|
||||
|
||||
SECTION("Single quoted string") {
|
||||
bool success = deserializeJson(variant, "\'hello world\'");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(variant.is<char*>());
|
||||
REQUIRE_THAT(variant.as<char*>(), Equals("hello world"));
|
||||
}
|
||||
|
||||
SECTION("True") {
|
||||
bool success = deserializeJson(variant, "true");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(variant.is<bool>());
|
||||
REQUIRE(variant == true);
|
||||
}
|
||||
|
||||
SECTION("False") {
|
||||
bool success = deserializeJson(variant, "false");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(variant.is<bool>());
|
||||
REQUIRE(variant == false);
|
||||
}
|
||||
|
||||
SECTION("OpenBrace") {
|
||||
bool success = deserializeJson(variant, "{");
|
||||
|
||||
REQUIRE(success == false);
|
||||
}
|
||||
|
||||
SECTION("Incomplete string") {
|
||||
bool success = deserializeJson(variant, "\"hello");
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(variant.is<char*>());
|
||||
REQUIRE_THAT(variant.as<char*>(), Equals("hello"));
|
||||
}
|
||||
}
|
79
test/JsonParser/StaticJsonArray.cpp
Normal file
79
test/JsonParser/StaticJsonArray.cpp
Normal file
@ -0,0 +1,79 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("deserializeJson(StaticJsonArray&)") {
|
||||
SECTION("BufferOfTheRightSizeForEmptyArray") {
|
||||
StaticJsonArray<JSON_ARRAY_SIZE(0)> arr;
|
||||
char input[] = "[]";
|
||||
|
||||
bool success = deserializeJson(arr, input);
|
||||
|
||||
REQUIRE(success == true);
|
||||
}
|
||||
|
||||
SECTION("TooSmallBufferForArrayWithOneValue") {
|
||||
StaticJsonArray<JSON_ARRAY_SIZE(1) - 1> arr;
|
||||
char input[] = "[1]";
|
||||
|
||||
bool success = deserializeJson(arr, input);
|
||||
|
||||
REQUIRE(success == false);
|
||||
}
|
||||
|
||||
SECTION("BufferOfTheRightSizeForArrayWithOneValue") {
|
||||
StaticJsonArray<JSON_ARRAY_SIZE(1)> arr;
|
||||
char input[] = "[1]";
|
||||
|
||||
bool success = deserializeJson(arr, input);
|
||||
|
||||
REQUIRE(success == true);
|
||||
}
|
||||
|
||||
SECTION("TooSmallBufferForArrayWithNestedObject") {
|
||||
StaticJsonArray<JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(0) - 1> arr;
|
||||
char input[] = "[{}]";
|
||||
|
||||
bool success = deserializeJson(arr, input);
|
||||
|
||||
REQUIRE(success == false);
|
||||
}
|
||||
|
||||
SECTION("BufferOfTheRightSizeForArrayWithNestedObject") {
|
||||
StaticJsonArray<JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(0)> arr;
|
||||
char input[] = "[{}]";
|
||||
|
||||
bool success = deserializeJson(arr, input);
|
||||
|
||||
REQUIRE(success == true);
|
||||
}
|
||||
|
||||
SECTION("CharPtrNull") {
|
||||
StaticJsonArray<100> arr;
|
||||
|
||||
bool success = deserializeJson(arr, static_cast<char*>(0));
|
||||
|
||||
REQUIRE(success == false);
|
||||
}
|
||||
|
||||
SECTION("ConstCharPtrNull") {
|
||||
StaticJsonArray<100> arr;
|
||||
|
||||
bool success = deserializeJson(arr, static_cast<const char*>(0));
|
||||
|
||||
REQUIRE(success == false);
|
||||
}
|
||||
|
||||
SECTION("CopyStringNotSpaces") {
|
||||
StaticJsonArray<100> arr;
|
||||
|
||||
deserializeJson(arr, " [ \"1234567\" ] ");
|
||||
|
||||
REQUIRE(JSON_ARRAY_SIZE(1) + sizeof("1234567") == arr.memoryUsage());
|
||||
// note: we use a string of 8 bytes to be sure that the StaticJsonBuffer
|
||||
// will not insert bytes to enforce alignement
|
||||
}
|
||||
}
|
69
test/JsonParser/StaticJsonObject.cpp
Normal file
69
test/JsonParser/StaticJsonObject.cpp
Normal file
@ -0,0 +1,69 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("deserializeJson(StaticJsonObject&)") {
|
||||
SECTION("BufferOfTheRightSizeForEmptyObject") {
|
||||
StaticJsonObject<JSON_OBJECT_SIZE(0)> obj;
|
||||
char input[] = "{}";
|
||||
|
||||
bool success = deserializeJson(obj, input);
|
||||
|
||||
REQUIRE(success == true);
|
||||
}
|
||||
|
||||
SECTION("TooSmallBufferForObjectWithOneValue") {
|
||||
StaticJsonObject<JSON_OBJECT_SIZE(1) - 1> obj;
|
||||
char input[] = "{\"a\":1}";
|
||||
|
||||
bool success = deserializeJson(obj, input);
|
||||
|
||||
REQUIRE(success == false);
|
||||
}
|
||||
|
||||
SECTION("BufferOfTheRightSizeForObjectWithOneValue") {
|
||||
StaticJsonObject<JSON_OBJECT_SIZE(1)> obj;
|
||||
char input[] = "{\"a\":1}";
|
||||
|
||||
bool success = deserializeJson(obj, input);
|
||||
|
||||
REQUIRE(success == true);
|
||||
}
|
||||
|
||||
SECTION("TooSmallBufferForObjectWithNestedObject") {
|
||||
StaticJsonObject<JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(0) - 1> obj;
|
||||
char input[] = "{\"a\":[]}";
|
||||
|
||||
bool success = deserializeJson(obj, input);
|
||||
|
||||
REQUIRE(success == false);
|
||||
}
|
||||
|
||||
SECTION("BufferOfTheRightSizeForObjectWithNestedObject") {
|
||||
StaticJsonObject<JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(0)> obj;
|
||||
char input[] = "{\"a\":[]}";
|
||||
|
||||
bool success = deserializeJson(obj, input);
|
||||
|
||||
REQUIRE(success == true);
|
||||
}
|
||||
|
||||
SECTION("CharPtrNull") {
|
||||
StaticJsonObject<100> obj;
|
||||
|
||||
bool success = deserializeJson(obj, static_cast<char*>(0));
|
||||
|
||||
REQUIRE(success == false);
|
||||
}
|
||||
|
||||
SECTION("ConstCharPtrNull") {
|
||||
StaticJsonObject<100> obj;
|
||||
|
||||
bool success = deserializeJson(obj, static_cast<const char*>(0));
|
||||
|
||||
REQUIRE(success == false);
|
||||
}
|
||||
}
|
48
test/JsonParser/nestingLimit.cpp
Normal file
48
test/JsonParser/nestingLimit.cpp
Normal file
@ -0,0 +1,48 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
bool tryParseArray(const char *json, uint8_t nestingLimit) {
|
||||
DynamicJsonArray array;
|
||||
return deserializeJson(array, json, nestingLimit);
|
||||
}
|
||||
|
||||
bool tryParseObject(const char *json, uint8_t nestingLimit) {
|
||||
DynamicJsonObject obj;
|
||||
return deserializeJson(obj, json, nestingLimit);
|
||||
}
|
||||
|
||||
TEST_CASE("JsonParser nestingLimit") {
|
||||
SECTION("ParseArrayWithNestingLimit0") {
|
||||
REQUIRE(true == tryParseArray("[]", 0));
|
||||
REQUIRE(false == tryParseArray("[[]]", 0));
|
||||
}
|
||||
|
||||
SECTION("ParseArrayWithNestingLimit1") {
|
||||
REQUIRE(true == tryParseArray("[[]]", 1));
|
||||
REQUIRE(false == tryParseArray("[[[]]]", 1));
|
||||
}
|
||||
|
||||
SECTION("ParseArrayWithNestingLimit2") {
|
||||
REQUIRE(true == tryParseArray("[[[]]]", 2));
|
||||
REQUIRE(false == tryParseArray("[[[[]]]]", 2));
|
||||
}
|
||||
|
||||
SECTION("ParseObjectWithNestingLimit0") {
|
||||
REQUIRE(true == tryParseObject("{}", 0));
|
||||
REQUIRE(false == tryParseObject("{\"key\":{}}", 0));
|
||||
}
|
||||
|
||||
SECTION("ParseObjectWithNestingLimit1") {
|
||||
REQUIRE(true == tryParseObject("{\"key\":{}}", 1));
|
||||
REQUIRE(false == tryParseObject("{\"key\":{\"key\":{}}}", 1));
|
||||
}
|
||||
|
||||
SECTION("ParseObjectWithNestingLimit2") {
|
||||
REQUIRE(true == tryParseObject("{\"key\":{\"key\":{}}}", 2));
|
||||
REQUIRE(false == tryParseObject("{\"key\":{\"key\":{\"key\":{}}}}", 2));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user