forked from bblanchon/ArduinoJson
Test that the size is decreased when object are removed
This commit is contained in:
@ -30,6 +30,26 @@ JsonValue JsonObject::operator[](char const* key)
|
|||||||
return JsonValue(node);
|
return JsonValue(node);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void JsonObject::remove(char const* key)
|
||||||
|
{
|
||||||
|
JsonNode* firstChild = _node->content.asObject.child;
|
||||||
|
JsonNode* lastChild = 0;
|
||||||
|
|
||||||
|
for (JsonNode* child = firstChild; child; child = child->next)
|
||||||
|
{
|
||||||
|
const char* childKey = child->content.asKey.key;
|
||||||
|
|
||||||
|
if (!strcmp(childKey, key))
|
||||||
|
{
|
||||||
|
if (lastChild)
|
||||||
|
lastChild->next = child->next;
|
||||||
|
else
|
||||||
|
_node->content.asObject.child = child->next;
|
||||||
|
}
|
||||||
|
lastChild = child;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool JsonObject::operator==(JsonObject const& other) const
|
bool JsonObject::operator==(JsonObject const& other) const
|
||||||
{
|
{
|
||||||
return _node == other._node;
|
return _node == other._node;
|
||||||
|
@ -23,6 +23,7 @@ public:
|
|||||||
size_t size();
|
size_t size();
|
||||||
|
|
||||||
JsonValue operator[](const char* key);
|
JsonValue operator[](const char* key);
|
||||||
|
void remove(const char* key);
|
||||||
|
|
||||||
bool operator==(const JsonObject& other) const;
|
bool operator==(const JsonObject& other) const;
|
||||||
|
|
||||||
|
@ -32,6 +32,18 @@ TEST_F(JsonObjectTests, DoNotGrow_WhenSameValueIsAdded)
|
|||||||
EXPECT_EQ(1, object.size());
|
EXPECT_EQ(1, object.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_F(JsonObjectTests, Shrink_WhenValuesAreRemoved)
|
||||||
|
{
|
||||||
|
object["hello"];
|
||||||
|
object["world"];
|
||||||
|
|
||||||
|
object.remove("hello");
|
||||||
|
EXPECT_EQ(1, object.size());
|
||||||
|
|
||||||
|
object.remove("world");
|
||||||
|
EXPECT_EQ(0, object.size());
|
||||||
|
}
|
||||||
|
|
||||||
TEST_F(JsonObjectTests, CanStoreIntegers)
|
TEST_F(JsonObjectTests, CanStoreIntegers)
|
||||||
{
|
{
|
||||||
object["hello"] = 123;
|
object["hello"] = 123;
|
||||||
|
Reference in New Issue
Block a user