link.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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("JsonVariant::link()") {
  7. StaticJsonDocument<1024> doc1, doc2;
  8. JsonVariant variant = doc1.to<JsonVariant>();
  9. SECTION("JsonVariant::link(JsonDocument&)") {
  10. doc2["hello"] = "world";
  11. variant.link(doc2);
  12. CHECK(variant.as<std::string>() == "{\"hello\":\"world\"}");
  13. CHECK(variant.memoryUsage() == 0);
  14. // altering the linked document should change the result
  15. doc2["hello"] = "WORLD!";
  16. CHECK(variant.as<std::string>() == "{\"hello\":\"WORLD!\"}");
  17. }
  18. SECTION("JsonVariant::link(MemberProxy)") {
  19. doc2["obj"]["hello"] = "world";
  20. variant.link(doc2["obj"]);
  21. CHECK(variant.as<std::string>() == "{\"hello\":\"world\"}");
  22. CHECK(variant.memoryUsage() == 0);
  23. // altering the linked document should change the result
  24. doc2["obj"]["hello"] = "WORLD!";
  25. CHECK(variant.as<std::string>() == "{\"hello\":\"WORLD!\"}");
  26. }
  27. SECTION("JsonVariant::link(ElementProxy)") {
  28. doc2[0]["hello"] = "world";
  29. variant.link(doc2[0]);
  30. CHECK(variant.as<std::string>() == "{\"hello\":\"world\"}");
  31. CHECK(variant.memoryUsage() == 0);
  32. // altering the linked document should change the result
  33. doc2[0]["hello"] = "WORLD!";
  34. CHECK(variant.as<std::string>() == "{\"hello\":\"WORLD!\"}");
  35. }
  36. SECTION("target is unbound") {
  37. JsonVariant unbound;
  38. variant["hello"] = "world";
  39. variant.link(unbound);
  40. CHECK(variant.isUnbound() == false);
  41. CHECK(variant.isNull() == true);
  42. CHECK(variant.memoryUsage() == 0);
  43. CHECK(variant.size() == 0);
  44. }
  45. SECTION("variant is unbound") {
  46. JsonVariant unbound;
  47. doc2["hello"] = "world";
  48. unbound.link(doc2);
  49. CHECK(unbound.isUnbound() == true);
  50. CHECK(unbound.isNull() == true);
  51. CHECK(unbound.memoryUsage() == 0);
  52. CHECK(unbound.size() == 0);
  53. }
  54. }