Files
ArduinoJson/extras/tests/TextFormatter/writeString.cpp

58 lines
1.1 KiB
C++
Raw Normal View History

// ArduinoJson - https://arduinojson.org
2021-01-25 09:14:15 +01:00
// Copyright Benoit Blanchon 2014-2021
2014-10-23 23:39:22 +02:00
// MIT License
#include <catch.hpp>
2014-10-16 16:23:24 +02:00
#include <ArduinoJson/Json/TextFormatter.hpp>
2019-10-31 19:27:23 +01:00
#include <ArduinoJson/Serialization/Writers/StaticStringWriter.hpp>
2014-10-16 16:23:24 +02:00
using namespace ARDUINOJSON_NAMESPACE;
2014-10-16 16:23:24 +02:00
void check(const char* input, std::string expected) {
char output[64] = {0};
StaticStringWriter sb(output, sizeof(output));
TextFormatter<StaticStringWriter> writer(sb);
writer.writeString(input);
REQUIRE(expected == output);
REQUIRE(writer.bytesWritten() == expected.size());
}
2014-10-16 16:23:24 +02:00
TEST_CASE("TextFormatter::writeString()") {
SECTION("EmptyString") {
check("", "\"\"");
}
2014-10-16 16:23:24 +02:00
SECTION("QuotationMark") {
check("\"", "\"\\\"\"");
}
2014-10-16 16:23:24 +02:00
SECTION("ReverseSolidus") {
check("\\", "\"\\\\\"");
}
2014-10-16 16:23:24 +02:00
SECTION("Solidus") {
check("/", "\"/\""); // but the JSON format allows \/
}
2014-10-16 16:23:24 +02:00
SECTION("Backspace") {
check("\b", "\"\\b\"");
}
2014-10-16 16:23:24 +02:00
SECTION("Formfeed") {
check("\f", "\"\\f\"");
}
2014-10-16 16:23:24 +02:00
SECTION("Newline") {
check("\n", "\"\\n\"");
}
2014-10-16 16:23:24 +02:00
SECTION("CarriageReturn") {
check("\r", "\"\\r\"");
}
2014-10-16 16:23:24 +02:00
SECTION("HorizontalTab") {
check("\t", "\"\\t\"");
}
2014-10-23 23:45:36 +02:00
}