mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-23 23:37:37 +02:00
@ -179,3 +179,52 @@ TEST_CASE("JsonArray::add<T>()") {
|
||||
REQUIRE(doc.as<std::string>() == "[42]");
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("JsonObject::add(JsonObject) ") {
|
||||
JsonDocument doc1;
|
||||
doc1[std::string("key1")] = std::string("value1");
|
||||
|
||||
TimebombAllocator allocator(10);
|
||||
SpyingAllocator spy(&allocator);
|
||||
JsonDocument doc2(&spy);
|
||||
JsonArray array = doc2.to<JsonArray>();
|
||||
|
||||
SECTION("success") {
|
||||
bool result = array.add(doc1.as<JsonObject>());
|
||||
|
||||
REQUIRE(result == true);
|
||||
REQUIRE(doc2.as<std::string>() == "[{\"key1\":\"value1\"}]");
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("key1")),
|
||||
Allocate(sizeofString("value1")),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("partial failure") { // issue #2081
|
||||
allocator.setCountdown(2);
|
||||
|
||||
bool result = array.add(doc1.as<JsonObject>());
|
||||
|
||||
REQUIRE(result == false);
|
||||
REQUIRE(doc2.as<std::string>() == "[]");
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("key1")),
|
||||
AllocateFail(sizeofString("value1")),
|
||||
Deallocate(sizeofString("key1")),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("complete failure") {
|
||||
allocator.setCountdown(0);
|
||||
|
||||
bool result = array.add(doc1.as<JsonObject>());
|
||||
|
||||
REQUIRE(result == false);
|
||||
REQUIRE(doc2.as<std::string>() == "[]");
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
AllocateFail(sizeofPool()),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -90,15 +90,57 @@ TEST_CASE("JsonDocument::add<T>()") {
|
||||
REQUIRE(doc.as<std::string>() == "[[1,2]]");
|
||||
}
|
||||
|
||||
SECTION("JsonObject") {
|
||||
JsonObject object = doc.add<JsonObject>();
|
||||
object["hello"] = "world";
|
||||
REQUIRE(doc.as<std::string>() == "[{\"hello\":\"world\"}]");
|
||||
}
|
||||
|
||||
SECTION("JsonVariant") {
|
||||
JsonVariant variant = doc.add<JsonVariant>();
|
||||
variant.set(42);
|
||||
REQUIRE(doc.as<std::string>() == "[42]");
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("JsonObject::add(JsonObject) ") {
|
||||
JsonDocument doc1;
|
||||
doc1[std::string("hello")] = std::string("world");
|
||||
|
||||
TimebombAllocator allocator(10);
|
||||
SpyingAllocator spy(&allocator);
|
||||
JsonDocument doc2(&spy);
|
||||
|
||||
SECTION("success") {
|
||||
bool result = doc2.add(doc1.as<JsonObject>());
|
||||
|
||||
REQUIRE(result == true);
|
||||
REQUIRE(doc2.as<std::string>() == "[{\"hello\":\"world\"}]");
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("hello")),
|
||||
Allocate(sizeofString("world")),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("partial failure") { // issue #2081
|
||||
allocator.setCountdown(2);
|
||||
|
||||
bool result = doc2.add(doc1.as<JsonObject>());
|
||||
|
||||
REQUIRE(result == false);
|
||||
REQUIRE(doc2.as<std::string>() == "[]");
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("hello")),
|
||||
AllocateFail(sizeofString("world")),
|
||||
Deallocate(sizeofString("hello")),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("complete failure") {
|
||||
allocator.setCountdown(0);
|
||||
|
||||
bool result = doc2.add(doc1.as<JsonObject>());
|
||||
|
||||
REQUIRE(result == false);
|
||||
REQUIRE(doc2.as<std::string>() == "[]");
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
AllocateFail(sizeofPool()),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -118,7 +118,7 @@ TEST_CASE("JsonObject::set()") {
|
||||
bool success = obj3.set(obj1);
|
||||
|
||||
REQUIRE(success == false);
|
||||
REQUIRE(doc3.as<std::string>() == "{\"hello\":[null]}");
|
||||
REQUIRE(doc3.as<std::string>() == "{\"hello\":[]}");
|
||||
}
|
||||
|
||||
SECTION("destination is null") {
|
||||
|
@ -6,6 +6,8 @@
|
||||
#include <stdint.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
#include "Allocators.hpp"
|
||||
|
||||
TEST_CASE("JsonVariant::add(T)") {
|
||||
JsonDocument doc;
|
||||
JsonVariant var = doc.to<JsonVariant>();
|
||||
@ -56,15 +58,58 @@ TEST_CASE("JsonVariant::add<T>()") {
|
||||
REQUIRE(doc.as<std::string>() == "[[1,2]]");
|
||||
}
|
||||
|
||||
SECTION("JsonObject") {
|
||||
JsonObject object = var.add<JsonObject>();
|
||||
object["hello"] = "world";
|
||||
REQUIRE(doc.as<std::string>() == "[{\"hello\":\"world\"}]");
|
||||
}
|
||||
|
||||
SECTION("JsonVariant") {
|
||||
JsonVariant variant = var.add<JsonVariant>();
|
||||
variant.set(42);
|
||||
REQUIRE(doc.as<std::string>() == "[42]");
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("JsonObject::add(JsonObject) ") {
|
||||
JsonDocument doc1;
|
||||
doc1[std::string("hello")] = std::string("world");
|
||||
|
||||
TimebombAllocator allocator(10);
|
||||
SpyingAllocator spy(&allocator);
|
||||
JsonDocument doc2(&spy);
|
||||
JsonVariant variant = doc2.to<JsonVariant>();
|
||||
|
||||
SECTION("success") {
|
||||
bool result = variant.add(doc1.as<JsonObject>());
|
||||
|
||||
REQUIRE(result == true);
|
||||
REQUIRE(doc2.as<std::string>() == "[{\"hello\":\"world\"}]");
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("hello")),
|
||||
Allocate(sizeofString("world")),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("partial failure") { // issue #2081
|
||||
allocator.setCountdown(2);
|
||||
|
||||
bool result = variant.add(doc1.as<JsonObject>());
|
||||
|
||||
REQUIRE(result == false);
|
||||
REQUIRE(doc2.as<std::string>() == "[]");
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("hello")),
|
||||
AllocateFail(sizeofString("world")),
|
||||
Deallocate(sizeofString("hello")),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("complete failure") {
|
||||
allocator.setCountdown(0);
|
||||
|
||||
bool result = variant.add(doc1.as<JsonObject>());
|
||||
|
||||
REQUIRE(result == false);
|
||||
REQUIRE(doc2.as<std::string>() == "[]");
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
AllocateFail(sizeofPool()),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user