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

41 lines
872 B
C++
Raw Normal View History

// ArduinoJson - https://arduinojson.org
2023-02-16 11:45:01 +01:00
// Copyright © 2014-2023, 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"
2023-04-17 09:46:41 +02:00
using ArduinoJson::detail::sizeofString;
2019-02-25 11:44:22 +01:00
TEST_CASE("JsonVariant::clear()") {
2023-07-24 17:21:25 +02:00
SpyingAllocator allocator;
JsonDocument doc(&allocator);
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(std::string("hello"));
var.clear();
2023-07-24 17:21:25 +02:00
REQUIRE(allocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(sizeofString(5))
<< AllocatorLog::Deallocate(sizeofString(5)));
2023-04-17 09:46:41 +02:00
}
2019-02-25 11:44:22 +01:00
}