2021-03-29 17:14:01 +02:00
|
|
|
// ArduinoJson - https://arduinojson.org
|
2025-02-24 15:18:26 +01:00
|
|
|
// Copyright © 2014-2025, Benoit BLANCHON
|
2019-01-29 14:09:09 +01:00
|
|
|
// MIT License
|
|
|
|
|
2023-03-29 19:28:44 +02:00
|
|
|
#define ARDUINOJSON_ENABLE_ARDUINO_STRING 1
|
|
|
|
#define ARDUINOJSON_ENABLE_PROGMEM 1
|
2019-01-29 14:09:09 +01:00
|
|
|
#include <ArduinoJson.h>
|
2023-03-29 19:28:44 +02:00
|
|
|
|
2019-01-29 14:09:09 +01:00
|
|
|
#include <catch.hpp>
|
|
|
|
|
2023-07-24 17:21:25 +02:00
|
|
|
#include "Allocators.hpp"
|
2024-06-07 09:35:45 +02:00
|
|
|
#include "Literals.hpp"
|
2023-07-24 17:21:25 +02:00
|
|
|
|
2023-03-29 19:28:44 +02:00
|
|
|
using ArduinoJson::detail::sizeofArray;
|
|
|
|
|
2023-08-09 10:57:52 +02:00
|
|
|
TEST_CASE("JsonDocument::add(T)") {
|
2023-07-25 14:53:54 +02:00
|
|
|
SpyingAllocator spy;
|
|
|
|
JsonDocument doc(&spy);
|
2019-01-29 14:09:09 +01:00
|
|
|
|
|
|
|
SECTION("integer") {
|
|
|
|
doc.add(42);
|
|
|
|
|
|
|
|
REQUIRE(doc.as<std::string>() == "[42]");
|
2023-07-26 06:06:38 +02:00
|
|
|
REQUIRE(spy.log() == AllocatorLog{
|
|
|
|
Allocate(sizeofPool()),
|
|
|
|
});
|
2019-01-29 14:09:09 +01:00
|
|
|
}
|
|
|
|
|
2024-11-25 11:32:03 +01:00
|
|
|
SECTION("string literal") {
|
2019-01-29 14:09:09 +01:00
|
|
|
doc.add("hello");
|
|
|
|
|
|
|
|
REQUIRE(doc.as<std::string>() == "[\"hello\"]");
|
2023-07-26 06:06:38 +02:00
|
|
|
REQUIRE(spy.log() == AllocatorLog{
|
|
|
|
Allocate(sizeofPool()),
|
|
|
|
});
|
2019-01-29 14:09:09 +01:00
|
|
|
}
|
2023-03-29 19:28:44 +02:00
|
|
|
|
2024-11-25 11:32:03 +01:00
|
|
|
SECTION("const char*") {
|
|
|
|
const char* value = "hello";
|
|
|
|
doc.add(value);
|
|
|
|
|
|
|
|
REQUIRE(doc.as<std::string>() == "[\"hello\"]");
|
|
|
|
REQUIRE(spy.log() == AllocatorLog{
|
|
|
|
Allocate(sizeofPool()),
|
|
|
|
Allocate(sizeofString("hello")),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-03-29 19:28:44 +02:00
|
|
|
SECTION("std::string") {
|
2024-06-07 09:35:45 +02:00
|
|
|
doc.add("example"_s);
|
|
|
|
doc.add("example"_s);
|
2023-03-29 19:28:44 +02:00
|
|
|
|
|
|
|
CHECK(doc[0].as<const char*>() == doc[1].as<const char*>());
|
2023-07-26 06:06:38 +02:00
|
|
|
REQUIRE(spy.log() == AllocatorLog{
|
|
|
|
Allocate(sizeofPool()),
|
|
|
|
Allocate(sizeofString("example")),
|
|
|
|
});
|
2023-03-29 19:28:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("char*") {
|
|
|
|
char value[] = "example";
|
|
|
|
doc.add(value);
|
|
|
|
doc.add(value);
|
|
|
|
|
|
|
|
CHECK(doc[0].as<const char*>() == doc[1].as<const char*>());
|
2023-07-26 06:06:38 +02:00
|
|
|
REQUIRE(spy.log() == AllocatorLog{
|
|
|
|
Allocate(sizeofPool()),
|
|
|
|
Allocate(sizeofString("example")),
|
|
|
|
});
|
2023-03-29 19:28:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Arduino String") {
|
|
|
|
doc.add(String("example"));
|
|
|
|
doc.add(String("example"));
|
|
|
|
|
|
|
|
CHECK(doc[0].as<const char*>() == doc[1].as<const char*>());
|
2023-07-26 06:06:38 +02:00
|
|
|
REQUIRE(spy.log() == AllocatorLog{
|
|
|
|
Allocate(sizeofPool()),
|
|
|
|
Allocate(sizeofString("example")),
|
|
|
|
});
|
2023-03-29 19:28:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Flash string") {
|
|
|
|
doc.add(F("example"));
|
|
|
|
doc.add(F("example"));
|
|
|
|
|
|
|
|
CHECK(doc[0].as<const char*>() == doc[1].as<const char*>());
|
2023-07-26 06:06:38 +02:00
|
|
|
REQUIRE(spy.log() == AllocatorLog{
|
|
|
|
Allocate(sizeofPool()),
|
|
|
|
Allocate(sizeofString("example")),
|
|
|
|
});
|
2023-03-29 19:28:44 +02:00
|
|
|
}
|
2024-11-07 14:19:47 +01:00
|
|
|
|
|
|
|
#ifdef HAS_VARIABLE_LENGTH_ARRAY
|
|
|
|
SECTION("VLA") {
|
|
|
|
size_t i = 16;
|
|
|
|
char vla[i];
|
|
|
|
strcpy(vla, "example");
|
|
|
|
|
|
|
|
doc.add(vla);
|
|
|
|
doc.add(vla);
|
|
|
|
|
|
|
|
CHECK(doc[0].as<const char*>() == doc[1].as<const char*>());
|
|
|
|
REQUIRE("example"_s == doc[0]);
|
|
|
|
REQUIRE(spy.log() == AllocatorLog{
|
|
|
|
Allocate(sizeofPool()),
|
|
|
|
Allocate(sizeofString("example")),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
#endif
|
2019-01-29 14:09:09 +01:00
|
|
|
}
|
2023-08-09 10:57:52 +02:00
|
|
|
|
|
|
|
TEST_CASE("JsonDocument::add<T>()") {
|
|
|
|
JsonDocument doc;
|
|
|
|
|
|
|
|
SECTION("JsonArray") {
|
|
|
|
JsonArray array = doc.add<JsonArray>();
|
|
|
|
array.add(1);
|
|
|
|
array.add(2);
|
|
|
|
REQUIRE(doc.as<std::string>() == "[[1,2]]");
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("JsonVariant") {
|
|
|
|
JsonVariant variant = doc.add<JsonVariant>();
|
|
|
|
variant.set(42);
|
|
|
|
REQUIRE(doc.as<std::string>() == "[42]");
|
|
|
|
}
|
|
|
|
}
|
2024-05-23 18:36:24 +02:00
|
|
|
|
|
|
|
TEST_CASE("JsonObject::add(JsonObject) ") {
|
|
|
|
JsonDocument doc1;
|
2024-06-07 09:35:45 +02:00
|
|
|
doc1["hello"_s] = "world"_s;
|
2024-05-23 18:36:24 +02:00
|
|
|
|
|
|
|
TimebombAllocator allocator(10);
|
|
|
|
SpyingAllocator spy(&allocator);
|
|
|
|
JsonDocument doc2(&spy);
|
|
|
|
|
|
|
|
SECTION("success") {
|
|
|
|
bool result = doc2.add(doc1.as<JsonObject>());
|
|
|
|
|
|
|
|
REQUIRE(result == true);
|
|
|
|
REQUIRE(doc2.as<std::string>() == "[{\"hello\":\"world\"}]");
|
|
|
|
REQUIRE(spy.log() == AllocatorLog{
|
|
|
|
Allocate(sizeofPool()),
|
|
|
|
Allocate(sizeofString("hello")),
|
|
|
|
Allocate(sizeofString("world")),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("partial failure") { // issue #2081
|
|
|
|
allocator.setCountdown(2);
|
|
|
|
|
|
|
|
bool result = doc2.add(doc1.as<JsonObject>());
|
|
|
|
|
|
|
|
REQUIRE(result == false);
|
|
|
|
REQUIRE(doc2.as<std::string>() == "[]");
|
|
|
|
REQUIRE(spy.log() == AllocatorLog{
|
|
|
|
Allocate(sizeofPool()),
|
|
|
|
Allocate(sizeofString("hello")),
|
|
|
|
AllocateFail(sizeofString("world")),
|
|
|
|
Deallocate(sizeofString("hello")),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("complete failure") {
|
|
|
|
allocator.setCountdown(0);
|
|
|
|
|
|
|
|
bool result = doc2.add(doc1.as<JsonObject>());
|
|
|
|
|
|
|
|
REQUIRE(result == false);
|
|
|
|
REQUIRE(doc2.as<std::string>() == "[]");
|
|
|
|
REQUIRE(spy.log() == AllocatorLog{
|
|
|
|
AllocateFail(sizeofPool()),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|