remove.cpp 896 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2023
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. #include <string>
  7. TEST_CASE("JsonObject::remove()") {
  8. DynamicJsonBuffer jb;
  9. SECTION("SizeDecreased_WhenValuesAreRemoved") {
  10. JsonObject& obj = jb.createObject();
  11. obj["hello"] = 1;
  12. obj.remove("hello");
  13. REQUIRE(0 == obj.size());
  14. }
  15. SECTION("SizeUntouched_WhenRemoveIsCalledWithAWrongKey") {
  16. JsonObject& obj = jb.createObject();
  17. obj["hello"] = 1;
  18. obj.remove("world");
  19. REQUIRE(1 == obj.size());
  20. }
  21. SECTION("RemoveByIterator") {
  22. JsonObject& obj = jb.parseObject("{\"a\":0,\"b\":1,\"c\":2}");
  23. for (JsonObject::iterator it = obj.begin(); it != obj.end(); ++it) {
  24. if (it->value == 1) obj.remove(it);
  25. }
  26. std::string result;
  27. obj.printTo(result);
  28. REQUIRE("{\"a\":0,\"c\":2}" == result);
  29. }
  30. }