copy.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2023, Benoit BLANCHON
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. #include "Allocators.hpp"
  7. using ArduinoJson::detail::sizeofString;
  8. TEST_CASE("JsonObject::set()") {
  9. SpyingAllocator allocator;
  10. JsonDocument doc1(&allocator);
  11. JsonDocument doc2(&allocator);
  12. JsonObject obj1 = doc1.to<JsonObject>();
  13. JsonObject obj2 = doc2.to<JsonObject>();
  14. SECTION("doesn't copy static string in key or value") {
  15. obj1["hello"] = "world";
  16. allocator.clearLog();
  17. bool success = obj2.set(obj1);
  18. REQUIRE(success == true);
  19. REQUIRE(obj2["hello"] == std::string("world"));
  20. REQUIRE(allocator.log() == AllocatorLog()
  21. << AllocatorLog::Allocate(sizeofPool()));
  22. }
  23. SECTION("copy local string value") {
  24. obj1["hello"] = std::string("world");
  25. allocator.clearLog();
  26. bool success = obj2.set(obj1);
  27. REQUIRE(success == true);
  28. REQUIRE(obj2["hello"] == std::string("world"));
  29. REQUIRE(allocator.log() == AllocatorLog()
  30. << AllocatorLog::Allocate(sizeofPool())
  31. << AllocatorLog::Allocate(sizeofString(5)));
  32. }
  33. SECTION("copy local key") {
  34. obj1[std::string("hello")] = "world";
  35. allocator.clearLog();
  36. bool success = obj2.set(obj1);
  37. REQUIRE(success == true);
  38. REQUIRE(obj2["hello"] == std::string("world"));
  39. REQUIRE(allocator.log() == AllocatorLog()
  40. << AllocatorLog::Allocate(sizeofString(5))
  41. << AllocatorLog::Allocate(sizeofPool()));
  42. }
  43. SECTION("copy string from deserializeJson()") {
  44. deserializeJson(doc1, "{'hello':'world'}");
  45. allocator.clearLog();
  46. bool success = obj2.set(obj1);
  47. REQUIRE(success == true);
  48. REQUIRE(obj2["hello"] == std::string("world"));
  49. REQUIRE(allocator.log() == AllocatorLog()
  50. << AllocatorLog::Allocate(sizeofString(5))
  51. << AllocatorLog::Allocate(sizeofPool())
  52. << AllocatorLog::Allocate(sizeofString(5)));
  53. }
  54. SECTION("copy string from deserializeMsgPack()") {
  55. deserializeMsgPack(doc1, "\x81\xA5hello\xA5world");
  56. allocator.clearLog();
  57. bool success = obj2.set(obj1);
  58. REQUIRE(success == true);
  59. REQUIRE(obj2["hello"] == std::string("world"));
  60. REQUIRE(allocator.log() == AllocatorLog()
  61. << AllocatorLog::Allocate(sizeofString(5))
  62. << AllocatorLog::Allocate(sizeofPool())
  63. << AllocatorLog::Allocate(sizeofString(5)));
  64. }
  65. SECTION("should work with JsonObjectConst") {
  66. obj1["hello"] = "world";
  67. obj2.set(static_cast<JsonObjectConst>(obj1));
  68. REQUIRE(obj2["hello"] == std::string("world"));
  69. }
  70. SECTION("copy fails in the middle of an object") {
  71. TimebombAllocator timebomb(2);
  72. JsonDocument doc3(&timebomb);
  73. JsonObject obj3 = doc3.to<JsonObject>();
  74. obj1[std::string("a")] = 1;
  75. obj1[std::string("b")] = 2;
  76. bool success = obj3.set(obj1);
  77. REQUIRE(success == false);
  78. REQUIRE(doc3.as<std::string>() == "{\"a\":1}");
  79. }
  80. SECTION("copy fails in the middle of an array") {
  81. TimebombAllocator timebomb(1);
  82. JsonDocument doc3(&timebomb);
  83. JsonObject obj3 = doc3.to<JsonObject>();
  84. obj1["hello"][0] = std::string("world");
  85. bool success = obj3.set(obj1);
  86. REQUIRE(success == false);
  87. REQUIRE(doc3.as<std::string>() == "{\"hello\":[null]}");
  88. }
  89. SECTION("destination is null") {
  90. JsonObject null;
  91. obj1["hello"] = "world";
  92. bool success = null.set(obj1);
  93. REQUIRE(success == false);
  94. }
  95. SECTION("source is null") {
  96. JsonObject null;
  97. obj1["hello"] = "world";
  98. bool success = obj1.set(null);
  99. REQUIRE(success == false);
  100. }
  101. }