2021-03-29 17:14:01 +02:00
|
|
|
// ArduinoJson - https://arduinojson.org
|
2023-02-16 11:45:01 +01:00
|
|
|
// Copyright © 2014-2023, Benoit BLANCHON
|
2018-09-11 16:05:56 +02:00
|
|
|
// MIT License
|
|
|
|
|
|
|
|
#include <ArduinoJson.h>
|
|
|
|
#include <catch.hpp>
|
2023-04-17 10:41:37 +02:00
|
|
|
#include "Allocators.hpp"
|
2018-09-11 16:05:56 +02:00
|
|
|
|
|
|
|
TEST_CASE("JsonVariant::set(JsonVariant)") {
|
2023-07-25 14:46:25 +02:00
|
|
|
KillswitchAllocator killswitch;
|
|
|
|
SpyingAllocator spyingAllocator(&killswitch);
|
2023-07-17 18:15:13 +02:00
|
|
|
JsonDocument doc1(&spyingAllocator);
|
|
|
|
JsonDocument doc2(&spyingAllocator);
|
2018-09-11 16:05:56 +02:00
|
|
|
JsonVariant var1 = doc1.to<JsonVariant>();
|
|
|
|
JsonVariant var2 = doc2.to<JsonVariant>();
|
|
|
|
|
|
|
|
SECTION("stores JsonArray by copy") {
|
|
|
|
JsonArray arr = doc2.to<JsonArray>();
|
2018-11-09 17:27:32 +01:00
|
|
|
JsonObject obj = arr.createNestedObject();
|
|
|
|
obj["hello"] = "world";
|
2018-09-11 16:05:56 +02:00
|
|
|
|
2018-10-04 11:16:23 +02:00
|
|
|
var1.set(arr);
|
|
|
|
|
|
|
|
arr[0] = 666;
|
2018-11-09 17:27:32 +01:00
|
|
|
REQUIRE(var1.as<std::string>() == "[{\"hello\":\"world\"}]");
|
2018-09-11 16:05:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("stores JsonObject by copy") {
|
|
|
|
JsonObject obj = doc2.to<JsonObject>();
|
2018-11-09 17:27:32 +01:00
|
|
|
JsonArray arr = obj.createNestedArray("value");
|
|
|
|
arr.add(42);
|
2018-09-11 16:05:56 +02:00
|
|
|
|
2018-10-04 11:16:23 +02:00
|
|
|
var1.set(obj);
|
|
|
|
|
|
|
|
obj["value"] = 666;
|
2018-11-09 17:27:32 +01:00
|
|
|
REQUIRE(var1.as<std::string>() == "{\"value\":[42]}");
|
2018-09-11 16:05:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("stores const char* by reference") {
|
|
|
|
var1.set("hello!!");
|
2023-04-17 10:41:37 +02:00
|
|
|
spyingAllocator.clearLog();
|
|
|
|
|
2018-09-11 16:05:56 +02:00
|
|
|
var2.set(var1);
|
|
|
|
|
2023-07-26 06:06:38 +02:00
|
|
|
REQUIRE(spyingAllocator.log() == AllocatorLog{});
|
2018-09-11 16:05:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("stores char* by copy") {
|
|
|
|
char str[] = "hello!!";
|
|
|
|
var1.set(str);
|
2023-04-17 10:41:37 +02:00
|
|
|
spyingAllocator.clearLog();
|
|
|
|
|
2018-09-11 16:05:56 +02:00
|
|
|
var2.set(var1);
|
|
|
|
|
2023-07-26 06:06:38 +02:00
|
|
|
REQUIRE(spyingAllocator.log() == AllocatorLog{
|
|
|
|
Allocate(sizeofString("hello!!")),
|
|
|
|
});
|
2023-04-17 10:41:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("fails gracefully if string allocation fails") {
|
|
|
|
char str[] = "hello!!";
|
|
|
|
var1.set(str);
|
2023-07-25 14:46:25 +02:00
|
|
|
killswitch.on();
|
2023-04-17 10:41:37 +02:00
|
|
|
spyingAllocator.clearLog();
|
|
|
|
|
|
|
|
var2.set(var1);
|
|
|
|
|
|
|
|
REQUIRE(doc2.overflowed() == true);
|
2023-07-26 06:06:38 +02:00
|
|
|
REQUIRE(spyingAllocator.log() == AllocatorLog{
|
|
|
|
AllocateFail(sizeofString("hello!!")),
|
|
|
|
});
|
2018-09-11 16:05:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("stores std::string by copy") {
|
|
|
|
var1.set(std::string("hello!!"));
|
2023-04-17 10:41:37 +02:00
|
|
|
spyingAllocator.clearLog();
|
|
|
|
|
2018-09-11 16:05:56 +02:00
|
|
|
var2.set(var1);
|
|
|
|
|
2023-07-26 06:06:38 +02:00
|
|
|
REQUIRE(spyingAllocator.log() == AllocatorLog{
|
|
|
|
Allocate(sizeofString("hello!!")),
|
|
|
|
});
|
2018-09-11 16:05:56 +02:00
|
|
|
}
|
|
|
|
|
2023-05-02 09:36:40 +02:00
|
|
|
SECTION("stores Serialized<const char*> by copy") {
|
|
|
|
var1.set(serialized("hello!!", 7));
|
2023-04-17 10:41:37 +02:00
|
|
|
spyingAllocator.clearLog();
|
|
|
|
|
2018-09-11 16:05:56 +02:00
|
|
|
var2.set(var1);
|
|
|
|
|
2023-07-26 06:06:38 +02:00
|
|
|
REQUIRE(spyingAllocator.log() == AllocatorLog{
|
|
|
|
Allocate(sizeofString("hello!!")),
|
|
|
|
});
|
2018-09-11 16:05:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("stores Serialized<char*> by copy") {
|
|
|
|
char str[] = "hello!!";
|
2020-07-08 09:38:27 +02:00
|
|
|
var1.set(serialized(str, 7));
|
2023-04-17 10:41:37 +02:00
|
|
|
spyingAllocator.clearLog();
|
|
|
|
|
2018-09-11 16:05:56 +02:00
|
|
|
var2.set(var1);
|
|
|
|
|
2023-07-26 06:06:38 +02:00
|
|
|
REQUIRE(spyingAllocator.log() == AllocatorLog{
|
|
|
|
Allocate(sizeofString("hello!!")),
|
|
|
|
});
|
2018-09-11 16:05:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("stores Serialized<std::string> by copy") {
|
2020-07-08 09:38:27 +02:00
|
|
|
var1.set(serialized(std::string("hello!!")));
|
2023-04-17 10:41:37 +02:00
|
|
|
spyingAllocator.clearLog();
|
|
|
|
|
2018-09-11 16:05:56 +02:00
|
|
|
var2.set(var1);
|
|
|
|
|
2023-07-26 06:06:38 +02:00
|
|
|
REQUIRE(spyingAllocator.log() == AllocatorLog{
|
|
|
|
Allocate(sizeofString("hello!!")),
|
|
|
|
});
|
2023-04-17 10:41:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("fails gracefully if raw string allocation fails") {
|
|
|
|
var1.set(serialized(std::string("hello!!")));
|
2023-07-25 14:46:25 +02:00
|
|
|
killswitch.on();
|
2023-04-17 10:41:37 +02:00
|
|
|
spyingAllocator.clearLog();
|
|
|
|
|
|
|
|
var2.set(var1);
|
|
|
|
|
|
|
|
REQUIRE(doc2.overflowed() == true);
|
2023-07-26 06:06:38 +02:00
|
|
|
REQUIRE(spyingAllocator.log() == AllocatorLog{
|
|
|
|
AllocateFail(sizeofString("hello!!")),
|
|
|
|
});
|
2018-09-11 16:05:56 +02:00
|
|
|
}
|
2021-09-10 08:40:02 +02:00
|
|
|
|
|
|
|
SECTION("destination is unbound") {
|
|
|
|
JsonVariant unboundVariant;
|
|
|
|
|
|
|
|
unboundVariant.set(var1);
|
|
|
|
|
2021-12-16 14:55:26 +01:00
|
|
|
REQUIRE(unboundVariant.isUnbound());
|
2021-09-10 08:40:02 +02:00
|
|
|
REQUIRE(unboundVariant.isNull());
|
|
|
|
}
|
2018-09-11 16:05:56 +02:00
|
|
|
}
|