Files
ArduinoJson/extras/tests/JsonVariant/clear.cpp

41 lines
850 B
C++
Raw Normal View History

// ArduinoJson - https://arduinojson.org
2024-01-03 08:47:06 +01:00
// Copyright © 2014-2024, Benoit BLANCHON
2019-02-25 11:44:22 +01:00
// MIT License
#include <ArduinoJson.h>
#include <stdint.h>
#include <catch.hpp>
2023-07-24 17:21:25 +02:00
#include "Allocators.hpp"
#include "Literals.hpp"
2023-07-24 17:21:25 +02:00
2019-02-25 11:44:22 +01:00
TEST_CASE("JsonVariant::clear()") {
SpyingAllocator spy;
JsonDocument doc(&spy);
2019-02-25 11:44:22 +01:00
JsonVariant var = doc.to<JsonVariant>();
SECTION("size goes back to zero") {
var.add(42);
var.clear();
REQUIRE(var.size() == 0);
}
SECTION("isNull() return true") {
var.add("hello");
var.clear();
REQUIRE(var.isNull() == true);
}
2023-04-17 09:46:41 +02:00
SECTION("releases owned string") {
var.set("hello"_s);
2023-04-17 09:46:41 +02:00
var.clear();
2023-07-24 17:21:25 +02:00
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofString("hello")),
Deallocate(sizeofString("hello")),
});
2023-04-17 09:46:41 +02:00
}
2019-02-25 11:44:22 +01:00
}