Merge branch 'qml' of ssh://drop.maemo.org/git/mdictionary into qml
[mdictionary] / tests / StarDictPluginTests / test.cpp
1 /*******************************************************************************
2
3     This file is part of mDictionary.
4
5     mDictionary is free software: you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation, either version 3 of the License, or
8     (at your option) any later version.
9
10     mDictionary is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with mDictionary.  If not, see <http://www.gnu.org/licenses/>.
17
18     Copyright 2010 Comarch S.A.
19
20 *******************************************************************************/
21
22 #include <QtTest/QtTest>
23 #include <QList>
24 #include <QByteArray>
25 #include "../../src/plugins/stardict/StarDictPlugin.h"
26 #include <QString>
27 #include <QTest>
28 #include <QtEndian>
29
30  class StarDictTests: public QObject
31  {
32      Q_OBJECT
33
34  private slots:
35      void readTest_till0();
36      void readTest_bytes();
37      void interpretTest();
38  };
39
40
41
42 void StarDictTests::readTest_till0() {
43     StarDictPlugin plug;
44     QList<QByteArray> data;
45     data << "asdf\0" << "\0" << "a\0a\0" << "" << "asdf" << "╖\0" << "aᑇo\0";
46     QList<QByteArray> expected;
47     expected << "asdf" << "" << "a" << "" << "asdf" << "╖" << "aᑇo";
48     QListIterator<QByteArray> itData(data), itExpected(expected);
49
50     while(itData.hasNext()) {
51         QByteArray tmp = itData.next();
52         QCOMPARE(itExpected.next(), plug.read(tmp.begin(), tmp.end(), 0));
53     }
54
55
56 }
57
58 void StarDictTests::readTest_bytes() {
59     StarDictPlugin plug;
60     QList<QByteArray> data;
61     data << "asdf\0" << "\0" << "a\0a\0" << "" << "asdf" << "╖\0" << "aᑇo\0";
62     QList<QByteArray> expected;
63     expected << "asdf" << "" << "a" << "" << "asdf" << "╖" << "aᑇo";
64     QListIterator<QByteArray> itData(data), itExpected(expected);
65
66     while(itData.hasNext()) {
67         QByteArray tmp = itData.next();
68         QByteArray exp = itExpected.next();
69         QCOMPARE(exp, plug.read(tmp.begin(), tmp.end(), exp.length()));
70     }
71
72 }
73
74
75 void StarDictTests::interpretTest() {
76     StarDictPlugin plug;
77     QList<QByteArray> data;
78     data << "<k>a</k>sdf\0" << "\0" << "aᑇo\0" << "<k>asdf⍕ᑘቖፔ</k>\0asd"
79                 << "<k>asdf⍕ᑘቖፔ</k>";
80     QList<QChar> modes;
81
82     //Test transparent modes
83     modes.clear();
84     modes << 'm' << 'l' << 't' << 'y' << 'k' << 'w' << 'h' << 'r';
85     QList<QByteArray> expected;
86     expected << "<k>a</k>sdf" << "" << "aᑇo" << "<k>asdf⍕ᑘቖፔ</k>"
87                 << "<k>asdf⍕ᑘቖፔ</k>";
88     foreach(QChar mode, modes) {
89         QListIterator<QByteArray> itData(data), itExpected(expected);
90         while(itData.hasNext()) {
91             QByteArray tmp = itData.next();
92             QByteArray exp = itExpected.next();
93             QCOMPARE(QString::fromUtf8(exp), plug.interpret(tmp.begin(), tmp.end(),
94                     mode, "key", false));
95         }
96     }
97
98
99     //Test pango
100     expected.clear();
101     expected << "<k>a</k>sdf" << "" << "aᑇo" << "<k>asdf⍕ᑘቖፔ</k>"
102                 << "<k>asdf⍕ᑘቖፔ</k>";
103     QListIterator<QByteArray> itData = QListIterator<QByteArray>(data);
104     QListIterator<QByteArray> itExpected = QListIterator<QByteArray>(expected);
105     while(itData.hasNext()) {
106         QByteArray tmp = itData.next();
107         QByteArray exp = itExpected.next();
108         QCOMPARE("<key>key</key>" + QString::fromUtf8(exp),
109                 plug.interpret(tmp.begin(), tmp.end(),
110                 'g', "key", false));
111     }
112
113
114     //Test xdxf
115     expected.clear();
116     expected << "<key>a</key><t>sdf</t>" << "" << "aᑇo"
117              << "<key>asdf⍕ᑘቖፔ</key><t></t>"
118              << "<key>asdf⍕ᑘቖፔ</key><t></t>";
119     itData = QListIterator<QByteArray>(data);
120     itExpected = QListIterator<QByteArray>(expected);
121     while(itData.hasNext()) {
122         QByteArray tmp = itData.next();
123         QByteArray exp = itExpected.next();
124         QCOMPARE(QString::fromUtf8(exp),
125                 plug.interpret(tmp.begin(), tmp.end(),
126                 'x', "key", false));
127     }
128
129
130 }
131
132
133 QTEST_MAIN(StarDictTests)
134 #include "test.moc"