subscript.cpp 920 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 "Allocators.hpp"
  7. #include "Literals.hpp"
  8. TEST_CASE("JsonObjectConst::operator[]") {
  9. JsonDocument doc;
  10. doc["hello"] = "world";
  11. JsonObjectConst obj = doc.as<JsonObjectConst>();
  12. SECTION("supports const char*") {
  13. REQUIRE(obj["hello"] == "world"); // issue #2019
  14. }
  15. SECTION("supports std::string") {
  16. REQUIRE(obj["hello"_s] == "world"); // issue #2019
  17. }
  18. #if defined(HAS_VARIABLE_LENGTH_ARRAY) && \
  19. !defined(SUBSCRIPT_CONFLICTS_WITH_BUILTIN_OPERATOR)
  20. SECTION("supports VLA") {
  21. size_t i = 16;
  22. char vla[i];
  23. strcpy(vla, "hello");
  24. REQUIRE("world"_s == obj[vla]);
  25. }
  26. #endif
  27. SECTION("supports JsonVariant") {
  28. doc["key"] = "hello";
  29. REQUIRE(obj[obj["key"]] == "world");
  30. REQUIRE(obj[obj["foo"]] == nullptr);
  31. }
  32. }