copy.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. using ArduinoJson::detail::sizeofString;
  8. TEST_CASE("JsonVariant::set(JsonVariant)") {
  9. ControllableAllocator allocator;
  10. SpyingAllocator spyingAllocator(&allocator);
  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.createNestedObject();
  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.createNestedArray("value");
  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() ==
  43. AllocatorLog() << AllocatorLog::Allocate(sizeofString((7))));
  44. }
  45. SECTION("fails gracefully if string allocation fails") {
  46. char str[] = "hello!!";
  47. var1.set(str);
  48. allocator.disable();
  49. spyingAllocator.clearLog();
  50. var2.set(var1);
  51. REQUIRE(doc2.overflowed() == true);
  52. REQUIRE(spyingAllocator.log() ==
  53. AllocatorLog() << AllocatorLog::AllocateFail(sizeofString((7))));
  54. }
  55. SECTION("stores std::string by copy") {
  56. var1.set(std::string("hello!!"));
  57. spyingAllocator.clearLog();
  58. var2.set(var1);
  59. REQUIRE(spyingAllocator.log() ==
  60. AllocatorLog() << AllocatorLog::Allocate(sizeofString((7))));
  61. }
  62. SECTION("stores Serialized<const char*> by copy") {
  63. var1.set(serialized("hello!!", 7));
  64. spyingAllocator.clearLog();
  65. var2.set(var1);
  66. REQUIRE(spyingAllocator.log() ==
  67. AllocatorLog() << AllocatorLog::Allocate(sizeofString((7))));
  68. }
  69. SECTION("stores Serialized<char*> by copy") {
  70. char str[] = "hello!!";
  71. var1.set(serialized(str, 7));
  72. spyingAllocator.clearLog();
  73. var2.set(var1);
  74. REQUIRE(spyingAllocator.log() ==
  75. AllocatorLog() << AllocatorLog::Allocate(sizeofString((7))));
  76. }
  77. SECTION("stores Serialized<std::string> by copy") {
  78. var1.set(serialized(std::string("hello!!")));
  79. spyingAllocator.clearLog();
  80. var2.set(var1);
  81. REQUIRE(spyingAllocator.log() ==
  82. AllocatorLog() << AllocatorLog::Allocate(sizeofString((7))));
  83. }
  84. SECTION("fails gracefully if raw string allocation fails") {
  85. var1.set(serialized(std::string("hello!!")));
  86. allocator.disable();
  87. spyingAllocator.clearLog();
  88. var2.set(var1);
  89. REQUIRE(doc2.overflowed() == true);
  90. REQUIRE(spyingAllocator.log() ==
  91. AllocatorLog() << AllocatorLog::AllocateFail(sizeofString((7))));
  92. }
  93. SECTION("destination is unbound") {
  94. JsonVariant unboundVariant;
  95. unboundVariant.set(var1);
  96. REQUIRE(unboundVariant.isUnbound());
  97. REQUIRE(unboundVariant.isNull());
  98. }
  99. }