subscript.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. }
  103. TEST_CASE("JsonVariantConst::operator[]") {
  104. DynamicJsonDocument doc(4096);
  105. JsonVariant var = doc.to<JsonVariant>();
  106. JsonVariantConst cvar = var;
  107. SECTION("The JsonVariant is null") {
  108. REQUIRE(0 == cvar.size());
  109. REQUIRE(cvar["0"].isNull());
  110. REQUIRE(cvar[0].isNull());
  111. }
  112. SECTION("The JsonVariant is a string") {
  113. var.set("hello world");
  114. REQUIRE(0 == cvar.size());
  115. REQUIRE(cvar["0"].isNull());
  116. REQUIRE(cvar[0].isNull());
  117. }
  118. SECTION("The JsonVariant is a JsonArray") {
  119. JsonArray array = var.to<JsonArray>();
  120. SECTION("get value") {
  121. array.add("element at index 0");
  122. array.add("element at index 1");
  123. REQUIRE(2 == cvar.size());
  124. REQUIRE(std::string("element at index 0") == cvar[0]);
  125. REQUIRE(std::string("element at index 1") == cvar[1]);
  126. REQUIRE(std::string("element at index 0") ==
  127. var[static_cast<unsigned char>(0)]); // issue #381
  128. REQUIRE(cvar[666].isNull());
  129. REQUIRE(cvar[3].isNull());
  130. REQUIRE(cvar["0"].isNull());
  131. }
  132. }
  133. SECTION("The JsonVariant is a JsonObject") {
  134. JsonObject object = var.to<JsonObject>();
  135. SECTION("get value") {
  136. object["a"] = "element at key \"a\"";
  137. object["b"] = "element at key \"b\"";
  138. REQUIRE(2 == cvar.size());
  139. REQUIRE(std::string("element at key \"a\"") == cvar["a"]);
  140. REQUIRE(std::string("element at key \"b\"") == cvar["b"]);
  141. REQUIRE(cvar["c"].isNull());
  142. REQUIRE(cvar[0].isNull());
  143. }
  144. }
  145. SECTION("Auto promote null JsonVariant to JsonObject") {
  146. var["hello"] = "world";
  147. REQUIRE(var.is<JsonObject>() == true);
  148. }
  149. SECTION("Don't auto promote non-null JsonVariant to JsonObject") {
  150. var.set(42);
  151. var["hello"] = "world";
  152. REQUIRE(var.is<JsonObject>() == false);
  153. }
  154. SECTION("Don't auto promote null JsonVariant to JsonObject when reading") {
  155. const char* value = var["hello"];
  156. REQUIRE(var.is<JsonObject>() == false);
  157. REQUIRE(value == 0);
  158. }
  159. }