JsonArrayPretty.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2022, Benoit BLANCHON
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. static void checkArray(JsonArray array, std::string expected) {
  7. std::string actual;
  8. size_t actualLen = serializeJsonPretty(array, actual);
  9. size_t measuredLen = measureJsonPretty(array);
  10. CHECK(actualLen == expected.size());
  11. CHECK(measuredLen == expected.size());
  12. REQUIRE(expected == actual);
  13. }
  14. TEST_CASE("serializeJsonPretty(JsonArray)") {
  15. DynamicJsonDocument doc(4096);
  16. JsonArray array = doc.to<JsonArray>();
  17. SECTION("Empty") {
  18. checkArray(array, "[]");
  19. }
  20. SECTION("OneElement") {
  21. array.add(1);
  22. checkArray(array,
  23. "[\r\n"
  24. " 1\r\n"
  25. "]");
  26. }
  27. SECTION("TwoElements") {
  28. array.add(1);
  29. array.add(2);
  30. checkArray(array,
  31. "[\r\n"
  32. " 1,\r\n"
  33. " 2\r\n"
  34. "]");
  35. }
  36. SECTION("EmptyNestedArrays") {
  37. array.createNestedArray();
  38. array.createNestedArray();
  39. checkArray(array,
  40. "[\r\n"
  41. " [],\r\n"
  42. " []\r\n"
  43. "]");
  44. }
  45. SECTION("NestedArrays") {
  46. JsonArray nested1 = array.createNestedArray();
  47. nested1.add(1);
  48. nested1.add(2);
  49. JsonObject nested2 = array.createNestedObject();
  50. nested2["key"] = 3;
  51. checkArray(array,
  52. "[\r\n"
  53. " [\r\n"
  54. " 1,\r\n"
  55. " 2\r\n"
  56. " ],\r\n"
  57. " {\r\n"
  58. " \"key\": 3\r\n"
  59. " }\r\n"
  60. "]");
  61. }
  62. }