2017-01-06 21:07:34 +01:00
|
|
|
// Copyright Benoit Blanchon 2014-2017
|
2015-09-28 22:14:50 +02:00
|
|
|
// MIT License
|
|
|
|
//
|
|
|
|
// Arduino JSON library
|
2017-03-25 22:05:06 +01:00
|
|
|
// https://bblanchon.github.io/ArduinoJson/
|
2016-01-07 22:35:12 +01:00
|
|
|
// If you like this project, please add a star!
|
2015-09-28 22:14:50 +02:00
|
|
|
|
|
|
|
#include <ArduinoJson.h>
|
2017-04-18 18:22:24 +02:00
|
|
|
#include <catch.hpp>
|
|
|
|
#include <string>
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
TEST_CASE("JsonObject::remove()") {
|
|
|
|
DynamicJsonBuffer jb;
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("SizeDecreased_WhenValuesAreRemoved") {
|
|
|
|
JsonObject& obj = jb.createObject();
|
|
|
|
obj["hello"] = 1;
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
obj.remove("hello");
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
REQUIRE(0 == obj.size());
|
|
|
|
}
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("SizeUntouched_WhenRemoveIsCalledWithAWrongKey") {
|
|
|
|
JsonObject& obj = jb.createObject();
|
|
|
|
obj["hello"] = 1;
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
obj.remove("world");
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
REQUIRE(1 == obj.size());
|
|
|
|
}
|
2017-04-12 21:00:13 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("RemoveByIterator") {
|
|
|
|
JsonObject& obj = jb.parseObject("{\"a\":0,\"b\":1,\"c\":2}");
|
2017-04-12 21:00:13 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
for (JsonObject::iterator it = obj.begin(); it != obj.end(); ++it) {
|
|
|
|
if (it->value == 1) obj.remove(it);
|
|
|
|
}
|
2017-04-12 21:00:13 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
std::string result;
|
|
|
|
obj.printTo(result);
|
|
|
|
REQUIRE("{\"a\":0,\"c\":2}" == result);
|
|
|
|
}
|
2017-04-12 21:00:13 +02:00
|
|
|
}
|