2017-01-06 21:07:34 +01:00
|
|
|
// 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/
|
2016-01-07 22:35:12 +01:00
|
|
|
// If you like this project, please add a star!
|
2014-10-23 23:39:22 +02:00
|
|
|
|
2014-10-16 16:23:24 +02:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
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;
|
|
|
|
|
2015-07-10 22:11:26 +02:00
|
|
|
class JsonWriter_WriteString_Tests : public testing::Test {
|
2014-10-23 23:13:13 +02:00
|
|
|
protected:
|
2014-10-23 19:54:00 +02:00
|
|
|
void whenInputIs(const char *input) {
|
2015-08-10 17:22:22 +02:00
|
|
|
StaticStringBuilder sb(buffer, sizeof(buffer));
|
2015-07-10 22:11:26 +02:00
|
|
|
JsonWriter writer(sb);
|
|
|
|
writer.writeString(input);
|
|
|
|
returnValue = writer.bytesWritten();
|
2014-10-23 19:54:00 +02:00
|
|
|
}
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
void outputMustBe(const char *expected) {
|
|
|
|
EXPECT_STREQ(expected, buffer);
|
|
|
|
EXPECT_EQ(strlen(expected), returnValue);
|
|
|
|
}
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 23:13:13 +02:00
|
|
|
private:
|
2014-10-23 19:54:00 +02:00
|
|
|
char buffer[1024];
|
|
|
|
size_t returnValue;
|
2014-10-16 16:23:24 +02:00
|
|
|
};
|
|
|
|
|
2015-07-10 22:11:26 +02:00
|
|
|
TEST_F(JsonWriter_WriteString_Tests, Null) {
|
2014-10-23 19:54:00 +02:00
|
|
|
whenInputIs(0);
|
|
|
|
outputMustBe("null");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2015-07-10 22:11:26 +02:00
|
|
|
TEST_F(JsonWriter_WriteString_Tests, EmptyString) {
|
2014-10-23 19:54:00 +02:00
|
|
|
whenInputIs("");
|
|
|
|
outputMustBe("\"\"");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2015-07-10 22:11:26 +02:00
|
|
|
TEST_F(JsonWriter_WriteString_Tests, QuotationMark) {
|
2014-10-23 19:54:00 +02:00
|
|
|
whenInputIs("\"");
|
|
|
|
outputMustBe("\"\\\"\"");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2015-07-10 22:11:26 +02:00
|
|
|
TEST_F(JsonWriter_WriteString_Tests, ReverseSolidus) {
|
2014-10-23 19:54:00 +02:00
|
|
|
whenInputIs("\\");
|
|
|
|
outputMustBe("\"\\\\\"");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2015-07-10 22:11:26 +02:00
|
|
|
TEST_F(JsonWriter_WriteString_Tests, Solidus) {
|
2014-10-23 19:54:00 +02:00
|
|
|
whenInputIs("/");
|
2014-10-23 23:13:13 +02:00
|
|
|
outputMustBe("\"/\""); // but the JSON format allows \/
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2015-07-10 22:11:26 +02:00
|
|
|
TEST_F(JsonWriter_WriteString_Tests, Backspace) {
|
2014-10-23 19:54:00 +02:00
|
|
|
whenInputIs("\b");
|
|
|
|
outputMustBe("\"\\b\"");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2015-07-10 22:11:26 +02:00
|
|
|
TEST_F(JsonWriter_WriteString_Tests, Formfeed) {
|
2014-10-23 19:54:00 +02:00
|
|
|
whenInputIs("\f");
|
|
|
|
outputMustBe("\"\\f\"");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2015-07-10 22:11:26 +02:00
|
|
|
TEST_F(JsonWriter_WriteString_Tests, Newline) {
|
2014-10-23 19:54:00 +02:00
|
|
|
whenInputIs("\n");
|
|
|
|
outputMustBe("\"\\n\"");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2015-07-10 22:11:26 +02:00
|
|
|
TEST_F(JsonWriter_WriteString_Tests, CarriageReturn) {
|
2014-10-23 19:54:00 +02:00
|
|
|
whenInputIs("\r");
|
|
|
|
outputMustBe("\"\\r\"");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2015-07-10 22:11:26 +02:00
|
|
|
TEST_F(JsonWriter_WriteString_Tests, HorizontalTab) {
|
2014-10-23 19:54:00 +02:00
|
|
|
whenInputIs("\t");
|
|
|
|
outputMustBe("\"\\t\"");
|
2014-10-23 23:45:36 +02:00
|
|
|
}
|