clear.cpp 850 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2025, Benoit BLANCHON
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <stdint.h>
  6. #include <catch.hpp>
  7. #include "Allocators.hpp"
  8. #include "Literals.hpp"
  9. TEST_CASE("JsonVariant::clear()") {
  10. SpyingAllocator spy;
  11. JsonDocument doc(&spy);
  12. JsonVariant var = doc.to<JsonVariant>();
  13. SECTION("size goes back to zero") {
  14. var.add(42);
  15. var.clear();
  16. REQUIRE(var.size() == 0);
  17. }
  18. SECTION("isNull() return true") {
  19. var.add("hello");
  20. var.clear();
  21. REQUIRE(var.isNull() == true);
  22. }
  23. SECTION("releases owned string") {
  24. var.set("hello"_s);
  25. var.clear();
  26. REQUIRE(spy.log() == AllocatorLog{
  27. Allocate(sizeofString("hello")),
  28. Deallocate(sizeofString("hello")),
  29. });
  30. }
  31. }