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-04-07 14:43:16 +02:00
|
|
|
using ArduinoJson::detail::sizeofString;
|
2023-03-29 19:28:44 +02:00
|
|
|
|
2019-01-29 14:09:09 +01:00
|
|
|
TEST_CASE("JsonDocument::add()") {
|
2023-07-24 17:21:25 +02:00
|
|
|
SpyingAllocator allocator;
|
|
|
|
JsonDocument doc(&allocator);
|
2019-01-29 14:09:09 +01:00
|
|
|
|
|
|
|
SECTION("integer") {
|
|
|
|
doc.add(42);
|
|
|
|
|
|
|
|
REQUIRE(doc.as<std::string>() == "[42]");
|
2023-07-24 17:21:25 +02:00
|
|
|
REQUIRE(allocator.log() == AllocatorLog()
|
|
|
|
<< 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-24 17:21:25 +02:00
|
|
|
REQUIRE(allocator.log() == AllocatorLog()
|
|
|
|
<< 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-24 17:21:25 +02:00
|
|
|
REQUIRE(allocator.log() == AllocatorLog()
|
|
|
|
<< AllocatorLog::Allocate(sizeofPool())
|
|
|
|
<< AllocatorLog::Allocate(sizeofString(7)));
|
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-24 17:21:25 +02:00
|
|
|
REQUIRE(allocator.log() == AllocatorLog()
|
|
|
|
<< AllocatorLog::Allocate(sizeofPool())
|
|
|
|
<< AllocatorLog::Allocate(sizeofString(7)));
|
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-24 17:21:25 +02:00
|
|
|
REQUIRE(allocator.log() == AllocatorLog()
|
|
|
|
<< AllocatorLog::Allocate(sizeofPool())
|
|
|
|
<< AllocatorLog::Allocate(sizeofString(7)));
|
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-24 17:21:25 +02:00
|
|
|
REQUIRE(allocator.log() == AllocatorLog()
|
|
|
|
<< AllocatorLog::Allocate(sizeofPool())
|
|
|
|
<< AllocatorLog::Allocate(sizeofString(7)));
|
2023-03-29 19:28:44 +02:00
|
|
|
}
|
2019-01-29 14:09:09 +01:00
|
|
|
}
|