JsonArray_PrettyPrintTo_Tests.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Arduino JSON library
  3. * Benoit Blanchon 2014 - MIT License
  4. */
  5. #include <gtest/gtest.h>
  6. #include <JsonArray.h>
  7. #include <JsonObject.h>
  8. #include <JsonValue.h>
  9. #include <StaticJsonBuffer.h>
  10. class JsonArray_PrettyPrintTo_Tests : public testing::Test
  11. {
  12. protected:
  13. JsonArray array;
  14. StaticJsonBuffer<30> json;
  15. virtual void SetUp()
  16. {
  17. array = json.createArray();
  18. }
  19. void outputMustBe(const char* expected)
  20. {
  21. size_t n = array.prettyPrintTo(buffer, sizeof(buffer));
  22. EXPECT_STREQ(expected, buffer);
  23. EXPECT_EQ(strlen(expected), n);
  24. }
  25. private:
  26. char buffer[256];
  27. };
  28. TEST_F(JsonArray_PrettyPrintTo_Tests, Empty)
  29. {
  30. outputMustBe("[]");
  31. }
  32. TEST_F(JsonArray_PrettyPrintTo_Tests, OneElement)
  33. {
  34. array.add(1);
  35. outputMustBe(
  36. "[\r\n"
  37. " 1\r\n"
  38. "]");
  39. }
  40. TEST_F(JsonArray_PrettyPrintTo_Tests, TwoElements)
  41. {
  42. array.add(1);
  43. array.add(2);
  44. outputMustBe(
  45. "[\r\n"
  46. " 1,\r\n"
  47. " 2\r\n"
  48. "]");
  49. }
  50. TEST_F(JsonArray_PrettyPrintTo_Tests, EmptyNestedArrays)
  51. {
  52. array.createNestedArray();
  53. array.createNestedArray();
  54. outputMustBe(
  55. "[\r\n"
  56. " [],\r\n"
  57. " []\r\n"
  58. "]");
  59. }
  60. TEST_F(JsonArray_PrettyPrintTo_Tests, NestedArrays)
  61. {
  62. JsonArray nested1 = array.createNestedArray();
  63. nested1.add(1);
  64. nested1.add(2);
  65. JsonObject nested2 = array.createNestedObject();
  66. nested2["key"] = 3;
  67. outputMustBe(
  68. "[\r\n"
  69. " [\r\n"
  70. " 1,\r\n"
  71. " 2\r\n"
  72. " ],\r\n"
  73. " {\r\n"
  74. " \"key\": 3\r\n"
  75. " }\r\n"
  76. "]");
  77. }