writeString.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright Benoit Blanchon 2014-2017
  2. // MIT License
  3. //
  4. // Arduino JSON library
  5. // https://bblanchon.github.io/ArduinoJson/
  6. // If you like this project, please add a star!
  7. #include <catch.hpp>
  8. #include <ArduinoJson/Serialization/JsonWriter.hpp>
  9. #include <ArduinoJson/Serialization/StaticStringBuilder.hpp>
  10. using namespace ArduinoJson::Internals;
  11. void check(const char* input, std::string expected) {
  12. char output[1024];
  13. StaticStringBuilder sb(output, sizeof(output));
  14. JsonWriter writer(sb);
  15. writer.writeString(input);
  16. REQUIRE(expected == output);
  17. REQUIRE(writer.bytesWritten() == expected.size());
  18. }
  19. TEST_CASE("JsonWriter::writeString()") {
  20. SECTION("Null") {
  21. check(0, "null");
  22. }
  23. SECTION("EmptyString") {
  24. check("", "\"\"");
  25. }
  26. SECTION("QuotationMark") {
  27. check("\"", "\"\\\"\"");
  28. }
  29. SECTION("ReverseSolidus") {
  30. check("\\", "\"\\\\\"");
  31. }
  32. SECTION("Solidus") {
  33. check("/", "\"/\""); // but the JSON format allows \/
  34. }
  35. SECTION("Backspace") {
  36. check("\b", "\"\\b\"");
  37. }
  38. SECTION("Formfeed") {
  39. check("\f", "\"\\f\"");
  40. }
  41. SECTION("Newline") {
  42. check("\n", "\"\\n\"");
  43. }
  44. SECTION("CarriageReturn") {
  45. check("\r", "\"\\r\"");
  46. }
  47. SECTION("HorizontalTab") {
  48. check("\t", "\"\\t\"");
  49. }
  50. }