| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- // ArduinoJson - https://arduinojson.org
- // Copyright © 2014-2025, Benoit BLANCHON
- // MIT License
- #include <ArduinoJson.h>
- #include <catch.hpp>
- #include "Literals.hpp"
- TEST_CASE("JsonDocument::containsKey()") {
- JsonDocument doc;
- SECTION("returns true on object") {
- doc["hello"] = "world";
- REQUIRE(doc.containsKey("hello") == true);
- }
- SECTION("returns true when value is null") {
- doc["hello"] = static_cast<const char*>(0);
- REQUIRE(doc.containsKey("hello") == true);
- }
- SECTION("returns true when key is a std::string") {
- doc["hello"] = "world";
- REQUIRE(doc.containsKey("hello"_s) == true);
- }
- SECTION("returns false on object") {
- doc["world"] = "hello";
- REQUIRE(doc.containsKey("hello") == false);
- }
- SECTION("returns false on array") {
- doc.add("hello");
- REQUIRE(doc.containsKey("hello") == false);
- }
- SECTION("returns false on null") {
- REQUIRE(doc.containsKey("hello") == false);
- }
- SECTION("supports JsonVariant") {
- doc["hello"] = "world";
- doc["key"] = "hello";
- REQUIRE(doc.containsKey(doc["key"]) == true);
- REQUIRE(doc.containsKey(doc["foo"]) == false);
- }
- #ifdef HAS_VARIABLE_LENGTH_ARRAY
- SECTION("supports VLAs") {
- size_t i = 16;
- char vla[i];
- strcpy(vla, "hello");
- doc["hello"] = "world";
- REQUIRE(doc.containsKey(vla) == true);
- }
- #endif
- }
- TEST_CASE("MemberProxy::containsKey()") {
- JsonDocument doc;
- const auto& mp = doc["hello"];
- SECTION("containsKey(const char*)") {
- mp["key"] = "value";
- REQUIRE(mp.containsKey("key") == true);
- REQUIRE(mp.containsKey("key") == true);
- }
- SECTION("containsKey(std::string)") {
- mp["key"] = "value";
- REQUIRE(mp.containsKey("key"_s) == true);
- REQUIRE(mp.containsKey("key"_s) == true);
- }
- #ifdef HAS_VARIABLE_LENGTH_ARRAY
- SECTION("supports VLAs") {
- size_t i = 16;
- char vla[i];
- strcpy(vla, "hello");
- mp["hello"] = "world";
- REQUIRE(mp.containsKey(vla) == true);
- }
- #endif
- }
- TEST_CASE("JsonObject::containsKey()") {
- JsonDocument doc;
- JsonObject obj = doc.to<JsonObject>();
- obj["hello"] = 42;
- SECTION("returns true only if key is present") {
- REQUIRE(false == obj.containsKey("world"));
- REQUIRE(true == obj.containsKey("hello"));
- }
- SECTION("returns false after remove()") {
- obj.remove("hello");
- REQUIRE(false == obj.containsKey("hello"));
- }
- #ifdef HAS_VARIABLE_LENGTH_ARRAY
- SECTION("key is a VLA") {
- size_t i = 16;
- char vla[i];
- strcpy(vla, "hello");
- REQUIRE(true == obj.containsKey(vla));
- }
- #endif
- SECTION("key is a JsonVariant") {
- doc["key"] = "hello";
- REQUIRE(true == obj.containsKey(obj["key"]));
- REQUIRE(false == obj.containsKey(obj["hello"]));
- }
- SECTION("std::string") {
- REQUIRE(true == obj.containsKey("hello"_s));
- }
- SECTION("unsigned char[]") {
- unsigned char key[] = "hello";
- REQUIRE(true == obj.containsKey(key));
- }
- }
- TEST_CASE("JsonObjectConst::containsKey()") {
- JsonDocument doc;
- doc["hello"] = 42;
- auto obj = doc.as<JsonObjectConst>();
- SECTION("supports const char*") {
- REQUIRE(false == obj.containsKey("world"));
- REQUIRE(true == obj.containsKey("hello"));
- }
- SECTION("supports std::string") {
- REQUIRE(false == obj.containsKey("world"_s));
- REQUIRE(true == obj.containsKey("hello"_s));
- }
- #ifdef HAS_VARIABLE_LENGTH_ARRAY
- SECTION("supports VLA") {
- size_t i = 16;
- char vla[i];
- strcpy(vla, "hello");
- REQUIRE(true == obj.containsKey(vla));
- }
- #endif
- SECTION("supports JsonVariant") {
- doc["key"] = "hello";
- REQUIRE(true == obj.containsKey(obj["key"]));
- REQUIRE(false == obj.containsKey(obj["hello"]));
- }
- }
- TEST_CASE("JsonVariant::containsKey()") {
- JsonDocument doc;
- JsonVariant var = doc.to<JsonVariant>();
- SECTION("returns false is unbound") {
- CHECK_FALSE(JsonVariant().containsKey("hello"));
- }
- SECTION("containsKey(const char*)") {
- var["hello"] = "world";
- REQUIRE(var.containsKey("hello") == true);
- REQUIRE(var.containsKey("world") == false);
- }
- SECTION("containsKey(std::string)") {
- var["hello"] = "world";
- REQUIRE(var.containsKey("hello"_s) == true);
- REQUIRE(var.containsKey("world"_s) == false);
- }
- SECTION("containsKey(JsonVariant)") {
- var["hello"] = "world";
- var["key"] = "hello";
- REQUIRE(var.containsKey(doc["key"]) == true);
- REQUIRE(var.containsKey(doc["foo"]) == false);
- }
- #ifdef HAS_VARIABLE_LENGTH_ARRAY
- SECTION("supports VLAs") {
- size_t i = 16;
- char vla[i];
- strcpy(vla, "hello");
- var["hello"] = "world";
- REQUIRE(var.containsKey(vla) == true);
- }
- #endif
- }
- TEST_CASE("JsonVariantConst::containsKey()") {
- JsonDocument doc;
- doc["hello"] = "world";
- JsonVariantConst var = doc.as<JsonVariant>();
- SECTION("support const char*") {
- REQUIRE(var.containsKey("hello") == true);
- REQUIRE(var.containsKey("world") == false);
- }
- SECTION("support std::string") {
- REQUIRE(var.containsKey("hello"_s) == true);
- REQUIRE(var.containsKey("world"_s) == false);
- }
- #ifdef HAS_VARIABLE_LENGTH_ARRAY
- SECTION("supports VLA") {
- size_t i = 16;
- char vla[i];
- strcpy(vla, "hello");
- REQUIRE(true == var.containsKey(vla));
- }
- #endif
- SECTION("support JsonVariant") {
- doc["key"] = "hello";
- REQUIRE(var.containsKey(var["key"]) == true);
- REQUIRE(var.containsKey(var["foo"]) == false);
- }
- }
|