Files
ArduinoJson/test/QuotedString_ExtractFrom_Tests.cpp

142 lines
3.0 KiB
C++
Raw Normal View History

2014-10-16 21:54:42 +02:00
#include <gtest/gtest.h>
#include <ArduinoJson/Internals/QuotedString.h>
2014-10-16 21:54:42 +02:00
using namespace ArduinoJson::Internals;
class QuotedString_ExtractFrom_Tests : public testing::Test
2014-10-16 21:54:42 +02:00
{
protected:
void whenInputIs(const char* json)
{
strcpy(_jsonString, json);
2014-10-18 22:25:48 +02:00
_result = QuotedString::extractFrom(_jsonString, &_trailing);
2014-10-16 21:54:42 +02:00
}
2014-10-18 22:25:48 +02:00
void resultMustBe(const char* expected)
2014-10-16 21:54:42 +02:00
{
EXPECT_STREQ(expected, _result);
2014-10-18 22:25:48 +02:00
}
void trailingMustBe(const char* expected)
{
EXPECT_STREQ(expected, _trailing);
2014-10-16 21:54:42 +02:00
}
private:
2014-10-16 21:54:42 +02:00
char _jsonString[256];
char* _result;
2014-10-18 22:25:48 +02:00
char* _trailing;
2014-10-16 21:54:42 +02:00
};
2014-10-17 19:57:00 +02:00
TEST_F(QuotedString_ExtractFrom_Tests, EmptyDoubleQuotedString)
2014-10-17 19:57:00 +02:00
{
whenInputIs("\"\"");
2014-10-18 22:25:48 +02:00
resultMustBe("");
trailingMustBe("");
2014-10-17 19:57:00 +02:00
}
TEST_F(QuotedString_ExtractFrom_Tests, EmptySingleQuotedString)
{
whenInputIs("''");
2014-10-18 22:25:48 +02:00
resultMustBe("");
trailingMustBe("");
}
TEST_F(QuotedString_ExtractFrom_Tests, SimpleDoubleQuotedString)
2014-10-16 21:54:42 +02:00
{
whenInputIs("\"hello world\"");
2014-10-18 22:25:48 +02:00
resultMustBe("hello world");
trailingMustBe("");
}
TEST_F(QuotedString_ExtractFrom_Tests, DoubleQuotedStringWithTrailing)
{
whenInputIs("\"hello\" world");
resultMustBe("hello");
trailingMustBe(" world");
}
TEST_F(QuotedString_ExtractFrom_Tests, SingleQuotedStringWithTrailing)
{
whenInputIs("'hello' world");
resultMustBe("hello");
trailingMustBe(" world");
2014-10-16 21:54:42 +02:00
}
TEST_F(QuotedString_ExtractFrom_Tests, CurlyBraces)
2014-10-16 21:54:42 +02:00
{
whenInputIs("\"{hello:world}\"");
2014-10-18 22:25:48 +02:00
resultMustBe("{hello:world}");
2014-10-16 21:54:42 +02:00
}
TEST_F(QuotedString_ExtractFrom_Tests, SquareBraquets)
2014-10-16 21:54:42 +02:00
{
whenInputIs("\"[hello,world]\"");
2014-10-18 22:25:48 +02:00
resultMustBe("[hello,world]");
2014-10-16 21:54:42 +02:00
}
TEST_F(QuotedString_ExtractFrom_Tests, EscapedDoubleQuote)
2014-10-16 21:54:42 +02:00
{
whenInputIs("\"hello \\\"world\\\"\"");
2014-10-18 22:25:48 +02:00
resultMustBe("hello \"world\"");
2014-10-17 19:57:00 +02:00
}
TEST_F(QuotedString_ExtractFrom_Tests, EscapedSingleQuote)
2014-10-17 19:57:00 +02:00
{
whenInputIs("\"hello \\\'world\\\'\"");
2014-10-18 22:25:48 +02:00
resultMustBe("hello 'world'");
2014-10-17 22:32:55 +02:00
}
TEST_F(QuotedString_ExtractFrom_Tests, EscapedSolidus)
2014-10-17 22:32:55 +02:00
{
whenInputIs("\"hello \\/world\\/\"");
2014-10-18 22:25:48 +02:00
resultMustBe("hello /world/");
2014-10-17 22:32:55 +02:00
}
TEST_F(QuotedString_ExtractFrom_Tests, EscapedReverseSolidus)
2014-10-17 22:32:55 +02:00
{
whenInputIs("\"hello \\\\world\\\\\"");
2014-10-18 22:25:48 +02:00
resultMustBe("hello \\world\\");
2014-10-17 22:32:55 +02:00
}
TEST_F(QuotedString_ExtractFrom_Tests, EscapedBackspace)
2014-10-17 22:32:55 +02:00
{
whenInputIs("\"hello \\bworld\\b\"");
2014-10-18 22:25:48 +02:00
resultMustBe("hello \bworld\b");
2014-10-17 22:32:55 +02:00
}
TEST_F(QuotedString_ExtractFrom_Tests, EscapedFormfeed)
2014-10-17 22:32:55 +02:00
{
whenInputIs("\"hello \\fworld\\f\"");
2014-10-18 22:25:48 +02:00
resultMustBe("hello \fworld\f");
2014-10-17 22:32:55 +02:00
}
TEST_F(QuotedString_ExtractFrom_Tests, EscapedNewline)
2014-10-17 22:32:55 +02:00
{
whenInputIs("\"hello \\nworld\\n\"");
2014-10-18 22:25:48 +02:00
resultMustBe("hello \nworld\n");
2014-10-17 22:32:55 +02:00
}
TEST_F(QuotedString_ExtractFrom_Tests, EscapedCarriageReturn)
2014-10-17 22:32:55 +02:00
{
whenInputIs("\"hello \\rworld\\r\"");
2014-10-18 22:25:48 +02:00
resultMustBe("hello \rworld\r");
2014-10-17 22:32:55 +02:00
}
TEST_F(QuotedString_ExtractFrom_Tests, EscapedTab)
2014-10-17 22:32:55 +02:00
{
whenInputIs("\"hello \\tworld\\t\"");
2014-10-18 22:25:48 +02:00
resultMustBe("hello \tworld\t");
2014-10-17 22:32:55 +02:00
}
TEST_F(QuotedString_ExtractFrom_Tests, AllEscapedCharsTogether)
2014-10-17 22:32:55 +02:00
{
whenInputIs("\"1\\\"2\\\\3\\/4\\b5\\f6\\n7\\r8\\t9\"");
2014-10-18 22:25:48 +02:00
resultMustBe("1\"2\\3/4\b5\f6\n7\r8\t9");
2014-10-16 21:54:42 +02:00
}