QuotedString_PrintTo_Tests.cpp 1.6 KB

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