constructor.cpp 4.2 KB

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