Files
ArduinoJson/test/JsonWriter/writeString.cpp

62 lines
1.2 KiB
C++
Raw Normal View History

// ArduinoJson - arduinojson.org
2018-01-05 09:20:01 +01:00
// Copyright Benoit Blanchon 2014-2018
2014-10-23 23:39:22 +02:00
// MIT License
#include <catch.hpp>
2014-10-16 16:23:24 +02:00
#include <ArduinoJson/Serialization/JsonWriter.hpp>
#include <ArduinoJson/Serialization/StaticStringBuilder.hpp>
2014-10-16 16:23:24 +02:00
using namespace ArduinoJson::Internals;
void check(const char* input, std::string expected) {
char output[1024];
StaticStringBuilder sb(output, sizeof(output));
JsonWriter<StaticStringBuilder> writer(sb);
writer.writeString(input);
REQUIRE(expected == output);
REQUIRE(writer.bytesWritten() == expected.size());
}
2014-10-16 16:23: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
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
}