set.cpp 2.9 KB

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