2021-03-29 17:14:01 +02:00
|
|
|
// ArduinoJson - https://arduinojson.org
|
2023-02-16 11:45:01 +01:00
|
|
|
// Copyright © 2014-2023, Benoit BLANCHON
|
2019-02-25 13:21:10 +01:00
|
|
|
// MIT License
|
|
|
|
|
|
|
|
#include <ArduinoJson.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <catch.hpp>
|
|
|
|
|
2023-07-24 17:21:25 +02:00
|
|
|
#include "Allocators.hpp"
|
|
|
|
|
2023-04-17 09:46:41 +02:00
|
|
|
using ArduinoJson::detail::sizeofArray;
|
|
|
|
|
|
|
|
TEST_CASE("JsonVariant::remove(int)") {
|
2023-07-25 14:53:54 +02:00
|
|
|
SpyingAllocator spy;
|
|
|
|
JsonDocument doc(&spy);
|
2019-02-25 13:21:10 +01:00
|
|
|
|
2023-04-17 09:46:41 +02:00
|
|
|
SECTION("release top level strings") {
|
|
|
|
doc.add(std::string("hello"));
|
|
|
|
doc.add(std::string("hello"));
|
|
|
|
doc.add(std::string("world"));
|
|
|
|
|
|
|
|
JsonVariant var = doc.as<JsonVariant>();
|
|
|
|
REQUIRE(var.as<std::string>() == "[\"hello\",\"hello\",\"world\"]");
|
|
|
|
|
2023-07-25 14:53:54 +02:00
|
|
|
spy.clearLog();
|
2023-04-17 09:46:41 +02:00
|
|
|
var.remove(1);
|
|
|
|
REQUIRE(var.as<std::string>() == "[\"hello\",\"world\"]");
|
2023-07-26 06:06:38 +02:00
|
|
|
REQUIRE(spy.log() == AllocatorLog{});
|
2019-02-25 13:21:10 +01:00
|
|
|
|
2023-07-25 14:53:54 +02:00
|
|
|
spy.clearLog();
|
2019-02-25 13:21:10 +01:00
|
|
|
var.remove(1);
|
2023-04-17 09:46:41 +02:00
|
|
|
REQUIRE(var.as<std::string>() == "[\"hello\"]");
|
2023-07-26 06:06:38 +02:00
|
|
|
REQUIRE(spy.log() == AllocatorLog{
|
|
|
|
Deallocate(sizeofString("world")),
|
|
|
|
});
|
2019-02-25 13:21:10 +01:00
|
|
|
|
2023-07-25 14:53:54 +02:00
|
|
|
spy.clearLog();
|
2023-04-17 09:46:41 +02:00
|
|
|
var.remove(0);
|
|
|
|
REQUIRE(var.as<std::string>() == "[]");
|
2023-07-26 06:06:38 +02:00
|
|
|
REQUIRE(spy.log() == AllocatorLog{
|
|
|
|
Deallocate(sizeofString("hello")),
|
|
|
|
});
|
2019-02-25 13:21:10 +01:00
|
|
|
}
|
|
|
|
|
2023-04-17 09:46:41 +02:00
|
|
|
SECTION("release strings in nested array") {
|
|
|
|
doc[0][0] = std::string("hello");
|
2019-02-25 13:21:10 +01:00
|
|
|
|
2023-04-17 09:46:41 +02:00
|
|
|
JsonVariant var = doc.as<JsonVariant>();
|
|
|
|
REQUIRE(var.as<std::string>() == "[[\"hello\"]]");
|
2019-02-25 13:21:10 +01:00
|
|
|
|
2023-07-25 14:53:54 +02:00
|
|
|
spy.clearLog();
|
2023-04-17 09:46:41 +02:00
|
|
|
var.remove(0);
|
2023-07-24 17:21:25 +02:00
|
|
|
|
2023-04-17 09:46:41 +02:00
|
|
|
REQUIRE(var.as<std::string>() == "[]");
|
2023-07-26 06:06:38 +02:00
|
|
|
REQUIRE(spy.log() == AllocatorLog{
|
|
|
|
Deallocate(sizeofString("hello")),
|
|
|
|
});
|
2019-02-25 13:21:10 +01:00
|
|
|
}
|
2023-04-17 09:46:41 +02:00
|
|
|
}
|
2019-02-25 13:21:10 +01:00
|
|
|
|
2023-04-17 09:46:41 +02:00
|
|
|
TEST_CASE("JsonVariant::remove(const char *)") {
|
2023-07-17 18:15:13 +02:00
|
|
|
JsonDocument doc;
|
2023-04-17 09:46:41 +02:00
|
|
|
JsonVariant var = doc.to<JsonVariant>();
|
2019-02-25 13:21:10 +01:00
|
|
|
|
2023-04-17 09:46:41 +02:00
|
|
|
var["a"] = 1;
|
|
|
|
var["b"] = 2;
|
2019-02-25 13:21:10 +01:00
|
|
|
|
2023-04-17 09:46:41 +02:00
|
|
|
var.remove("a");
|
|
|
|
|
|
|
|
REQUIRE(var.as<std::string>() == "{\"b\":2}");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("JsonVariant::remove(std::string)") {
|
2023-07-17 18:15:13 +02:00
|
|
|
JsonDocument doc;
|
2023-04-17 09:46:41 +02:00
|
|
|
JsonVariant var = doc.to<JsonVariant>();
|
|
|
|
|
|
|
|
var["a"] = 1;
|
|
|
|
var["b"] = 2;
|
|
|
|
|
|
|
|
var.remove(std::string("b"));
|
|
|
|
|
|
|
|
REQUIRE(var.as<std::string>() == "{\"a\":1}");
|
2019-02-25 13:21:10 +01:00
|
|
|
}
|