copy.cpp 3.7 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("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 const char* by reference") {
  32. var1.set("hello!!");
  33. spyingAllocator.clearLog();
  34. var2.set(var1);
  35. REQUIRE(spyingAllocator.log() == AllocatorLog{});
  36. }
  37. SECTION("stores char* by copy") {
  38. char str[] = "hello!!";
  39. var1.set(str);
  40. spyingAllocator.clearLog();
  41. var2.set(var1);
  42. REQUIRE(spyingAllocator.log() == AllocatorLog{
  43. Allocate(sizeofString("hello!!")),
  44. });
  45. }
  46. SECTION("fails gracefully if string allocation fails") {
  47. char str[] = "hello!!";
  48. var1.set(str);
  49. killswitch.on();
  50. spyingAllocator.clearLog();
  51. var2.set(var1);
  52. REQUIRE(doc2.overflowed() == true);
  53. REQUIRE(spyingAllocator.log() == AllocatorLog{
  54. AllocateFail(sizeofString("hello!!")),
  55. });
  56. }
  57. SECTION("stores std::string by copy") {
  58. var1.set("hello!!"_s);
  59. spyingAllocator.clearLog();
  60. var2.set(var1);
  61. REQUIRE(spyingAllocator.log() == AllocatorLog{
  62. Allocate(sizeofString("hello!!")),
  63. });
  64. }
  65. SECTION("stores Serialized<const char*> by copy") {
  66. var1.set(serialized("hello!!", 7));
  67. spyingAllocator.clearLog();
  68. var2.set(var1);
  69. REQUIRE(spyingAllocator.log() == AllocatorLog{
  70. Allocate(sizeofString("hello!!")),
  71. });
  72. }
  73. SECTION("stores Serialized<char*> by copy") {
  74. char str[] = "hello!!";
  75. var1.set(serialized(str, 7));
  76. spyingAllocator.clearLog();
  77. var2.set(var1);
  78. REQUIRE(spyingAllocator.log() == AllocatorLog{
  79. Allocate(sizeofString("hello!!")),
  80. });
  81. }
  82. SECTION("stores Serialized<std::string> by copy") {
  83. var1.set(serialized("hello!!"_s));
  84. spyingAllocator.clearLog();
  85. var2.set(var1);
  86. REQUIRE(spyingAllocator.log() == AllocatorLog{
  87. Allocate(sizeofString("hello!!")),
  88. });
  89. }
  90. SECTION("fails gracefully if raw string allocation fails") {
  91. var1.set(serialized("hello!!"_s));
  92. killswitch.on();
  93. spyingAllocator.clearLog();
  94. var2.set(var1);
  95. REQUIRE(doc2.overflowed() == true);
  96. REQUIRE(spyingAllocator.log() == AllocatorLog{
  97. AllocateFail(sizeofString("hello!!")),
  98. });
  99. }
  100. SECTION("destination is unbound") {
  101. JsonVariant unboundVariant;
  102. unboundVariant.set(var1);
  103. REQUIRE(unboundVariant.isUnbound());
  104. REQUIRE(unboundVariant.isNull());
  105. }
  106. }