arduino/libraries/ArduinoJson/test/JsonObject/remove.cpp

73 lines
1.5 KiB
C++
Raw Normal View History

2018-11-03 20:21:33 +00:00
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
#include <string>
TEST_CASE("JsonObject::remove()") {
DynamicJsonDocument doc;
JsonObject obj = doc.to<JsonObject>();
2018-11-13 14:50:48 +00:00
obj["a"] = 0;
obj["b"] = 1;
obj["c"] = 2;
std::string result;
2018-11-03 20:21:33 +00:00
2018-11-13 14:50:48 +00:00
SECTION("remove(key)") {
SECTION("Remove first") {
obj.remove("a");
serializeJson(obj, result);
REQUIRE("{\"b\":1,\"c\":2}" == result);
}
2018-11-03 20:21:33 +00:00
2018-11-13 14:50:48 +00:00
SECTION("Remove middle") {
obj.remove("b");
serializeJson(obj, result);
REQUIRE("{\"a\":0,\"c\":2}" == result);
}
2018-11-03 20:21:33 +00:00
2018-11-13 14:50:48 +00:00
SECTION("Remove last") {
obj.remove("c");
serializeJson(obj, result);
REQUIRE("{\"a\":0,\"b\":1}" == result);
}
2018-11-03 20:21:33 +00:00
}
2018-11-13 14:50:48 +00:00
SECTION("remove(iterator)") {
JsonObject::iterator it = obj.begin();
2018-11-03 20:21:33 +00:00
2018-11-13 14:50:48 +00:00
SECTION("Remove first") {
obj.remove(it);
serializeJson(obj, result);
REQUIRE("{\"b\":1,\"c\":2}" == result);
}
2018-11-03 20:21:33 +00:00
2018-11-13 14:50:48 +00:00
SECTION("Remove middle") {
++it;
obj.remove(it);
serializeJson(obj, result);
REQUIRE("{\"a\":0,\"c\":2}" == result);
}
2018-11-03 20:21:33 +00:00
2018-11-13 14:50:48 +00:00
SECTION("Remove last") {
it += 2;
obj.remove(it);
serializeJson(obj, result);
REQUIRE("{\"a\":0,\"b\":1}" == result);
2018-11-03 20:21:33 +00:00
}
2018-11-13 14:50:48 +00:00
}
#ifdef HAS_VARIABLE_LENGTH_ARRAY
SECTION("key is a vla") {
int i = 16;
char vla[i];
strcpy(vla, "b");
obj.remove(vla);
2018-11-03 20:21:33 +00:00
serializeJson(obj, result);
REQUIRE("{\"a\":0,\"c\":2}" == result);
}
2018-11-13 14:50:48 +00:00
#endif
2018-11-03 20:21:33 +00:00
}