containsKey.cpp 926 B

12345678910111213141516171819202122232425262728293031323334353637383940
  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("JsonObjectConst::containsKey()") {
  8. JsonDocument doc;
  9. doc["hello"] = 42;
  10. auto obj = doc.as<JsonObjectConst>();
  11. SECTION("supports const char*") {
  12. REQUIRE(false == obj.containsKey("world"));
  13. REQUIRE(true == obj.containsKey("hello"));
  14. }
  15. SECTION("supports std::string") {
  16. REQUIRE(false == obj.containsKey("world"_s));
  17. REQUIRE(true == obj.containsKey("hello"_s));
  18. }
  19. #ifdef HAS_VARIABLE_LENGTH_ARRAY
  20. SECTION("supports VLA") {
  21. size_t i = 16;
  22. char vla[i];
  23. strcpy(vla, "hello");
  24. REQUIRE(true == obj.containsKey(vla));
  25. }
  26. #endif
  27. SECTION("supports JsonVariant") {
  28. doc["key"] = "hello";
  29. REQUIRE(true == obj.containsKey(obj["key"]));
  30. REQUIRE(false == obj.containsKey(obj["hello"]));
  31. }
  32. }