StaticJsonDocument.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. doc1 = doc2;
  30. std::string json;
  31. serializeJson(doc1, json);
  32. REQUIRE(json == "{\"hello\":\"world\"}");
  33. }
  34. SECTION("Copy constructor") {
  35. StaticJsonDocument<200> doc1;
  36. deserializeJson(doc1, "{\"hello\":\"world\"}");
  37. StaticJsonDocument<200> doc2 = doc1;
  38. std::string json;
  39. serializeJson(doc2, json);
  40. REQUIRE(json == "{\"hello\":\"world\"}");
  41. }
  42. SECTION("Assign from StaticJsonDocument of different capacity") {
  43. StaticJsonDocument<200> doc1;
  44. StaticJsonDocument<300> doc2;
  45. doc1.to<JsonVariant>().set(666);
  46. deserializeJson(doc2, "{\"hello\":\"world\"}");
  47. doc1 = doc2;
  48. std::string json;
  49. serializeJson(doc1, json);
  50. REQUIRE(json == "{\"hello\":\"world\"}");
  51. }
  52. SECTION("Assign from DynamicJsonDocument") {
  53. StaticJsonDocument<200> doc1;
  54. DynamicJsonDocument doc2(4096);
  55. doc1.to<JsonVariant>().set(666);
  56. deserializeJson(doc2, "{\"hello\":\"world\"}");
  57. doc1 = doc2;
  58. std::string json;
  59. serializeJson(doc1, json);
  60. REQUIRE(json == "{\"hello\":\"world\"}");
  61. }
  62. SECTION("Construct from StaticJsonDocument of different size") {
  63. StaticJsonDocument<300> doc2;
  64. deserializeJson(doc2, "{\"hello\":\"world\"}");
  65. StaticJsonDocument<200> doc1 = doc2;
  66. std::string json;
  67. serializeJson(doc1, json);
  68. REQUIRE(json == "{\"hello\":\"world\"}");
  69. }
  70. SECTION("Construct from DynamicJsonDocument") {
  71. DynamicJsonDocument doc2(4096);
  72. deserializeJson(doc2, "{\"hello\":\"world\"}");
  73. StaticJsonDocument<200> doc1 = doc2;
  74. std::string json;
  75. serializeJson(doc1, json);
  76. REQUIRE(json == "{\"hello\":\"world\"}");
  77. }
  78. }