| 1 |
// Copyright (C) 2016 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
#include <QtTest/QtTest> |
| 5 |
#include <QtCore/QFile> |
| 6 |
#include <QtCore/QTextStream> |
| 7 |
|
| 8 |
using namespace Qt::Literals::StringLiterals; |
| 9 |
|
| 10 |
class tst_lconvert : public QObject |
| 11 |
{ |
| 12 |
Q_OBJECT |
| 13 |
|
| 14 |
public: |
| 15 |
tst_lconvert() |
| 16 |
: dataDir(QFINDTESTDATA("data/")) |
| 17 |
, lconvert(QLibraryInfo::path(QLibraryInfo::BinariesPath) + "/lconvert") {} |
| 18 |
|
| 19 |
private slots: |
| 20 |
void initTestCase(); |
| 21 |
void readverifies_data(); |
| 22 |
void readverifies(); |
| 23 |
void converts_data(); |
| 24 |
void converts(); |
| 25 |
void roundtrips_data(); |
| 26 |
void roundtrips(); |
| 27 |
void chains_data(); |
| 28 |
void chains(); |
| 29 |
void merge(); |
| 30 |
|
| 31 |
private: |
| 32 |
void doWait(QProcess *cvt, int stage); |
| 33 |
void doCompare(QIODevice *actual, const QString &expectedFn); |
| 34 |
void verifyReadFail(const QString &fn); |
| 35 |
// args can be empty or have one element less than stations |
| 36 |
void convertChain(const QString &inFileName, const QString &outFileName, |
| 37 |
const QStringList &stations, const QList<QStringList> &args); |
| 38 |
void convertRoundtrip(const QString &fileName, const QStringList &stations, |
| 39 |
const QList<QStringList> &args); |
| 40 |
|
| 41 |
QString dataDir; |
| 42 |
QString lconvert; |
| 43 |
}; |
| 44 |
|
| 45 |
static void writePoMessages(QTextStream &out, int num, const QString &str) |
| 46 |
{ |
| 47 |
for (int i = 0; i < (1 << num); ++i) { |
| 48 |
out << "\n"; |
| 49 |
out << "msgid \"singular " << str << " " << i << "\"\n"; |
| 50 |
out << "msgid_plural \"plural " << str << " " << i << "\"\n"; |
| 51 |
for (int j = 0; j < num; ++j) { |
| 52 |
QString tr; |
| 53 |
if ((i & (1 << j)) == 0) |
| 54 |
tr = u"translated %1 %2 %3"_s.arg(str).arg(i).arg(j); |
| 55 |
out << "msgstr[" << j << "] \"" << tr << "\"\n"; |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
static void createPluralPoFile(const QString &outdir, int num, const QString &lang, |
| 61 |
const QString &pluralForms) |
| 62 |
{ |
| 63 |
QString filename = outdir + u"plural-%1.po"_s.arg(num); |
| 64 |
QFile file(filename); |
| 65 |
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { |
| 66 |
qWarning() << "Cannot write file in" << outdir; |
| 67 |
return; |
| 68 |
} |
| 69 |
|
| 70 |
QTextStream out(&file); |
| 71 |
out << R"(msgid "" |
| 72 |
msgstr "" |
| 73 |
"MIME-Version: 1.0\n" |
| 74 |
"Content-Type: text/plain; charset=UTF-8\n" |
| 75 |
"Content-Transfer-Encoding: 8bit\n" |
| 76 |
"X-FooBar: yup\n" |
| 77 |
"X-Language: )" << lang << R"(\n" |
| 78 |
"Plural-Forms: )" << pluralForms << "\\n\"\n"; |
| 79 |
|
| 80 |
writePoMessages(out, num, "one"_L1); |
| 81 |
writePoMessages(out, num, "two"_L1); |
| 82 |
writePoMessages(out, num, "three"_L1); |
| 83 |
writePoMessages(out, num, "four"_L1); |
| 84 |
} |
| 85 |
|
| 86 |
void tst_lconvert::initTestCase() |
| 87 |
{ |
| 88 |
if (!QFile::exists(dataDir + QLatin1String("plural-1.po"))) { |
| 89 |
createPluralPoFile(dataDir, 1, u"zh_CN"_s, u"nplurals=1; plural=0;"_s); |
| 90 |
createPluralPoFile(dataDir, 2, u"de_DE"_s, u"nplurals=2; plural=(n != 1);"_s); |
| 91 |
createPluralPoFile(dataDir, 3, u"pl_PL"_s, |
| 92 |
u"nplurals=3; plural=(n==1 ? 0 : n%10>=2 && "_s |
| 93 |
u"n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);"_s); |
| 94 |
} |
| 95 |
QVERIFY(QFile::exists(dataDir + QLatin1String("plural-1.po"))); |
| 96 |
} |
| 97 |
|
| 98 |
void tst_lconvert::doWait(QProcess *cvt, int stage) |
| 99 |
{ |
| 100 |
if (QTest::currentTestFailed()) { |
| 101 |
cvt->kill(); |
| 102 |
cvt->waitForFinished(); |
| 103 |
} else { |
| 104 |
QVERIFY2(cvt->waitForFinished(3000), |
| 105 |
qPrintable(QString("Process %1 hung").arg(stage))); |
| 106 |
QVERIFY2(cvt->exitStatus() == QProcess::NormalExit, |
| 107 |
qPrintable(QString("Process %1 crashed").arg(stage))); |
| 108 |
QVERIFY2(cvt->exitCode() == 0, |
| 109 |
qPrintable(QString("Process %1 exited with status %2. Errors:\n%3") |
| 110 |
.arg(stage).arg(cvt->exitCode()) |
| 111 |
.arg(QString::fromUtf8(cvt->readAllStandardError())))); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
void tst_lconvert::doCompare(QIODevice *actualDev, const QString &expectedFn) |
| 116 |
{ |
| 117 |
QList<QByteArray> actual = actualDev->readAll().split('\n'); |
| 118 |
|
| 119 |
QFile file(expectedFn); |
| 120 |
QVERIFY(file.open(QIODevice::ReadOnly | QIODevice::Text)); |
| 121 |
QList<QByteArray> expected = file.readAll().split('\n'); |
| 122 |
|
| 123 |
int i = 0, ei = expected.size(), gi = actual.size(); |
| 124 |
for (; ; i++) { |
| 125 |
if (i == gi) { |
| 126 |
if (i == ei) |
| 127 |
return; |
| 128 |
gi = 0; |
| 129 |
break; |
| 130 |
} else if (i == ei) { |
| 131 |
ei = 0; |
| 132 |
break; |
| 133 |
} else if (actual.at(i) != expected.at(i)) { |
| 134 |
while ((ei - 1) >= i && (gi - 1) >= i && actual.at(gi - 1) == expected.at(ei - 1)) |
| 135 |
ei--, gi--; |
| 136 |
break; |
| 137 |
} |
| 138 |
} |
| 139 |
QByteArray diff; |
| 140 |
for (int j = qMax(0, i - 3); j < i; j++) |
| 141 |
diff += expected.at(j) + '\n'; |
| 142 |
diff += "<<<<<<< got\n"; |
| 143 |
for (int j = i; j < gi; j++) { |
| 144 |
diff += actual.at(j) + '\n'; |
| 145 |
if (j >= i + 5) { |
| 146 |
diff += "...\n"; |
| 147 |
break; |
| 148 |
} |
| 149 |
} |
| 150 |
diff += "=========\n"; |
| 151 |
for (int j = i; j < ei; j++) { |
| 152 |
diff += expected.at(j) + '\n'; |
| 153 |
if (j >= i + 5) { |
| 154 |
diff += "...\n"; |
| 155 |
break; |
| 156 |
} |
| 157 |
} |
| 158 |
diff += ">>>>>>> expected\n"; |
| 159 |
for (int j = ei; j < qMin(ei + 3, expected.size()); j++) |
| 160 |
diff += expected.at(j) + '\n'; |
| 161 |
QFAIL(qPrintable("Output for " + expectedFn + " does not meet expectations:\n" + diff)); |
| 162 |
} |
| 163 |
|
| 164 |
void tst_lconvert::verifyReadFail(const QString &fn) |
| 165 |
{ |
| 166 |
QProcess cvt; |
| 167 |
cvt.start(lconvert, QStringList() << (dataDir + fn)); |
| 168 |
QVERIFY2(cvt.waitForStarted(), qPrintable(cvt.errorString())); |
| 169 |
QVERIFY(cvt.waitForFinished(10000)); |
| 170 |
QVERIFY(cvt.exitStatus() == QProcess::NormalExit); |
| 171 |
QVERIFY2(cvt.exitCode() == 2, "Accepted invalid input"); |
| 172 |
} |
| 173 |
|
| 174 |
void tst_lconvert::convertChain(const QString &_inFileName, const QString &_outFileName, |
| 175 |
const QStringList &stations, const QList<QStringList> &argList) |
| 176 |
{ |
| 177 |
QList<QProcess *> cvts; |
| 178 |
|
| 179 |
QString fileName = dataDir + _inFileName; |
| 180 |
QString outFileName = dataDir + _outFileName; |
| 181 |
|
| 182 |
for (int i = 0; i < stations.size() - 1; i++) { |
| 183 |
QProcess *cvt = new QProcess(this); |
| 184 |
if (cvts.isEmpty()) |
| 185 |
cvt->setStandardInputFile(fileName); |
| 186 |
else |
| 187 |
cvts.last()->setStandardOutputProcess(cvt); |
| 188 |
cvts.append(cvt); |
| 189 |
} |
| 190 |
for (int i = 0; i < stations.size() - 1; i++) { |
| 191 |
QStringList args; |
| 192 |
if (!argList.isEmpty()) |
| 193 |
args += argList[i]; |
| 194 |
args << "-if" << stations[i] << "-i" << "-" << "-of" << stations[i + 1]; |
| 195 |
cvts.at(i)->start(lconvert, args, QIODevice::ReadWrite | QIODevice::Text); |
| 196 |
} |
| 197 |
for (QProcess *cvt : std::as_const(cvts)) |
| 198 |
QVERIFY2(cvt->waitForStarted(), qPrintable(cvt->errorString())); |
| 199 |
int st = 0; |
| 200 |
for (QProcess *cvt : std::as_const(cvts)) |
| 201 |
doWait(cvt, ++st); |
| 202 |
|
| 203 |
if (!QTest::currentTestFailed()) |
| 204 |
doCompare(cvts.last(), outFileName); |
| 205 |
|
| 206 |
qDeleteAll(cvts); |
| 207 |
} |
| 208 |
|
| 209 |
void tst_lconvert::convertRoundtrip(const QString &_fileName, const QStringList &stations, |
| 210 |
const QList<QStringList> &argList) |
| 211 |
{ |
| 212 |
convertChain(_fileName, _fileName, stations, argList); |
| 213 |
} |
| 214 |
|
| 215 |
void tst_lconvert::readverifies_data() |
| 216 |
{ |
| 217 |
QTest::addColumn<QString>("fileName"); |
| 218 |
QTest::addColumn<QString>("format"); |
| 219 |
|
| 220 |
QTest::newRow("ts") << "test20.ts" << "ts"; |
| 221 |
QTest::newRow("empty comment") << "test-empty-comment.po" << "po"; |
| 222 |
QTest::newRow("translator comment") << "test-translator-comment.po" << "po"; |
| 223 |
QTest::newRow("developer comment") << "test-developer-comment.po" << "po"; |
| 224 |
QTest::newRow("kde context") << "test-kde-ctxt.po" << "po"; |
| 225 |
QTest::newRow("kde fuzzy") << "test-kde-fuzzy.po" << "po"; |
| 226 |
QTest::newRow("kde plurals") << "test-kde-plurals.po" << "po"; |
| 227 |
QTest::newRow("kde multiline") << "test-kde-multiline.po" << "po"; |
| 228 |
QTest::newRow("po linewrapping") << "wrapping.po" << "po"; |
| 229 |
QTest::newRow("relative locations") << "relative.ts" << "ts"; |
| 230 |
QTest::newRow("message ids") << "msgid.ts" << "ts"; |
| 231 |
QTest::newRow("length variants") << "variants.ts" << "ts"; |
| 232 |
QTest::newRow("qph") << "phrasebook.qph" << "qph"; |
| 233 |
} |
| 234 |
|
| 235 |
void tst_lconvert::readverifies() |
| 236 |
{ |
| 237 |
QFETCH(QString, fileName); |
| 238 |
QFETCH(QString, format); |
| 239 |
|
| 240 |
convertRoundtrip(fileName, QStringList() << format << format, QList<QStringList>()); |
| 241 |
} |
| 242 |
|
| 243 |
void tst_lconvert::converts_data() |
| 244 |
{ |
| 245 |
QTest::addColumn<QString>("inFileName"); |
| 246 |
QTest::addColumn<QString>("outFileName"); |
| 247 |
QTest::addColumn<QString>("format"); |
| 248 |
|
| 249 |
QTest::newRow("broken utf8") << "test-broken-utf8.po" << "test-broken-utf8.po.out" << "po"; |
| 250 |
QTest::newRow("line joins") << "test-slurp.po" << "test-slurp.po.out" << "po"; |
| 251 |
QTest::newRow("escapes") << "test-escapes.po" << "test-escapes.po.out" << "po"; |
| 252 |
QTest::newRow("xlf seg") << "test-trans_seg.xlf" << "test-trans_seg.ts.out" << "ts"; |
| 253 |
} |
| 254 |
|
| 255 |
void tst_lconvert::converts() |
| 256 |
{ |
| 257 |
QFETCH(QString, inFileName); |
| 258 |
QFETCH(QString, outFileName); |
| 259 |
QFETCH(QString, format); |
| 260 |
|
| 261 |
QString outFileNameFq = dataDir + outFileName; |
| 262 |
|
| 263 |
QProcess cvt; |
| 264 |
cvt.start(lconvert, |
| 265 |
QStringList() << "-i" << (dataDir + inFileName) << "-of" << format, |
| 266 |
QIODevice::ReadWrite | QIODevice::Text); |
| 267 |
QVERIFY2(cvt.waitForStarted(), qPrintable(cvt.errorString())); |
| 268 |
doWait(&cvt, 0); |
| 269 |
if (QTest::currentTestFailed()) |
| 270 |
return; |
| 271 |
|
| 272 |
doCompare(&cvt, outFileNameFq); |
| 273 |
} |
| 274 |
|
| 275 |
Q_DECLARE_METATYPE(QList<QStringList>); |
| 276 |
|
| 277 |
void tst_lconvert::chains_data() |
| 278 |
{ |
| 279 |
QTest::addColumn<QString>("inFileName"); |
| 280 |
QTest::addColumn<QString>("outFileName"); |
| 281 |
QTest::addColumn<QStringList>("stations"); |
| 282 |
QTest::addColumn<QList<QStringList> >("args"); |
| 283 |
|
| 284 |
QTest::newRow("no-untranslated") << "untranslated.ts" << "untranslated.ts.out" |
| 285 |
<< QStringList({"ts", "ts"}) |
| 286 |
<< QList<QStringList>({QStringList("-no-untranslated")}); |
| 287 |
} |
| 288 |
|
| 289 |
void tst_lconvert::chains() |
| 290 |
{ |
| 291 |
QFETCH(QString, inFileName); |
| 292 |
QFETCH(QString, outFileName); |
| 293 |
QFETCH(QStringList, stations); |
| 294 |
QFETCH(QList<QStringList>, args); |
| 295 |
|
| 296 |
convertChain(inFileName, outFileName, stations, args); |
| 297 |
} |
| 298 |
|
| 299 |
void tst_lconvert::roundtrips_data() |
| 300 |
{ |
| 301 |
QTest::addColumn<QString>("fileName"); |
| 302 |
QTest::addColumn<QStringList>("stations"); |
| 303 |
QTest::addColumn<QList<QStringList> >("args"); |
| 304 |
|
| 305 |
QStringList poTsPo; poTsPo << "po" << "ts" << "po"; |
| 306 |
QStringList poXlfPo; poXlfPo << "po" << "xlf" << "po"; |
| 307 |
QStringList tsPoTs; tsPoTs << "ts" << "po" << "ts"; |
| 308 |
QStringList tsXlfTs; tsXlfTs << "ts" << "xlf" << "ts"; |
| 309 |
QStringList tsQmTs; tsQmTs << "ts" << "qm" << "ts"; |
| 310 |
QStringList qmTsQm; qmTsQm << "qm" << "ts" << "qm"; |
| 311 |
|
| 312 |
QList<QStringList> noArgs; |
| 313 |
QList<QStringList> filterPoArgs; filterPoArgs << QStringList() << (QStringList() << "-drop-tag" << "po:*"); |
| 314 |
QList<QStringList> outDeArgs; outDeArgs << QStringList() << (QStringList() << "-target-language" << "de"); |
| 315 |
QList<QStringList> outCnArgs; outCnArgs << QStringList() << (QStringList() << "-target-language" << "cn"); |
| 316 |
|
| 317 |
QTest::newRow("po-ts-po (translator comment)") << "test-translator-comment.po" << poTsPo << noArgs; |
| 318 |
QTest::newRow("po-xliff-po (translator comment)") << "test-translator-comment.po" << poXlfPo << noArgs; |
| 319 |
QTest::newRow("po-ts-po (developer comment)") << "test-developer-comment.po" << poTsPo << noArgs; |
| 320 |
QTest::newRow("po-xliff-po (developer comment)") << "test-developer-comment.po" << poXlfPo << noArgs; |
| 321 |
|
| 322 |
QTest::newRow("ts20-po-ts20") << "test20.ts" << tsPoTs << filterPoArgs; |
| 323 |
QTest::newRow("po-ts-po (de)") << "test1-de.po" << poTsPo << noArgs; |
| 324 |
QTest::newRow("po-ts-po (cn)") << "test1-cn.po" << poTsPo << noArgs; |
| 325 |
QTest::newRow("po-xliff-po (de)") << "test1-de.po" << poXlfPo << noArgs; |
| 326 |
QTest::newRow("po-xliff-po (cn)") << "test1-cn.po" << poXlfPo << noArgs; |
| 327 |
|
| 328 |
QTest::newRow("po-ts-po (singular)") << "singular.po" << poTsPo << noArgs; |
| 329 |
QTest::newRow("po-ts-po (plural-1)") << "plural-1.po" << poTsPo << noArgs; |
| 330 |
QTest::newRow("po-ts-po (plural-2)") << "plural-2.po" << poTsPo << noArgs; |
| 331 |
QTest::newRow("po-ts-po (plural-3)") << "plural-3.po" << poTsPo << noArgs; |
| 332 |
QTest::newRow("po-xliff-po (singular)") << "singular.po" << poXlfPo << noArgs; |
| 333 |
QTest::newRow("po-xliff-po (plural-1)") << "plural-1.po" << poXlfPo << noArgs; |
| 334 |
QTest::newRow("po-xliff-po (plural-2)") << "plural-2.po" << poXlfPo << noArgs; |
| 335 |
QTest::newRow("po-xliff-po (plural-3)") << "plural-3.po" << poXlfPo << noArgs; |
| 336 |
|
| 337 |
QTest::newRow("po-ts-po (references)") << "test-refs.po" << poTsPo << noArgs; |
| 338 |
|
| 339 |
QTest::newRow("ts-qm-ts (plurals-de)") << "plurals-de.ts" << tsQmTs << outDeArgs; |
| 340 |
QTest::newRow("ts-qm-ts (plurals-cn)") << "plurals-cn.ts" << tsQmTs << outCnArgs; |
| 341 |
QTest::newRow("ts-qm-ts (variants)") << "variants.ts" << tsQmTs << outDeArgs; |
| 342 |
QTest::newRow("ts-po-ts (msgid)") << "msgid.ts" << tsPoTs << noArgs; |
| 343 |
QTest::newRow("ts-xliff-ts (msgid)") << "msgid.ts" << tsXlfTs << noArgs; |
| 344 |
QTest::newRow("ts-qm-ts (emptymsg)") << "emptymsg.ts" << tsQmTs << noArgs; |
| 345 |
QTest::newRow("ts-xliff-ts (emptymsg)") << "emptymsg.ts" << tsXlfTs << noArgs; |
| 346 |
|
| 347 |
QTest::newRow("ts-po-ts (endless loop)") << "endless-po-loop.ts" << tsPoTs << noArgs; |
| 348 |
QTest::newRow("ts-qm-ts (whitespace)") << "whitespace.ts" << tsQmTs << noArgs; |
| 349 |
QTest::newRow("qm-ts-qm (untranslated)") << "untranslated.qm" << qmTsQm << noArgs; |
| 350 |
} |
| 351 |
|
| 352 |
void tst_lconvert::roundtrips() |
| 353 |
{ |
| 354 |
QFETCH(QString, fileName); |
| 355 |
QFETCH(QStringList, stations); |
| 356 |
QFETCH(QList<QStringList>, args); |
| 357 |
|
| 358 |
convertRoundtrip(fileName, stations, args); |
| 359 |
} |
| 360 |
|
| 361 |
void tst_lconvert::merge() |
| 362 |
{ |
| 363 |
QProcess cvt; |
| 364 |
QStringList args; |
| 365 |
args << (dataDir + "idxmerge.ts") << (dataDir + "idxmerge-add.ts"); |
| 366 |
cvt.start(lconvert, args, QIODevice::ReadWrite | QIODevice::Text); |
| 367 |
doWait(&cvt, 1); |
| 368 |
if (!QTest::currentTestFailed()) |
| 369 |
doCompare(&cvt, dataDir + "idxmerge.ts.out"); |
| 370 |
} |
| 371 |
|
| 372 |
QTEST_APPLESS_MAIN(tst_lconvert) |
| 373 |
|
| 374 |
#include "tst_lconvert.moc" |