writeString.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2022, Benoit BLANCHON
  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[64] = {0};
  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("EmptyString") {
  18. check("", "\"\"");
  19. }
  20. SECTION("QuotationMark") {
  21. check("\"", "\"\\\"\"");
  22. }
  23. SECTION("ReverseSolidus") {
  24. check("\\", "\"\\\\\"");
  25. }
  26. SECTION("Solidus") {
  27. check("/", "\"/\""); // but the JSON format allows \/
  28. }
  29. SECTION("Backspace") {
  30. check("\b", "\"\\b\"");
  31. }
  32. SECTION("Formfeed") {
  33. check("\f", "\"\\f\"");
  34. }
  35. SECTION("Newline") {
  36. check("\n", "\"\\n\"");
  37. }
  38. SECTION("CarriageReturn") {
  39. check("\r", "\"\\r\"");
  40. }
  41. SECTION("HorizontalTab") {
  42. check("\t", "\"\\t\"");
  43. }
  44. }