std_string.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2024, Benoit BLANCHON
  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)
  9. *p++ = '*';
  10. }
  11. TEST_CASE("std::string") {
  12. JsonDocument doc;
  13. SECTION("operator[]") {
  14. char json[] = "{\"key\":\"value\"}";
  15. deserializeJson(doc, json);
  16. JsonObject obj = doc.as<JsonObject>();
  17. REQUIRE(std::string("value") == obj[std::string("key")]);
  18. }
  19. SECTION("operator[] const") {
  20. char json[] = "{\"key\":\"value\"}";
  21. deserializeJson(doc, json);
  22. JsonObject obj = doc.as<JsonObject>();
  23. REQUIRE(std::string("value") == obj[std::string("key")]);
  24. }
  25. SECTION("containsKey()") {
  26. char json[] = "{\"key\":\"value\"}";
  27. deserializeJson(doc, json);
  28. JsonObject obj = doc.as<JsonObject>();
  29. REQUIRE(true == obj.containsKey(std::string("key")));
  30. }
  31. SECTION("remove()") {
  32. JsonObject obj = doc.to<JsonObject>();
  33. obj["key"] = "value";
  34. obj.remove(std::string("key"));
  35. REQUIRE(0 == obj.size());
  36. }
  37. SECTION("operator[], set key") {
  38. std::string key("hello");
  39. JsonObject obj = doc.to<JsonObject>();
  40. obj[key] = "world";
  41. eraseString(key);
  42. REQUIRE(std::string("world") == obj["hello"]);
  43. }
  44. SECTION("operator[], set value") {
  45. std::string value("world");
  46. JsonObject obj = doc.to<JsonObject>();
  47. obj["hello"] = value;
  48. eraseString(value);
  49. REQUIRE(std::string("world") == obj["hello"]);
  50. }
  51. }