containsKey.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2024, 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("support 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. }
  39. TEST_CASE("MemberProxy::containsKey()") {
  40. JsonDocument doc;
  41. auto mp = doc["hello"];
  42. SECTION("containsKey(const char*)") {
  43. mp["key"] = "value";
  44. REQUIRE(mp.containsKey("key") == true);
  45. REQUIRE(mp.containsKey("key") == true);
  46. }
  47. SECTION("containsKey(std::string)") {
  48. mp["key"] = "value";
  49. REQUIRE(mp.containsKey("key"_s) == true);
  50. REQUIRE(mp.containsKey("key"_s) == true);
  51. }
  52. }
  53. TEST_CASE("JsonObject::containsKey()") {
  54. JsonDocument doc;
  55. JsonObject obj = doc.to<JsonObject>();
  56. obj["hello"] = 42;
  57. SECTION("returns true only if key is present") {
  58. REQUIRE(false == obj.containsKey("world"));
  59. REQUIRE(true == obj.containsKey("hello"));
  60. }
  61. SECTION("returns false after remove()") {
  62. obj.remove("hello");
  63. REQUIRE(false == obj.containsKey("hello"));
  64. }
  65. #ifdef HAS_VARIABLE_LENGTH_ARRAY
  66. SECTION("key is a VLA") {
  67. size_t i = 16;
  68. char vla[i];
  69. strcpy(vla, "hello");
  70. REQUIRE(true == obj.containsKey(vla));
  71. }
  72. #endif
  73. SECTION("key is a JsonVariant") {
  74. doc["key"] = "hello";
  75. REQUIRE(true == obj.containsKey(obj["key"]));
  76. REQUIRE(false == obj.containsKey(obj["hello"]));
  77. }
  78. SECTION("std::string") {
  79. REQUIRE(true == obj.containsKey("hello"_s));
  80. }
  81. SECTION("unsigned char[]") {
  82. unsigned char key[] = "hello";
  83. REQUIRE(true == obj.containsKey(key));
  84. }
  85. }
  86. TEST_CASE("JsonObjectConst::containsKey()") {
  87. JsonDocument doc;
  88. doc["hello"] = 42;
  89. auto obj = doc.as<JsonObjectConst>();
  90. SECTION("supports const char*") {
  91. REQUIRE(false == obj.containsKey("world"));
  92. REQUIRE(true == obj.containsKey("hello"));
  93. }
  94. SECTION("supports std::string") {
  95. REQUIRE(false == obj.containsKey("world"_s));
  96. REQUIRE(true == obj.containsKey("hello"_s));
  97. }
  98. #ifdef HAS_VARIABLE_LENGTH_ARRAY
  99. SECTION("supports VLA") {
  100. size_t i = 16;
  101. char vla[i];
  102. strcpy(vla, "hello");
  103. REQUIRE(true == obj.containsKey(vla));
  104. }
  105. #endif
  106. SECTION("supports JsonVariant") {
  107. doc["key"] = "hello";
  108. REQUIRE(true == obj.containsKey(obj["key"]));
  109. REQUIRE(false == obj.containsKey(obj["hello"]));
  110. }
  111. }
  112. TEST_CASE("JsonVariant::containsKey()") {
  113. JsonDocument doc;
  114. JsonVariant var = doc.to<JsonVariant>();
  115. SECTION("returns false is unbound") {
  116. CHECK_FALSE(JsonVariant().containsKey("hello"));
  117. }
  118. SECTION("containsKey(const char*)") {
  119. var["hello"] = "world";
  120. REQUIRE(var.containsKey("hello") == true);
  121. REQUIRE(var.containsKey("world") == false);
  122. }
  123. SECTION("containsKey(std::string)") {
  124. var["hello"] = "world";
  125. REQUIRE(var.containsKey("hello"_s) == true);
  126. REQUIRE(var.containsKey("world"_s) == false);
  127. }
  128. SECTION("containsKey(JsonVariant)") {
  129. var["hello"] = "world";
  130. var["key"] = "hello";
  131. REQUIRE(var.containsKey(doc["key"]) == true);
  132. REQUIRE(var.containsKey(doc["foo"]) == false);
  133. }
  134. }
  135. TEST_CASE("JsonVariantConst::containsKey()") {
  136. JsonDocument doc;
  137. doc["hello"] = "world";
  138. JsonVariantConst var = doc.as<JsonVariant>();
  139. SECTION("support const char*") {
  140. REQUIRE(var.containsKey("hello") == true);
  141. REQUIRE(var.containsKey("world") == false);
  142. }
  143. SECTION("support std::string") {
  144. REQUIRE(var.containsKey("hello"_s) == true);
  145. REQUIRE(var.containsKey("world"_s) == false);
  146. }
  147. #ifdef HAS_VARIABLE_LENGTH_ARRAY
  148. SECTION("supports VLA") {
  149. size_t i = 16;
  150. char vla[i];
  151. strcpy(vla, "hello");
  152. REQUIRE(true == var.containsKey(vla));
  153. }
  154. #endif
  155. SECTION("support JsonVariant") {
  156. doc["key"] = "hello";
  157. REQUIRE(var.containsKey(var["key"]) == true);
  158. REQUIRE(var.containsKey(var["foo"]) == false);
  159. }
  160. }