remove.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2019
  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. int 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. }