set.cpp 3.5 KB

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