JsonArray_PrettyPrintTo_Tests.cpp 1.6 KB

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