constructor.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. using ArduinoJson::detail::addPadding;
  9. TEST_CASE("JsonDocument constructor") {
  10. SpyingAllocator spyingAllocator;
  11. SECTION("JsonDocument(size_t)") {
  12. { JsonDocument doc(&spyingAllocator); }
  13. REQUIRE(spyingAllocator.log() == AllocatorLog{});
  14. }
  15. SECTION("JsonDocument(const JsonDocument&)") {
  16. {
  17. JsonDocument doc1(&spyingAllocator);
  18. doc1.set("The size of this string is 32!!"_s);
  19. JsonDocument doc2(doc1);
  20. REQUIRE(doc1.as<std::string>() == "The size of this string is 32!!");
  21. REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
  22. }
  23. REQUIRE(spyingAllocator.log() == AllocatorLog{
  24. Allocate(sizeofStringBuffer()),
  25. Allocate(sizeofStringBuffer()),
  26. Deallocate(sizeofStringBuffer()),
  27. Deallocate(sizeofStringBuffer()),
  28. });
  29. }
  30. SECTION("JsonDocument(JsonDocument&&)") {
  31. {
  32. JsonDocument doc1(&spyingAllocator);
  33. doc1.set("The size of this string is 32!!"_s);
  34. JsonDocument doc2(std::move(doc1));
  35. REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
  36. // NOLINTNEXTLINE(clang-analyzer-cplusplus.Move)
  37. REQUIRE(doc1.as<std::string>() == "null");
  38. }
  39. REQUIRE(spyingAllocator.log() == AllocatorLog{
  40. Allocate(sizeofStringBuffer()),
  41. Deallocate(sizeofStringBuffer()),
  42. });
  43. }
  44. SECTION("JsonDocument(JsonObject, Allocator*)") {
  45. JsonDocument doc1;
  46. JsonObject obj = doc1.to<JsonObject>();
  47. obj["hello"] = "world";
  48. JsonDocument doc2(obj, &spyingAllocator);
  49. REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
  50. REQUIRE(spyingAllocator.log() == AllocatorLog{
  51. Allocate(sizeofPool()),
  52. Allocate(sizeofString("hello")),
  53. Allocate(sizeofString("world")),
  54. });
  55. }
  56. SECTION("JsonDocument(JsonObject)") {
  57. JsonDocument doc1;
  58. JsonObject obj = doc1.to<JsonObject>();
  59. obj["hello"] = "world";
  60. JsonDocument doc2(obj);
  61. REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
  62. }
  63. SECTION("JsonDocument(JsonArray, Allocator*)") {
  64. JsonDocument doc1;
  65. JsonArray arr = doc1.to<JsonArray>();
  66. arr.add("hello");
  67. JsonDocument doc2(arr, &spyingAllocator);
  68. REQUIRE(doc2.as<std::string>() == "[\"hello\"]");
  69. REQUIRE(spyingAllocator.log() == AllocatorLog{
  70. Allocate(sizeofPool()),
  71. Allocate(sizeofString("hello")),
  72. });
  73. }
  74. SECTION("JsonDocument(JsonArray)") {
  75. JsonDocument doc1;
  76. JsonArray arr = doc1.to<JsonArray>();
  77. arr.add("hello");
  78. JsonDocument doc2(arr);
  79. REQUIRE(doc2.as<std::string>() == "[\"hello\"]");
  80. }
  81. SECTION("JsonDocument(JsonVariant, Allocator*)") {
  82. JsonDocument doc1;
  83. deserializeJson(doc1, "\"hello\"");
  84. JsonDocument doc2(doc1.as<JsonVariant>(), &spyingAllocator);
  85. REQUIRE(doc2.as<std::string>() == "hello");
  86. REQUIRE(spyingAllocator.log() == AllocatorLog{
  87. Allocate(sizeofString("hello")),
  88. });
  89. }
  90. SECTION("JsonDocument(JsonVariant)") {
  91. JsonDocument doc1;
  92. deserializeJson(doc1, "\"hello\"");
  93. JsonDocument doc2(doc1.as<JsonVariant>());
  94. REQUIRE(doc2.as<std::string>() == "hello");
  95. }
  96. SECTION("JsonDocument(JsonVariantConst)") {
  97. JsonDocument doc1;
  98. deserializeJson(doc1, "\"hello\"");
  99. JsonDocument doc2(doc1.as<JsonVariantConst>());
  100. REQUIRE(doc2.as<std::string>() == "hello");
  101. }
  102. SECTION("JsonDocument(ElementProxy)") {
  103. JsonDocument doc1;
  104. deserializeJson(doc1, "[\"hello\",\"world\"]");
  105. JsonDocument doc2(doc1[1]);
  106. REQUIRE(doc2.as<std::string>() == "world");
  107. }
  108. SECTION("JsonDocument(MemberProxy)") {
  109. JsonDocument doc1;
  110. deserializeJson(doc1, "{\"hello\":\"world\"}");
  111. JsonDocument doc2(doc1["hello"]);
  112. REQUIRE(doc2.as<std::string>() == "world");
  113. }
  114. }