| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- // ArduinoJson - https://arduinojson.org
- // Copyright © 2014-2025, Benoit BLANCHON
- // MIT License
- #include <ArduinoJson.h>
- #include <catch.hpp>
- #include "Allocators.hpp"
- #include "Literals.hpp"
- TEST_CASE("JsonDocument constructor") {
- SpyingAllocator spyingAllocator;
- SECTION("JsonDocument(size_t)") {
- { JsonDocument doc(&spyingAllocator); }
- REQUIRE(spyingAllocator.log() == AllocatorLog{});
- }
- SECTION("JsonDocument(const JsonDocument&)") {
- {
- JsonDocument doc1(&spyingAllocator);
- doc1.set("The size of this string is 32!!"_s);
- JsonDocument doc2(doc1);
- REQUIRE(doc1.as<std::string>() == "The size of this string is 32!!");
- REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
- }
- REQUIRE(spyingAllocator.log() == AllocatorLog{
- Allocate(sizeofStringBuffer()),
- Allocate(sizeofStringBuffer()),
- Deallocate(sizeofStringBuffer()),
- Deallocate(sizeofStringBuffer()),
- });
- }
- SECTION("JsonDocument(JsonDocument&&)") {
- {
- JsonDocument doc1(&spyingAllocator);
- doc1.set("The size of this string is 32!!"_s);
- JsonDocument doc2(std::move(doc1));
- REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
- // NOLINTNEXTLINE(clang-analyzer-cplusplus.Move)
- REQUIRE(doc1.as<std::string>() == "null");
- }
- REQUIRE(spyingAllocator.log() == AllocatorLog{
- Allocate(sizeofStringBuffer()),
- Deallocate(sizeofStringBuffer()),
- });
- }
- SECTION("JsonDocument(JsonObject, Allocator*)") {
- JsonDocument doc1;
- JsonObject obj = doc1.to<JsonObject>();
- obj["hello"] = "world";
- JsonDocument doc2(obj, &spyingAllocator);
- REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
- REQUIRE(spyingAllocator.log() == AllocatorLog{
- Allocate(sizeofPool()),
- Allocate(sizeofString("hello")),
- Allocate(sizeofString("world")),
- });
- }
- SECTION("JsonDocument(JsonObject)") {
- JsonDocument doc1;
- JsonObject obj = doc1.to<JsonObject>();
- obj["hello"] = "world";
- JsonDocument doc2(obj);
- REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
- }
- SECTION("JsonDocument(JsonArray, Allocator*)") {
- JsonDocument doc1;
- JsonArray arr = doc1.to<JsonArray>();
- arr.add("hello");
- JsonDocument doc2(arr, &spyingAllocator);
- REQUIRE(doc2.as<std::string>() == "[\"hello\"]");
- REQUIRE(spyingAllocator.log() == AllocatorLog{
- Allocate(sizeofPool()),
- Allocate(sizeofString("hello")),
- });
- }
- SECTION("JsonDocument(JsonArray)") {
- JsonDocument doc1;
- JsonArray arr = doc1.to<JsonArray>();
- arr.add("hello");
- JsonDocument doc2(arr);
- REQUIRE(doc2.as<std::string>() == "[\"hello\"]");
- }
- SECTION("JsonDocument(JsonVariant, Allocator*)") {
- JsonDocument doc1;
- deserializeJson(doc1, "\"hello\"");
- JsonDocument doc2(doc1.as<JsonVariant>(), &spyingAllocator);
- REQUIRE(doc2.as<std::string>() == "hello");
- REQUIRE(spyingAllocator.log() == AllocatorLog{
- Allocate(sizeofString("hello")),
- });
- }
- SECTION("JsonDocument(JsonVariant)") {
- JsonDocument doc1;
- deserializeJson(doc1, "\"hello\"");
- JsonDocument doc2(doc1.as<JsonVariant>());
- REQUIRE(doc2.as<std::string>() == "hello");
- }
- SECTION("JsonDocument(JsonVariantConst)") {
- JsonDocument doc1;
- deserializeJson(doc1, "\"hello\"");
- JsonDocument doc2(doc1.as<JsonVariantConst>());
- REQUIRE(doc2.as<std::string>() == "hello");
- }
- SECTION("JsonDocument(ElementProxy)") {
- JsonDocument doc1;
- deserializeJson(doc1, "[\"hello\",\"world\"]");
- JsonDocument doc2(doc1[1]);
- REQUIRE(doc2.as<std::string>() == "world");
- }
- SECTION("JsonDocument(MemberProxy)") {
- JsonDocument doc1;
- deserializeJson(doc1, "{\"hello\":\"world\"}");
- JsonDocument doc2(doc1["hello"]);
- REQUIRE(doc2.as<std::string>() == "world");
- }
- }
|