Tests: make allocator assertions more readable

This commit is contained in:
Benoit Blanchon
2023-07-26 06:06:38 +02:00
parent 30ec507989
commit 9a11d98117
31 changed files with 1127 additions and 1287 deletions

View File

@ -8,14 +8,13 @@
#include "Allocators.hpp"
using ArduinoJson::detail::addPadding;
using ArduinoJson::detail::sizeofString;
TEST_CASE("JsonDocument constructor") {
SpyingAllocator spyingAllocator;
SECTION("JsonDocument(size_t)") {
{ JsonDocument doc(&spyingAllocator); }
REQUIRE(spyingAllocator.log() == AllocatorLog());
REQUIRE(spyingAllocator.log() == AllocatorLog{});
}
SECTION("JsonDocument(const JsonDocument&)") {
@ -28,11 +27,12 @@ TEST_CASE("JsonDocument constructor") {
REQUIRE(doc1.as<std::string>() == "The size of this string is 32!!");
REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
}
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(sizeofString(31))
<< AllocatorLog::Allocate(sizeofString(31))
<< AllocatorLog::Deallocate(sizeofString(31))
<< AllocatorLog::Deallocate(sizeofString(31)));
REQUIRE(spyingAllocator.log() == AllocatorLog{
Allocate(sizeofStringBuffer()),
Allocate(sizeofStringBuffer()),
Deallocate(sizeofStringBuffer()),
Deallocate(sizeofStringBuffer()),
});
}
SECTION("JsonDocument(JsonDocument&&)") {
@ -45,9 +45,10 @@ TEST_CASE("JsonDocument constructor") {
REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
REQUIRE(doc1.as<std::string>() == "null");
}
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(sizeofString(31))
<< AllocatorLog::Deallocate(sizeofString(31)));
REQUIRE(spyingAllocator.log() == AllocatorLog{
Allocate(sizeofStringBuffer()),
Deallocate(sizeofStringBuffer()),
});
}
SECTION("JsonDocument(JsonObject)") {
@ -58,8 +59,9 @@ TEST_CASE("JsonDocument constructor") {
JsonDocument doc2(obj, &spyingAllocator);
REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(sizeofPool()));
REQUIRE(spyingAllocator.log() == AllocatorLog{
Allocate(sizeofPool()),
});
}
SECTION("Construct from JsonArray") {
@ -70,8 +72,9 @@ TEST_CASE("JsonDocument constructor") {
JsonDocument doc2(arr, &spyingAllocator);
REQUIRE(doc2.as<std::string>() == "[\"hello\"]");
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(sizeofPool()));
REQUIRE(spyingAllocator.log() == AllocatorLog{
Allocate(sizeofPool()),
});
}
SECTION("Construct from JsonVariant") {
@ -81,7 +84,8 @@ TEST_CASE("JsonDocument constructor") {
JsonDocument doc2(doc1.as<JsonVariant>(), &spyingAllocator);
REQUIRE(doc2.as<std::string>() == "hello");
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(sizeofString(5)));
REQUIRE(spyingAllocator.log() == AllocatorLog{
Allocate(sizeofString("hello")),
});
}
}