remove.cpp 853 B

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