JsonParser_String_Tests.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include <gtest/gtest.h>
  2. #include <ArduinoJson/StaticJsonBuffer.h>
  3. #include <ArduinoJson/JsonValue.h>
  4. class JsonParser_String_Tests : public testing::Test
  5. {
  6. protected:
  7. void whenInputIs(const char* json)
  8. {
  9. strcpy(_jsonString, json);
  10. _result = _jsonBuffer.parseValue(_jsonString);
  11. }
  12. void outputMustBe(const char* expected)
  13. {
  14. EXPECT_STREQ(expected, _result);
  15. }
  16. char _jsonString[256];
  17. StaticJsonBuffer<42> _jsonBuffer;
  18. const char* _result;
  19. };
  20. TEST_F(JsonParser_String_Tests, EmptyString)
  21. {
  22. whenInputIs("\"\"");
  23. outputMustBe("");
  24. }
  25. TEST_F(JsonParser_String_Tests, SimpleString)
  26. {
  27. whenInputIs("\"hello world\"");
  28. outputMustBe("hello world");
  29. }
  30. TEST_F(JsonParser_String_Tests, CurlyBraces)
  31. {
  32. whenInputIs("\"{hello:world}\"");
  33. outputMustBe("{hello:world}");
  34. }
  35. TEST_F(JsonParser_String_Tests, SquareBraquets)
  36. {
  37. whenInputIs("\"[hello,world]\"");
  38. outputMustBe("[hello,world]");
  39. }
  40. TEST_F(JsonParser_String_Tests, EscapedDoubleQuote)
  41. {
  42. whenInputIs("\"hello \\\"world\\\"\"");
  43. outputMustBe("hello \"world\"");
  44. }
  45. TEST_F(JsonParser_String_Tests, EscapedSingleQuote)
  46. {
  47. whenInputIs("\"hello \\\'world\\\'\"");
  48. outputMustBe("hello 'world'");
  49. }
  50. TEST_F(JsonParser_String_Tests, EscapedSolidus)
  51. {
  52. whenInputIs("\"hello \\/world\\/\"");
  53. outputMustBe("hello /world/");
  54. }
  55. TEST_F(JsonParser_String_Tests, EscapedReverseSolidus)
  56. {
  57. whenInputIs("\"hello \\\\world\\\\\"");
  58. outputMustBe("hello \\world\\");
  59. }
  60. TEST_F(JsonParser_String_Tests, EscapedBackspace)
  61. {
  62. whenInputIs("\"hello \\bworld\\b");
  63. outputMustBe("hello \bworld\b");
  64. }
  65. TEST_F(JsonParser_String_Tests, EscapedFormfeed)
  66. {
  67. whenInputIs("\"hello \\fworld\\f");
  68. outputMustBe("hello \fworld\f");
  69. }
  70. TEST_F(JsonParser_String_Tests, EscapedNewline)
  71. {
  72. whenInputIs("\"hello \\nworld\\n");
  73. outputMustBe("hello \nworld\n");
  74. }
  75. TEST_F(JsonParser_String_Tests, EscapedCarriageReturn)
  76. {
  77. whenInputIs("\"hello \\rworld\\r");
  78. outputMustBe("hello \rworld\r");
  79. }
  80. TEST_F(JsonParser_String_Tests, EscapedTab)
  81. {
  82. whenInputIs("\"hello \\tworld\\t");
  83. outputMustBe("hello \tworld\t");
  84. }
  85. TEST_F(JsonParser_String_Tests, AllEscapedCharsTogether)
  86. {
  87. whenInputIs("\"1\\\"2\\\\3\\/4\\b5\\f6\\n7\\r8\\t9\"");
  88. outputMustBe("1\"2\\3/4\b5\f6\n7\r8\t9");
  89. }