copy.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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("JsonVariant::set(JsonVariant)") {
  9. KillswitchAllocator killswitch;
  10. SpyingAllocator spyingAllocator(&killswitch);
  11. JsonDocument doc1(&spyingAllocator);
  12. JsonDocument doc2(&spyingAllocator);
  13. JsonVariant var1 = doc1.to<JsonVariant>();
  14. JsonVariant var2 = doc2.to<JsonVariant>();
  15. SECTION("stores JsonArray by copy") {
  16. JsonArray arr = doc2.to<JsonArray>();
  17. JsonObject obj = arr.add<JsonObject>();
  18. obj["hello"] = "world";
  19. var1.set(arr);
  20. arr[0] = 666;
  21. REQUIRE(var1.as<std::string>() == "[{\"hello\":\"world\"}]");
  22. }
  23. SECTION("stores JsonObject by copy") {
  24. JsonObject obj = doc2.to<JsonObject>();
  25. JsonArray arr = obj["value"].to<JsonArray>();
  26. arr.add(42);
  27. var1.set(obj);
  28. obj["value"] = 666;
  29. REQUIRE(var1.as<std::string>() == "{\"value\":[42]}");
  30. }
  31. SECTION("stores string literals by copy") {
  32. var1.set("hello!!");
  33. spyingAllocator.clearLog();
  34. var2.set(var1);
  35. REQUIRE(spyingAllocator.log() == AllocatorLog{
  36. Allocate(sizeofString("hello!!")),
  37. });
  38. }
  39. SECTION("stores char* by copy") {
  40. char str[] = "hello!!";
  41. var1.set(str);
  42. spyingAllocator.clearLog();
  43. var2.set(var1);
  44. REQUIRE(spyingAllocator.log() == AllocatorLog{
  45. Allocate(sizeofString("hello!!")),
  46. });
  47. }
  48. SECTION("fails gracefully if string allocation fails") {
  49. char str[] = "hello!!";
  50. var1.set(str);
  51. killswitch.on();
  52. spyingAllocator.clearLog();
  53. var2.set(var1);
  54. REQUIRE(doc2.overflowed() == true);
  55. REQUIRE(spyingAllocator.log() == AllocatorLog{
  56. AllocateFail(sizeofString("hello!!")),
  57. });
  58. }
  59. SECTION("stores std::string by copy") {
  60. var1.set("hello!!"_s);
  61. spyingAllocator.clearLog();
  62. var2.set(var1);
  63. REQUIRE(spyingAllocator.log() == AllocatorLog{
  64. Allocate(sizeofString("hello!!")),
  65. });
  66. }
  67. SECTION("stores Serialized<const char*> by copy") {
  68. var1.set(serialized("hello!!", 7));
  69. spyingAllocator.clearLog();
  70. var2.set(var1);
  71. REQUIRE(spyingAllocator.log() == AllocatorLog{
  72. Allocate(sizeofString("hello!!")),
  73. });
  74. }
  75. SECTION("stores Serialized<char*> by copy") {
  76. char str[] = "hello!!";
  77. var1.set(serialized(str, 7));
  78. spyingAllocator.clearLog();
  79. var2.set(var1);
  80. REQUIRE(spyingAllocator.log() == AllocatorLog{
  81. Allocate(sizeofString("hello!!")),
  82. });
  83. }
  84. SECTION("stores Serialized<std::string> by copy") {
  85. var1.set(serialized("hello!!"_s));
  86. spyingAllocator.clearLog();
  87. var2.set(var1);
  88. REQUIRE(spyingAllocator.log() == AllocatorLog{
  89. Allocate(sizeofString("hello!!")),
  90. });
  91. }
  92. SECTION("fails gracefully if raw string allocation fails") {
  93. var1.set(serialized("hello!!"_s));
  94. killswitch.on();
  95. spyingAllocator.clearLog();
  96. var2.set(var1);
  97. REQUIRE(doc2.overflowed() == true);
  98. REQUIRE(spyingAllocator.log() == AllocatorLog{
  99. AllocateFail(sizeofString("hello!!")),
  100. });
  101. }
  102. SECTION("destination is unbound") {
  103. JsonVariant unboundVariant;
  104. unboundVariant.set(var1);
  105. REQUIRE(unboundVariant.isUnbound());
  106. REQUIRE(unboundVariant.isNull());
  107. }
  108. }