subscript.cpp 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2019
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. TEST_CASE("JsonDocument::operator[]") {
  7. DynamicJsonDocument doc(4096);
  8. const JsonDocument& cdoc = doc;
  9. SECTION("object") {
  10. deserializeJson(doc, "{\"hello\":\"world\"}");
  11. SECTION("const char*") {
  12. REQUIRE(doc["hello"] == "world");
  13. REQUIRE(cdoc["hello"] == "world");
  14. }
  15. SECTION("std::string") {
  16. REQUIRE(doc[std::string("hello")] == "world");
  17. REQUIRE(cdoc[std::string("hello")] == "world");
  18. }
  19. SECTION("supports operator|") {
  20. REQUIRE((doc["hello"] | "nope") == std::string("world"));
  21. REQUIRE((doc["world"] | "nope") == std::string("nope"));
  22. }
  23. }
  24. SECTION("array") {
  25. deserializeJson(doc, "[\"hello\",\"world\"]");
  26. REQUIRE(doc[1] == "world");
  27. REQUIRE(cdoc[1] == "world");
  28. }
  29. }
  30. TEST_CASE("JsonDocument automatically promotes to object") {
  31. DynamicJsonDocument doc(4096);
  32. doc["one"]["two"]["three"] = 4;
  33. REQUIRE(doc["one"]["two"]["three"] == 4);
  34. }