writeString.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2019
  3. // MIT License
  4. #include <catch.hpp>
  5. #include <ArduinoJson/Json/TextFormatter.hpp>
  6. #include <ArduinoJson/Serialization/Writers/StaticStringWriter.hpp>
  7. using namespace ARDUINOJSON_NAMESPACE;
  8. void check(const char* input, std::string expected) {
  9. char output[1024];
  10. StaticStringWriter sb(output, sizeof(output));
  11. TextFormatter<StaticStringWriter> writer(sb);
  12. writer.writeString(input);
  13. REQUIRE(expected == output);
  14. REQUIRE(writer.bytesWritten() == expected.size());
  15. }
  16. TEST_CASE("TextFormatter::writeString()") {
  17. SECTION("Null") {
  18. check(0, "null");
  19. }
  20. SECTION("EmptyString") {
  21. check("", "\"\"");
  22. }
  23. SECTION("QuotationMark") {
  24. check("\"", "\"\\\"\"");
  25. }
  26. SECTION("ReverseSolidus") {
  27. check("\\", "\"\\\\\"");
  28. }
  29. SECTION("Solidus") {
  30. check("/", "\"/\""); // but the JSON format allows \/
  31. }
  32. SECTION("Backspace") {
  33. check("\b", "\"\\b\"");
  34. }
  35. SECTION("Formfeed") {
  36. check("\f", "\"\\f\"");
  37. }
  38. SECTION("Newline") {
  39. check("\n", "\"\\n\"");
  40. }
  41. SECTION("CarriageReturn") {
  42. check("\r", "\"\\r\"");
  43. }
  44. SECTION("HorizontalTab") {
  45. check("\t", "\"\\t\"");
  46. }
  47. }