2017-11-07 20:42:50 +01:00
|
|
|
// ArduinoJson - arduinojson.org
|
2023-03-16 17:50:34 +01:00
|
|
|
// Copyright Benoit Blanchon 2014-2023
|
2014-10-23 23:39:22 +02:00
|
|
|
// MIT License
|
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
#include <catch.hpp>
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2016-12-21 22:09:13 +01:00
|
|
|
#include <ArduinoJson/Serialization/JsonWriter.hpp>
|
|
|
|
#include <ArduinoJson/Serialization/StaticStringBuilder.hpp>
|
2014-10-16 16:23:24 +02:00
|
|
|
|
|
|
|
using namespace ArduinoJson::Internals;
|
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
void check(const char* input, std::string expected) {
|
|
|
|
char output[1024];
|
|
|
|
StaticStringBuilder sb(output, sizeof(output));
|
2017-04-22 11:33:40 +02:00
|
|
|
JsonWriter<StaticStringBuilder> writer(sb);
|
2017-04-18 18:22:24 +02:00
|
|
|
writer.writeString(input);
|
|
|
|
REQUIRE(expected == output);
|
|
|
|
REQUIRE(writer.bytesWritten() == expected.size());
|
|
|
|
}
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
TEST_CASE("JsonWriter::writeString()") {
|
|
|
|
SECTION("Null") {
|
|
|
|
check(0, "null");
|
2014-10-23 19:54:00 +02:00
|
|
|
}
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("EmptyString") {
|
|
|
|
check("", "\"\"");
|
|
|
|
}
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("QuotationMark") {
|
|
|
|
check("\"", "\"\\\"\"");
|
|
|
|
}
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("ReverseSolidus") {
|
|
|
|
check("\\", "\"\\\\\"");
|
|
|
|
}
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("Solidus") {
|
|
|
|
check("/", "\"/\""); // but the JSON format allows \/
|
|
|
|
}
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("Backspace") {
|
|
|
|
check("\b", "\"\\b\"");
|
|
|
|
}
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("Formfeed") {
|
|
|
|
check("\f", "\"\\f\"");
|
|
|
|
}
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("Newline") {
|
|
|
|
check("\n", "\"\\n\"");
|
|
|
|
}
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("CarriageReturn") {
|
|
|
|
check("\r", "\"\\r\"");
|
|
|
|
}
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("HorizontalTab") {
|
|
|
|
check("\t", "\"\\t\"");
|
|
|
|
}
|
2014-10-23 23:45:36 +02:00
|
|
|
}
|