JsonArray_PrettyPrintTo_Tests.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. protected:
  13. JsonArray array;
  14. StaticJsonBuffer<30> json;
  15. virtual void SetUp() { array = json.createArray(); }
  16. void outputMustBe(const char *expected) {
  17. size_t n = array.prettyPrintTo(buffer, sizeof(buffer));
  18. EXPECT_STREQ(expected, buffer);
  19. EXPECT_EQ(strlen(expected), n);
  20. }
  21. private:
  22. char buffer[256];
  23. };
  24. TEST_F(JsonArray_PrettyPrintTo_Tests, Empty) { outputMustBe("[]"); }
  25. TEST_F(JsonArray_PrettyPrintTo_Tests, OneElement) {
  26. array.add(1);
  27. outputMustBe("[\r\n"
  28. " 1\r\n"
  29. "]");
  30. }
  31. TEST_F(JsonArray_PrettyPrintTo_Tests, TwoElements) {
  32. array.add(1);
  33. array.add(2);
  34. outputMustBe("[\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("[\r\n"
  43. " [],\r\n"
  44. " []\r\n"
  45. "]");
  46. }
  47. TEST_F(JsonArray_PrettyPrintTo_Tests, NestedArrays) {
  48. JsonArray nested1 = array.createNestedArray();
  49. nested1.add(1);
  50. nested1.add(2);
  51. JsonObject nested2 = array.createNestedObject();
  52. nested2["key"] = 3;
  53. outputMustBe("[\r\n"
  54. " [\r\n"
  55. " 1,\r\n"
  56. " 2\r\n"
  57. " ],\r\n"
  58. " {\r\n"
  59. " \"key\": 3\r\n"
  60. " }\r\n"
  61. "]");
  62. }