JsonParser_String_Tests.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. private:
  17. char _jsonString[256];
  18. StaticJsonBuffer<42> _jsonBuffer;
  19. const char* _result;
  20. };
  21. TEST_F(JsonParser_String_Tests, EmptyDoubleQuotedString)
  22. {
  23. whenInputIs("\"\"");
  24. outputMustBe("");
  25. }
  26. TEST_F(JsonParser_String_Tests, EmptySingleQuotedString)
  27. {
  28. whenInputIs("''");
  29. outputMustBe("");
  30. }
  31. TEST_F(JsonParser_String_Tests, SimpleDoubleQuotedString)
  32. {
  33. whenInputIs("\"hello world\"");
  34. outputMustBe("hello world");
  35. }
  36. TEST_F(JsonParser_String_Tests, CurlyBraces)
  37. {
  38. whenInputIs("\"{hello:world}\"");
  39. outputMustBe("{hello:world}");
  40. }
  41. TEST_F(JsonParser_String_Tests, SquareBraquets)
  42. {
  43. whenInputIs("\"[hello,world]\"");
  44. outputMustBe("[hello,world]");
  45. }
  46. TEST_F(JsonParser_String_Tests, EscapedDoubleQuote)
  47. {
  48. whenInputIs("\"hello \\\"world\\\"\"");
  49. outputMustBe("hello \"world\"");
  50. }
  51. TEST_F(JsonParser_String_Tests, EscapedSingleQuote)
  52. {
  53. whenInputIs("\"hello \\\'world\\\'\"");
  54. outputMustBe("hello 'world'");
  55. }
  56. TEST_F(JsonParser_String_Tests, EscapedSolidus)
  57. {
  58. whenInputIs("\"hello \\/world\\/\"");
  59. outputMustBe("hello /world/");
  60. }
  61. TEST_F(JsonParser_String_Tests, EscapedReverseSolidus)
  62. {
  63. whenInputIs("\"hello \\\\world\\\\\"");
  64. outputMustBe("hello \\world\\");
  65. }
  66. TEST_F(JsonParser_String_Tests, EscapedBackspace)
  67. {
  68. whenInputIs("\"hello \\bworld\\b\"");
  69. outputMustBe("hello \bworld\b");
  70. }
  71. TEST_F(JsonParser_String_Tests, EscapedFormfeed)
  72. {
  73. whenInputIs("\"hello \\fworld\\f\"");
  74. outputMustBe("hello \fworld\f");
  75. }
  76. TEST_F(JsonParser_String_Tests, EscapedNewline)
  77. {
  78. whenInputIs("\"hello \\nworld\\n\"");
  79. outputMustBe("hello \nworld\n");
  80. }
  81. TEST_F(JsonParser_String_Tests, EscapedCarriageReturn)
  82. {
  83. whenInputIs("\"hello \\rworld\\r\"");
  84. outputMustBe("hello \rworld\r");
  85. }
  86. TEST_F(JsonParser_String_Tests, EscapedTab)
  87. {
  88. whenInputIs("\"hello \\tworld\\t\"");
  89. outputMustBe("hello \tworld\t");
  90. }
  91. TEST_F(JsonParser_String_Tests, AllEscapedCharsTogether)
  92. {
  93. whenInputIs("\"1\\\"2\\\\3\\/4\\b5\\f6\\n7\\r8\\t9\"");
  94. outputMustBe("1\"2\\3/4\b5\f6\n7\r8\t9");
  95. }