QuotedString_ExtractFrom_Tests.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #include <gtest/gtest.h>
  2. #include <ArduinoJson/Internals/QuotedString.hpp>
  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, NoQuotes)
  32. {
  33. whenInputIs("hello world");
  34. resultMustBe(0);
  35. }
  36. TEST_F(QuotedString_ExtractFrom_Tests, EmptySingleQuotedString)
  37. {
  38. whenInputIs("''");
  39. resultMustBe("");
  40. trailingMustBe("");
  41. }
  42. TEST_F(QuotedString_ExtractFrom_Tests, SimpleDoubleQuotedString)
  43. {
  44. whenInputIs("\"hello world\"");
  45. resultMustBe("hello world");
  46. trailingMustBe("");
  47. }
  48. TEST_F(QuotedString_ExtractFrom_Tests, DoubleQuotedStringWithTrailing)
  49. {
  50. whenInputIs("\"hello\" world");
  51. resultMustBe("hello");
  52. trailingMustBe(" world");
  53. }
  54. TEST_F(QuotedString_ExtractFrom_Tests, SingleQuotedStringWithTrailing)
  55. {
  56. whenInputIs("'hello' world");
  57. resultMustBe("hello");
  58. trailingMustBe(" world");
  59. }
  60. TEST_F(QuotedString_ExtractFrom_Tests, CurlyBraces)
  61. {
  62. whenInputIs("\"{hello:world}\"");
  63. resultMustBe("{hello:world}");
  64. }
  65. TEST_F(QuotedString_ExtractFrom_Tests, SquareBraquets)
  66. {
  67. whenInputIs("\"[hello,world]\"");
  68. resultMustBe("[hello,world]");
  69. }
  70. TEST_F(QuotedString_ExtractFrom_Tests, EscapedDoubleQuote)
  71. {
  72. whenInputIs("\"hello \\\"world\\\"\"");
  73. resultMustBe("hello \"world\"");
  74. }
  75. TEST_F(QuotedString_ExtractFrom_Tests, EscapedSingleQuote)
  76. {
  77. whenInputIs("\"hello \\\'world\\\'\"");
  78. resultMustBe("hello 'world'");
  79. }
  80. TEST_F(QuotedString_ExtractFrom_Tests, EscapedSolidus)
  81. {
  82. whenInputIs("\"hello \\/world\\/\"");
  83. resultMustBe("hello /world/");
  84. }
  85. TEST_F(QuotedString_ExtractFrom_Tests, EscapedReverseSolidus)
  86. {
  87. whenInputIs("\"hello \\\\world\\\\\"");
  88. resultMustBe("hello \\world\\");
  89. }
  90. TEST_F(QuotedString_ExtractFrom_Tests, EscapedBackspace)
  91. {
  92. whenInputIs("\"hello \\bworld\\b\"");
  93. resultMustBe("hello \bworld\b");
  94. }
  95. TEST_F(QuotedString_ExtractFrom_Tests, EscapedFormfeed)
  96. {
  97. whenInputIs("\"hello \\fworld\\f\"");
  98. resultMustBe("hello \fworld\f");
  99. }
  100. TEST_F(QuotedString_ExtractFrom_Tests, EscapedNewline)
  101. {
  102. whenInputIs("\"hello \\nworld\\n\"");
  103. resultMustBe("hello \nworld\n");
  104. }
  105. TEST_F(QuotedString_ExtractFrom_Tests, EscapedCarriageReturn)
  106. {
  107. whenInputIs("\"hello \\rworld\\r\"");
  108. resultMustBe("hello \rworld\r");
  109. }
  110. TEST_F(QuotedString_ExtractFrom_Tests, EscapedTab)
  111. {
  112. whenInputIs("\"hello \\tworld\\t\"");
  113. resultMustBe("hello \tworld\t");
  114. }
  115. TEST_F(QuotedString_ExtractFrom_Tests, AllEscapedCharsTogether)
  116. {
  117. whenInputIs("\"1\\\"2\\\\3\\/4\\b5\\f6\\n7\\r8\\t9\"");
  118. resultMustBe("1\"2\\3/4\b5\f6\n7\r8\t9");
  119. }