containsKey.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2025, Benoit BLANCHON
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. #include "Literals.hpp"
  7. TEST_CASE("JsonDocument::containsKey()") {
  8. JsonDocument doc;
  9. SECTION("returns true on object") {
  10. doc["hello"] = "world";
  11. REQUIRE(doc.containsKey("hello") == true);
  12. }
  13. SECTION("returns true when value is null") {
  14. doc["hello"] = static_cast<const char*>(0);
  15. REQUIRE(doc.containsKey("hello") == true);
  16. }
  17. SECTION("returns true when key is a std::string") {
  18. doc["hello"] = "world";
  19. REQUIRE(doc.containsKey("hello"_s) == true);
  20. }
  21. SECTION("returns false on object") {
  22. doc["world"] = "hello";
  23. REQUIRE(doc.containsKey("hello") == false);
  24. }
  25. SECTION("returns false on array") {
  26. doc.add("hello");
  27. REQUIRE(doc.containsKey("hello") == false);
  28. }
  29. SECTION("returns false on null") {
  30. REQUIRE(doc.containsKey("hello") == false);
  31. }
  32. SECTION("supports JsonVariant") {
  33. doc["hello"] = "world";
  34. doc["key"] = "hello";
  35. REQUIRE(doc.containsKey(doc["key"]) == true);
  36. REQUIRE(doc.containsKey(doc["foo"]) == false);
  37. }
  38. #ifdef HAS_VARIABLE_LENGTH_ARRAY
  39. SECTION("supports VLAs") {
  40. size_t i = 16;
  41. char vla[i];
  42. strcpy(vla, "hello");
  43. doc["hello"] = "world";
  44. REQUIRE(doc.containsKey(vla) == true);
  45. }
  46. #endif
  47. }
  48. TEST_CASE("MemberProxy::containsKey()") {
  49. JsonDocument doc;
  50. const auto& mp = doc["hello"];
  51. SECTION("containsKey(const char*)") {
  52. mp["key"] = "value";
  53. REQUIRE(mp.containsKey("key") == true);
  54. REQUIRE(mp.containsKey("key") == true);
  55. }
  56. SECTION("containsKey(std::string)") {
  57. mp["key"] = "value";
  58. REQUIRE(mp.containsKey("key"_s) == true);
  59. REQUIRE(mp.containsKey("key"_s) == true);
  60. }
  61. #ifdef HAS_VARIABLE_LENGTH_ARRAY
  62. SECTION("supports VLAs") {
  63. size_t i = 16;
  64. char vla[i];
  65. strcpy(vla, "hello");
  66. mp["hello"] = "world";
  67. REQUIRE(mp.containsKey(vla) == true);
  68. }
  69. #endif
  70. }
  71. TEST_CASE("JsonObject::containsKey()") {
  72. JsonDocument doc;
  73. JsonObject obj = doc.to<JsonObject>();
  74. obj["hello"] = 42;
  75. SECTION("returns true only if key is present") {
  76. REQUIRE(false == obj.containsKey("world"));
  77. REQUIRE(true == obj.containsKey("hello"));
  78. }
  79. SECTION("returns false after remove()") {
  80. obj.remove("hello");
  81. REQUIRE(false == obj.containsKey("hello"));
  82. }
  83. #ifdef HAS_VARIABLE_LENGTH_ARRAY
  84. SECTION("key is a VLA") {
  85. size_t i = 16;
  86. char vla[i];
  87. strcpy(vla, "hello");
  88. REQUIRE(true == obj.containsKey(vla));
  89. }
  90. #endif
  91. SECTION("key is a JsonVariant") {
  92. doc["key"] = "hello";
  93. REQUIRE(true == obj.containsKey(obj["key"]));
  94. REQUIRE(false == obj.containsKey(obj["hello"]));
  95. }
  96. SECTION("std::string") {
  97. REQUIRE(true == obj.containsKey("hello"_s));
  98. }
  99. SECTION("unsigned char[]") {
  100. unsigned char key[] = "hello";
  101. REQUIRE(true == obj.containsKey(key));
  102. }
  103. }
  104. TEST_CASE("JsonObjectConst::containsKey()") {
  105. JsonDocument doc;
  106. doc["hello"] = 42;
  107. auto obj = doc.as<JsonObjectConst>();
  108. SECTION("supports const char*") {
  109. REQUIRE(false == obj.containsKey("world"));
  110. REQUIRE(true == obj.containsKey("hello"));
  111. }
  112. SECTION("supports std::string") {
  113. REQUIRE(false == obj.containsKey("world"_s));
  114. REQUIRE(true == obj.containsKey("hello"_s));
  115. }
  116. #ifdef HAS_VARIABLE_LENGTH_ARRAY
  117. SECTION("supports VLA") {
  118. size_t i = 16;
  119. char vla[i];
  120. strcpy(vla, "hello");
  121. REQUIRE(true == obj.containsKey(vla));
  122. }
  123. #endif
  124. SECTION("supports JsonVariant") {
  125. doc["key"] = "hello";
  126. REQUIRE(true == obj.containsKey(obj["key"]));
  127. REQUIRE(false == obj.containsKey(obj["hello"]));
  128. }
  129. }
  130. TEST_CASE("JsonVariant::containsKey()") {
  131. JsonDocument doc;
  132. JsonVariant var = doc.to<JsonVariant>();
  133. SECTION("returns false is unbound") {
  134. CHECK_FALSE(JsonVariant().containsKey("hello"));
  135. }
  136. SECTION("containsKey(const char*)") {
  137. var["hello"] = "world";
  138. REQUIRE(var.containsKey("hello") == true);
  139. REQUIRE(var.containsKey("world") == false);
  140. }
  141. SECTION("containsKey(std::string)") {
  142. var["hello"] = "world";
  143. REQUIRE(var.containsKey("hello"_s) == true);
  144. REQUIRE(var.containsKey("world"_s) == false);
  145. }
  146. SECTION("containsKey(JsonVariant)") {
  147. var["hello"] = "world";
  148. var["key"] = "hello";
  149. REQUIRE(var.containsKey(doc["key"]) == true);
  150. REQUIRE(var.containsKey(doc["foo"]) == false);
  151. }
  152. #ifdef HAS_VARIABLE_LENGTH_ARRAY
  153. SECTION("supports VLAs") {
  154. size_t i = 16;
  155. char vla[i];
  156. strcpy(vla, "hello");
  157. var["hello"] = "world";
  158. REQUIRE(var.containsKey(vla) == true);
  159. }
  160. #endif
  161. }
  162. TEST_CASE("JsonVariantConst::containsKey()") {
  163. JsonDocument doc;
  164. doc["hello"] = "world";
  165. JsonVariantConst var = doc.as<JsonVariant>();
  166. SECTION("support const char*") {
  167. REQUIRE(var.containsKey("hello") == true);
  168. REQUIRE(var.containsKey("world") == false);
  169. }
  170. SECTION("support std::string") {
  171. REQUIRE(var.containsKey("hello"_s) == true);
  172. REQUIRE(var.containsKey("world"_s) == false);
  173. }
  174. #ifdef HAS_VARIABLE_LENGTH_ARRAY
  175. SECTION("supports VLA") {
  176. size_t i = 16;
  177. char vla[i];
  178. strcpy(vla, "hello");
  179. REQUIRE(true == var.containsKey(vla));
  180. }
  181. #endif
  182. SECTION("support JsonVariant") {
  183. doc["key"] = "hello";
  184. REQUIRE(var.containsKey(var["key"]) == true);
  185. REQUIRE(var.containsKey(var["foo"]) == false);
  186. }
  187. }