EscapedStringTests.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include <gtest/gtest.h>
  2. #include <Internals/EscapedString.h>
  3. #include <Internals/StringBuilder.h>
  4. using namespace ArduinoJson::Internals;
  5. class EscapedStringTests : public testing::Test
  6. {
  7. protected:
  8. void whenInputIs(const char* input)
  9. {
  10. StringBuilder sb(buffer, sizeof(buffer));
  11. returnValue = EscapedString::printTo(input, &sb);
  12. }
  13. void outputMustBe(const char* expected)
  14. {
  15. EXPECT_STREQ(expected, buffer);
  16. EXPECT_EQ(strlen(expected), returnValue);
  17. }
  18. private:
  19. char buffer[1024];
  20. size_t returnValue;
  21. };
  22. TEST_F(EscapedStringTests, Null)
  23. {
  24. whenInputIs(0);
  25. outputMustBe("null");
  26. }
  27. TEST_F(EscapedStringTests, EmptyString)
  28. {
  29. whenInputIs("");
  30. outputMustBe("\"\"");
  31. }
  32. TEST_F(EscapedStringTests, QuotationMark)
  33. {
  34. whenInputIs("\"");
  35. outputMustBe("\"\\\"\"");
  36. }
  37. TEST_F(EscapedStringTests, ReverseSolidus)
  38. {
  39. whenInputIs("\\");
  40. outputMustBe("\"\\\\\"");
  41. }
  42. TEST_F(EscapedStringTests, Solidus)
  43. {
  44. whenInputIs("/");
  45. outputMustBe("\"/\""); // but the JSON format allows \/
  46. }
  47. TEST_F(EscapedStringTests, Backspace)
  48. {
  49. whenInputIs("\b");
  50. outputMustBe("\"\\b\"");
  51. }
  52. TEST_F(EscapedStringTests, Formfeed)
  53. {
  54. whenInputIs("\f");
  55. outputMustBe("\"\\f\"");
  56. }
  57. TEST_F(EscapedStringTests, Newline)
  58. {
  59. whenInputIs("\n");
  60. outputMustBe("\"\\n\"");
  61. }
  62. TEST_F(EscapedStringTests, CarriageReturn)
  63. {
  64. whenInputIs("\r");
  65. outputMustBe("\"\\r\"");
  66. }
  67. TEST_F(EscapedStringTests, HorizontalTab)
  68. {
  69. whenInputIs("\t");
  70. outputMustBe("\"\\t\"");
  71. }