EscapedStringTests.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Arduino JSON library
  3. * Benoit Blanchon 2014 - MIT License
  4. */
  5. #include "CppUnitTest.h"
  6. #include "EscapedString.h"
  7. #include "StringBuilder.h"
  8. using namespace Microsoft::VisualStudio::CppUnitTestFramework;
  9. using namespace ArduinoJson::Internals;
  10. namespace JsonGeneratorTests
  11. {
  12. TEST_CLASS(EscapedStringTests)
  13. {
  14. char buffer[1024];
  15. size_t returnValue;
  16. public:
  17. TEST_METHOD(Null)
  18. {
  19. whenInputIs(0);
  20. outputMustBe("null");
  21. }
  22. TEST_METHOD(EmptyString)
  23. {
  24. whenInputIs("");
  25. outputMustBe("\"\"");
  26. }
  27. TEST_METHOD(QuotationMark)
  28. {
  29. whenInputIs("\"");
  30. outputMustBe("\"\\\"\"");
  31. }
  32. TEST_METHOD(ReverseSolidus)
  33. {
  34. whenInputIs("\\");
  35. outputMustBe("\"\\\\\"");
  36. }
  37. TEST_METHOD(Solidus)
  38. {
  39. whenInputIs("/");
  40. outputMustBe("\"/\""); // but the JSON format allows \/
  41. }
  42. TEST_METHOD(Backspace)
  43. {
  44. whenInputIs("\b");
  45. outputMustBe("\"\\b\"");
  46. }
  47. TEST_METHOD(Formfeed)
  48. {
  49. whenInputIs("\f");
  50. outputMustBe("\"\\f\"");
  51. }
  52. TEST_METHOD(Newline)
  53. {
  54. whenInputIs("\n");
  55. outputMustBe("\"\\n\"");
  56. }
  57. TEST_METHOD(CarriageReturn)
  58. {
  59. whenInputIs("\r");
  60. outputMustBe("\"\\r\"");
  61. }
  62. TEST_METHOD(HorizontalTab)
  63. {
  64. whenInputIs("\t");
  65. outputMustBe("\"\\t\"");
  66. }
  67. private:
  68. void whenInputIs(const char* input)
  69. {
  70. StringBuilder sb(buffer, sizeof(buffer));
  71. returnValue = EscapedString::printTo(input, sb);
  72. }
  73. void outputMustBe(const char* expected)
  74. {
  75. Assert::AreEqual(expected, buffer);
  76. Assert::AreEqual(strlen(expected), returnValue);
  77. }
  78. };
  79. }