set.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2025, 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. Allocate(sizeofStaticStringPool()),
  23. });
  24. }
  25. SECTION("copy local string key and value") {
  26. obj1["hello"_s] = "world"_s;
  27. spy.clearLog();
  28. bool success = obj2.set(obj1);
  29. REQUIRE(success == true);
  30. REQUIRE(obj2["hello"] == "world"_s);
  31. REQUIRE(spy.log() == AllocatorLog{
  32. Allocate(sizeofPool()),
  33. Allocate(sizeofString("hello")),
  34. Allocate(sizeofString("world")),
  35. });
  36. }
  37. SECTION("copy string from deserializeJson()") {
  38. deserializeJson(doc1, "{'hello':'world'}");
  39. spy.clearLog();
  40. bool success = obj2.set(obj1);
  41. REQUIRE(success == true);
  42. REQUIRE(obj2["hello"] == "world"_s);
  43. REQUIRE(spy.log() == AllocatorLog{
  44. Allocate(sizeofPool()),
  45. Allocate(sizeofString("hello")),
  46. Allocate(sizeofString("world")),
  47. });
  48. }
  49. SECTION("copy string from deserializeMsgPack()") {
  50. deserializeMsgPack(doc1, "\x81\xA5hello\xA5world");
  51. spy.clearLog();
  52. bool success = obj2.set(obj1);
  53. REQUIRE(success == true);
  54. REQUIRE(obj2["hello"] == "world"_s);
  55. REQUIRE(spy.log() == AllocatorLog{
  56. Allocate(sizeofPool()),
  57. Allocate(sizeofString("hello")),
  58. Allocate(sizeofString("world")),
  59. });
  60. }
  61. SECTION("should work with JsonObjectConst") {
  62. obj1["hello"] = "world";
  63. obj2.set(static_cast<JsonObjectConst>(obj1));
  64. REQUIRE(obj2["hello"] == "world"_s);
  65. }
  66. SECTION("copy fails in the middle of an object") {
  67. TimebombAllocator timebomb(2);
  68. JsonDocument doc3(&timebomb);
  69. JsonObject obj3 = doc3.to<JsonObject>();
  70. obj1["alpha"_s] = 1;
  71. obj1["beta"_s] = 2;
  72. bool success = obj3.set(obj1);
  73. REQUIRE(success == false);
  74. REQUIRE(doc3.as<std::string>() == "{\"alpha\":1}");
  75. }
  76. SECTION("copy fails in the middle of an array") {
  77. TimebombAllocator timebomb(2);
  78. JsonDocument doc3(&timebomb);
  79. JsonObject obj3 = doc3.to<JsonObject>();
  80. obj1["hello"][0] = "world"_s;
  81. bool success = obj3.set(obj1);
  82. REQUIRE(success == false);
  83. REQUIRE(doc3.as<std::string>() == "{\"hello\":[]}");
  84. }
  85. SECTION("destination is null") {
  86. JsonObject null;
  87. obj1["hello"] = "world";
  88. bool success = null.set(obj1);
  89. REQUIRE(success == false);
  90. }
  91. SECTION("source is null") {
  92. JsonObject null;
  93. obj1["hello"] = "world";
  94. bool success = obj1.set(null);
  95. REQUIRE(success == false);
  96. }
  97. }