JsonArray_PrettyPrintTo_Tests.cpp 1.6 KB

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