JsonArray_PrettyPrintTo_Tests.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright Benoit Blanchon 2014-2016
  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 <gtest/gtest.h>
  8. #include <ArduinoJson.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, sizeof(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) { outputMustBe("[]"); }
  25. TEST_F(JsonArray_PrettyPrintTo_Tests, OneElement) {
  26. array.add(1);
  27. outputMustBe(
  28. "[\r\n"
  29. " 1\r\n"
  30. "]");
  31. }
  32. TEST_F(JsonArray_PrettyPrintTo_Tests, TwoElements) {
  33. array.add(1);
  34. array.add(2);
  35. outputMustBe(
  36. "[\r\n"
  37. " 1,\r\n"
  38. " 2\r\n"
  39. "]");
  40. }
  41. TEST_F(JsonArray_PrettyPrintTo_Tests, EmptyNestedArrays) {
  42. array.createNestedArray();
  43. array.createNestedArray();
  44. outputMustBe(
  45. "[\r\n"
  46. " [],\r\n"
  47. " []\r\n"
  48. "]");
  49. }
  50. TEST_F(JsonArray_PrettyPrintTo_Tests, NestedArrays) {
  51. JsonArray& nested1 = array.createNestedArray();
  52. nested1.add(1);
  53. nested1.add(2);
  54. JsonObject& nested2 = array.createNestedObject();
  55. nested2["key"] = 3;
  56. outputMustBe(
  57. "[\r\n"
  58. " [\r\n"
  59. " 1,\r\n"
  60. " 2\r\n"
  61. " ],\r\n"
  62. " {\r\n"
  63. " \"key\": 3\r\n"
  64. " }\r\n"
  65. "]");
  66. }