Browse Source

Rework main.cpp into an autotest

QString version needs to be tested.
Girish Ramakrishnan 8 years ago
parent
commit
e133515b22
9 changed files with 70 additions and 71 deletions
  1. 0 0
      benchmark/benchmark.pro
  2. 3 3
      benchmark/main.cpp
  3. 3 10
      json.pro
  4. 0 58
      main.cpp
  5. 1 0
      src/json.pri
  6. 0 0
      tests/data/test.html
  7. 0 0
      tests/data/test.json
  8. 13 0
      tests/tests.pro
  9. 50 0
      tests/tst_json.cpp

benchmark/bench.pro → benchmark/benchmark.pro


+ 3 - 3
benchmark/main.cpp

@@ -23,7 +23,7 @@ private slots:
 
 void tst_Json::testNumbers()
 {
-    QFile file("numbers.json");
+    QFile file(QLatin1String("numbers.json"));
     file.open(QFile::ReadOnly);
     QByteArray ba = file.readAll();
     QString data = QString::fromLocal8Bit(ba.constData(), ba.size());
@@ -40,7 +40,7 @@ void tst_Json::testNumbers()
 
 void tst_Json::testString()
 {
-    QFile file("test.json");
+    QFile file(QLatin1String("test.json"));
     file.open(QFile::ReadOnly);
     QByteArray ba = file.readAll();
     QString data = QString::fromLocal8Bit(ba.constData(), ba.size());
@@ -57,7 +57,7 @@ void tst_Json::testString()
 
 void tst_Json::testByteArray()
 {
-    QFile file("test.json");
+    QFile file(QLatin1String("test.json"));
     file.open(QFile::ReadOnly);
     QByteArray testJson = file.readAll();
 

+ 3 - 10
json.pro

@@ -1,11 +1,4 @@
-TEMPLATE = app
-TARGET = 
-DEPENDPATH += .
-INCLUDEPATH += . src
-
-DEFINES += QT_NO_CAST_FROM_ASCII QT_NO_CAST_TO_ASCII QT_NO_CAST_FROM_BYTEARRAY
-
-include(src/json.pri)
-
-SOURCES += main.cpp
+TEMPLATE = subdirs
+CONFIG += ordered
 
+SUBDIRS = tests benchmark

+ 0 - 58
main.cpp

@@ -1,58 +0,0 @@
-/****************************************************************************
-**
-** Copyright (c) 2010 Girish Ramakrishnan <girish@forwardbias.in>
-**
-** Use, modification and distribution is allowed without limitation,
-** warranty, liability or support of any kind.
-**
-****************************************************************************/
-
-#include <QtGui>
-#include "json.h"
-
-int main(int argc, char *argv[])
-{
-    QApplication app(argc, argv);
-
-    QFile file(QLatin1String("tests/test.json"));
-    file.open(QFile::ReadOnly);
-    QByteArray testJson = file.readAll();
-
-    // test bytearray input
-    JsonReader reader;
-    if (!reader.parse(testJson)) {
-        qDebug() << "Failed to parse: " << reader.errorString();
-        return 0;
-    }
-    QVariant result = reader.result();
-
-    JsonWriter writer;
-    writer.setAutoFormatting(true);
-    // Check if parse(stringize(parse(json))) yields same result
-    QByteArray jsonAgain = writer.toByteArray(result);
-    reader.parse(jsonAgain);
-    if (reader.result() == result) {
-        qDebug() << "Test bytearray passed";
-    } else {
-        qDebug() << "Test bytearray failed";
-    }
-
-    // test string input
-    QString input = QString::fromUtf8(testJson.constData(), testJson.size());
-    if (!reader.parse(input)) {
-        qDebug() << "Failed to parse string: " << reader.errorString();
-        return 0;
-    }
-    result = reader.result();
-
-    QString jsonAgainStr = writer.toString(result);
-    reader.parse(jsonAgainStr);
-    if (reader.result() == result) {
-        qDebug() << "Test string passed";
-    } else {
-        qDebug() << "Test string failed" << reader.errorString();
-    }
-
-    return 0;
-}
-

+ 1 - 0
src/json.pri

@@ -1,3 +1,4 @@
 INCLUDEPATH += $$PWD
 SOURCES += $$PWD/json.cpp
 
+DEFINES += QT_NO_CAST_FROM_ASCII QT_NO_CAST_TO_ASCII QT_NO_CAST_FROM_BYTEARRAY

tests/test.html → tests/data/test.html


tests/test.json → tests/data/test.json


+ 13 - 0
tests/tests.pro

@@ -0,0 +1,13 @@
+QT += testlib
+
+include(../src/json.pri)
+SOURCES += tst_json.cpp
+
+TARGET = tst_json
+
+wince {
+   DEFINES += SRCDIR=\\\"\\\"
+} else {
+    DEFINES += SRCDIR=\\\"$$PWD/\\\"
+}
+

+ 50 - 0
tests/tst_json.cpp

@@ -0,0 +1,50 @@
+#include <QtTest/QtTest>
+#include "json.h"
+
+class tst_Json : public QObject
+{
+    Q_OBJECT
+
+private slots:
+    void basic_data();
+    void basic();
+};
+
+static QByteArray unformat(const QByteArray &input)
+{
+    return input;
+}
+
+void tst_Json::basic_data()
+{
+    QTest::addColumn<QByteArray>("json");
+    QTest::addColumn<QByteArray>("reference");
+    QDir dir;
+    dir.cd(QLatin1String(SRCDIR "data/"));
+    foreach(QString filename , dir.entryList(QStringList() << QLatin1String("*.json"))) {
+        QFile jsonFile(dir.filePath(filename));
+        jsonFile.open(QFile::ReadOnly);
+        QByteArray json = jsonFile.readAll();
+        QByteArray reference = unformat(json);
+        QTest::newRow(dir.filePath(filename).toLatin1().data()) << json << reference;
+    }
+}
+
+void tst_Json::basic()
+{
+    QFETCH(QByteArray, json);
+    QFETCH(QByteArray, reference);
+
+    JsonReader reader;
+    QVERIFY2(reader.parse(json), reader.errorString().toLocal8Bit().constData());
+    QVariant result = reader.result();
+    JsonWriter writer;
+    QByteArray jsonAgain = writer.toByteArray(result);
+    QVERIFY2(reader.parse(jsonAgain), reader.errorString().toLocal8Bit().constData());
+    QVERIFY(result == reader.result());
+}
+
+QTEST_MAIN(tst_Json)
+
+#include "tst_json.moc"
+