Skip to content

Commit

Permalink
tst_QtJson: add matching escape-generating test
Browse files Browse the repository at this point in the history
Change-Id: Ie72b0dd0fbe84d2caae0fffd16a11596eb61a90e
Reviewed-by: Ievgenii Meshcheriakov <[email protected]>
Reviewed-by: Lars Knoll <[email protected]>
Reviewed-by: Edward Welbourne <[email protected]>
  • Loading branch information
thiagomacieira committed Sep 6, 2021
1 parent b631440 commit 115d99b
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions tests/auto/corelib/serialization/json/tst_qtjson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ private Q_SLOTS:

void parseEscapes_data();
void parseEscapes();
void makeEscapes_data();
void makeEscapes();

void assignObjects();
void assignArrays();
Expand Down Expand Up @@ -2509,6 +2511,59 @@ void tst_QtJson::parseEscapes()
QCOMPARE(array.first().toString(), result);
}

void tst_QtJson::makeEscapes_data()
{
QTest::addColumn<QString>("input");
QTest::addColumn<QByteArray>("result");

auto addUnicodeRow = [](char16_t c) {
char buf[32]; // more than enough
snprintf(buf, std::size(buf), "\\u%04x", c);
QTest::addRow("U+%04X", c) << QString(c) << QByteArray(buf);
};


QTest::addRow("quote") << "\"" << QByteArray(R"(\")");
QTest::addRow("backslash") << "\\" << QByteArray(R"(\\)");
//QTest::addRow("slash") << "/" << QByteArray(R"(\/)"); // does not get escaped
QTest::addRow("backspace") << "\b" << QByteArray(R"(\b)");
QTest::addRow("form-feed") << "\f" << QByteArray(R"(\f)");
QTest::addRow("newline") << "\n" << QByteArray(R"(\n)");
QTest::addRow("carriage-return") << "\r" << QByteArray(R"(\r)");
QTest::addRow("tab") << "\t" << QByteArray(R"(\t)");

// control characters other than the above
for (char16_t c = 0; c < 0x20; ++c) {
if (c && strchr("\b\f\n\r\t", c))
continue;
addUnicodeRow(c);
}
// unpaired surrogates
addUnicodeRow(char16_t(0xd800));
addUnicodeRow(char16_t(0xdc00));

QString improperlyPaired;
improperlyPaired.append(char16_t(0xdc00));
improperlyPaired.append(char16_t(0xd800));
QTest::addRow("inverted-surrogates") << improperlyPaired << QByteArray("\\udc00\\ud800");
}

void tst_QtJson::makeEscapes()
{
QFETCH(QString, input);
QFETCH(QByteArray, result);

QJsonArray array = { input };
QByteArray json = QJsonDocument(array).toJson(QJsonDocument::Compact);

QVERIFY(json.startsWith("[\""));
result.prepend("[\"");
QVERIFY(json.endsWith("\"]"));
result.append("\"]");

QCOMPARE(json, result);
}

void tst_QtJson::assignObjects()
{
const char *json =
Expand Down

0 comments on commit 115d99b

Please sign in to comment.