QuotedString_ExtractFrom_Tests.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #include <gtest/gtest.h>
  2. #include <ArduinoJson/Internals/QuotedString.h>
  3. using namespace ArduinoJson::Internals;
  4. class QuotedString_ExtractFrom_Tests : public testing::Test
  5. {
  6. protected:
  7. void whenInputIs(const char* json)
  8. {
  9. strcpy(_jsonString, json);
  10. _result = QuotedString::extractFrom(_jsonString, &_trailing);
  11. }
  12. void resultMustBe(const char* expected)
  13. {
  14. EXPECT_STREQ(expected, _result);
  15. }
  16. void trailingMustBe(const char* expected)
  17. {
  18. EXPECT_STREQ(expected, _trailing);
  19. }
  20. private:
  21. char _jsonString[256];
  22. char* _result;
  23. char* _trailing;
  24. };
  25. TEST_F(QuotedString_ExtractFrom_Tests, EmptyDoubleQuotedString)
  26. {
  27. whenInputIs("\"\"");
  28. resultMustBe("");
  29. trailingMustBe("");
  30. }
  31. TEST_F(QuotedString_ExtractFrom_Tests, EmptySingleQuotedString)
  32. {
  33. whenInputIs("''");
  34. resultMustBe("");
  35. trailingMustBe("");
  36. }
  37. TEST_F(QuotedString_ExtractFrom_Tests, SimpleDoubleQuotedString)
  38. {
  39. whenInputIs("\"hello world\"");
  40. resultMustBe("hello world");
  41. trailingMustBe("");
  42. }
  43. TEST_F(QuotedString_ExtractFrom_Tests, DoubleQuotedStringWithTrailing)
  44. {
  45. whenInputIs("\"hello\" world");
  46. resultMustBe("hello");
  47. trailingMustBe(" world");
  48. }
  49. TEST_F(QuotedString_ExtractFrom_Tests, SingleQuotedStringWithTrailing)
  50. {
  51. whenInputIs("'hello' world");
  52. resultMustBe("hello");
  53. trailingMustBe(" world");
  54. }
  55. TEST_F(QuotedString_ExtractFrom_Tests, CurlyBraces)
  56. {
  57. whenInputIs("\"{hello:world}\"");
  58. resultMustBe("{hello:world}");
  59. }
  60. TEST_F(QuotedString_ExtractFrom_Tests, SquareBraquets)
  61. {
  62. whenInputIs("\"[hello,world]\"");
  63. resultMustBe("[hello,world]");
  64. }
  65. TEST_F(QuotedString_ExtractFrom_Tests, EscapedDoubleQuote)
  66. {
  67. whenInputIs("\"hello \\\"world\\\"\"");
  68. resultMustBe("hello \"world\"");
  69. }
  70. TEST_F(QuotedString_ExtractFrom_Tests, EscapedSingleQuote)
  71. {
  72. whenInputIs("\"hello \\\'world\\\'\"");
  73. resultMustBe("hello 'world'");
  74. }
  75. TEST_F(QuotedString_ExtractFrom_Tests, EscapedSolidus)
  76. {
  77. whenInputIs("\"hello \\/world\\/\"");
  78. resultMustBe("hello /world/");
  79. }
  80. TEST_F(QuotedString_ExtractFrom_Tests, EscapedReverseSolidus)
  81. {
  82. whenInputIs("\"hello \\\\world\\\\\"");
  83. resultMustBe("hello \\world\\");
  84. }
  85. TEST_F(QuotedString_ExtractFrom_Tests, EscapedBackspace)
  86. {
  87. whenInputIs("\"hello \\bworld\\b\"");
  88. resultMustBe("hello \bworld\b");
  89. }
  90. TEST_F(QuotedString_ExtractFrom_Tests, EscapedFormfeed)
  91. {
  92. whenInputIs("\"hello \\fworld\\f\"");
  93. resultMustBe("hello \fworld\f");
  94. }
  95. TEST_F(QuotedString_ExtractFrom_Tests, EscapedNewline)
  96. {
  97. whenInputIs("\"hello \\nworld\\n\"");
  98. resultMustBe("hello \nworld\n");
  99. }
  100. TEST_F(QuotedString_ExtractFrom_Tests, EscapedCarriageReturn)
  101. {
  102. whenInputIs("\"hello \\rworld\\r\"");
  103. resultMustBe("hello \rworld\r");
  104. }
  105. TEST_F(QuotedString_ExtractFrom_Tests, EscapedTab)
  106. {
  107. whenInputIs("\"hello \\tworld\\t\"");
  108. resultMustBe("hello \tworld\t");
  109. }
  110. TEST_F(QuotedString_ExtractFrom_Tests, AllEscapedCharsTogether)
  111. {
  112. whenInputIs("\"1\\\"2\\\\3\\/4\\b5\\f6\\n7\\r8\\t9\"");
  113. resultMustBe("1\"2\\3/4\b5\f6\n7\r8\t9");
  114. }