createNested.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2020
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <stdint.h>
  6. #include <catch.hpp>
  7. TEST_CASE("JsonVariant::createNestedObject()") {
  8. DynamicJsonDocument doc(4096);
  9. JsonVariant variant = doc.to<JsonVariant>();
  10. SECTION("promotes to array") {
  11. JsonObject obj = variant.createNestedObject();
  12. obj["value"] = 42;
  13. REQUIRE(variant.is<JsonArray>() == true);
  14. REQUIRE(variant[0]["value"] == 42);
  15. REQUIRE(obj.isNull() == false);
  16. }
  17. SECTION("works on MemberProxy") {
  18. JsonObject obj = variant["items"].createNestedObject();
  19. obj["value"] = 42;
  20. REQUIRE(variant["items"][0]["value"] == 42);
  21. }
  22. }
  23. TEST_CASE("JsonVariant::createNestedArray()") {
  24. DynamicJsonDocument doc(4096);
  25. JsonVariant variant = doc.to<JsonVariant>();
  26. SECTION("promotes to array") {
  27. JsonArray arr = variant.createNestedArray();
  28. REQUIRE(variant.is<JsonArray>() == true);
  29. REQUIRE(arr.isNull() == false);
  30. }
  31. SECTION("works on MemberProxy") {
  32. JsonArray arr = variant["items"].createNestedArray();
  33. arr.add(42);
  34. REQUIRE(variant["items"][0][0] == 42);
  35. }
  36. }
  37. TEST_CASE("JsonVariant::createNestedObject(key)") {
  38. DynamicJsonDocument doc(4096);
  39. JsonVariant variant = doc.to<JsonVariant>();
  40. SECTION("promotes to object") {
  41. JsonObject obj = variant.createNestedObject("weather");
  42. obj["temp"] = 42;
  43. REQUIRE(variant.is<JsonObject>() == true);
  44. REQUIRE(variant["weather"]["temp"] == 42);
  45. }
  46. SECTION("works on MemberProxy") {
  47. JsonObject obj = variant["status"].createNestedObject("weather");
  48. obj["temp"] = 42;
  49. REQUIRE(variant["status"]["weather"]["temp"] == 42);
  50. }
  51. }
  52. TEST_CASE("JsonVariant::createNestedArray(key)") {
  53. DynamicJsonDocument doc(4096);
  54. JsonVariant variant = doc.to<JsonVariant>();
  55. SECTION("promotes to object") {
  56. JsonArray arr = variant.createNestedArray("items");
  57. REQUIRE(variant.is<JsonObject>() == true);
  58. REQUIRE(arr.isNull() == false);
  59. }
  60. SECTION("works on MemberProxy") {
  61. JsonArray arr = variant["weather"].createNestedArray("temp");
  62. arr.add(42);
  63. REQUIRE(variant["weather"]["temp"][0] == 42);
  64. }
  65. }