Added copy-constructor and copy-assignment-operator for JsonDocument (issue #827)

This commit is contained in:
Benoit Blanchon
2018-10-13 13:21:30 +02:00
parent 8230f8fc9b
commit 84f199f0dd
7 changed files with 204 additions and 19 deletions

View File

@ -6,9 +6,8 @@
#include <catch.hpp>
TEST_CASE("StaticJsonDocument") {
StaticJsonDocument<200> doc;
SECTION("serializeJson()") {
StaticJsonDocument<200> doc;
JsonObject obj = doc.to<JsonObject>();
obj["hello"] = "world";
@ -17,4 +16,87 @@ TEST_CASE("StaticJsonDocument") {
REQUIRE(json == "{\"hello\":\"world\"}");
}
SECTION("Copy assignment") {
StaticJsonDocument<200> doc1, doc2;
doc1.to<JsonVariant>().set(666);
deserializeJson(doc2, "{\"hello\":\"world\"}");
doc2.nestingLimit = 42;
doc1 = doc2;
std::string json;
serializeJson(doc1, json);
REQUIRE(json == "{\"hello\":\"world\"}");
REQUIRE(doc1.nestingLimit == 42);
}
SECTION("Copy constructor") {
StaticJsonDocument<200> doc1;
deserializeJson(doc1, "{\"hello\":\"world\"}");
doc1.nestingLimit = 42;
StaticJsonDocument<200> doc2 = doc1;
std::string json;
serializeJson(doc2, json);
REQUIRE(json == "{\"hello\":\"world\"}");
REQUIRE(doc2.nestingLimit == 42);
}
SECTION("Assign from StaticJsonDocument of different capacity") {
StaticJsonDocument<200> doc1;
StaticJsonDocument<300> doc2;
doc1.to<JsonVariant>().set(666);
deserializeJson(doc2, "{\"hello\":\"world\"}");
doc2.nestingLimit = 42;
doc1 = doc2;
std::string json;
serializeJson(doc1, json);
REQUIRE(json == "{\"hello\":\"world\"}");
REQUIRE(doc1.nestingLimit == 42);
}
SECTION("Assign from DynamicJsonDocument") {
StaticJsonDocument<200> doc1;
DynamicJsonDocument doc2;
doc1.to<JsonVariant>().set(666);
deserializeJson(doc2, "{\"hello\":\"world\"}");
doc2.nestingLimit = 42;
doc1 = doc2;
std::string json;
serializeJson(doc1, json);
REQUIRE(json == "{\"hello\":\"world\"}");
REQUIRE(doc1.nestingLimit == 42);
}
SECTION("Construct from StaticJsonDocument of different size") {
StaticJsonDocument<300> doc2;
deserializeJson(doc2, "{\"hello\":\"world\"}");
doc2.nestingLimit = 42;
StaticJsonDocument<200> doc1 = doc2;
std::string json;
serializeJson(doc1, json);
REQUIRE(json == "{\"hello\":\"world\"}");
REQUIRE(doc1.nestingLimit == 42);
}
SECTION("Construct from DynamicJsonDocument") {
DynamicJsonDocument doc2;
deserializeJson(doc2, "{\"hello\":\"world\"}");
doc2.nestingLimit = 42;
StaticJsonDocument<200> doc1 = doc2;
std::string json;
serializeJson(doc1, json);
REQUIRE(json == "{\"hello\":\"world\"}");
REQUIRE(doc1.nestingLimit == 42);
}
}