subscript.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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("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. }
  35. TEST_CASE("JsonDocument automatically promotes to array") {
  36. DynamicJsonDocument doc(4096);
  37. doc[2] = 2;
  38. REQUIRE(doc.as<std::string>() == "[null,null,2]");
  39. }