subscript.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2018
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. TEST_CASE("JsonVariant::operator[]") {
  7. DynamicJsonDocument doc;
  8. JsonVariant var = doc.to<JsonVariant>();
  9. SECTION("The JsonVariant is undefined") {
  10. REQUIRE(0 == var.size());
  11. REQUIRE(var["0"].isNull());
  12. REQUIRE(var[0].isNull());
  13. }
  14. SECTION("The JsonVariant is a string") {
  15. var.set("hello world");
  16. REQUIRE(0 == var.size());
  17. REQUIRE(var["0"].isNull());
  18. REQUIRE(var[0].isNull());
  19. }
  20. SECTION("The JsonVariant is a JsonArray") {
  21. DynamicJsonDocument doc2;
  22. JsonArray array = doc2.to<JsonArray>();
  23. var.set(array);
  24. SECTION("get value") {
  25. array.add("element at index 0");
  26. array.add("element at index 1");
  27. REQUIRE(2 == var.size());
  28. REQUIRE(std::string("element at index 0") == var[0]);
  29. REQUIRE(std::string("element at index 1") == var[1]);
  30. REQUIRE(std::string("element at index 0") ==
  31. var[static_cast<unsigned char>(0)]); // issue #381
  32. REQUIRE(var[666].isNull());
  33. REQUIRE(var[3].isNull());
  34. REQUIRE(var["0"].isNull());
  35. }
  36. SECTION("set value") {
  37. array.add("hello");
  38. var[0] = "world";
  39. REQUIRE(1 == var.size());
  40. REQUIRE(std::string("world") == var[0]);
  41. }
  42. SECTION("set value in a nested object") {
  43. array.createNestedObject();
  44. var[0]["hello"] = "world";
  45. REQUIRE(1 == var.size());
  46. REQUIRE(1 == var[0].size());
  47. REQUIRE(std::string("world") == var[0]["hello"]);
  48. }
  49. }
  50. SECTION("The JsonVariant is a JsonObject") {
  51. DynamicJsonDocument doc2;
  52. JsonObject object = doc2.to<JsonObject>();
  53. var.set(object);
  54. SECTION("get value") {
  55. object["a"] = "element at key \"a\"";
  56. object["b"] = "element at key \"b\"";
  57. REQUIRE(2 == var.size());
  58. REQUIRE(std::string("element at key \"a\"") == var["a"]);
  59. REQUIRE(std::string("element at key \"b\"") == var["b"]);
  60. REQUIRE(var["c"].isNull());
  61. REQUIRE(var[0].isNull());
  62. }
  63. SECTION("set value, key is a const char*") {
  64. var["hello"] = "world";
  65. REQUIRE(1 == var.size());
  66. REQUIRE(std::string("world") == var["hello"]);
  67. }
  68. SECTION("set value, key is a char[]") {
  69. char key[] = "hello";
  70. var[key] = "world";
  71. key[0] = '!'; // make sure the key is duplicated
  72. REQUIRE(1 == var.size());
  73. REQUIRE(std::string("world") == var["hello"]);
  74. }
  75. }
  76. #if defined(HAS_VARIABLE_LENGTH_ARRAY) && \
  77. !defined(SUBSCRIPT_CONFLICTS_WITH_BUILTIN_OPERATOR)
  78. SECTION("key is a VLA") {
  79. int i = 16;
  80. char vla[i];
  81. strcpy(vla, "hello");
  82. deserializeJson(doc, "{\"hello\":\"world\"}");
  83. JsonVariant variant = doc.as<JsonVariant>();
  84. REQUIRE(std::string("world") == variant[vla]);
  85. }
  86. SECTION("key is a VLA, const JsonVariant") {
  87. int i = 16;
  88. char vla[i];
  89. strcpy(vla, "hello");
  90. deserializeJson(doc, "{\"hello\":\"world\"}");
  91. const JsonVariant variant = doc.as<JsonVariant>();
  92. REQUIRE(std::string("world") == variant[vla]);
  93. }
  94. #endif
  95. }