constructor.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. });
  53. }
  54. SECTION("JsonDocument(JsonObject)") {
  55. JsonDocument doc1;
  56. JsonObject obj = doc1.to<JsonObject>();
  57. obj["hello"] = "world";
  58. JsonDocument doc2(obj);
  59. REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
  60. }
  61. SECTION("JsonDocument(JsonArray, Allocator*)") {
  62. JsonDocument doc1;
  63. JsonArray arr = doc1.to<JsonArray>();
  64. arr.add("hello");
  65. JsonDocument doc2(arr, &spyingAllocator);
  66. REQUIRE(doc2.as<std::string>() == "[\"hello\"]");
  67. REQUIRE(spyingAllocator.log() == AllocatorLog{
  68. Allocate(sizeofPool()),
  69. });
  70. }
  71. SECTION("JsonDocument(JsonArray)") {
  72. JsonDocument doc1;
  73. JsonArray arr = doc1.to<JsonArray>();
  74. arr.add("hello");
  75. JsonDocument doc2(arr);
  76. REQUIRE(doc2.as<std::string>() == "[\"hello\"]");
  77. }
  78. SECTION("JsonDocument(JsonVariant, Allocator*)") {
  79. JsonDocument doc1;
  80. deserializeJson(doc1, "\"hello\"");
  81. JsonDocument doc2(doc1.as<JsonVariant>(), &spyingAllocator);
  82. REQUIRE(doc2.as<std::string>() == "hello");
  83. REQUIRE(spyingAllocator.log() == AllocatorLog{
  84. Allocate(sizeofString("hello")),
  85. });
  86. }
  87. SECTION("JsonDocument(JsonVariant)") {
  88. JsonDocument doc1;
  89. deserializeJson(doc1, "\"hello\"");
  90. JsonDocument doc2(doc1.as<JsonVariant>());
  91. REQUIRE(doc2.as<std::string>() == "hello");
  92. }
  93. SECTION("JsonDocument(JsonVariantConst)") {
  94. JsonDocument doc1;
  95. deserializeJson(doc1, "\"hello\"");
  96. JsonDocument doc2(doc1.as<JsonVariantConst>());
  97. REQUIRE(doc2.as<std::string>() == "hello");
  98. }
  99. SECTION("JsonDocument(ElementProxy)") {
  100. JsonDocument doc1;
  101. deserializeJson(doc1, "[\"hello\",\"world\"]");
  102. JsonDocument doc2(doc1[1]);
  103. REQUIRE(doc2.as<std::string>() == "world");
  104. }
  105. SECTION("JsonDocument(MemberProxy)") {
  106. JsonDocument doc1;
  107. deserializeJson(doc1, "{\"hello\":\"world\"}");
  108. JsonDocument doc2(doc1["hello"]);
  109. REQUIRE(doc2.as<std::string>() == "world");
  110. }
  111. }