mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-22 23:07:29 +02:00
Changed the array subscript to automatically add missing elements
This commit is contained in:
@ -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)") {
|
||||
|
25
extras/tests/ElementProxy/subscript.cpp
Normal file
25
extras/tests/ElementProxy/subscript.cpp
Normal 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
@ -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;
|
||||
|
@ -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]");
|
||||
}
|
||||
|
@ -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") {
|
||||
|
@ -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]}");
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user