JsonArray_PrettyPrintTo_Tests.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. size_t n = array.prettyPrintTo(_buffer, sizeof(_buffer));
  16. EXPECT_STREQ(expected, _buffer);
  17. EXPECT_EQ(strlen(expected), n);
  18. }
  19. private:
  20. char _buffer[256];
  21. };
  22. TEST_F(JsonArray_PrettyPrintTo_Tests, Empty) { outputMustBe("[]"); }
  23. TEST_F(JsonArray_PrettyPrintTo_Tests, OneElement) {
  24. array.add(1);
  25. outputMustBe(
  26. "[\r\n"
  27. " 1\r\n"
  28. "]");
  29. }
  30. TEST_F(JsonArray_PrettyPrintTo_Tests, TwoElements) {
  31. array.add(1);
  32. array.add(2);
  33. outputMustBe(
  34. "[\r\n"
  35. " 1,\r\n"
  36. " 2\r\n"
  37. "]");
  38. }
  39. TEST_F(JsonArray_PrettyPrintTo_Tests, EmptyNestedArrays) {
  40. array.createNestedArray();
  41. array.createNestedArray();
  42. outputMustBe(
  43. "[\r\n"
  44. " [],\r\n"
  45. " []\r\n"
  46. "]");
  47. }
  48. TEST_F(JsonArray_PrettyPrintTo_Tests, NestedArrays) {
  49. JsonArray& nested1 = array.createNestedArray();
  50. nested1.add(1);
  51. nested1.add(2);
  52. JsonObject& nested2 = array.createNestedObject();
  53. nested2["key"] = 3;
  54. outputMustBe(
  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. }