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
|
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"
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("const char*") {
|
|
|
|
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
|
|
|
|
|
|
|
SECTION("std::string") {
|
|
|
|
doc.add(std::string("example"));
|
|
|
|
doc.add(std::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("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
|
|
|
}
|
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("JsonObject") {
|
|
|
|
JsonObject object = doc.add<JsonObject>();
|
|
|
|
object["hello"] = "world";
|
|
|
|
REQUIRE(doc.as<std::string>() == "[{\"hello\":\"world\"}]");
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("JsonVariant") {
|
|
|
|
JsonVariant variant = doc.add<JsonVariant>();
|
|
|
|
variant.set(42);
|
|
|
|
REQUIRE(doc.as<std::string>() == "[42]");
|
|
|
|
}
|
|
|
|
}
|