Files
ArduinoJson/extras/tests/JsonObject/copy.cpp

142 lines
3.6 KiB
C++
Raw Normal View History

// ArduinoJson - https://arduinojson.org
2023-02-16 11:45:01 +01:00
// Copyright © 2014-2023, Benoit BLANCHON
2018-10-04 11:16:23 +02:00
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
#include "Allocators.hpp"
TEST_CASE("JsonObject::set()") {
SpyingAllocator spy;
JsonDocument doc1(&spy);
JsonDocument doc2(&spy);
2018-10-04 11:16:23 +02:00
JsonObject obj1 = doc1.to<JsonObject>();
JsonObject obj2 = doc2.to<JsonObject>();
SECTION("doesn't copy static string in key or value") {
obj1["hello"] = "world";
spy.clearLog();
2018-10-04 11:16:23 +02:00
2020-02-22 14:17:10 +01:00
bool success = obj2.set(obj1);
2018-10-04 11:16:23 +02:00
2020-02-22 14:17:10 +01:00
REQUIRE(success == true);
2018-10-04 11:16:23 +02:00
REQUIRE(obj2["hello"] == std::string("world"));
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
});
2018-10-04 11:16:23 +02:00
}
SECTION("copy local string value") {
obj1["hello"] = std::string("world");
spy.clearLog();
2018-10-04 11:16:23 +02:00
2020-02-22 14:17:10 +01:00
bool success = obj2.set(obj1);
2018-10-04 11:16:23 +02:00
2020-02-22 14:17:10 +01:00
REQUIRE(success == true);
2018-10-04 11:16:23 +02:00
REQUIRE(obj2["hello"] == std::string("world"));
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofString("world")),
});
2018-10-04 11:16:23 +02:00
}
SECTION("copy local key") {
obj1[std::string("hello")] = "world";
spy.clearLog();
2018-10-04 11:16:23 +02:00
2020-02-22 14:17:10 +01:00
bool success = obj2.set(obj1);
2018-10-04 11:16:23 +02:00
2020-02-22 14:17:10 +01:00
REQUIRE(success == true);
2018-10-04 11:16:23 +02:00
REQUIRE(obj2["hello"] == std::string("world"));
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofString("hello")),
Allocate(sizeofPool()),
});
2018-10-04 11:16:23 +02:00
}
SECTION("copy string from deserializeJson()") {
deserializeJson(doc1, "{'hello':'world'}");
spy.clearLog();
2018-10-04 11:16:23 +02:00
2020-02-22 14:17:10 +01:00
bool success = obj2.set(obj1);
2018-10-04 11:16:23 +02:00
2020-02-22 14:17:10 +01:00
REQUIRE(success == true);
2018-10-04 11:16:23 +02:00
REQUIRE(obj2["hello"] == std::string("world"));
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofString("hello")),
Allocate(sizeofPool()),
Allocate(sizeofString("world")),
});
2018-10-04 11:16:23 +02:00
}
SECTION("copy string from deserializeMsgPack()") {
deserializeMsgPack(doc1, "\x81\xA5hello\xA5world");
spy.clearLog();
2018-10-04 11:16:23 +02:00
2020-02-22 14:17:10 +01:00
bool success = obj2.set(obj1);
2018-10-04 11:16:23 +02:00
2020-02-22 14:17:10 +01:00
REQUIRE(success == true);
2018-10-04 11:16:23 +02:00
REQUIRE(obj2["hello"] == std::string("world"));
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofString("hello")),
Allocate(sizeofPool()),
Allocate(sizeofString("world")),
});
2018-10-04 11:16:23 +02:00
}
SECTION("should work with JsonObjectConst") {
obj1["hello"] = "world";
obj2.set(static_cast<JsonObjectConst>(obj1));
REQUIRE(obj2["hello"] == std::string("world"));
}
2020-02-22 14:17:10 +01:00
SECTION("copy fails in the middle of an object") {
2023-07-24 17:21:25 +02:00
TimebombAllocator timebomb(2);
JsonDocument doc3(&timebomb);
2020-02-22 14:17:10 +01:00
JsonObject obj3 = doc3.to<JsonObject>();
obj1[std::string("a")] = 1;
obj1[std::string("b")] = 2;
2020-02-22 14:17:10 +01:00
bool success = obj3.set(obj1);
REQUIRE(success == false);
2023-04-11 10:03:47 +02:00
REQUIRE(doc3.as<std::string>() == "{\"a\":1}");
2020-02-22 14:17:10 +01:00
}
SECTION("copy fails in the middle of an array") {
2023-07-24 17:21:25 +02:00
TimebombAllocator timebomb(1);
JsonDocument doc3(&timebomb);
2020-02-22 14:17:10 +01:00
JsonObject obj3 = doc3.to<JsonObject>();
obj1["hello"][0] = std::string("world");
2020-02-22 14:17:10 +01:00
bool success = obj3.set(obj1);
REQUIRE(success == false);
REQUIRE(doc3.as<std::string>() == "{\"hello\":[null]}");
2020-02-22 14:17:10 +01:00
}
2020-02-23 13:03:14 +01:00
SECTION("destination is null") {
JsonObject null;
obj1["hello"] = "world";
bool success = null.set(obj1);
REQUIRE(success == false);
}
SECTION("source is null") {
JsonObject null;
obj1["hello"] = "world";
bool success = obj1.set(null);
REQUIRE(success == false);
}
2018-10-04 11:16:23 +02:00
}