copy.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2019
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. TEST_CASE("JsonObject::set()") {
  7. DynamicJsonDocument doc1(4096);
  8. DynamicJsonDocument doc2(4096);
  9. JsonObject obj1 = doc1.to<JsonObject>();
  10. JsonObject obj2 = doc2.to<JsonObject>();
  11. SECTION("doesn't copy static string in key or value") {
  12. obj1["hello"] = "world";
  13. obj2.set(obj1);
  14. REQUIRE(doc1.memoryUsage() == doc2.memoryUsage());
  15. REQUIRE(obj2["hello"] == std::string("world"));
  16. }
  17. SECTION("copy local string value") {
  18. obj1["hello"] = std::string("world");
  19. obj2.set(obj1);
  20. REQUIRE(doc1.memoryUsage() == doc2.memoryUsage());
  21. REQUIRE(obj2["hello"] == std::string("world"));
  22. }
  23. SECTION("copy local key") {
  24. obj1[std::string("hello")] = "world";
  25. obj2.set(obj1);
  26. REQUIRE(doc1.memoryUsage() == doc2.memoryUsage());
  27. REQUIRE(obj2["hello"] == std::string("world"));
  28. }
  29. SECTION("copy string from deserializeJson()") {
  30. deserializeJson(doc1, "{'hello':'world'}");
  31. obj2.set(obj1);
  32. REQUIRE(doc1.memoryUsage() == doc2.memoryUsage());
  33. REQUIRE(obj2["hello"] == std::string("world"));
  34. }
  35. SECTION("copy string from deserializeMsgPack()") {
  36. deserializeMsgPack(doc1, "\x81\xA5hello\xA5world");
  37. obj2.set(obj1);
  38. REQUIRE(doc1.memoryUsage() == doc2.memoryUsage());
  39. REQUIRE(obj2["hello"] == std::string("world"));
  40. }
  41. SECTION("should work with JsonObjectConst") {
  42. obj1["hello"] = "world";
  43. obj2.set(static_cast<JsonObjectConst>(obj1));
  44. REQUIRE(doc1.memoryUsage() == doc2.memoryUsage());
  45. REQUIRE(obj2["hello"] == std::string("world"));
  46. }
  47. }