Files
ArduinoJson/test/JsonWriter/writeString.cpp

65 lines
1.3 KiB
C++
Raw Normal View History

// Copyright Benoit Blanchon 2014-2017
2014-10-23 23:39:22 +02:00
// MIT License
//
// Arduino JSON library
2017-03-25 22:05:06 +01:00
// https://bblanchon.github.io/ArduinoJson/
// If you like this project, please add a star!
2014-10-23 23:39:22 +02:00
#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
}