Changed the array subscript to automatically add missing elements

This commit is contained in:
Benoit Blanchon
2020-02-20 08:59:25 +01:00
parent 0001dabfd1
commit d8724e0a0b
24 changed files with 209 additions and 93 deletions

View File

@ -9,7 +9,6 @@ using namespace ARDUINOJSON_NAMESPACE;
TEST_CASE("ElementProxy::set()") {
DynamicJsonDocument doc(4096);
doc.addElement();
ElementProxy<JsonDocument&> ep = doc[0];
SECTION("set(int)") {

View File

@ -0,0 +1,25 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2020
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
using namespace ARDUINOJSON_NAMESPACE;
TEST_CASE("MemberProxy::operator[]") {
DynamicJsonDocument doc(4096);
ElementProxy<JsonDocument&> ep = doc[1];
SECTION("set member") {
ep["world"] = 42;
REQUIRE(doc.as<std::string>() == "[null,{\"world\":42}]");
}
SECTION("set element") {
ep[2] = 42;
REQUIRE(doc.as<std::string>() == "[null,[null,null,42]]");
}
}

File diff suppressed because one or more lines are too long

View File

@ -9,7 +9,20 @@
TEST_CASE("JsonArray::operator[]") {
DynamicJsonDocument doc(4096);
JsonArray array = doc.to<JsonArray>();
array.add(0);
SECTION("Pad with null") {
array[2] = 2;
array[5] = 5;
REQUIRE(array.size() == 6);
REQUIRE(array[0].isNull() == true);
REQUIRE(array[1].isNull() == true);
REQUIRE(array[2].isNull() == false);
REQUIRE(array[3].isNull() == true);
REQUIRE(array[4].isNull() == true);
REQUIRE(array[5].isNull() == false);
REQUIRE(array[2] == 2);
REQUIRE(array[5] == 5);
}
SECTION("int") {
array[0] = 123;

View File

@ -43,3 +43,11 @@ TEST_CASE("JsonDocument automatically promotes to object") {
REQUIRE(doc["one"]["two"]["three"] == 4);
}
TEST_CASE("JsonDocument automatically promotes to array") {
DynamicJsonDocument doc(4096);
doc[2] = 2;
REQUIRE(doc.as<std::string>() == "[null,null,2]");
}

View File

@ -43,10 +43,10 @@ TEST_CASE("JsonVariant::operator[]") {
SECTION("set value") {
array.add("hello");
var[0] = "world";
var[1] = "world";
REQUIRE(1 == var.size());
REQUIRE(std::string("world") == var[0]);
REQUIRE(var.size() == 2);
REQUIRE(std::string("world") == var[1]);
}
SECTION("set value in a nested object") {

View File

@ -11,9 +11,15 @@ TEST_CASE("MemberProxy::operator[]") {
DynamicJsonDocument doc(4096);
MemberProxy<JsonDocument&, const char*> mp = doc["hello"];
SECTION("set integer") {
SECTION("set member") {
mp["world"] = 42;
REQUIRE(doc.as<std::string>() == "{\"hello\":{\"world\":42}}");
}
SECTION("set element") {
mp[2] = 42;
REQUIRE(doc.as<std::string>() == "{\"hello\":[null,null,42]}");
}
}