Files
ArduinoJson/test/QuotedString_ExtractFrom_Tests.cpp

131 lines
3.1 KiB
C++
Raw Normal View History

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