JsonArray_PrettyPrintTo_Tests.cpp 1.7 KB

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