copy.cpp 3.7 KB

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