QuotedString_PrintTo_Tests.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include <gtest/gtest.h>
  2. #include <ArduinoJson/Internals/QuotedString.h>
  3. #include <ArduinoJson/Internals/StringBuilder.h>
  4. using namespace ArduinoJson::Internals;
  5. class QuotedString_PrintTo_Tests : public testing::Test
  6. {
  7. protected:
  8. void whenInputIs(const char* input)
  9. {
  10. StringBuilder sb(buffer, sizeof(buffer));
  11. returnValue = QuotedString::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(QuotedString_PrintTo_Tests, Null)
  23. {
  24. whenInputIs(0);
  25. outputMustBe("null");
  26. }
  27. TEST_F(QuotedString_PrintTo_Tests, EmptyString)
  28. {
  29. whenInputIs("");
  30. outputMustBe("\"\"");
  31. }
  32. TEST_F(QuotedString_PrintTo_Tests, QuotationMark)
  33. {
  34. whenInputIs("\"");
  35. outputMustBe("\"\\\"\"");
  36. }
  37. TEST_F(QuotedString_PrintTo_Tests, ReverseSolidus)
  38. {
  39. whenInputIs("\\");
  40. outputMustBe("\"\\\\\"");
  41. }
  42. TEST_F(QuotedString_PrintTo_Tests, Solidus)
  43. {
  44. whenInputIs("/");
  45. outputMustBe("\"/\""); // but the JSON format allows \/
  46. }
  47. TEST_F(QuotedString_PrintTo_Tests, Backspace)
  48. {
  49. whenInputIs("\b");
  50. outputMustBe("\"\\b\"");
  51. }
  52. TEST_F(QuotedString_PrintTo_Tests, Formfeed)
  53. {
  54. whenInputIs("\f");
  55. outputMustBe("\"\\f\"");
  56. }
  57. TEST_F(QuotedString_PrintTo_Tests, Newline)
  58. {
  59. whenInputIs("\n");
  60. outputMustBe("\"\\n\"");
  61. }
  62. TEST_F(QuotedString_PrintTo_Tests, CarriageReturn)
  63. {
  64. whenInputIs("\r");
  65. outputMustBe("\"\\r\"");
  66. }
  67. TEST_F(QuotedString_PrintTo_Tests, HorizontalTab)
  68. {
  69. whenInputIs("\t");
  70. outputMustBe("\"\\t\"");
  71. }