mirror of
https://github.com/eledio-devices/thirdparty-ArduinoJson.git
synced 2025-11-01 00:38:27 +01:00
Test array subscript
This commit is contained in:
@@ -131,7 +131,7 @@ TEST_CASE("JsonVariant::operator[]") {
|
||||
#endif
|
||||
|
||||
SECTION("get value from linked object") {
|
||||
StaticJsonDocument<1024> doc1, doc2;
|
||||
StaticJsonDocument<1024> doc2;
|
||||
doc2["hello"] = "world";
|
||||
var.link(doc2);
|
||||
|
||||
@@ -139,7 +139,7 @@ TEST_CASE("JsonVariant::operator[]") {
|
||||
}
|
||||
|
||||
SECTION("set value to linked object") {
|
||||
StaticJsonDocument<1024> doc1, doc2;
|
||||
StaticJsonDocument<1024> doc2;
|
||||
doc2["hello"] = "world";
|
||||
var.link(doc2);
|
||||
|
||||
@@ -148,6 +148,25 @@ TEST_CASE("JsonVariant::operator[]") {
|
||||
CHECK(doc.as<std::string>() == "{\"hello\":\"world\"}");
|
||||
CHECK(doc2.as<std::string>() == "{\"hello\":\"world\"}");
|
||||
}
|
||||
|
||||
SECTION("get value from linked array") {
|
||||
StaticJsonDocument<1024> doc2;
|
||||
doc2.add(42);
|
||||
var.link(doc2);
|
||||
|
||||
CHECK(var[0].as<int>() == 42);
|
||||
}
|
||||
|
||||
SECTION("set value to linked array") {
|
||||
StaticJsonDocument<1024> doc2;
|
||||
doc2.add(42);
|
||||
var.link(doc2);
|
||||
|
||||
var[0] = 666; // no-op
|
||||
|
||||
CHECK(doc.as<std::string>() == "[42]");
|
||||
CHECK(doc2.as<std::string>() == "[42]");
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("JsonVariantConst::operator[]") {
|
||||
@@ -222,10 +241,18 @@ TEST_CASE("JsonVariantConst::operator[]") {
|
||||
}
|
||||
|
||||
SECTION("get value from linked object") {
|
||||
StaticJsonDocument<1024> doc1, doc2;
|
||||
StaticJsonDocument<1024> doc2;
|
||||
doc2["hello"] = "world";
|
||||
var.link(doc2);
|
||||
|
||||
CHECK(cvar["hello"].as<std::string>() == "world");
|
||||
}
|
||||
|
||||
SECTION("get value from linked array") {
|
||||
StaticJsonDocument<1024> doc2;
|
||||
doc2.add(42);
|
||||
var.link(doc2);
|
||||
|
||||
CHECK(cvar[0].as<int>() == 42);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,13 +88,12 @@ class VariantData {
|
||||
bool asBoolean() const;
|
||||
|
||||
CollectionData *asArray() {
|
||||
return isArray() ? &_content.asCollection : 0;
|
||||
return isArrayStrict() ? &_content.asCollection : 0;
|
||||
}
|
||||
|
||||
const CollectionData *asArray() const {
|
||||
// TODO
|
||||
// if (isPointer())
|
||||
// return _content.asPointer->asArray();
|
||||
if (isPointer())
|
||||
return _content.asPointer->asArray();
|
||||
return const_cast<VariantData *>(this)->asArray();
|
||||
}
|
||||
|
||||
@@ -302,13 +301,14 @@ class VariantData {
|
||||
}
|
||||
|
||||
VariantData *getElement(size_t index) const {
|
||||
return isArray() ? _content.asCollection.getElement(index) : 0;
|
||||
const CollectionData *col = asArray();
|
||||
return col ? col->getElement(index) : 0;
|
||||
}
|
||||
|
||||
VariantData *getOrAddElement(size_t index, MemoryPool *pool) {
|
||||
if (isNull())
|
||||
toArray();
|
||||
if (!isArray())
|
||||
if (!isArrayStrict())
|
||||
return 0;
|
||||
return _content.asCollection.getOrAddElement(index, pool);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user