Added JsonDocument::remove() and JsonVariant::remove()

This commit is contained in:
Benoit Blanchon
2019-02-25 13:21:10 +01:00
parent bc2ce178ea
commit c9d6bd76c9
14 changed files with 298 additions and 2 deletions

View File

@ -5,6 +5,7 @@
add_executable(ElementProxyTests
add.cpp
clear.cpp
remove.cpp
set.cpp
size.cpp
)

View File

@ -0,0 +1,56 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2019
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
using namespace ARDUINOJSON_NAMESPACE;
TEST_CASE("ElementProxy::remove()") {
DynamicJsonDocument doc(4096);
doc.addElement();
ElementProxy<JsonDocument&> ep = doc[0];
SECTION("remove(int)") {
ep.add(1);
ep.add(2);
ep.add(3);
ep.remove(1);
REQUIRE(ep.as<std::string>() == "[1,3]");
}
SECTION("remove(const char *)") {
ep["a"] = 1;
ep["b"] = 2;
ep.remove("a");
REQUIRE(ep.as<std::string>() == "{\"b\":2}");
}
SECTION("remove(std::string)") {
ep["a"] = 1;
ep["b"] = 2;
ep.remove(std::string("b"));
REQUIRE(ep.as<std::string>() == "{\"a\":1}");
}
#ifdef HAS_VARIABLE_LENGTH_ARRAY
SECTION("remove(vla)") {
ep["a"] = 1;
ep["b"] = 2;
int i = 4;
char vla[i];
strcpy(vla, "b");
ep.remove(vla);
REQUIRE(ep.as<std::string>() == "{\"a\":1}");
}
#endif
}

View File

@ -8,6 +8,7 @@ add_executable(JsonDocumentTests
DynamicJsonDocument.cpp
isNull.cpp
nesting.cpp
remove.cpp
size.cpp
StaticJsonDocument.cpp
subscript.cpp

View File

@ -0,0 +1,52 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2019
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("JsonDocument::remove()") {
DynamicJsonDocument doc(4096);
SECTION("remove(int)") {
doc.add(1);
doc.add(2);
doc.add(3);
doc.remove(1);
REQUIRE(doc.as<std::string>() == "[1,3]");
}
SECTION("remove(const char *)") {
doc["a"] = 1;
doc["b"] = 2;
doc.remove("a");
REQUIRE(doc.as<std::string>() == "{\"b\":2}");
}
SECTION("remove(std::string)") {
doc["a"] = 1;
doc["b"] = 2;
doc.remove(std::string("b"));
REQUIRE(doc.as<std::string>() == "{\"a\":1}");
}
#ifdef HAS_VARIABLE_LENGTH_ARRAY
SECTION("remove(vla)") {
doc["a"] = 1;
doc["b"] = 2;
int i = 4;
char vla[i];
strcpy(vla, "b");
doc.remove(vla);
REQUIRE(doc.as<std::string>() == "{\"a\":1}");
}
#endif
}

View File

@ -15,6 +15,7 @@ add_executable(JsonVariantTests
misc.cpp
nesting.cpp
or.cpp
remove.cpp
set.cpp
subscript.cpp
types.cpp

View File

@ -0,0 +1,42 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2019
// MIT License
#include <ArduinoJson.h>
#include <stdint.h>
#include <catch.hpp>
static const char* null = 0;
TEST_CASE("JsonVariant::remove()") {
DynamicJsonDocument doc(4096);
JsonVariant var = doc.to<JsonVariant>();
SECTION("remove(int)") {
var.add(1);
var.add(2);
var.add(3);
var.remove(1);
REQUIRE(var.as<std::string>() == "[1,3]");
}
SECTION("remove(const char *)") {
var["a"] = 1;
var["b"] = 2;
var.remove("a");
REQUIRE(var.as<std::string>() == "{\"b\":2}");
}
SECTION("remove(std::string)") {
var["a"] = 1;
var["b"] = 2;
var.remove(std::string("b"));
REQUIRE(var.as<std::string>() == "{\"a\":1}");
}
}

View File

@ -5,9 +5,10 @@
add_executable(MemberProxyTests
add.cpp
clear.cpp
subscript.cpp
remove.cpp
set.cpp
size.cpp
subscript.cpp
)
target_link_libraries(MemberProxyTests catch)

View File

@ -0,0 +1,55 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2019
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
using namespace ARDUINOJSON_NAMESPACE;
TEST_CASE("MemberProxy::remove()") {
DynamicJsonDocument doc(4096);
MemberProxy<JsonDocument&, const char*> mp = doc["hello"];
SECTION("remove(int)") {
mp.add(1);
mp.add(2);
mp.add(3);
mp.remove(1);
REQUIRE(mp.as<std::string>() == "[1,3]");
}
SECTION("remove(const char *)") {
mp["a"] = 1;
mp["b"] = 2;
mp.remove("a");
REQUIRE(mp.as<std::string>() == "{\"b\":2}");
}
SECTION("remove(std::string)") {
mp["a"] = 1;
mp["b"] = 2;
mp.remove(std::string("b"));
REQUIRE(mp.as<std::string>() == "{\"a\":1}");
}
#ifdef HAS_VARIABLE_LENGTH_ARRAY
SECTION("remove(vla)") {
mp["a"] = 1;
mp["b"] = 2;
int i = 4;
char vla[i];
strcpy(vla, "b");
mp.remove(vla);
REQUIRE(mp.as<std::string>() == "{\"a\":1}");
}
#endif
}