JsonArrayPretty.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2018
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. static void check(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. DynamicJsonArray array;
  16. SECTION("Empty") {
  17. check(array, "[]");
  18. }
  19. SECTION("OneElement") {
  20. array.add(1);
  21. check(array,
  22. "[\r\n"
  23. " 1\r\n"
  24. "]");
  25. }
  26. SECTION("TwoElements") {
  27. array.add(1);
  28. array.add(2);
  29. check(array,
  30. "[\r\n"
  31. " 1,\r\n"
  32. " 2\r\n"
  33. "]");
  34. }
  35. SECTION("EmptyNestedArrays") {
  36. array.createNestedArray();
  37. array.createNestedArray();
  38. check(array,
  39. "[\r\n"
  40. " [],\r\n"
  41. " []\r\n"
  42. "]");
  43. }
  44. SECTION("NestedArrays") {
  45. JsonArray& nested1 = array.createNestedArray();
  46. nested1.add(1);
  47. nested1.add(2);
  48. JsonObject& nested2 = array.createNestedObject();
  49. nested2["key"] = 3;
  50. check(array,
  51. "[\r\n"
  52. " [\r\n"
  53. " 1,\r\n"
  54. " 2\r\n"
  55. " ],\r\n"
  56. " {\r\n"
  57. " \"key\": 3\r\n"
  58. " }\r\n"
  59. "]");
  60. }
  61. }