JsonArray_PrettyPrintTo_Tests.cpp 1.7 KB

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