PrettyPrint_Array_Tests.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Arduino JSON library
  3. * Benoit Blanchon 2014 - MIT License
  4. */
  5. #include "CppUnitTest.h"
  6. #include "PrettyPrintDecorator.h"
  7. #include "StringBuilder.h"
  8. using namespace ArduinoJson::Internals;
  9. using namespace Microsoft::VisualStudio::CppUnitTestFramework;
  10. namespace JsonGeneratorTests
  11. {
  12. TEST_CLASS(PrettyPrint_Array_Tests)
  13. {
  14. char buffer[1024];
  15. size_t returnValue;
  16. public:
  17. TEST_METHOD(EmptyArray)
  18. {
  19. whenInputIs("[]");
  20. outputMustBe("[]");
  21. }
  22. TEST_METHOD(OneElement)
  23. {
  24. whenInputIs("[1]");
  25. outputMustBe(
  26. "[\n"
  27. " 1\n"
  28. "]");
  29. }
  30. TEST_METHOD(TwoElements)
  31. {
  32. whenInputIs("[1,2]");
  33. outputMustBe(
  34. "[\n"
  35. " 1,\n"
  36. " 2\n"
  37. "]");
  38. }
  39. TEST_METHOD(EmptyNestedArrays)
  40. {
  41. whenInputIs("[[],[]]");
  42. outputMustBe(
  43. "[\n"
  44. " [],\n"
  45. " []\n"
  46. "]");
  47. }
  48. TEST_METHOD(NestedArrays)
  49. {
  50. whenInputIs("[[1,2],[3,4]]");
  51. outputMustBe(
  52. "[\n"
  53. " [\n"
  54. " 1,\n"
  55. " 2\n"
  56. " ],\n"
  57. " [\n"
  58. " 3,\n"
  59. " 4\n"
  60. " ]\n"
  61. "]");
  62. }
  63. private:
  64. void whenInputIs(const char input[])
  65. {
  66. StringBuilder sb(buffer, sizeof(buffer));
  67. PrettyPrintDecorator decorator(sb);
  68. returnValue = decorator.print(input);
  69. }
  70. void outputMustBe(const char* expected)
  71. {
  72. Assert::AreEqual(expected, buffer);
  73. Assert::AreEqual(strlen(expected), returnValue);
  74. }
  75. };
  76. }