subscript.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2019
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. TEST_CASE("JsonVariant::operator[]") {
  7. DynamicJsonDocument doc(4096);
  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. var[0].as<std::string>();
  27. // REQUIRE(std::string("element at index 0") == );
  28. REQUIRE(std::string("element at index 1") == var[1]);
  29. REQUIRE(std::string("element at index 0") ==
  30. var[static_cast<unsigned char>(0)]); // issue #381
  31. REQUIRE(var[666].isNull());
  32. REQUIRE(var[3].isNull());
  33. REQUIRE(var["0"].isNull());
  34. }
  35. SECTION("set value") {
  36. array.add("hello");
  37. var[0] = "world";
  38. REQUIRE(1 == var.size());
  39. REQUIRE(std::string("world") == var[0]);
  40. }
  41. SECTION("set value in a nested object") {
  42. array.createNestedObject();
  43. var[0]["hello"] = "world";
  44. REQUIRE(1 == var.size());
  45. REQUIRE(1 == var[0].size());
  46. REQUIRE(std::string("world") == var[0]["hello"]);
  47. }
  48. }
  49. SECTION("The JsonVariant is a JsonObject") {
  50. JsonObject object = var.to<JsonObject>();
  51. SECTION("get value") {
  52. object["a"] = "element at key \"a\"";
  53. object["b"] = "element at key \"b\"";
  54. REQUIRE(2 == var.size());
  55. REQUIRE(std::string("element at key \"a\"") == var["a"]);
  56. REQUIRE(std::string("element at key \"b\"") == var["b"]);
  57. REQUIRE(var["c"].isNull());
  58. REQUIRE(var[0].isNull());
  59. }
  60. SECTION("set value, key is a const char*") {
  61. var["hello"] = "world";
  62. REQUIRE(1 == var.size());
  63. REQUIRE(std::string("world") == var["hello"]);
  64. }
  65. SECTION("set value, key is a char[]") {
  66. char key[] = "hello";
  67. var[key] = "world";
  68. key[0] = '!'; // make sure the key is duplicated
  69. REQUIRE(1 == var.size());
  70. REQUIRE(std::string("world") == var["hello"]);
  71. }
  72. SECTION("var[key].to<JsonArray>()") {
  73. JsonArray arr = var["hello"].to<JsonArray>();
  74. REQUIRE(arr.isNull() == false);
  75. }
  76. }
  77. #if defined(HAS_VARIABLE_LENGTH_ARRAY) && \
  78. !defined(SUBSCRIPT_CONFLICTS_WITH_BUILTIN_OPERATOR)
  79. SECTION("key is a VLA") {
  80. int i = 16;
  81. char vla[i];
  82. strcpy(vla, "hello");
  83. deserializeJson(doc, "{\"hello\":\"world\"}");
  84. JsonVariant variant = doc.as<JsonVariant>();
  85. REQUIRE(std::string("world") == variant[vla]);
  86. }
  87. SECTION("key is a VLA, const JsonVariant") {
  88. int i = 16;
  89. char vla[i];
  90. strcpy(vla, "hello");
  91. deserializeJson(doc, "{\"hello\":\"world\"}");
  92. const JsonVariant variant = doc.as<JsonVariant>();
  93. REQUIRE(std::string("world") == variant[vla]);
  94. }
  95. #endif
  96. }
  97. TEST_CASE("JsonVariantConst::operator[]") {
  98. DynamicJsonDocument doc(4096);
  99. JsonVariant var = doc.to<JsonVariant>();
  100. JsonVariantConst cvar = var;
  101. SECTION("The JsonVariant is undefined") {
  102. REQUIRE(0 == cvar.size());
  103. REQUIRE(cvar["0"].isNull());
  104. REQUIRE(cvar[0].isNull());
  105. }
  106. SECTION("The JsonVariant is a string") {
  107. var.set("hello world");
  108. REQUIRE(0 == cvar.size());
  109. REQUIRE(cvar["0"].isNull());
  110. REQUIRE(cvar[0].isNull());
  111. }
  112. SECTION("The JsonVariant is a JsonArray") {
  113. JsonArray array = var.to<JsonArray>();
  114. SECTION("get value") {
  115. array.add("element at index 0");
  116. array.add("element at index 1");
  117. REQUIRE(2 == cvar.size());
  118. REQUIRE(std::string("element at index 0") == cvar[0]);
  119. REQUIRE(std::string("element at index 1") == cvar[1]);
  120. REQUIRE(std::string("element at index 0") ==
  121. var[static_cast<unsigned char>(0)]); // issue #381
  122. REQUIRE(cvar[666].isNull());
  123. REQUIRE(cvar[3].isNull());
  124. REQUIRE(cvar["0"].isNull());
  125. }
  126. }
  127. SECTION("The JsonVariant is a JsonObject") {
  128. JsonObject object = var.to<JsonObject>();
  129. SECTION("get value") {
  130. object["a"] = "element at key \"a\"";
  131. object["b"] = "element at key \"b\"";
  132. REQUIRE(2 == cvar.size());
  133. REQUIRE(std::string("element at key \"a\"") == cvar["a"]);
  134. REQUIRE(std::string("element at key \"b\"") == cvar["b"]);
  135. REQUIRE(cvar["c"].isNull());
  136. REQUIRE(cvar[0].isNull());
  137. }
  138. }
  139. SECTION("Auto promote null JsonVariant to JsonObject") {
  140. var["hello"] = "world";
  141. REQUIRE(var.is<JsonObject>() == true);
  142. }
  143. SECTION("Don't auto promote non-null JsonVariant to JsonObject") {
  144. var.set(42);
  145. var["hello"] = "world";
  146. REQUIRE(var.is<JsonObject>() == false);
  147. }
  148. SECTION("Don't auto promote null JsonVariant to JsonObject when reading") {
  149. const char* value = var["hello"];
  150. REQUIRE(var.is<JsonObject>() == false);
  151. REQUIRE(value == 0);
  152. }
  153. }