Added tests for StarDictPlugin
authorBartosz Szatkowski <bulislaw@linux.com>
Fri, 8 Oct 2010 11:42:35 +0000 (13:42 +0200)
committerBartosz Szatkowski <bulislaw@linux.com>
Fri, 8 Oct 2010 11:42:35 +0000 (13:42 +0200)
src/plugins/stardict/StarDictPlugin.cpp
src/plugins/stardict/StarDictPlugin.h
tests/StarDictPluginTests/StarDictPluginTests.pro [new file with mode: 0644]
tests/StarDictPluginTests/test.cpp [new file with mode: 0644]
tests/tests.pro

index 14691e7..af5a0d6 100644 (file)
@@ -174,7 +174,8 @@ QString StarDictPlugin::interpret(QByteArray::iterator it,
         int pos=result.indexOf("</t>");
         if(pos!=-1)
             result.remove(pos,4);
-        result+="</t>";
+        if(result.contains("<t>"))
+            result+="</t>";
     }
     else if(mode == 'y')
         result += QString::fromUtf8(read(it++, end));
@@ -187,6 +188,7 @@ QString StarDictPlugin::interpret(QByteArray::iterator it,
     else if(mode == 'r')
         result += QString::fromUtf8(read(it++, end));
 
+ /*
     else if(mode == 'W') {
         if(!last) {
             QByteArray tmp ;
@@ -194,7 +196,7 @@ QString StarDictPlugin::interpret(QByteArray::iterator it,
             tmp.append(*(it++));
             tmp.append(*(it++));
             tmp.append(*(it));
-            result += read(it++, end, (qint32)qFromBigEndian(tmp.data()));
+            result += read(it++, end, (qint32)qFromBigEndian(*(qint32*)tmp.data()));
         } else
             result += read(it++, end);
     } else if(mode == 'P') {
@@ -204,10 +206,10 @@ QString StarDictPlugin::interpret(QByteArray::iterator it,
             tmp.append(*(it++));
             tmp.append(*(it++));
             tmp.append(*(it));
-            result += read(it++, end, (qint32)qFromBigEndian(tmp.data()));
+            result += read(it++, end, (qint32)qFromBigEndian(*(qint32*)tmp.data()));
         } else
             result += read(it++, end);
-    }
+    } */
     return result;
 }
 
index c8efbae..480370c 100644 (file)
 #include <QDialog>
 #include <QRegExp>
 #include <QTime>
-#include <QSqlQuery>
-#include <QSqlDatabase>
-#include <QSqlError>
 #include <QFile>
-#include <QXmlStreamReader>
+//#include <QXmlStreamReader>
 #include <QtPlugin>
 #include <QHash>
 #include <QIcon>
@@ -209,6 +206,8 @@ private:
         \return QSting containing interpreted data chunk */
     QString interpret(QByteArray::iterator it, QByteArray::iterator end,
             QChar mode,QString key, bool last = false);
+
+    friend class StarDictTests;
 };
 
 #endif // XDXFPLUGIN_H
diff --git a/tests/StarDictPluginTests/StarDictPluginTests.pro b/tests/StarDictPluginTests/StarDictPluginTests.pro
new file mode 100644 (file)
index 0000000..dd59d57
--- /dev/null
@@ -0,0 +1,35 @@
+######################################################################
+# Automatically generated by qmake (2.01a) pt. pa? 8 12:09:49 2010
+######################################################################
+
+CONFIG += qtestlib core gui xml sql
+TEMPLATE = app
+TARGET = 
+DEPENDPATH += .
+INCLUDEPATH += .
+
+LIBS += -lz
+
+# Input
+HEADERS += ../../src/plugins/stardict/StarDictPlugin.h \
+           ../../src/include/CommonDictInterface.h \
+           ../../src/include/translation.h \
+           ../../src/include/Notify.h \
+           ../../src/include/settings.h \
+           ../../src/include/AccentsNormalizer.h \
+           ../../src/plugins/stardict/StarDictDialog.h \
+           ../../src/include/DictDialog.h \
+           ../../src/plugins/stardict/TranslationStarDict.h \
+           ../../src/plugins/stardict/StarDictReaderFactory.h \
+           ../../src/plugins/stardict/StarDialog.h \
+           ../../src/plugins/stardict/CompressedReader.h \
+           ../../src/plugins/stardict/UncompressedReader.h \
+           ../../src/plugins/stardict/StarDictReader.h
+SOURCES += test.cpp \
+           ../../src/plugins/stardict/StarDictPlugin.cpp \
+           ../../src/plugins/stardict/CompressedReader.cpp \
+           ../../src/plugins/stardict/UncompressedReader.cpp \
+           ../../src/plugins/stardict/StarDictDialog.cpp \
+           ../../src/plugins/stardict/StarDialog.cpp \
+           ../../src/plugins/stardict/TranslationStarDict.cpp \
+           ../../src/plugins/stardict/StarDictReaderFactory.cpp
diff --git a/tests/StarDictPluginTests/test.cpp b/tests/StarDictPluginTests/test.cpp
new file mode 100644 (file)
index 0000000..f84b41c
--- /dev/null
@@ -0,0 +1,134 @@
+/*******************************************************************************
+
+    This file is part of mDictionary.
+
+    mDictionary is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    mDictionary is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with mDictionary.  If not, see <http://www.gnu.org/licenses/>.
+
+    Copyright 2010 Comarch S.A.
+
+*******************************************************************************/
+
+#include <QtTest/QtTest>
+#include <QList>
+#include <QByteArray>
+#include "../../src/plugins/stardict/StarDictPlugin.h"
+#include <QString>
+#include <QTest>
+#include <QtEndian>
+
+ class StarDictTests: public QObject
+ {
+     Q_OBJECT
+
+ private slots:
+     void readTest_till0();
+     void readTest_bytes();
+     void interpretTest();
+ };
+
+
+
+void StarDictTests::readTest_till0() {
+    StarDictPlugin plug;
+    QList<QByteArray> data;
+    data << "asdf\0" << "\0" << "a\0a\0" << "" << "asdf" << "╖\0" << "aᑇo\0";
+    QList<QByteArray> expected;
+    expected << "asdf" << "" << "a" << "" << "asdf" << "╖" << "aᑇo";
+    QListIterator<QByteArray> itData(data), itExpected(expected);
+
+    while(itData.hasNext()) {
+        QByteArray tmp = itData.next();
+        QCOMPARE(itExpected.next(), plug.read(tmp.begin(), tmp.end(), 0));
+    }
+
+
+}
+
+void StarDictTests::readTest_bytes() {
+    StarDictPlugin plug;
+    QList<QByteArray> data;
+    data << "asdf\0" << "\0" << "a\0a\0" << "" << "asdf" << "╖\0" << "aᑇo\0";
+    QList<QByteArray> expected;
+    expected << "asdf" << "" << "a" << "" << "asdf" << "╖" << "aᑇo";
+    QListIterator<QByteArray> itData(data), itExpected(expected);
+
+    while(itData.hasNext()) {
+        QByteArray tmp = itData.next();
+        QByteArray exp = itExpected.next();
+        QCOMPARE(exp, plug.read(tmp.begin(), tmp.end(), exp.length()));
+    }
+
+}
+
+
+void StarDictTests::interpretTest() {
+    StarDictPlugin plug;
+    QList<QByteArray> data;
+    data << "<k>a</k>sdf\0" << "\0" << "aᑇo\0" << "<k>asdf⍕ᑘቖፔ</k>\0asd"
+                << "<k>asdf⍕ᑘቖፔ</k>";
+    QList<QChar> modes;
+
+    //Test transparent modes
+    modes.clear();
+    modes << 'm' << 'l' << 't' << 'y' << 'k' << 'w' << 'h' << 'r';
+    QList<QByteArray> expected;
+    expected << "<k>a</k>sdf" << "" << "aᑇo" << "<k>asdf⍕ᑘቖፔ</k>"
+                << "<k>asdf⍕ᑘቖፔ</k>";
+    foreach(QChar mode, modes) {
+        QListIterator<QByteArray> itData(data), itExpected(expected);
+        while(itData.hasNext()) {
+            QByteArray tmp = itData.next();
+            QByteArray exp = itExpected.next();
+            QCOMPARE(QString::fromUtf8(exp), plug.interpret(tmp.begin(), tmp.end(),
+                    mode, "key", false));
+        }
+    }
+
+
+    //Test pango
+    expected.clear();
+    expected << "<k>a</k>sdf" << "" << "aᑇo" << "<k>asdf⍕ᑘቖፔ</k>"
+                << "<k>asdf⍕ᑘቖፔ</k>";
+    QListIterator<QByteArray> itData = QListIterator<QByteArray>(data);
+    QListIterator<QByteArray> itExpected = QListIterator<QByteArray>(expected);
+    while(itData.hasNext()) {
+        QByteArray tmp = itData.next();
+        QByteArray exp = itExpected.next();
+        QCOMPARE("<key>key</key>" + QString::fromUtf8(exp),
+                plug.interpret(tmp.begin(), tmp.end(),
+                'g', "key", false));
+    }
+
+
+    //Test xdxf
+    expected.clear();
+    expected << "<key>a</key><t>sdf</t>" << "" << "aᑇo"
+             << "<key>asdf⍕ᑘቖፔ</key><t></t>"
+             << "<key>asdf⍕ᑘቖፔ</key><t></t>";
+    itData = QListIterator<QByteArray>(data);
+    itExpected = QListIterator<QByteArray>(expected);
+    while(itData.hasNext()) {
+        QByteArray tmp = itData.next();
+        QByteArray exp = itExpected.next();
+        QCOMPARE(QString::fromUtf8(exp),
+                plug.interpret(tmp.begin(), tmp.end(),
+                'x', "key", false));
+    }
+
+
+}
+
+
+QTEST_MAIN(StarDictTests)
+#include "test.moc"
index 0acde8b..8c5a289 100644 (file)
@@ -1,5 +1,5 @@
 TEMPLATE = subdirs
-SUBDIRS = mDictionaryTests GooglePluginTests XdxfPluginTests
+SUBDIRS = mDictionaryTests GooglePluginTests XdxfPluginTests StarDictPluginTests
 
 check.CONFIG = recursive
 check.recurse = mDictionaryTests GooglePluginTests XdxfPluginTests