copy.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. TEST_CASE("JsonObject::set()") {
  8. JsonDocument doc1;
  9. JsonDocument doc2;
  10. JsonObject obj1 = doc1.to<JsonObject>();
  11. JsonObject obj2 = doc2.to<JsonObject>();
  12. SECTION("doesn't copy static string in key or value") {
  13. obj1["hello"] = "world";
  14. bool success = obj2.set(obj1);
  15. REQUIRE(success == true);
  16. REQUIRE(doc1.memoryUsage() == doc2.memoryUsage());
  17. REQUIRE(obj2["hello"] == std::string("world"));
  18. }
  19. SECTION("copy local string value") {
  20. obj1["hello"] = std::string("world");
  21. bool success = obj2.set(obj1);
  22. REQUIRE(success == true);
  23. REQUIRE(doc1.memoryUsage() == doc2.memoryUsage());
  24. REQUIRE(obj2["hello"] == std::string("world"));
  25. }
  26. SECTION("copy local key") {
  27. obj1[std::string("hello")] = "world";
  28. bool success = obj2.set(obj1);
  29. REQUIRE(success == true);
  30. REQUIRE(doc1.memoryUsage() == doc2.memoryUsage());
  31. REQUIRE(obj2["hello"] == std::string("world"));
  32. }
  33. SECTION("copy string from deserializeJson()") {
  34. deserializeJson(doc1, "{'hello':'world'}");
  35. bool success = obj2.set(obj1);
  36. REQUIRE(success == true);
  37. REQUIRE(doc1.memoryUsage() == doc2.memoryUsage());
  38. REQUIRE(obj2["hello"] == std::string("world"));
  39. }
  40. SECTION("copy string from deserializeMsgPack()") {
  41. deserializeMsgPack(doc1, "\x81\xA5hello\xA5world");
  42. bool success = obj2.set(obj1);
  43. REQUIRE(success == true);
  44. REQUIRE(doc1.memoryUsage() == doc2.memoryUsage());
  45. REQUIRE(obj2["hello"] == std::string("world"));
  46. }
  47. SECTION("should work with JsonObjectConst") {
  48. obj1["hello"] = "world";
  49. obj2.set(static_cast<JsonObjectConst>(obj1));
  50. REQUIRE(doc1.memoryUsage() == doc2.memoryUsage());
  51. REQUIRE(obj2["hello"] == std::string("world"));
  52. }
  53. SECTION("copy fails in the middle of an object") {
  54. TimebombAllocator allocator(3);
  55. JsonDocument doc3(&allocator);
  56. JsonObject obj3 = doc3.to<JsonObject>();
  57. obj1[std::string("a")] = 1;
  58. obj1[std::string("b")] = 2;
  59. bool success = obj3.set(obj1);
  60. REQUIRE(success == false);
  61. REQUIRE(doc3.as<std::string>() == "{\"a\":1}");
  62. }
  63. SECTION("copy fails in the middle of an array") {
  64. TimebombAllocator allocator(2);
  65. JsonDocument doc3(&allocator);
  66. JsonObject obj3 = doc3.to<JsonObject>();
  67. obj1["hello"][0] = std::string("world");
  68. bool success = obj3.set(obj1);
  69. REQUIRE(success == false);
  70. REQUIRE(doc3.as<std::string>() == "{\"hello\":[null]}");
  71. }
  72. SECTION("destination is null") {
  73. JsonObject null;
  74. obj1["hello"] = "world";
  75. bool success = null.set(obj1);
  76. REQUIRE(success == false);
  77. }
  78. SECTION("source is null") {
  79. JsonObject null;
  80. obj1["hello"] = "world";
  81. bool success = obj1.set(null);
  82. REQUIRE(success == false);
  83. }
  84. }