// ArduinoJson - https://arduinojson.org // Copyright © 2014-2024, Benoit BLANCHON // MIT License #define ARDUINOJSON_ENABLE_ARDUINO_STRING 1 #define ARDUINOJSON_ENABLE_PROGMEM 1 #include #include #include "Allocators.hpp" using ArduinoJson::detail::sizeofArray; TEST_CASE("JsonDocument::add(T)") { SpyingAllocator spy; JsonDocument doc(&spy); SECTION("integer") { doc.add(42); REQUIRE(doc.as() == "[42]"); REQUIRE(spy.log() == AllocatorLog{ Allocate(sizeofPool()), }); } SECTION("const char*") { doc.add("hello"); REQUIRE(doc.as() == "[\"hello\"]"); REQUIRE(spy.log() == AllocatorLog{ Allocate(sizeofPool()), }); } SECTION("std::string") { doc.add(std::string("example")); doc.add(std::string("example")); CHECK(doc[0].as() == doc[1].as()); REQUIRE(spy.log() == AllocatorLog{ Allocate(sizeofPool()), Allocate(sizeofString("example")), }); } SECTION("char*") { char value[] = "example"; doc.add(value); doc.add(value); CHECK(doc[0].as() == doc[1].as()); REQUIRE(spy.log() == AllocatorLog{ Allocate(sizeofPool()), Allocate(sizeofString("example")), }); } SECTION("Arduino String") { doc.add(String("example")); doc.add(String("example")); CHECK(doc[0].as() == doc[1].as()); REQUIRE(spy.log() == AllocatorLog{ Allocate(sizeofPool()), Allocate(sizeofString("example")), }); } SECTION("Flash string") { doc.add(F("example")); doc.add(F("example")); CHECK(doc[0].as() == doc[1].as()); REQUIRE(spy.log() == AllocatorLog{ Allocate(sizeofPool()), Allocate(sizeofString("example")), }); } } TEST_CASE("JsonDocument::add()") { JsonDocument doc; SECTION("JsonArray") { JsonArray array = doc.add(); array.add(1); array.add(2); REQUIRE(doc.as() == "[[1,2]]"); } SECTION("JsonVariant") { JsonVariant variant = doc.add(); variant.set(42); REQUIRE(doc.as() == "[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()); REQUIRE(result == true); REQUIRE(doc2.as() == "[{\"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()); REQUIRE(result == false); REQUIRE(doc2.as() == "[]"); 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()); REQUIRE(result == false); REQUIRE(doc2.as() == "[]"); REQUIRE(spy.log() == AllocatorLog{ AllocateFail(sizeofPool()), }); } }