std_string.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2019
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. static void eraseString(std::string &str) {
  7. char *p = const_cast<char *>(str.c_str());
  8. while (*p) *p++ = '*';
  9. }
  10. TEST_CASE("std::string") {
  11. DynamicJsonDocument doc(4096);
  12. SECTION("operator[]") {
  13. char json[] = "{\"key\":\"value\"}";
  14. deserializeJson(doc, json);
  15. JsonObject obj = doc.as<JsonObject>();
  16. REQUIRE(std::string("value") == obj[std::string("key")]);
  17. }
  18. SECTION("operator[] const") {
  19. char json[] = "{\"key\":\"value\"}";
  20. deserializeJson(doc, json);
  21. JsonObject obj = doc.as<JsonObject>();
  22. REQUIRE(std::string("value") == obj[std::string("key")]);
  23. }
  24. SECTION("createNestedObject()") {
  25. JsonObject obj = doc.to<JsonObject>();
  26. std::string key = "key";
  27. char json[64];
  28. obj.createNestedObject(key);
  29. eraseString(key);
  30. serializeJson(doc, json, sizeof(json));
  31. REQUIRE(std::string("{\"key\":{}}") == json);
  32. }
  33. SECTION("createNestedArray()") {
  34. JsonObject obj = doc.to<JsonObject>();
  35. std::string key = "key";
  36. char json[64];
  37. obj.createNestedArray(key);
  38. eraseString(key);
  39. serializeJson(doc, json, sizeof(json));
  40. REQUIRE(std::string("{\"key\":[]}") == json);
  41. }
  42. SECTION("containsKey()") {
  43. char json[] = "{\"key\":\"value\"}";
  44. deserializeJson(doc, json);
  45. JsonObject obj = doc.as<JsonObject>();
  46. REQUIRE(true == obj.containsKey(std::string("key")));
  47. }
  48. SECTION("remove()") {
  49. JsonObject obj = doc.to<JsonObject>();
  50. obj["key"] = "value";
  51. obj.remove(std::string("key"));
  52. REQUIRE(0 == obj.size());
  53. }
  54. SECTION("operator[], set key") {
  55. std::string key("hello");
  56. JsonObject obj = doc.to<JsonObject>();
  57. obj[key] = "world";
  58. eraseString(key);
  59. REQUIRE(std::string("world") == obj["hello"]);
  60. }
  61. SECTION("operator[], set value") {
  62. std::string value("world");
  63. JsonObject obj = doc.to<JsonObject>();
  64. obj["hello"] = value;
  65. eraseString(value);
  66. REQUIRE(std::string("world") == obj["hello"]);
  67. }
  68. SECTION("memoryUsage() increases when adding a new key") {
  69. std::string key1("hello"), key2("world");
  70. JsonObject obj = doc.to<JsonObject>();
  71. obj[key1] = 1;
  72. size_t sizeBefore = doc.memoryUsage();
  73. obj[key2] = 2;
  74. size_t sizeAfter = doc.memoryUsage();
  75. REQUIRE(sizeAfter - sizeBefore >= key2.size());
  76. }
  77. SECTION("memoryUsage() remains when adding the same key") {
  78. std::string key("hello");
  79. JsonObject obj = doc.to<JsonObject>();
  80. obj[key] = 1;
  81. size_t sizeBefore = doc.memoryUsage();
  82. obj[key] = 2;
  83. size_t sizeAfter = doc.memoryUsage();
  84. REQUIRE(sizeBefore == sizeAfter);
  85. }
  86. }