JsonArray_PrettyPrintTo_Tests.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright Benoit Blanchon 2014-2017
  2. // MIT License
  3. //
  4. // Arduino JSON library
  5. // https://github.com/bblanchon/ArduinoJson
  6. // If you like this project, please add a star!
  7. #include <ArduinoJson.h>
  8. #include <gtest/gtest.h>
  9. class JsonArray_PrettyPrintTo_Tests : public testing::Test {
  10. public:
  11. JsonArray_PrettyPrintTo_Tests() : array(jsonBuffer.createArray()) {}
  12. protected:
  13. DynamicJsonBuffer jsonBuffer;
  14. JsonArray& array;
  15. void outputMustBe(const char* expected) {
  16. char actual[256];
  17. size_t actualLen = array.prettyPrintTo(actual);
  18. size_t measuredLen = array.measurePrettyLength();
  19. EXPECT_STREQ(expected, actual);
  20. EXPECT_EQ(strlen(expected), actualLen);
  21. EXPECT_EQ(strlen(expected), measuredLen);
  22. }
  23. };
  24. TEST_F(JsonArray_PrettyPrintTo_Tests, Empty) {
  25. outputMustBe("[]");
  26. }
  27. TEST_F(JsonArray_PrettyPrintTo_Tests, OneElement) {
  28. array.add(1);
  29. outputMustBe(
  30. "[\r\n"
  31. " 1\r\n"
  32. "]");
  33. }
  34. TEST_F(JsonArray_PrettyPrintTo_Tests, TwoElements) {
  35. array.add(1);
  36. array.add(2);
  37. outputMustBe(
  38. "[\r\n"
  39. " 1,\r\n"
  40. " 2\r\n"
  41. "]");
  42. }
  43. TEST_F(JsonArray_PrettyPrintTo_Tests, EmptyNestedArrays) {
  44. array.createNestedArray();
  45. array.createNestedArray();
  46. outputMustBe(
  47. "[\r\n"
  48. " [],\r\n"
  49. " []\r\n"
  50. "]");
  51. }
  52. TEST_F(JsonArray_PrettyPrintTo_Tests, NestedArrays) {
  53. JsonArray& nested1 = array.createNestedArray();
  54. nested1.add(1);
  55. nested1.add(2);
  56. JsonObject& nested2 = array.createNestedObject();
  57. nested2["key"] = 3;
  58. outputMustBe(
  59. "[\r\n"
  60. " [\r\n"
  61. " 1,\r\n"
  62. " 2\r\n"
  63. " ],\r\n"
  64. " {\r\n"
  65. " \"key\": 3\r\n"
  66. " }\r\n"
  67. "]");
  68. }