JsonArray_PrettyPrintTo_Tests.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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(
  28. "[\r\n"
  29. " 1\r\n"
  30. "]");
  31. }
  32. TEST_F(JsonArray_PrettyPrintTo_Tests, TwoElements) {
  33. array.add(1);
  34. array.add(2);
  35. outputMustBe(
  36. "[\r\n"
  37. " 1,\r\n"
  38. " 2\r\n"
  39. "]");
  40. }
  41. TEST_F(JsonArray_PrettyPrintTo_Tests, EmptyNestedArrays) {
  42. array.createNestedArray();
  43. array.createNestedArray();
  44. outputMustBe(
  45. "[\r\n"
  46. " [],\r\n"
  47. " []\r\n"
  48. "]");
  49. }
  50. TEST_F(JsonArray_PrettyPrintTo_Tests, NestedArrays) {
  51. JsonArray nested1 = array.createNestedArray();
  52. nested1.add(1);
  53. nested1.add(2);
  54. JsonObject nested2 = array.createNestedObject();
  55. nested2["key"] = 3;
  56. outputMustBe(
  57. "[\r\n"
  58. " [\r\n"
  59. " 1,\r\n"
  60. " 2\r\n"
  61. " ],\r\n"
  62. " {\r\n"
  63. " \"key\": 3\r\n"
  64. " }\r\n"
  65. "]");
  66. }