shallowCopy.cpp 2.1 KB

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