PrettyPrint_Object_Tests.cpp 2.2 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 ArduinoJson::Generator;
  10. using namespace Microsoft::VisualStudio::CppUnitTestFramework;
  11. namespace JsonGeneratorTests
  12. {
  13. TEST_CLASS(PrettyPrint_Object_Tests)
  14. {
  15. char buffer[1024];
  16. size_t returnValue;
  17. public:
  18. TEST_METHOD(EmptyObject)
  19. {
  20. whenInputIs("{}");
  21. outputMustBe("{}");
  22. }
  23. TEST_METHOD(OneMember)
  24. {
  25. whenInputIs("{\"key\":\"value\"}");
  26. outputMustBe(
  27. "{\n"
  28. " \"key\": \"value\"\n"
  29. "}");
  30. }
  31. TEST_METHOD(TwoMembers)
  32. {
  33. whenInputIs("{\"key1\":\"value1\",\"key2\":\"value2\"}");
  34. outputMustBe(
  35. "{\n"
  36. " \"key1\": \"value1\",\n"
  37. " \"key2\": \"value2\"\n"
  38. "}");
  39. }
  40. TEST_METHOD(EmptyNestedObjects)
  41. {
  42. whenInputIs("{\"key1\":{},\"key2\":{}}");
  43. outputMustBe(
  44. "{\n"
  45. " \"key1\": {},\n"
  46. " \"key2\": {}\n"
  47. "}");
  48. }
  49. TEST_METHOD(NestedObjects)
  50. {
  51. whenInputIs("{\"key1\":{\"a\":1},\"key2\":{\"b\":2}}");
  52. outputMustBe(
  53. "{\n"
  54. " \"key1\": {\n"
  55. " \"a\": 1\n"
  56. " },\n"
  57. " \"key2\": {\n"
  58. " \"b\": 2\n"
  59. " }\n"
  60. "}");
  61. }
  62. private:
  63. void whenInputIs(const char input[])
  64. {
  65. StringBuilder sb(buffer, sizeof(buffer));
  66. IndentedPrint indentedPrint(sb);
  67. PrettyPrintDecorator decorator(indentedPrint);
  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. }