remove.cpp 982 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Copyright Benoit Blanchon 2014-2017
  2. // MIT License
  3. //
  4. // Arduino JSON library
  5. // https://bblanchon.github.io/ArduinoJson/
  6. // If you like this project, please add a star!
  7. #include <ArduinoJson.h>
  8. #include <catch.hpp>
  9. #include <string>
  10. TEST_CASE("JsonObject::remove()") {
  11. DynamicJsonBuffer jb;
  12. SECTION("SizeDecreased_WhenValuesAreRemoved") {
  13. JsonObject& obj = jb.createObject();
  14. obj["hello"] = 1;
  15. obj.remove("hello");
  16. REQUIRE(0 == obj.size());
  17. }
  18. SECTION("SizeUntouched_WhenRemoveIsCalledWithAWrongKey") {
  19. JsonObject& obj = jb.createObject();
  20. obj["hello"] = 1;
  21. obj.remove("world");
  22. REQUIRE(1 == obj.size());
  23. }
  24. SECTION("RemoveByIterator") {
  25. JsonObject& obj = jb.parseObject("{\"a\":0,\"b\":1,\"c\":2}");
  26. for (JsonObject::iterator it = obj.begin(); it != obj.end(); ++it) {
  27. if (it->value == 1) obj.remove(it);
  28. }
  29. std::string result;
  30. obj.printTo(result);
  31. REQUIRE("{\"a\":0,\"c\":2}" == result);
  32. }
  33. }