containsKey.cpp 914 B

123456789101112131415161718192021222324252627282930313233343536373839
  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("JsonObject::containsKey()") {
  7. DynamicJsonDocument doc(4096);
  8. JsonObject obj = doc.to<JsonObject>();
  9. obj["hello"] = 42;
  10. SECTION("returns true only if key is present") {
  11. REQUIRE(false == obj.containsKey("world"));
  12. REQUIRE(true == obj.containsKey("hello"));
  13. }
  14. SECTION("works with JsonObjectConst") {
  15. JsonObjectConst cobj = obj;
  16. REQUIRE(false == cobj.containsKey("world"));
  17. REQUIRE(true == cobj.containsKey("hello"));
  18. }
  19. SECTION("returns false after remove()") {
  20. obj.remove("hello");
  21. REQUIRE(false == obj.containsKey("hello"));
  22. }
  23. #ifdef HAS_VARIABLE_LENGTH_ARRAY
  24. SECTION("key is a VLA") {
  25. size_t i = 16;
  26. char vla[i];
  27. strcpy(vla, "hello");
  28. REQUIRE(true == obj.containsKey(vla));
  29. }
  30. #endif
  31. }