summaryrefslogtreecommitdiffstats
path: root/tests/auto
diff options
context:
space:
mode:
authorAllan Sandfeld Jensen <allan.jensen@qt.io>2016-11-11 17:06:19 +0100
committerAllan Sandfeld Jensen <allan.jensen@qt.io>2016-11-15 14:19:23 +0000
commite133f0cca44181005f19d006d6c896abe59c1b33 (patch)
tree1620a9d8129a9e735d023a195601e8bd1f0dd99b /tests/auto
parent71d21ed500a20f3fbabb000243cef1168893c9d5 (diff)
Improve error offset in JSON parsing
Do not consume white-space after a token before the token has been parsed, otherwise we end up with misleading offsets. This also fixes a wrong error of illegal number in several cases. Change-Id: I492ca4de0346a1d0ab73b1c23d7a72dba812664c Reviewed-by: Lars Knoll <lars.knoll@qt.io> Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@qt.io>
Diffstat (limited to 'tests/auto')
-rw-r--r--tests/auto/corelib/json/tst_qtjson.cpp33
1 files changed, 33 insertions, 0 deletions
diff --git a/tests/auto/corelib/json/tst_qtjson.cpp b/tests/auto/corelib/json/tst_qtjson.cpp
index 17892b44a2..4a6584a0f6 100644
--- a/tests/auto/corelib/json/tst_qtjson.cpp
+++ b/tests/auto/corelib/json/tst_qtjson.cpp
@@ -139,6 +139,9 @@ private Q_SLOTS:
void removeNonLatinKey();
void documentFromVariant();
+ void parseErrorOffset_data();
+ void parseErrorOffset();
+
private:
QString testDataDir;
};
@@ -2830,5 +2833,35 @@ void tst_QtJson::documentFromVariant()
QCOMPARE(do1.object(), do2.object());
}
+void tst_QtJson::parseErrorOffset_data()
+{
+ QTest::addColumn<QByteArray>("json");
+ QTest::addColumn<int>("errorOffset");
+
+ QTest::newRow("Trailing comma in object") << QByteArray("{ \"value\": false, }") << 19;
+ QTest::newRow("Trailing comma in object plus whitespace") << QByteArray("{ \"value\": false, } ") << 19;
+ QTest::newRow("Trailing comma in array") << QByteArray("[ false, ]") << 10;
+ QTest::newRow("Trailing comma in array plus whitespace") << QByteArray("[ false, ] ") << 10;
+ QTest::newRow("Missing value in object") << QByteArray("{ \"value\": , } ") << 12;
+ QTest::newRow("Missing value in array") << QByteArray("[ \"value\" , , ] ") << 13;
+ QTest::newRow("Leading comma in object") << QByteArray("{ , \"value\": false}") << 3;
+ QTest::newRow("Leading comma in array") << QByteArray("[ , false]") << 3;
+ QTest::newRow("Stray ,") << QByteArray(" , ") << 3;
+ QTest::newRow("Stray [") << QByteArray(" [ ") << 5;
+ QTest::newRow("Stray }") << QByteArray(" } ") << 3;
+}
+
+void tst_QtJson::parseErrorOffset()
+{
+ QFETCH(QByteArray, json);
+ QFETCH(int, errorOffset);
+
+ QJsonParseError error;
+ QJsonDocument::fromJson(json, &error);
+
+ QVERIFY(error.error != QJsonParseError::NoError);
+ QCOMPARE(error.offset, errorOffset);
+}
+
QTEST_MAIN(tst_QtJson)
#include "tst_qtjson.moc"