Test: gather JsonDocument constructor and assignment tests

This commit is contained in:
Benoit Blanchon
2023-04-01 19:10:35 +02:00
parent bcf1339e89
commit 0643c2e708
5 changed files with 164 additions and 195 deletions

View File

@ -5,21 +5,56 @@
#include <ArduinoJson.h>
#include <catch.hpp>
#include "Allocators.hpp"
using ArduinoJson::detail::addPadding;
TEST_CASE("JsonDocument constructor") {
SECTION("Copy constructor") {
JsonDocument doc1(1234);
deserializeJson(doc1, "{\"hello\":\"world\"}");
SpyingAllocator spyingAllocator;
JsonDocument doc2 = doc1;
REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
REQUIRE(doc2.capacity() == doc1.capacity());
SECTION("JsonDocument(size_t)") {
{ JsonDocument doc(4096, &spyingAllocator); }
REQUIRE(spyingAllocator.log() == AllocatorLog()
<< AllocatorLog::Allocate(4096)
<< AllocatorLog::Deallocate(4096));
}
SECTION("Construct from JsonObject") {
SECTION("JsonDocument(const JsonDocument&)") {
{
JsonDocument doc1(4096, &spyingAllocator);
doc1.set(std::string("The size of this string is 32!!"));
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!!");
REQUIRE(doc2.capacity() == 4096);
}
REQUIRE(spyingAllocator.log() == AllocatorLog()
<< AllocatorLog::Allocate(4096)
<< AllocatorLog::Allocate(4096)
<< AllocatorLog::Deallocate(4096)
<< AllocatorLog::Deallocate(4096));
}
SECTION("JsonDocument(JsonDocument&&)") {
{
JsonDocument doc1(4096, &spyingAllocator);
doc1.set(std::string("The size of this string is 32!!"));
JsonDocument doc2(std::move(doc1));
REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
REQUIRE(doc1.as<std::string>() == "null");
REQUIRE(doc1.capacity() == 0);
REQUIRE(doc2.capacity() == 4096);
}
REQUIRE(spyingAllocator.log() == AllocatorLog()
<< AllocatorLog::Allocate(4096)
<< AllocatorLog::Deallocate(4096));
}
SECTION("JsonDocument(JsonObject)") {
JsonDocument doc1(200);
JsonObject obj = doc1.to<JsonObject>();
obj["hello"] = "world";