QuotedString_ExtractFrom_Tests.cpp 3.0 KB

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