Update to ArduinoJson 7.0.4

This commit is contained in:
iranl
2024-04-19 14:44:01 +02:00
parent 1378732081
commit 81be0a689a
444 changed files with 10842 additions and 9422 deletions

View File

@@ -1,12 +1,17 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2023, Benoit BLANCHON
// Copyright © 2014-2024, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("JsonArray::add()") {
DynamicJsonDocument doc(4096);
#include "Allocators.hpp"
using ArduinoJson::detail::sizeofArray;
TEST_CASE("JsonArray::add(T)") {
SpyingAllocator spy;
JsonDocument doc(&spy);
JsonArray array = doc.to<JsonArray>();
SECTION("int") {
@@ -51,7 +56,7 @@ TEST_CASE("JsonArray::add()") {
#endif
SECTION("nested array") {
DynamicJsonDocument doc2(4096);
JsonDocument doc2;
JsonArray arr = doc2.to<JsonArray>();
array.add(arr);
@@ -62,7 +67,7 @@ TEST_CASE("JsonArray::add()") {
}
SECTION("nested object") {
DynamicJsonDocument doc2(4096);
JsonDocument doc2;
JsonObject obj = doc2.to<JsonObject>();
array.add(obj);
@@ -74,7 +79,7 @@ TEST_CASE("JsonArray::add()") {
SECTION("array subscript") {
const char* str = "hello";
DynamicJsonDocument doc2(4096);
JsonDocument doc2;
JsonArray arr = doc2.to<JsonArray>();
arr.add(str);
@@ -85,7 +90,7 @@ TEST_CASE("JsonArray::add()") {
SECTION("object subscript") {
const char* str = "hello";
DynamicJsonDocument doc2(4096);
JsonDocument doc2;
JsonObject obj = doc2.to<JsonObject>();
obj["x"] = str;
@@ -96,43 +101,81 @@ TEST_CASE("JsonArray::add()") {
SECTION("should not duplicate const char*") {
array.add("world");
const size_t expectedSize = JSON_ARRAY_SIZE(1);
REQUIRE(expectedSize == doc.memoryUsage());
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
});
}
SECTION("should duplicate char*") {
array.add(const_cast<char*>("world"));
const size_t expectedSize = JSON_ARRAY_SIZE(1) + JSON_STRING_SIZE(5);
REQUIRE(expectedSize == doc.memoryUsage());
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofString("world")),
});
}
SECTION("should duplicate std::string") {
array.add(std::string("world"));
const size_t expectedSize = JSON_ARRAY_SIZE(1) + JSON_STRING_SIZE(5);
REQUIRE(expectedSize == doc.memoryUsage());
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofString("world")),
});
}
SECTION("should not duplicate serialized(const char*)") {
SECTION("should duplicate serialized(const char*)") {
array.add(serialized("{}"));
const size_t expectedSize = JSON_ARRAY_SIZE(1);
REQUIRE(expectedSize == doc.memoryUsage());
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofString("{}")),
});
}
SECTION("should duplicate serialized(char*)") {
array.add(serialized(const_cast<char*>("{}")));
const size_t expectedSize = JSON_ARRAY_SIZE(1) + JSON_STRING_SIZE(2);
REQUIRE(expectedSize == doc.memoryUsage());
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofString("{}")),
});
}
SECTION("should duplicate serialized(std::string)") {
array.add(serialized(std::string("{}")));
const size_t expectedSize = JSON_ARRAY_SIZE(1) + JSON_STRING_SIZE(2);
REQUIRE(expectedSize == doc.memoryUsage());
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofString("{}")),
});
}
SECTION("should duplicate serialized(std::string)") {
array.add(serialized(std::string("\0XX", 3)));
const size_t expectedSize = JSON_ARRAY_SIZE(1) + JSON_STRING_SIZE(3);
REQUIRE(expectedSize == doc.memoryUsage());
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofString(" XX")),
});
}
}
TEST_CASE("JsonArray::add<T>()") {
JsonDocument doc;
JsonArray array = doc.to<JsonArray>();
SECTION("add<JsonArray>()") {
JsonArray nestedArray = array.add<JsonArray>();
nestedArray.add(1);
nestedArray.add(2);
REQUIRE(doc.as<std::string>() == "[[1,2]]");
}
SECTION("add<JsonObject>()") {
JsonObject nestedObject = array.add<JsonObject>();
nestedObject["a"] = 1;
nestedObject["b"] = 2;
REQUIRE(doc.as<std::string>() == "[{\"a\":1,\"b\":2}]");
}
SECTION("add<JsonVariant>()") {
JsonVariant nestedVariant = array.add<JsonVariant>();
nestedVariant.set(42);
REQUIRE(doc.as<std::string>() == "[42]");
}
}