Add memoryUsage() to ElementProxy and MemberProxy (fixes #1730)

This commit is contained in:
Benoit Blanchon
2022-03-19 12:11:40 +01:00
parent 47f90b02c3
commit 3dc67c5663
6 changed files with 65 additions and 0 deletions

View File

@ -205,4 +205,26 @@ TEST_CASE("DynamicJsonDocument assignment") {
REQUIRE_JSON(doc2, "42");
REQUIRE(doc2.capacity() == 4096);
}
SECTION("Assign from MemberProxy") {
StaticJsonDocument<200> doc1;
doc1["value"] = 42;
DynamicJsonDocument doc2(4096);
doc2 = doc1["value"];
REQUIRE_JSON(doc2, "42");
REQUIRE(doc2.capacity() == 4096);
}
SECTION("Assign from ElementProxy") {
StaticJsonDocument<200> doc1;
doc1[0] = 42;
DynamicJsonDocument doc2(4096);
doc2 = doc1[0];
REQUIRE_JSON(doc2, "42");
REQUIRE(doc2.capacity() == 4096);
}
}

View File

@ -188,6 +188,21 @@ TEST_CASE("ElementProxy::size()") {
}
}
TEST_CASE("ElementProxy::memoryUsage()") {
DynamicJsonDocument doc(4096);
doc.addElement();
ElementProxy<JsonDocument&> ep = doc[0];
SECTION("returns 0 for null") {
REQUIRE(ep.memoryUsage() == 0);
}
SECTION("returns size for string") {
ep.set(std::string("hello"));
REQUIRE(ep.memoryUsage() == 6);
}
}
TEST_CASE("ElementProxy::operator[]") {
DynamicJsonDocument doc(4096);
ElementProxy<JsonDocument&> ep = doc[1];

View File

@ -229,6 +229,20 @@ TEST_CASE("MemberProxy::size()") {
}
}
TEST_CASE("MemberProxy::memoryUsage()") {
DynamicJsonDocument doc(4096);
MemberProxy<JsonDocument &, const char *> mp = doc["hello"];
SECTION("returns 0 when null") {
REQUIRE(mp.memoryUsage() == 0);
}
SECTION("return the size for a string") {
mp.set(std::string("hello"));
REQUIRE(mp.memoryUsage() == 6);
}
}
TEST_CASE("MemberProxy::operator[]") {
DynamicJsonDocument doc(4096);
MemberProxy<JsonDocument &, const char *> mp = doc["hello"];