subscript.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. JsonArray array = var.to<JsonArray>();
  22. SECTION("get value") {
  23. array.add("element at index 0");
  24. array.add("element at index 1");
  25. REQUIRE(2 == var.size());
  26. REQUIRE(std::string("element at index 0") == var[0]);
  27. REQUIRE(std::string("element at index 1") == var[1]);
  28. REQUIRE(std::string("element at index 0") ==
  29. var[static_cast<unsigned char>(0)]); // issue #381
  30. REQUIRE(var[666].isNull());
  31. REQUIRE(var[3].isNull());
  32. REQUIRE(var["0"].isNull());
  33. }
  34. SECTION("set value") {
  35. array.add("hello");
  36. var[0] = "world";
  37. REQUIRE(1 == var.size());
  38. REQUIRE(std::string("world") == var[0]);
  39. }
  40. SECTION("set value in a nested object") {
  41. array.createNestedObject();
  42. var[0]["hello"] = "world";
  43. REQUIRE(1 == var.size());
  44. REQUIRE(1 == var[0].size());
  45. REQUIRE(std::string("world") == var[0]["hello"]);
  46. }
  47. }
  48. SECTION("The JsonVariant is a JsonObject") {
  49. JsonObject object = var.to<JsonObject>();
  50. SECTION("get value") {
  51. object["a"] = "element at key \"a\"";
  52. object["b"] = "element at key \"b\"";
  53. REQUIRE(2 == var.size());
  54. REQUIRE(std::string("element at key \"a\"") == var["a"]);
  55. REQUIRE(std::string("element at key \"b\"") == var["b"]);
  56. REQUIRE(var["c"].isNull());
  57. REQUIRE(var[0].isNull());
  58. }
  59. SECTION("set value, key is a const char*") {
  60. var["hello"] = "world";
  61. REQUIRE(1 == var.size());
  62. REQUIRE(std::string("world") == var["hello"]);
  63. }
  64. SECTION("set value, key is a char[]") {
  65. char key[] = "hello";
  66. var[key] = "world";
  67. key[0] = '!'; // make sure the key is duplicated
  68. REQUIRE(1 == var.size());
  69. REQUIRE(std::string("world") == var["hello"]);
  70. }
  71. SECTION("var[key].to<JsonArray>()") {
  72. JsonArray arr = var["hello"].to<JsonArray>();
  73. REQUIRE(arr.isNull() == false);
  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. }
  96. TEST_CASE("JsonVariantConst::operator[]") {
  97. DynamicJsonDocument doc;
  98. JsonVariant var = doc.to<JsonVariant>();
  99. JsonVariantConst cvar = var;
  100. SECTION("The JsonVariant is undefined") {
  101. REQUIRE(0 == cvar.size());
  102. REQUIRE(cvar["0"].isNull());
  103. REQUIRE(cvar[0].isNull());
  104. }
  105. SECTION("The JsonVariant is a string") {
  106. var.set("hello world");
  107. REQUIRE(0 == cvar.size());
  108. REQUIRE(cvar["0"].isNull());
  109. REQUIRE(cvar[0].isNull());
  110. }
  111. SECTION("The JsonVariant is a JsonArray") {
  112. JsonArray array = var.to<JsonArray>();
  113. SECTION("get value") {
  114. array.add("element at index 0");
  115. array.add("element at index 1");
  116. REQUIRE(2 == cvar.size());
  117. REQUIRE(std::string("element at index 0") == cvar[0]);
  118. REQUIRE(std::string("element at index 1") == cvar[1]);
  119. REQUIRE(std::string("element at index 0") ==
  120. var[static_cast<unsigned char>(0)]); // issue #381
  121. REQUIRE(cvar[666].isNull());
  122. REQUIRE(cvar[3].isNull());
  123. REQUIRE(cvar["0"].isNull());
  124. }
  125. }
  126. SECTION("The JsonVariant is a JsonObject") {
  127. JsonObject object = var.to<JsonObject>();
  128. SECTION("get value") {
  129. object["a"] = "element at key \"a\"";
  130. object["b"] = "element at key \"b\"";
  131. REQUIRE(2 == cvar.size());
  132. REQUIRE(std::string("element at key \"a\"") == cvar["a"]);
  133. REQUIRE(std::string("element at key \"b\"") == cvar["b"]);
  134. REQUIRE(cvar["c"].isNull());
  135. REQUIRE(cvar[0].isNull());
  136. }
  137. }
  138. }