JsonStringTests.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Arduino JSON library
  3. * Benoit Blanchon 2014 - MIT License
  4. */
  5. #include "CppUnitTest.h"
  6. #include "JsonParser.h"
  7. using namespace Microsoft::VisualStudio::CppUnitTestFramework;
  8. using namespace ArduinoJson::Parser;
  9. namespace ArduinoJsonParserTests
  10. {
  11. TEST_CLASS(JsonStringTests)
  12. {
  13. const char* actual;
  14. char json[256];
  15. JsonParser<32> parser;
  16. public:
  17. TEST_METHOD(EmptyString)
  18. {
  19. whenInputIs("");
  20. outputMustBe(0);
  21. }
  22. TEST_METHOD(JustOneQuote)
  23. {
  24. whenInputIs("\"");
  25. outputMustBe(0);
  26. }
  27. TEST_METHOD(SimpleString)
  28. {
  29. whenInputIs("\"Hi!\"");
  30. outputMustBe("Hi!");
  31. }
  32. TEST_METHOD(EscapedQuote)
  33. {
  34. whenInputIs("\"12\\\"34\""); // ie 12\"34
  35. outputMustBe("12\"34");
  36. }
  37. TEST_METHOD(EscapedReverseSolidus)
  38. {
  39. whenInputIs("\"12\\\\34\""); // ie 12\\34
  40. outputMustBe("12\\34");
  41. }
  42. TEST_METHOD(EscapedSolidus)
  43. {
  44. whenInputIs("\"12\\/34\"");
  45. outputMustBe("12/34");
  46. }
  47. TEST_METHOD(EscapedBackspace)
  48. {
  49. whenInputIs("\"12\\b34\"");
  50. outputMustBe("12\b34");
  51. }
  52. TEST_METHOD(EscapedFormfeed)
  53. {
  54. whenInputIs("\"12\\f34\"");
  55. outputMustBe("12\f34");
  56. }
  57. TEST_METHOD(EscapedNewline)
  58. {
  59. whenInputIs("\"12\\n34\"");
  60. outputMustBe("12\n34");
  61. }
  62. TEST_METHOD(EscapedCarriageReturn)
  63. {
  64. whenInputIs("\"12\\r34\"");
  65. outputMustBe("12\r34");
  66. }
  67. TEST_METHOD(EscapedTab)
  68. {
  69. whenInputIs("\"12\\t34\"");
  70. outputMustBe("12\t34");
  71. }
  72. TEST_METHOD(AllEscapedCharsTogether)
  73. {
  74. whenInputIs("\"1\\\"2\\\\3\\/4\\b5\\f6\\n7\\r8\\t9\"");
  75. outputMustBe("1\"2\\3/4\b5\f6\n7\r8\t9");
  76. }
  77. private:
  78. void whenInputIs(const char* input)
  79. {
  80. strcpy(json, input);
  81. actual = parser.parse(json);
  82. }
  83. void outputMustBe(const char* expected)
  84. {
  85. Assert::AreEqual(expected, actual);
  86. }
  87. };
  88. }