Debian Codegraph @

tests/auto/linguist/lrelease/tst_lrelease.cpp

raw
1 // Copyright (C) 2016 The Qt Company Ltd.
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4 #include <QtCore/QDir>
5 #include <QtCore/QDebug>
6 #include <QtCore/QFile>
7 #include <QtCore/QByteArray>
8
9 #include <QtTest/QtTest>
10
11 class tst_lrelease : public QObject
12 {
13 Q_OBJECT
14
15 public:
16 tst_lrelease()
17 : lrelease(QLibraryInfo::path(QLibraryInfo::BinariesPath) + "/lrelease")
18 , dataDir(QFINDTESTDATA("testdata/"))
19 {}
20
21 private:
22
23 private slots:
24 void translate();
25 void compressed();
26 void idbased();
27 void markuntranslated();
28 void dupes();
29 void noTranslations();
30
31 private:
32 void doCompare(const QStringList &actual, const QString &expectedFn);
33
34 QString lrelease;
35 QString dataDir;
36 };
37
38 void tst_lrelease::doCompare(const QStringList &actual, const QString &expectedFn)
39 {
40 QFile file(expectedFn);
41 QVERIFY(file.open(QIODevice::ReadOnly | QIODevice::Text));
42 QStringList expected = QString(file.readAll()).trimmed().split('\n');
43
44 int i = 0, ei = expected.size(), gi = actual.size();
45 for (; ; i++) {
46 if (i == gi) {
47 if (i == ei)
48 return;
49 gi = 0;
50 break;
51 } else if (i == ei) {
52 ei = 0;
53 break;
54 } else if (!QRegularExpression(QRegularExpression::anchoredPattern(expected.at(i))).match(actual.at(i)).hasMatch()) {
55 while ((ei - 1) >= i && (gi - 1) >= i &&
56 (QRegularExpression(QRegularExpression::anchoredPattern(expected.at(ei - 1))).match(actual.at(gi - 1))).hasMatch())
57 ei--, gi--;
58 break;
59 }
60 }
61 QString diff;
62 for (int j = qMax(0, i - 3); j < i; j++)
63 diff += expected.at(j) + '\n';
64 diff += "<<<<<<< got\n";
65 for (int j = i; j < gi; j++) {
66 diff += actual.at(j) + '\n';
67 if (j >= i + 5) {
68 diff += "...\n";
69 break;
70 }
71 }
72 diff += "=========\n";
73 for (int j = i; j < ei; j++) {
74 diff += expected.at(j) + '\n';
75 if (j >= i + 5) {
76 diff += "...\n";
77 break;
78 }
79 }
80 diff += ">>>>>>> expected\n";
81 for (int j = ei; j < qMin(ei + 3, expected.size()); j++)
82 diff += expected.at(j) + '\n';
83 QFAIL(qPrintable("Output for " + expectedFn + " does not meet expectations:\n" + diff));
84 }
85
86 void tst_lrelease::translate()
87 {
88 QVERIFY(!QProcess::execute(lrelease, QStringList() << (dataDir + "translate.ts")));
89
90 QTranslator translator;
91 QVERIFY(translator.load(dataDir + "translate.qm"));
92 qApp->installTranslator(&translator);
93
94 QCOMPARE(QObject::tr("\nnewline at the start"), QString("\nNEWLINE AT THE START"));
95 QCOMPARE(QObject::tr("newline at the end\n"), QString("NEWLINE AT THE END\n"));
96 QCOMPARE(QObject::tr("newline and space at the end\n "), QString("NEWLINE AND SPACE AT THE END\n "));
97 QCOMPARE(QObject::tr("space and newline at the end \n"), QString("SPACE AND NEWLINE AT THE END \n"));
98 QCOMPARE(QObject::tr("\ttab at the start and newline at the end\n"), QString("\tTAB AT THE START AND NEWLINE AT THE END\n"));
99 QCOMPARE(QObject::tr("\n\tnewline and tab at the start"), QString("\n\tNEWLINE AND TAB AT THE START"));
100 QCOMPARE(QObject::tr(" \tspace and tab at the start"), QString(" \tSPACE AND TAB AT THE START"));
101 QCOMPARE(QObject::tr(" string that does not exist"), QString(" string that does not exist"));
102
103 QCOMPARE(QCoreApplication::translate("CubeForm", "Test"), QString::fromLatin1("BBBB"));
104 QCOMPARE(QCoreApplication::translate("", "Test", "Empty context"), QString("AAAA"));
105
106 // Test plurals
107 QString txed = QCoreApplication::translate("Plurals", "There are %n houses", 0, 0);
108 QCOMPARE(QString::fromLatin1("[%1]").arg(txed), QString("[There are 0 houses]"));
109 QCOMPARE(QCoreApplication::translate("Plurals", "There are %n houses", 0, 1), QString("There is 1 house"));
110 QCOMPARE(QCoreApplication::translate("Plurals", "There are %n houses", 0, 2), QString("There are 2 houses"));
111 QCOMPARE(QCoreApplication::translate("Plurals", "There are %n houses", 0, 3), QString("There are 3 houses"));
112
113
114 // More plurals
115 QCOMPARE(tr("There are %n cars", "More Plurals", 0) , QString("There are 0 cars"));
116 QCOMPARE(tr("There are %n cars", "More Plurals", 1) , QString("There is 1 car"));
117 QCOMPARE(tr("There are %n cars", "More Plurals", 2) , QString("There are 2 cars"));
118 QCOMPARE(tr("There are %n cars", "More Plurals", 3) , QString("There are 3 cars"));
119
120
121 QCOMPARE(QCoreApplication::translate("no_en", "Kj\xc3\xb8r K\xc3\xa5re, kj\xc3\xa6re"), QString::fromUtf8("Drive K\xc3\xa5re, dear"));
122 QCOMPARE(QCoreApplication::translate("en_no", "Drive K\xc3\xa5re, dear"), QString::fromUtf8("Kj\xc3\xb8r K\xc3\xa5re, kj\xc3\xa6re"));
123 QCOMPARE(QCoreApplication::translate("en_ch", "Chinese symbol:"), QString::fromUtf8("Chinese symbol:\xe7\xb0\x9f"));
124
125 // printf("halo\r\nhallo");
126 // QCOMPARE(tr("This\r\nwill fail"), QString("THIS\nWILL FAIL")); // \r\n = 0d 0a
127
128 QCOMPARE(tr("Completely random string"),
129 QString::fromLatin1("Super-lange Uebersetzung mit Schikanen\x9c"
130 "Mittlere Uebersetung\x9c"
131 "Kurze Uebers."));
132
133 qApp->removeTranslator(&translator);
134 }
135
136 void tst_lrelease::compressed()
137 {
138 QVERIFY(!QProcess::execute(lrelease, QStringList() << "-compress" << (dataDir + "compressed.ts")));
139
140 QTranslator translator;
141 QVERIFY(translator.load(dataDir + "compressed.qm"));
142 qApp->installTranslator(&translator);
143
144 QCOMPARE(QCoreApplication::translate("Context1", "Foo"), QString::fromLatin1("in first context"));
145 QCOMPARE(QCoreApplication::translate("Context2", "Bar"), QString::fromLatin1("in second context"));
146
147 QCOMPARE(QCoreApplication::translate("Action1", "Component Name"), QString::fromLatin1("translation in first context"));
148 QCOMPARE(QCoreApplication::translate("Action2", "Component Name"), QString::fromLatin1("translation in second context"));
149 QCOMPARE(QCoreApplication::translate("Action3", "Component Name"), QString::fromLatin1("translation in third context"));
150
151 }
152
153 void tst_lrelease::idbased()
154 {
155 QVERIFY(!QProcess::execute(lrelease, QStringList{ (dataDir + "idbased.ts") }));
156
157 QTranslator translator;
158 QVERIFY(translator.load(dataDir + "idbased.qm"));
159 qApp->installTranslator(&translator);
160
161 QCOMPARE(qtTrId("test_id"), QString::fromLatin1("This is a test string."));
162 QCOMPARE(qtTrId("untranslated_id"), QString::fromLatin1("This has no translation."));
163 }
164
165 void tst_lrelease::markuntranslated()
166 {
167 QVERIFY(!QProcess::execute(lrelease,
168 QStringList{ "-markuntranslated", "#", (dataDir + "idbased.ts") }));
169
170 QTranslator translator;
171 QVERIFY(translator.load(dataDir + "idbased.qm"));
172 qApp->installTranslator(&translator);
173
174 QCOMPARE(qtTrId("test_id"), QString::fromLatin1("This is a test string."));
175 QCOMPARE(qtTrId("untranslated_id"), QString::fromLatin1("#This has no translation."));
176 }
177
178 void tst_lrelease::dupes()
179 {
180 QProcess proc;
181 proc.start(lrelease, QStringList() << (dataDir + "dupes.ts"), QIODevice::ReadWrite | QIODevice::Text);
182 QVERIFY(proc.waitForFinished());
183 QVERIFY(proc.exitStatus() == QProcess::NormalExit);
184 doCompare(QString(proc.readAllStandardError()).trimmed().split('\n'), dataDir + "dupes.errors");
185 }
186
187 void tst_lrelease::noTranslations()
188 {
189 QProcess proc;
190 proc.start(lrelease, { dataDir + "no-translations.pro" });
191 QVERIFY(proc.waitForFinished());
192 QCOMPARE(proc.exitStatus(), QProcess::NormalExit);
193 QCOMPARE(proc.exitCode(), 0);
194 auto stderrOutput = proc.readAllStandardError();
195 QVERIFY(stderrOutput.contains("lrelease warning: Met no 'TRANSLATIONS' entry in project file"));
196 }
197
198 QTEST_MAIN(tst_lrelease)
199 #include "tst_lrelease.moc"