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;
|
|
|
|
|
|
|
|
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-26 06:06:38 +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-07-26 06:06:38 +02:00
|
|
|
REQUIRE(spyingAllocator.log() == AllocatorLog{
|
|
|
|
Allocate(sizeofStringBuffer()),
|
|
|
|
Allocate(sizeofStringBuffer()),
|
|
|
|
Deallocate(sizeofStringBuffer()),
|
|
|
|
Deallocate(sizeofStringBuffer()),
|
|
|
|
});
|
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-07-26 06:06:38 +02:00
|
|
|
REQUIRE(spyingAllocator.log() == AllocatorLog{
|
|
|
|
Allocate(sizeofStringBuffer()),
|
|
|
|
Deallocate(sizeofStringBuffer()),
|
|
|
|
});
|
2023-04-01 15:14:21 +02:00
|
|
|
}
|
|
|
|
|
2023-08-10 09:40:36 +02:00
|
|
|
SECTION("JsonDocument(JsonObject, Allocator*)") {
|
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-26 06:06:38 +02:00
|
|
|
REQUIRE(spyingAllocator.log() == AllocatorLog{
|
|
|
|
Allocate(sizeofPool()),
|
|
|
|
});
|
2023-04-01 15:14:21 +02:00
|
|
|
}
|
|
|
|
|
2023-08-10 09:40:36 +02:00
|
|
|
SECTION("JsonDocument(JsonObject)") {
|
|
|
|
JsonDocument doc1;
|
|
|
|
JsonObject obj = doc1.to<JsonObject>();
|
|
|
|
obj["hello"] = "world";
|
|
|
|
|
|
|
|
JsonDocument doc2(obj);
|
|
|
|
|
|
|
|
REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("JsonDocument(JsonArray, Allocator*)") {
|
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-26 06:06:38 +02:00
|
|
|
REQUIRE(spyingAllocator.log() == AllocatorLog{
|
|
|
|
Allocate(sizeofPool()),
|
|
|
|
});
|
2023-04-01 15:14:21 +02:00
|
|
|
}
|
|
|
|
|
2023-08-10 09:40:36 +02:00
|
|
|
SECTION("JsonDocument(JsonArray)") {
|
|
|
|
JsonDocument doc1;
|
|
|
|
JsonArray arr = doc1.to<JsonArray>();
|
|
|
|
arr.add("hello");
|
|
|
|
|
|
|
|
JsonDocument doc2(arr);
|
|
|
|
|
|
|
|
REQUIRE(doc2.as<std::string>() == "[\"hello\"]");
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("JsonDocument(JsonVariant, Allocator*)") {
|
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-26 06:06:38 +02:00
|
|
|
REQUIRE(spyingAllocator.log() == AllocatorLog{
|
|
|
|
Allocate(sizeofString("hello")),
|
|
|
|
});
|
2023-04-01 15:14:21 +02:00
|
|
|
}
|
2023-08-10 09:40:36 +02:00
|
|
|
|
|
|
|
SECTION("JsonDocument(JsonVariant)") {
|
|
|
|
JsonDocument doc1;
|
|
|
|
deserializeJson(doc1, "\"hello\"");
|
|
|
|
|
|
|
|
JsonDocument doc2(doc1.as<JsonVariant>());
|
|
|
|
|
|
|
|
REQUIRE(doc2.as<std::string>() == "hello");
|
|
|
|
}
|
2023-04-01 15:14:21 +02:00
|
|
|
}
|