StaticJsonDocument.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2018
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. TEST_CASE("StaticJsonDocument") {
  7. SECTION("capacity()") {
  8. SECTION("matches template argument") {
  9. StaticJsonDocument<256> doc;
  10. REQUIRE(doc.capacity() == 256);
  11. }
  12. SECTION("rounds up template argument") {
  13. StaticJsonDocument<253> doc;
  14. REQUIRE(doc.capacity() == 256);
  15. }
  16. }
  17. SECTION("serializeJson()") {
  18. StaticJsonDocument<200> doc;
  19. JsonObject obj = doc.to<JsonObject>();
  20. obj["hello"] = "world";
  21. std::string json;
  22. serializeJson(doc, json);
  23. REQUIRE(json == "{\"hello\":\"world\"}");
  24. }
  25. SECTION("Copy assignment") {
  26. StaticJsonDocument<200> doc1, doc2;
  27. doc1.to<JsonVariant>().set(666);
  28. deserializeJson(doc2, "{\"hello\":\"world\"}");
  29. doc2.nestingLimit = 42;
  30. doc1 = doc2;
  31. std::string json;
  32. serializeJson(doc1, json);
  33. REQUIRE(json == "{\"hello\":\"world\"}");
  34. REQUIRE(doc1.nestingLimit == 42);
  35. }
  36. SECTION("Copy constructor") {
  37. StaticJsonDocument<200> doc1;
  38. deserializeJson(doc1, "{\"hello\":\"world\"}");
  39. doc1.nestingLimit = 42;
  40. StaticJsonDocument<200> doc2 = doc1;
  41. std::string json;
  42. serializeJson(doc2, json);
  43. REQUIRE(json == "{\"hello\":\"world\"}");
  44. REQUIRE(doc2.nestingLimit == 42);
  45. }
  46. SECTION("Assign from StaticJsonDocument of different capacity") {
  47. StaticJsonDocument<200> doc1;
  48. StaticJsonDocument<300> doc2;
  49. doc1.to<JsonVariant>().set(666);
  50. deserializeJson(doc2, "{\"hello\":\"world\"}");
  51. doc2.nestingLimit = 42;
  52. doc1 = doc2;
  53. std::string json;
  54. serializeJson(doc1, json);
  55. REQUIRE(json == "{\"hello\":\"world\"}");
  56. REQUIRE(doc1.nestingLimit == 42);
  57. }
  58. SECTION("Assign from DynamicJsonDocument") {
  59. StaticJsonDocument<200> doc1;
  60. DynamicJsonDocument doc2;
  61. doc1.to<JsonVariant>().set(666);
  62. deserializeJson(doc2, "{\"hello\":\"world\"}");
  63. doc2.nestingLimit = 42;
  64. doc1 = doc2;
  65. std::string json;
  66. serializeJson(doc1, json);
  67. REQUIRE(json == "{\"hello\":\"world\"}");
  68. REQUIRE(doc1.nestingLimit == 42);
  69. }
  70. SECTION("Construct from StaticJsonDocument of different size") {
  71. StaticJsonDocument<300> doc2;
  72. deserializeJson(doc2, "{\"hello\":\"world\"}");
  73. doc2.nestingLimit = 42;
  74. StaticJsonDocument<200> doc1 = doc2;
  75. std::string json;
  76. serializeJson(doc1, json);
  77. REQUIRE(json == "{\"hello\":\"world\"}");
  78. REQUIRE(doc1.nestingLimit == 42);
  79. }
  80. SECTION("Construct from DynamicJsonDocument") {
  81. DynamicJsonDocument doc2;
  82. deserializeJson(doc2, "{\"hello\":\"world\"}");
  83. doc2.nestingLimit = 42;
  84. StaticJsonDocument<200> doc1 = doc2;
  85. std::string json;
  86. serializeJson(doc1, json);
  87. REQUIRE(json == "{\"hello\":\"world\"}");
  88. REQUIRE(doc1.nestingLimit == 42);
  89. }
  90. }