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,8 +8,6 @@
#include "Allocators.hpp"
using ArduinoJson::detail::sizeofString;
TEST_CASE("JsonVariant::clear()") {
SpyingAllocator spy;
JsonDocument doc(&spy);
@ -33,8 +31,9 @@ TEST_CASE("JsonVariant::clear()") {
var.set(std::string("hello"));
var.clear();
REQUIRE(spy.log() == AllocatorLog()
<< AllocatorLog::Allocate(sizeofString(5))
<< AllocatorLog::Deallocate(sizeofString(5)));
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofString("hello")),
Deallocate(sizeofString("hello")),
});
}
}

View File

@ -6,8 +6,6 @@
#include <catch.hpp>
#include "Allocators.hpp"
using ArduinoJson::detail::sizeofString;
TEST_CASE("JsonVariant::set(JsonVariant)") {
KillswitchAllocator killswitch;
SpyingAllocator spyingAllocator(&killswitch);
@ -44,7 +42,7 @@ TEST_CASE("JsonVariant::set(JsonVariant)") {
var2.set(var1);
REQUIRE(spyingAllocator.log() == AllocatorLog());
REQUIRE(spyingAllocator.log() == AllocatorLog{});
}
SECTION("stores char* by copy") {
@ -54,8 +52,9 @@ TEST_CASE("JsonVariant::set(JsonVariant)") {
var2.set(var1);
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(sizeofString((7))));
REQUIRE(spyingAllocator.log() == AllocatorLog{
Allocate(sizeofString("hello!!")),
});
}
SECTION("fails gracefully if string allocation fails") {
@ -67,8 +66,9 @@ TEST_CASE("JsonVariant::set(JsonVariant)") {
var2.set(var1);
REQUIRE(doc2.overflowed() == true);
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::AllocateFail(sizeofString((7))));
REQUIRE(spyingAllocator.log() == AllocatorLog{
AllocateFail(sizeofString("hello!!")),
});
}
SECTION("stores std::string by copy") {
@ -77,8 +77,9 @@ TEST_CASE("JsonVariant::set(JsonVariant)") {
var2.set(var1);
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(sizeofString((7))));
REQUIRE(spyingAllocator.log() == AllocatorLog{
Allocate(sizeofString("hello!!")),
});
}
SECTION("stores Serialized<const char*> by copy") {
@ -87,8 +88,9 @@ TEST_CASE("JsonVariant::set(JsonVariant)") {
var2.set(var1);
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(sizeofString((7))));
REQUIRE(spyingAllocator.log() == AllocatorLog{
Allocate(sizeofString("hello!!")),
});
}
SECTION("stores Serialized<char*> by copy") {
@ -98,8 +100,9 @@ TEST_CASE("JsonVariant::set(JsonVariant)") {
var2.set(var1);
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(sizeofString((7))));
REQUIRE(spyingAllocator.log() == AllocatorLog{
Allocate(sizeofString("hello!!")),
});
}
SECTION("stores Serialized<std::string> by copy") {
@ -108,8 +111,9 @@ TEST_CASE("JsonVariant::set(JsonVariant)") {
var2.set(var1);
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(sizeofString((7))));
REQUIRE(spyingAllocator.log() == AllocatorLog{
Allocate(sizeofString("hello!!")),
});
}
SECTION("fails gracefully if raw string allocation fails") {
@ -120,8 +124,9 @@ TEST_CASE("JsonVariant::set(JsonVariant)") {
var2.set(var1);
REQUIRE(doc2.overflowed() == true);
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::AllocateFail(sizeofString((7))));
REQUIRE(spyingAllocator.log() == AllocatorLog{
AllocateFail(sizeofString("hello!!")),
});
}
SECTION("destination is unbound") {

View File

@ -9,7 +9,6 @@
#include "Allocators.hpp"
using ArduinoJson::detail::sizeofArray;
using ArduinoJson::detail::sizeofString;
TEST_CASE("JsonVariant::remove(int)") {
SpyingAllocator spy;
@ -26,19 +25,21 @@ TEST_CASE("JsonVariant::remove(int)") {
spy.clearLog();
var.remove(1);
REQUIRE(var.as<std::string>() == "[\"hello\",\"world\"]");
REQUIRE(spy.log() == AllocatorLog());
REQUIRE(spy.log() == AllocatorLog{});
spy.clearLog();
var.remove(1);
REQUIRE(var.as<std::string>() == "[\"hello\"]");
REQUIRE(spy.log() == AllocatorLog()
<< AllocatorLog::Deallocate(sizeofString(5)));
REQUIRE(spy.log() == AllocatorLog{
Deallocate(sizeofString("world")),
});
spy.clearLog();
var.remove(0);
REQUIRE(var.as<std::string>() == "[]");
REQUIRE(spy.log() == AllocatorLog()
<< AllocatorLog::Deallocate(sizeofString(5)));
REQUIRE(spy.log() == AllocatorLog{
Deallocate(sizeofString("hello")),
});
}
SECTION("release strings in nested array") {
@ -51,8 +52,9 @@ TEST_CASE("JsonVariant::remove(int)") {
var.remove(0);
REQUIRE(var.as<std::string>() == "[]");
REQUIRE(spy.log() == AllocatorLog()
<< AllocatorLog::Deallocate(sizeofString(5)));
REQUIRE(spy.log() == AllocatorLog{
Deallocate(sizeofString("hello")),
});
}
}

View File

@ -8,7 +8,6 @@
#include "Allocators.hpp"
using ArduinoJson::detail::sizeofObject;
using ArduinoJson::detail::sizeofString;
enum ErrorCode { ERROR_01 = 1, ERROR_10 = 10 };
@ -186,32 +185,37 @@ TEST_CASE("JsonVariant::set() releases the previous value") {
SECTION("int") {
v.set(42);
REQUIRE(spy.log() == AllocatorLog()
<< AllocatorLog::Deallocate(sizeofString(5)));
REQUIRE(spy.log() == AllocatorLog{
Deallocate(sizeofString("world")),
});
}
SECTION("bool") {
v.set(false);
REQUIRE(spy.log() == AllocatorLog()
<< AllocatorLog::Deallocate(sizeofString(5)));
REQUIRE(spy.log() == AllocatorLog{
Deallocate(sizeofString("world")),
});
}
SECTION("const char*") {
v.set("hello");
REQUIRE(spy.log() == AllocatorLog()
<< AllocatorLog::Deallocate(sizeofString(5)));
REQUIRE(spy.log() == AllocatorLog{
Deallocate(sizeofString("world")),
});
}
SECTION("float") {
v.set(1.2);
REQUIRE(spy.log() == AllocatorLog()
<< AllocatorLog::Deallocate(sizeofString(5)));
REQUIRE(spy.log() == AllocatorLog{
Deallocate(sizeofString("world")),
});
}
SECTION("Serialized<const char*>") {
v.set(serialized("[]"));
REQUIRE(spy.log() == AllocatorLog()
<< AllocatorLog::Deallocate(sizeofString(5))
<< AllocatorLog::Allocate(sizeofString(2)));
REQUIRE(spy.log() == AllocatorLog{
Deallocate(sizeofString("world")),
Allocate(sizeofString("[]")),
});
}
}