PrettyPrint_Array_Tests.cpp 2.0 KB

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