QuotedString_PrintTo_Tests.cpp 1.7 KB

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