mirror of
https://github.com/eledio-devices/thirdparty-ArduinoJson.git
synced 2025-11-01 08:48:30 +01:00
Test set() instead of add()
This commit is contained in:
@@ -43,24 +43,4 @@ TEST_CASE("JsonVariant::add()") {
|
||||
|
||||
REQUIRE(var.as<std::string>() == "{\"val\":123}");
|
||||
}
|
||||
|
||||
SECTION("add JsonDocument to new variant") {
|
||||
StaticJsonDocument<128> doc2;
|
||||
doc2["hello"] = "world";
|
||||
|
||||
var.add(doc2);
|
||||
|
||||
CHECK(var.as<std::string>() == "[{\"hello\":\"world\"}]");
|
||||
CHECK(var.memoryUsage() == JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(1));
|
||||
}
|
||||
|
||||
SECTION("add JsonDocument* to new variant") {
|
||||
StaticJsonDocument<128> doc2;
|
||||
doc2["hello"] = "world";
|
||||
|
||||
var.add(&doc2);
|
||||
|
||||
CHECK(var.as<std::string>() == "[{\"hello\":\"world\"}]");
|
||||
CHECK(var.memoryUsage() == JSON_ARRAY_SIZE(1));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -155,18 +155,35 @@ TEST_CASE("JsonVariant::set() with not enough memory") {
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("JsonVariant::set(DynamicJsonDocument)") {
|
||||
DynamicJsonDocument doc1(1024);
|
||||
doc1["hello"] = "world";
|
||||
TEST_CASE("Copy/link from other document") {
|
||||
StaticJsonDocument<1024> doc1, doc2;
|
||||
JsonVariant variant = doc1.to<JsonVariant>();
|
||||
|
||||
DynamicJsonDocument doc2(1024);
|
||||
JsonVariant v = doc2.to<JsonVariant>();
|
||||
SECTION("JsonVariant::set(JsonDocument&)") {
|
||||
doc2["hello"] = "world";
|
||||
|
||||
// Should copy the doc
|
||||
v.set(doc1);
|
||||
doc1.clear();
|
||||
variant.set(doc2);
|
||||
|
||||
std::string json;
|
||||
serializeJson(doc2, json);
|
||||
REQUIRE(json == "{\"hello\":\"world\"}");
|
||||
CHECK(variant.as<std::string>() == "{\"hello\":\"world\"}");
|
||||
CHECK(variant.memoryUsage() == JSON_OBJECT_SIZE(1));
|
||||
|
||||
// altering the copied document should change the result
|
||||
doc2["hello"] = "WORLD!";
|
||||
|
||||
CHECK(variant.as<std::string>() == "{\"hello\":\"world\"}");
|
||||
}
|
||||
|
||||
SECTION("JsonVariant::set(JsonDocument*)") {
|
||||
doc2["hello"] = "world";
|
||||
|
||||
variant.set(&doc2);
|
||||
|
||||
CHECK(variant.as<std::string>() == "{\"hello\":\"world\"}");
|
||||
CHECK(variant.memoryUsage() == 0);
|
||||
|
||||
// altering the links document should change the result
|
||||
doc2["hello"] = "WORLD!";
|
||||
|
||||
CHECK(variant.as<std::string>() == "{\"hello\":\"WORLD!\"}");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user