clear.cpp 837 B

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