2017-11-07 20:42:50 +01:00
|
|
|
// ArduinoJson - arduinojson.org
|
2018-01-05 09:20:01 +01:00
|
|
|
// Copyright Benoit Blanchon 2014-2018
|
2015-09-28 22:14:50 +02:00
|
|
|
// MIT License
|
|
|
|
|
|
|
|
#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()") {
|
2018-04-17 21:27:45 +02:00
|
|
|
DynamicJsonDocument doc;
|
2018-07-02 09:35:21 +02:00
|
|
|
JsonObject obj = doc.to<JsonObject>();
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("SizeDecreased_WhenValuesAreRemoved") {
|
|
|
|
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") {
|
|
|
|
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") {
|
2018-04-17 21:27:45 +02:00
|
|
|
obj["a"] = 0;
|
|
|
|
obj["b"] = 1;
|
|
|
|
obj["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) {
|
2018-08-21 17:56:16 +02:00
|
|
|
if (it->value() == 1) obj.remove(it);
|
2017-04-18 18:22:24 +02:00
|
|
|
}
|
2017-04-12 21:00:13 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
std::string result;
|
2018-03-01 09:24:58 +01:00
|
|
|
serializeJson(obj, result);
|
2017-04-18 18:22:24 +02:00
|
|
|
REQUIRE("{\"a\":0,\"c\":2}" == result);
|
|
|
|
}
|
2017-04-12 21:00:13 +02:00
|
|
|
}
|