summaryrefslogtreecommitdiffstats
path: root/tests/auto/linguist/lconvert/tst_lconvert.cpp
blob: 743ac3ad58476122b73d6c49aff23b1f403c755f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/****************************************************************************
**
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the Qt Linguist of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QtTest/QtTest>
#include <QtCore/QFile>

class tst_lconvert : public QObject
{
    Q_OBJECT

public:
    tst_lconvert() : dataDir("data/"), binDir(QLibraryInfo::location(QLibraryInfo::BinariesPath)) {}

private slots:
    void initTestCase();
    void readverifies_data();
    void readverifies();
    void converts_data();
    void converts();
    void roundtrips_data();
    void roundtrips();
#if 0
    void chains_data();
    void chains();
#endif

private:
    void doWait(QProcess *cvt, int stage);
    void doCompare(QIODevice *actual, const QString &expectedFn);
    void verifyReadFail(const QString &fn);
    // args can be empty or have one element less than stations
    void convertChain(const QString &inFileName, const QString &outFileName,
            const QStringList &stations, const QList<QStringList> &args);
    void convertRoundtrip(const QString &fileName, const QStringList &stations,
            const QList<QStringList> &args);

    QString dataDir;
    QString binDir;
};

void tst_lconvert::initTestCase()
{
    if (!QFile::exists(QLatin1String("data/plural-1.po")))
        QProcess::execute(QLatin1String("perl"), QStringList() << QLatin1String("data/makeplurals.pl") << QLatin1String("data/"));
    QVERIFY(QFile::exists(QLatin1String("data/plural-1.po")));
}

void tst_lconvert::doWait(QProcess *cvt, int stage)
{
    if (QTest::currentTestFailed()) {
        cvt->kill();
        cvt->waitForFinished();
    } else {
        QVERIFY2(cvt->waitForFinished(3000),
                 qPrintable(QString("Process %1 hung").arg(stage)));
        QVERIFY2(cvt->exitStatus() == QProcess::NormalExit,
                 qPrintable(QString("Process %1 crashed").arg(stage)));
        QVERIFY2(cvt->exitCode() == 0,
                 qPrintable(QString("Process %1 exited with status %2. Errors:\n%3")
                            .arg(stage).arg(cvt->exitCode())
                            .arg(QString::fromUtf8(cvt->readAllStandardError()))));
    }
}

void tst_lconvert::doCompare(QIODevice *actualDev, const QString &expectedFn)
{
    QList<QByteArray> actual = actualDev->readAll().split('\n');

    QFile file(expectedFn);
    QVERIFY(file.open(QIODevice::ReadOnly | QIODevice::Text));
    QList<QByteArray> expected = file.readAll().split('\n');

    int i = 0, ei = expected.size(), gi = actual.size();
    for (; ; i++) {
        if (i == gi) {
            if (i == ei)
                return;
            gi = 0;
            break;
        } else if (i == ei) {
            ei = 0;
            break;
        } else if (actual.at(i) != expected.at(i)) {
            while ((ei - 1) >= i && (gi - 1) >= i && actual.at(gi - 1) == expected.at(ei - 1))
                ei--, gi--;
            break;
        }
    }
    QByteArray diff;
    for (int j = qMax(0, i - 3); j < i; j++)
        diff += expected.at(j) + '\n';
    diff += "<<<<<<< got\n";
    for (int j = i; j < gi; j++) {
        diff += actual.at(j) + '\n';
        if (j >= i + 5) {
            diff += "...\n";
            break;
        }
    }
    diff += "=========\n";
    for (int j = i; j < ei; j++) {
        diff += expected.at(j) + '\n';
        if (j >= i + 5) {
            diff += "...\n";
            break;
        }
    }
    diff += ">>>>>>> expected\n";
    for (int j = ei; j < qMin(ei + 3, expected.size()); j++)
        diff += expected.at(j) + '\n';
    QFAIL(qPrintable("Output for " + expectedFn + " does not meet expectations:\n" + diff));
}

void tst_lconvert::verifyReadFail(const QString &fn)
{
    QProcess cvt;
    cvt.start(binDir + "/lconvert", QStringList() << (dataDir + fn));
    QVERIFY(cvt.waitForFinished(10000));
    QVERIFY(cvt.exitStatus() == QProcess::NormalExit);
    QVERIFY2(cvt.exitCode() == 2, "Accepted invalid input");
}

void tst_lconvert::convertChain(const QString &_inFileName, const QString &_outFileName,
    const QStringList &stations, const QList<QStringList> &argList)
{
    QList<QProcess *> cvts;

    QString fileName = dataDir + _inFileName;
    QString outFileName = dataDir + _outFileName;

    for (int i = 0; i < stations.size() - 1; i++) {
        QProcess *cvt = new QProcess(this);
        if (cvts.isEmpty())
            cvt->setStandardInputFile(fileName);
        else
            cvts.last()->setStandardOutputProcess(cvt);
        cvts.append(cvt);
    }
    for (int i = 0; i < stations.size() - 1; i++) {
        QStringList args;
        if (!argList.isEmpty())
            args += argList[i];
        args << "-if" << stations[i] << "-i" << "-" << "-of" << stations[i + 1];
        cvts.at(i)->start(binDir + "/lconvert", args, QIODevice::ReadWrite | QIODevice::Text);
    }
    int st = 0;
    foreach (QProcess *cvt, cvts)
        doWait(cvt, ++st);

    if (!QTest::currentTestFailed())
        doCompare(cvts.last(), outFileName);

    qDeleteAll(cvts);
}

void tst_lconvert::convertRoundtrip(const QString &_fileName, const QStringList &stations,
    const QList<QStringList> &argList)
{
    convertChain(_fileName, _fileName, stations, argList);
}

void tst_lconvert::readverifies_data()
{
    QTest::addColumn<QString>("fileName");
    QTest::addColumn<QString>("format");

    QTest::newRow("empty comment") << "test-empty-comment.po" << "po";
    QTest::newRow("translator comment") << "test-translator-comment.po" << "po";
    QTest::newRow("developer comment") << "test-developer-comment.po" << "po";
    QTest::newRow("kde context") << "test-kde-ctxt.po" << "po";
    QTest::newRow("kde fuzzy") << "test-kde-fuzzy.po" << "po";
    QTest::newRow("kde plurals") << "test-kde-plurals.po" << "po";
    QTest::newRow("kde multiline") << "test-kde-multiline.po" << "po";
    QTest::newRow("po linewrapping") << "wrapping.po" << "po";
    QTest::newRow("relative locations") << "relative.ts" << "ts";
    QTest::newRow("message ids") << "msgid.ts" << "ts";
    QTest::newRow("length variants") << "variants.ts" << "ts";
    QTest::newRow("qph") << "phrasebook.qph" << "qph";
}

void tst_lconvert::readverifies()
{
    QFETCH(QString, fileName);
    QFETCH(QString, format);

    convertRoundtrip(fileName, QStringList() << format << format, QList<QStringList>());
}

void tst_lconvert::converts_data()
{
    QTest::addColumn<QString>("inFileName");
    QTest::addColumn<QString>("outFileName");
    QTest::addColumn<QString>("format");

    QTest::newRow("broken utf8") << "test-broken-utf8.po" << "test-broken-utf8.po.out" << "po";
    QTest::newRow("line joins") << "test-slurp.po" << "test-slurp.po.out" << "po";
    QTest::newRow("escapes") << "test-escapes.po" << "test-escapes.po.out" << "po";
}

void tst_lconvert::converts()
{
    QFETCH(QString, inFileName);
    QFETCH(QString, outFileName);
    QFETCH(QString, format);

    QString outFileNameFq = dataDir + outFileName;

    QProcess cvt;
    cvt.start(binDir + "/lconvert",
              QStringList() << "-i" << (dataDir + inFileName) << "-of" << format,
              QIODevice::ReadWrite | QIODevice::Text);
    doWait(&cvt, 0);
    if (QTest::currentTestFailed())
        return;

    doCompare(&cvt, outFileNameFq);
}

Q_DECLARE_METATYPE(QList<QStringList>);

#if 0
void tst_lconvert::chains_data()
{
    QTest::addColumn<QString>("inFileName");
    QTest::addColumn<QString>("outFileName");
    QTest::addColumn<QStringList>("stations");
    QTest::addColumn<QList<QStringList> >("args");

}

void tst_lconvert::chains()
{
    QFETCH(QString, inFileName);
    QFETCH(QString, outFileName);
    QFETCH(QStringList, stations);
    QFETCH(QList<QStringList>, args);

    convertChain(inFileName, outFileName, stations, args);
}
#endif

void tst_lconvert::roundtrips_data()
{
    QTest::addColumn<QString>("fileName");
    QTest::addColumn<QStringList>("stations");
    QTest::addColumn<QList<QStringList> >("args");

    QStringList poTsPo; poTsPo << "po" << "ts" << "po";
    QStringList poXlfPo; poXlfPo << "po" << "xlf" << "po";
    QStringList tsTs11Ts; tsTs11Ts << "ts" << "ts11" << "ts";
    QStringList tsPoTs; tsPoTs << "ts" << "po" << "ts";
    QStringList ts11PoTs11; ts11PoTs11 << "ts11" << "po" << "ts11";
    QStringList tsXlfTs; tsXlfTs << "ts" << "xlf" << "ts";
    QStringList tsQmTs; tsQmTs << "ts" << "qm" << "ts";

    QList<QStringList> noArgs;
    QList<QStringList> filterPoArgs; filterPoArgs << QStringList() << (QStringList() << "-drop-tag" << "po:*");
    QList<QStringList> outDeArgs; outDeArgs << QStringList() << (QStringList() << "-target-language" << "de");
    QList<QStringList> outCnArgs; outCnArgs << QStringList() << (QStringList() << "-target-language" << "cn");

    QTest::newRow("po-ts-po (translator comment)") << "test-translator-comment.po" << poTsPo << noArgs;
    QTest::newRow("po-xliff-po (translator comment)") << "test-translator-comment.po" << poXlfPo << noArgs;
    QTest::newRow("po-ts-po (developer comment)") << "test-developer-comment.po" << poTsPo << noArgs;
    QTest::newRow("po-xliff-po (developer comment)") << "test-developer-comment.po" << poXlfPo << noArgs;

    QTest::newRow("ts11-po-ts11") << "test11.ts" << ts11PoTs11 << filterPoArgs;
    QTest::newRow("ts20-po-ts20") << "test20.ts" << tsPoTs << filterPoArgs;
    QTest::newRow("po-ts-po (de)") << "test1-de.po" << poTsPo << noArgs;
    QTest::newRow("po-ts-po (cn)") << "test1-cn.po" << poTsPo << noArgs;
    QTest::newRow("po-xliff-po (de)") << "test1-de.po" << poXlfPo << noArgs;
    QTest::newRow("po-xliff-po (cn)") << "test1-cn.po" << poXlfPo << noArgs;

    QTest::newRow("po-ts-po (singular)") << "singular.po" << poTsPo << noArgs;
    QTest::newRow("po-ts-po (plural-1)") << "plural-1.po" << poTsPo << noArgs;
    QTest::newRow("po-ts-po (plural-2)") << "plural-2.po" << poTsPo << noArgs;
    QTest::newRow("po-ts-po (plural-3)") << "plural-3.po" << poTsPo << noArgs;
    QTest::newRow("po-xliff-po (singular)") << "singular.po" << poXlfPo << noArgs;
    QTest::newRow("po-xliff-po (plural-1)") << "plural-1.po" << poXlfPo << noArgs;
    QTest::newRow("po-xliff-po (plural-2)") << "plural-2.po" << poXlfPo << noArgs;
    QTest::newRow("po-xliff-po (plural-3)") << "plural-3.po" << poXlfPo << noArgs;

    QTest::newRow("po-ts-po (references)") << "test-refs.po" << poTsPo << noArgs;

    QTest::newRow("ts20-ts11-ts20 (utf8)") << "codec-utf8.ts" << tsTs11Ts << noArgs;
    QTest::newRow("ts20-ts11-ts20 (cp1252)") << "codec-cp1252.ts" << tsTs11Ts << noArgs;
    QTest::newRow("ts20-ts11-ts20 (dual-encoding)") << "dual-encoding.ts" << tsTs11Ts << noArgs;

    QTest::newRow("ts-qm-ts (dual-encoding)") << "dual-encoding.ts" << tsQmTs << noArgs;
    QTest::newRow("ts-qm-ts (plurals-de)") << "plurals-de.ts" << tsQmTs << outDeArgs;
    QTest::newRow("ts-qm-ts (plurals-cn)") << "plurals-cn.ts" << tsQmTs << outCnArgs;
    QTest::newRow("ts-qm-ts (variants)") << "variants.ts" << tsQmTs << outDeArgs;
    QTest::newRow("ts-po-ts (msgid)") << "msgid.ts" << tsPoTs << noArgs;
    QTest::newRow("ts-xliff-ts (msgid)") << "msgid.ts" << tsXlfTs << noArgs;

    QTest::newRow("ts-po-ts (endless loop)") << "endless-po-loop.ts" << tsPoTs << noArgs;
}

void tst_lconvert::roundtrips()
{
    QFETCH(QString, fileName);
    QFETCH(QStringList, stations);
    QFETCH(QList<QStringList>, args);

    convertRoundtrip(fileName, stations, args);
}

QTEST_APPLESS_MAIN(tst_lconvert)

#include "tst_lconvert.moc"