prettyPrintTo.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright Benoit Blanchon 2014-2017
  2. // MIT License
  3. //
  4. // Arduino JSON library
  5. // https://bblanchon.github.io/ArduinoJson/
  6. // If you like this project, please add a star!
  7. #include <ArduinoJson.h>
  8. #include <catch.hpp>
  9. static void check(JsonArray& array, std::string expected) {
  10. std::string actual;
  11. size_t actualLen = array.prettyPrintTo(actual);
  12. size_t measuredLen = array.measurePrettyLength();
  13. CHECK(actualLen == expected.size());
  14. CHECK(measuredLen == expected.size());
  15. REQUIRE(expected == actual);
  16. }
  17. TEST_CASE("JsonArray::prettyPrintTo()") {
  18. DynamicJsonBuffer jb;
  19. JsonArray& array = jb.createArray();
  20. SECTION("Empty") {
  21. check(array, "[]");
  22. }
  23. SECTION("OneElement") {
  24. array.add(1);
  25. check(array,
  26. "[\r\n"
  27. " 1\r\n"
  28. "]");
  29. }
  30. SECTION("TwoElements") {
  31. array.add(1);
  32. array.add(2);
  33. check(array,
  34. "[\r\n"
  35. " 1,\r\n"
  36. " 2\r\n"
  37. "]");
  38. }
  39. SECTION("EmptyNestedArrays") {
  40. array.createNestedArray();
  41. array.createNestedArray();
  42. check(array,
  43. "[\r\n"
  44. " [],\r\n"
  45. " []\r\n"
  46. "]");
  47. }
  48. SECTION("NestedArrays") {
  49. JsonArray& nested1 = array.createNestedArray();
  50. nested1.add(1);
  51. nested1.add(2);
  52. JsonObject& nested2 = array.createNestedObject();
  53. nested2["key"] = 3;
  54. check(array,
  55. "[\r\n"
  56. " [\r\n"
  57. " 1,\r\n"
  58. " 2\r\n"
  59. " ],\r\n"
  60. " {\r\n"
  61. " \"key\": 3\r\n"
  62. " }\r\n"
  63. "]");
  64. }
  65. }