EscapedStringTests.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. EscapedString escapedString;
  17. public:
  18. TEST_METHOD(Null)
  19. {
  20. whenInputIs(0);
  21. outputMustBe("null");
  22. }
  23. TEST_METHOD(EmptyString)
  24. {
  25. whenInputIs("");
  26. outputMustBe("\"\"");
  27. }
  28. TEST_METHOD(QuotationMark)
  29. {
  30. whenInputIs("\"");
  31. outputMustBe("\"\\\"\"");
  32. }
  33. TEST_METHOD(ReverseSolidus)
  34. {
  35. whenInputIs("\\");
  36. outputMustBe("\"\\\\\"");
  37. }
  38. TEST_METHOD(Solidus)
  39. {
  40. whenInputIs("/");
  41. outputMustBe("\"/\""); // but the JSON format allows \/
  42. }
  43. TEST_METHOD(Backspace)
  44. {
  45. whenInputIs("\b");
  46. outputMustBe("\"\\b\"");
  47. }
  48. TEST_METHOD(Formfeed)
  49. {
  50. whenInputIs("\f");
  51. outputMustBe("\"\\f\"");
  52. }
  53. TEST_METHOD(Newline)
  54. {
  55. whenInputIs("\n");
  56. outputMustBe("\"\\n\"");
  57. }
  58. TEST_METHOD(CarriageReturn)
  59. {
  60. whenInputIs("\r");
  61. outputMustBe("\"\\r\"");
  62. }
  63. TEST_METHOD(HorizontalTab)
  64. {
  65. whenInputIs("\t");
  66. outputMustBe("\"\\t\"");
  67. }
  68. private:
  69. void whenInputIs(const char* input)
  70. {
  71. StringBuilder sb(buffer, sizeof(buffer));
  72. escapedString.set(input);
  73. returnValue = escapedString.printTo(sb);
  74. }
  75. void outputMustBe(const char* expected)
  76. {
  77. Assert::AreEqual(expected, buffer);
  78. Assert::AreEqual(strlen(expected), returnValue);
  79. }
  80. };
  81. }