2023-04-01 15:14:21 +02:00
|
|
|
// ArduinoJson - https://arduinojson.org
|
|
|
|
// Copyright © 2014-2023, Benoit BLANCHON
|
|
|
|
// MIT License
|
|
|
|
|
|
|
|
#include <ArduinoJson.h>
|
|
|
|
#include <catch.hpp>
|
|
|
|
|
2023-04-01 19:10:35 +02:00
|
|
|
#include "Allocators.hpp"
|
|
|
|
|
2023-04-01 15:14:21 +02:00
|
|
|
using ArduinoJson::detail::addPadding;
|
2023-04-11 10:03:47 +02:00
|
|
|
using ArduinoJson::detail::sizeofString;
|
2023-04-01 15:14:21 +02:00
|
|
|
|
|
|
|
TEST_CASE("JsonDocument constructor") {
|
2023-04-01 19:10:35 +02:00
|
|
|
SpyingAllocator spyingAllocator;
|
|
|
|
|
|
|
|
SECTION("JsonDocument(size_t)") {
|
2023-07-17 18:15:13 +02:00
|
|
|
{ JsonDocument doc(&spyingAllocator); }
|
2023-07-17 14:39:57 +02:00
|
|
|
REQUIRE(spyingAllocator.log() == AllocatorLog());
|
2023-04-01 19:10:35 +02:00
|
|
|
}
|
2023-04-01 15:14:21 +02:00
|
|
|
|
2023-04-01 19:10:35 +02:00
|
|
|
SECTION("JsonDocument(const JsonDocument&)") {
|
|
|
|
{
|
2023-07-17 18:15:13 +02:00
|
|
|
JsonDocument doc1(&spyingAllocator);
|
2023-04-01 19:10:35 +02:00
|
|
|
doc1.set(std::string("The size of this string is 32!!"));
|
2023-04-01 15:14:21 +02:00
|
|
|
|
2023-04-01 19:10:35 +02:00
|
|
|
JsonDocument doc2(doc1);
|
|
|
|
|
|
|
|
REQUIRE(doc1.as<std::string>() == "The size of this string is 32!!");
|
|
|
|
REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
|
|
|
|
}
|
2023-04-11 10:03:47 +02:00
|
|
|
REQUIRE(spyingAllocator.log() ==
|
2023-07-17 14:39:57 +02:00
|
|
|
AllocatorLog() << AllocatorLog::Allocate(sizeofString(31))
|
2023-04-11 10:03:47 +02:00
|
|
|
<< AllocatorLog::Allocate(sizeofString(31))
|
|
|
|
<< AllocatorLog::Deallocate(sizeofString(31))
|
2023-07-17 14:39:57 +02:00
|
|
|
<< AllocatorLog::Deallocate(sizeofString(31)));
|
2023-04-01 19:10:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("JsonDocument(JsonDocument&&)") {
|
|
|
|
{
|
2023-07-17 18:15:13 +02:00
|
|
|
JsonDocument doc1(&spyingAllocator);
|
2023-04-01 19:10:35 +02:00
|
|
|
doc1.set(std::string("The size of this string is 32!!"));
|
|
|
|
|
|
|
|
JsonDocument doc2(std::move(doc1));
|
2023-04-01 15:14:21 +02:00
|
|
|
|
2023-04-01 19:10:35 +02:00
|
|
|
REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
|
|
|
|
REQUIRE(doc1.as<std::string>() == "null");
|
|
|
|
}
|
2023-04-11 10:03:47 +02:00
|
|
|
REQUIRE(spyingAllocator.log() ==
|
2023-07-17 14:39:57 +02:00
|
|
|
AllocatorLog() << AllocatorLog::Allocate(sizeofString(31))
|
|
|
|
<< AllocatorLog::Deallocate(sizeofString(31)));
|
2023-04-01 15:14:21 +02:00
|
|
|
}
|
|
|
|
|
2023-04-01 19:10:35 +02:00
|
|
|
SECTION("JsonDocument(JsonObject)") {
|
2023-07-17 18:15:13 +02:00
|
|
|
JsonDocument doc1;
|
2023-04-01 15:14:21 +02:00
|
|
|
JsonObject obj = doc1.to<JsonObject>();
|
|
|
|
obj["hello"] = "world";
|
|
|
|
|
2023-04-02 16:47:37 +02:00
|
|
|
JsonDocument doc2(obj, &spyingAllocator);
|
2023-04-01 15:14:21 +02:00
|
|
|
|
|
|
|
REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
|
2023-07-03 09:23:18 +02:00
|
|
|
REQUIRE(spyingAllocator.log() ==
|
2023-07-17 14:39:57 +02:00
|
|
|
AllocatorLog() << AllocatorLog::Allocate(sizeofPoolList())
|
|
|
|
<< AllocatorLog::Allocate(sizeofPool()));
|
2023-04-01 15:14:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Construct from JsonArray") {
|
2023-07-17 18:15:13 +02:00
|
|
|
JsonDocument doc1;
|
2023-04-01 15:14:21 +02:00
|
|
|
JsonArray arr = doc1.to<JsonArray>();
|
|
|
|
arr.add("hello");
|
|
|
|
|
2023-04-02 16:47:37 +02:00
|
|
|
JsonDocument doc2(arr, &spyingAllocator);
|
2023-04-01 15:14:21 +02:00
|
|
|
|
|
|
|
REQUIRE(doc2.as<std::string>() == "[\"hello\"]");
|
2023-07-17 14:39:57 +02:00
|
|
|
REQUIRE(spyingAllocator.log() ==
|
|
|
|
AllocatorLog() << AllocatorLog::Allocate(sizeofPoolList())
|
|
|
|
<< AllocatorLog::Allocate(sizeofPool()));
|
2023-04-01 15:14:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Construct from JsonVariant") {
|
2023-07-17 18:15:13 +02:00
|
|
|
JsonDocument doc1;
|
2023-04-02 16:47:37 +02:00
|
|
|
deserializeJson(doc1, "\"hello\"");
|
2023-04-01 15:14:21 +02:00
|
|
|
|
2023-04-02 16:47:37 +02:00
|
|
|
JsonDocument doc2(doc1.as<JsonVariant>(), &spyingAllocator);
|
2023-04-01 15:14:21 +02:00
|
|
|
|
2023-04-02 16:47:37 +02:00
|
|
|
REQUIRE(doc2.as<std::string>() == "hello");
|
2023-07-03 09:23:18 +02:00
|
|
|
REQUIRE(spyingAllocator.log() ==
|
2023-07-17 14:39:57 +02:00
|
|
|
AllocatorLog() << AllocatorLog::Allocate(sizeofString(5)));
|
2023-04-01 15:14:21 +02:00
|
|
|
}
|
|
|
|
}
|