constructor.cpp 4.2 KB

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