containsKey.cpp 955 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2024, Benoit BLANCHON
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <stdint.h>
  6. #include <catch.hpp>
  7. #include "Literals.hpp"
  8. TEST_CASE("JsonVariantConst::containsKey()") {
  9. JsonDocument doc;
  10. doc["hello"] = "world";
  11. JsonVariantConst var = doc.as<JsonVariant>();
  12. SECTION("support const char*") {
  13. REQUIRE(var.containsKey("hello") == true);
  14. REQUIRE(var.containsKey("world") == false);
  15. }
  16. SECTION("support std::string") {
  17. REQUIRE(var.containsKey("hello"_s) == true);
  18. REQUIRE(var.containsKey("world"_s) == false);
  19. }
  20. #ifdef HAS_VARIABLE_LENGTH_ARRAY
  21. SECTION("supports VLA") {
  22. size_t i = 16;
  23. char vla[i];
  24. strcpy(vla, "hello");
  25. REQUIRE(true == var.containsKey(vla));
  26. }
  27. #endif
  28. SECTION("support JsonVariant") {
  29. doc["key"] = "hello";
  30. REQUIRE(var.containsKey(var["key"]) == true);
  31. REQUIRE(var.containsKey(var["foo"]) == false);
  32. }
  33. }