remove.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2022, Benoit BLANCHON
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. #include <string>
  7. TEST_CASE("JsonObject::remove()") {
  8. DynamicJsonDocument doc(4096);
  9. JsonObject obj = doc.to<JsonObject>();
  10. obj["a"] = 0;
  11. obj["b"] = 1;
  12. obj["c"] = 2;
  13. std::string result;
  14. SECTION("remove(key)") {
  15. SECTION("Remove first") {
  16. obj.remove("a");
  17. serializeJson(obj, result);
  18. REQUIRE("{\"b\":1,\"c\":2}" == result);
  19. }
  20. SECTION("Remove middle") {
  21. obj.remove("b");
  22. serializeJson(obj, result);
  23. REQUIRE("{\"a\":0,\"c\":2}" == result);
  24. }
  25. SECTION("Remove last") {
  26. obj.remove("c");
  27. serializeJson(obj, result);
  28. REQUIRE("{\"a\":0,\"b\":1}" == result);
  29. }
  30. }
  31. SECTION("remove(iterator)") {
  32. JsonObject::iterator it = obj.begin();
  33. SECTION("Remove first") {
  34. obj.remove(it);
  35. serializeJson(obj, result);
  36. REQUIRE("{\"b\":1,\"c\":2}" == result);
  37. }
  38. SECTION("Remove middle") {
  39. ++it;
  40. obj.remove(it);
  41. serializeJson(obj, result);
  42. REQUIRE("{\"a\":0,\"c\":2}" == result);
  43. }
  44. SECTION("Remove last") {
  45. it += 2;
  46. obj.remove(it);
  47. serializeJson(obj, result);
  48. REQUIRE("{\"a\":0,\"b\":1}" == result);
  49. }
  50. }
  51. #ifdef HAS_VARIABLE_LENGTH_ARRAY
  52. SECTION("key is a vla") {
  53. size_t i = 16;
  54. char vla[i];
  55. strcpy(vla, "b");
  56. obj.remove(vla);
  57. serializeJson(obj, result);
  58. REQUIRE("{\"a\":0,\"c\":2}" == result);
  59. }
  60. #endif
  61. SECTION("remove by key on unbound reference") {
  62. JsonObject unboundObject;
  63. unboundObject.remove("key");
  64. }
  65. SECTION("remove by iterator on unbound reference") {
  66. JsonObject unboundObject;
  67. unboundObject.remove(unboundObject.begin());
  68. }
  69. }