copy.cpp 3.6 KB

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