QuotedString_ExtractFrom_Tests.cpp 3.3 KB

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