subscript.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2022, Benoit BLANCHON
  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 null") {
  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[1] = "world";
  38. REQUIRE(var.size() == 2);
  39. REQUIRE(std::string("world") == var[1]);
  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. SECTION("variant[0] when variant contains an integer") {
  49. var.set(123);
  50. var[0] = 345; // no-op
  51. REQUIRE(var.is<int>());
  52. REQUIRE(var.as<int>() == 123);
  53. }
  54. }
  55. SECTION("The JsonVariant is a JsonObject") {
  56. JsonObject object = var.to<JsonObject>();
  57. SECTION("get value") {
  58. object["a"] = "element at key \"a\"";
  59. object["b"] = "element at key \"b\"";
  60. REQUIRE(2 == var.size());
  61. REQUIRE(std::string("element at key \"a\"") == var["a"]);
  62. REQUIRE(std::string("element at key \"b\"") == var["b"]);
  63. REQUIRE(var["c"].isNull());
  64. REQUIRE(var[0].isNull());
  65. }
  66. SECTION("set value, key is a const char*") {
  67. var["hello"] = "world";
  68. REQUIRE(1 == var.size());
  69. REQUIRE(std::string("world") == var["hello"]);
  70. }
  71. SECTION("set value, key is a char[]") {
  72. char key[] = "hello";
  73. var[key] = "world";
  74. key[0] = '!'; // make sure the key is duplicated
  75. REQUIRE(1 == var.size());
  76. REQUIRE(std::string("world") == var["hello"]);
  77. }
  78. SECTION("var[key].to<JsonArray>()") {
  79. JsonArray arr = var["hello"].to<JsonArray>();
  80. REQUIRE(arr.isNull() == false);
  81. }
  82. }
  83. #if defined(HAS_VARIABLE_LENGTH_ARRAY) && \
  84. !defined(SUBSCRIPT_CONFLICTS_WITH_BUILTIN_OPERATOR)
  85. SECTION("key is a VLA") {
  86. size_t i = 16;
  87. char vla[i];
  88. strcpy(vla, "hello");
  89. deserializeJson(doc, "{\"hello\":\"world\"}");
  90. JsonVariant variant = doc.as<JsonVariant>();
  91. REQUIRE(std::string("world") == variant[vla]);
  92. }
  93. SECTION("key is a VLA, const JsonVariant") {
  94. size_t i = 16;
  95. char vla[i];
  96. strcpy(vla, "hello");
  97. deserializeJson(doc, "{\"hello\":\"world\"}");
  98. const JsonVariant variant = doc.as<JsonVariant>();
  99. REQUIRE(std::string("world") == variant[vla]);
  100. }
  101. #endif
  102. SECTION("get value from linked object") {
  103. StaticJsonDocument<1024> doc2;
  104. doc2["hello"] = "world";
  105. var.link(doc2);
  106. CHECK(var["hello"].as<std::string>() == "world");
  107. }
  108. SECTION("set value to linked object") {
  109. StaticJsonDocument<1024> doc2;
  110. doc2["hello"] = "world";
  111. var.link(doc2);
  112. var["tutu"] = "toto"; // no-op
  113. CHECK(doc.as<std::string>() == "{\"hello\":\"world\"}");
  114. CHECK(doc2.as<std::string>() == "{\"hello\":\"world\"}");
  115. }
  116. SECTION("get value from linked array") {
  117. StaticJsonDocument<1024> doc2;
  118. doc2.add(42);
  119. var.link(doc2);
  120. CHECK(var[0].as<int>() == 42);
  121. }
  122. SECTION("set value to linked array") {
  123. StaticJsonDocument<1024> doc2;
  124. doc2.add(42);
  125. var.link(doc2);
  126. var[0] = 666; // no-op
  127. CHECK(doc.as<std::string>() == "[42]");
  128. CHECK(doc2.as<std::string>() == "[42]");
  129. }
  130. }
  131. TEST_CASE("JsonVariantConst::operator[]") {
  132. DynamicJsonDocument doc(4096);
  133. JsonVariant var = doc.to<JsonVariant>();
  134. JsonVariantConst cvar = var;
  135. SECTION("The JsonVariant is null") {
  136. REQUIRE(0 == cvar.size());
  137. REQUIRE(cvar["0"].isNull());
  138. REQUIRE(cvar[0].isNull());
  139. }
  140. SECTION("The JsonVariant is a string") {
  141. var.set("hello world");
  142. REQUIRE(0 == cvar.size());
  143. REQUIRE(cvar["0"].isNull());
  144. REQUIRE(cvar[0].isNull());
  145. }
  146. SECTION("The JsonVariant is a JsonArray") {
  147. JsonArray array = var.to<JsonArray>();
  148. SECTION("get value") {
  149. array.add("element at index 0");
  150. array.add("element at index 1");
  151. REQUIRE(2 == cvar.size());
  152. REQUIRE(std::string("element at index 0") == cvar[0]);
  153. REQUIRE(std::string("element at index 1") == cvar[1]);
  154. REQUIRE(std::string("element at index 0") ==
  155. var[static_cast<unsigned char>(0)]); // issue #381
  156. REQUIRE(cvar[666].isNull());
  157. REQUIRE(cvar[3].isNull());
  158. REQUIRE(cvar["0"].isNull());
  159. }
  160. }
  161. SECTION("The JsonVariant is a JsonObject") {
  162. JsonObject object = var.to<JsonObject>();
  163. SECTION("get value") {
  164. object["a"] = "element at key \"a\"";
  165. object["b"] = "element at key \"b\"";
  166. REQUIRE(2 == cvar.size());
  167. REQUIRE(std::string("element at key \"a\"") == cvar["a"]);
  168. REQUIRE(std::string("element at key \"b\"") == cvar["b"]);
  169. REQUIRE(cvar["c"].isNull());
  170. REQUIRE(cvar[0].isNull());
  171. }
  172. }
  173. SECTION("Auto promote null JsonVariant to JsonObject") {
  174. var["hello"] = "world";
  175. REQUIRE(var.is<JsonObject>() == true);
  176. }
  177. SECTION("Don't auto promote non-null JsonVariant to JsonObject") {
  178. var.set(42);
  179. var["hello"] = "world";
  180. REQUIRE(var.is<JsonObject>() == false);
  181. }
  182. SECTION("Don't auto promote null JsonVariant to JsonObject when reading") {
  183. const char* value = var["hello"];
  184. REQUIRE(var.is<JsonObject>() == false);
  185. REQUIRE(value == 0);
  186. }
  187. SECTION("get value from linked object") {
  188. StaticJsonDocument<1024> doc2;
  189. doc2["hello"] = "world";
  190. var.link(doc2);
  191. CHECK(cvar["hello"].as<std::string>() == "world");
  192. }
  193. SECTION("get value from linked array") {
  194. StaticJsonDocument<1024> doc2;
  195. doc2.add(42);
  196. var.link(doc2);
  197. CHECK(cvar[0].as<int>() == 42);
  198. }
  199. }