2021-03-29 17:14:01 +02:00
|
|
|
// ArduinoJson - https://arduinojson.org
|
2024-01-03 08:47:06 +01:00
|
|
|
// Copyright © 2014-2024, 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"
|
2024-06-07 09:35:45 +02:00
|
|
|
#include "Literals.hpp"
|
2023-07-24 17:21:25 +02:00
|
|
|
|
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") {
|
2024-06-07 09:35:45 +02:00
|
|
|
doc.add("hello"_s);
|
|
|
|
doc.add("hello"_s);
|
|
|
|
doc.add("world"_s);
|
2023-04-17 09:46:41 +02:00
|
|
|
|
|
|
|
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") {
|
2024-06-07 09:35:45 +02:00
|
|
|
doc[0][0] = "hello"_s;
|
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;
|
|
|
|
|
2024-06-07 09:35:45 +02:00
|
|
|
var.remove("b"_s);
|
2023-04-17 09:46:41 +02:00
|
|
|
|
|
|
|
REQUIRE(var.as<std::string>() == "{\"a\":1}");
|
2019-02-25 13:21:10 +01:00
|
|
|
}
|
2024-05-14 21:06:02 +02:00
|
|
|
|
|
|
|
TEST_CASE("JsonVariant::remove(JsonVariant) from object") {
|
|
|
|
JsonDocument doc;
|
|
|
|
JsonVariant var = doc.to<JsonVariant>();
|
|
|
|
|
|
|
|
var["a"] = "a";
|
|
|
|
var["b"] = 2;
|
|
|
|
var["c"] = "b";
|
|
|
|
|
|
|
|
var.remove(var["c"]);
|
|
|
|
|
|
|
|
REQUIRE(var.as<std::string>() == "{\"a\":\"a\",\"c\":\"b\"}");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("JsonVariant::remove(JsonVariant) from array") {
|
|
|
|
JsonDocument doc;
|
|
|
|
JsonVariant var = doc.to<JsonVariant>();
|
|
|
|
|
|
|
|
var[0] = 3;
|
|
|
|
var[1] = 2;
|
|
|
|
var[2] = 1;
|
|
|
|
|
|
|
|
var.remove(var[2]);
|
|
|
|
var.remove(var[3]); // noop
|
|
|
|
|
|
|
|
REQUIRE(var.as<std::string>() == "[3,1]");
|
|
|
|
}
|