prettyPrintTo.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. #include <string>
  10. void check(const JsonObject &obj, const std::string expected) {
  11. char json[256];
  12. size_t actualLen = obj.prettyPrintTo(json);
  13. size_t measuredLen = obj.measurePrettyLength();
  14. REQUIRE(json == expected);
  15. REQUIRE(expected.size() == actualLen);
  16. REQUIRE(expected.size() == measuredLen);
  17. }
  18. TEST_CASE("JsonObject::prettyPrintTo()") {
  19. DynamicJsonBuffer jb;
  20. JsonObject &obj = jb.createObject();
  21. SECTION("EmptyObject") {
  22. check(obj, "{}");
  23. }
  24. SECTION("OneMember") {
  25. obj["key"] = "value";
  26. check(obj,
  27. "{\r\n"
  28. " \"key\": \"value\"\r\n"
  29. "}");
  30. }
  31. SECTION("TwoMembers") {
  32. obj["key1"] = "value1";
  33. obj["key2"] = "value2";
  34. check(obj,
  35. "{\r\n"
  36. " \"key1\": \"value1\",\r\n"
  37. " \"key2\": \"value2\"\r\n"
  38. "}");
  39. }
  40. SECTION("EmptyNestedContainers") {
  41. obj.createNestedObject("key1");
  42. obj.createNestedArray("key2");
  43. check(obj,
  44. "{\r\n"
  45. " \"key1\": {},\r\n"
  46. " \"key2\": []\r\n"
  47. "}");
  48. }
  49. SECTION("NestedContainers") {
  50. JsonObject &nested1 = obj.createNestedObject("key1");
  51. nested1["a"] = 1;
  52. JsonArray &nested2 = obj.createNestedArray("key2");
  53. nested2.add(2);
  54. check(obj,
  55. "{\r\n"
  56. " \"key1\": {\r\n"
  57. " \"a\": 1\r\n"
  58. " },\r\n"
  59. " \"key2\": [\r\n"
  60. " 2\r\n"
  61. " ]\r\n"
  62. "}");
  63. }
  64. }