|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the Qt Linguist of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "testlupdate.h" |
|
43 #if CHECK_SIMTEXTH |
|
44 #include "../shared/simtexth.h" |
|
45 #endif |
|
46 |
|
47 #include <QtCore/QDir> |
|
48 #include <QtCore/QDebug> |
|
49 #include <QtCore/QFile> |
|
50 #include <QtCore/QByteArray> |
|
51 |
|
52 #include <QtTest/QtTest> |
|
53 |
|
54 class tst_lupdate : public QObject |
|
55 { |
|
56 Q_OBJECT |
|
57 public: |
|
58 tst_lupdate() { m_basePath = QDir::currentPath() + QLatin1String("/testdata/"); } |
|
59 |
|
60 private slots: |
|
61 void good_data(); |
|
62 void good(); |
|
63 void output_ts(); |
|
64 void commandline_data(); |
|
65 void commandline(); |
|
66 #if CHECK_SIMTEXTH |
|
67 void simtexth(); |
|
68 void simtexth_data(); |
|
69 #endif |
|
70 |
|
71 private: |
|
72 TestLUpdate m_lupdate; |
|
73 QString m_basePath; |
|
74 |
|
75 void doCompare(const QStringList &actual, const QString &expectedFn, bool err); |
|
76 void doCompare(const QString &actualFn, const QString &expectedFn, bool err); |
|
77 }; |
|
78 |
|
79 |
|
80 void tst_lupdate::doCompare(const QStringList &actual, const QString &expectedFn, bool err) |
|
81 { |
|
82 QFile file(expectedFn); |
|
83 QVERIFY(file.open(QIODevice::ReadOnly)); |
|
84 QStringList expected = QString(file.readAll()).trimmed().remove('\r').split('\n'); |
|
85 |
|
86 int i = 0, ei = expected.size(), gi = actual.size(); |
|
87 for (; ; i++) { |
|
88 if (i == gi) { |
|
89 if (i == ei) |
|
90 return; |
|
91 gi = 0; |
|
92 break; |
|
93 } else if (i == ei) { |
|
94 ei = 0; |
|
95 break; |
|
96 } else { |
|
97 QString act = actual.at(i); |
|
98 act.remove('\r'); |
|
99 if (err ? !QRegExp(expected.at(i)).exactMatch(act) : |
|
100 (act != expected.at(i))) { |
|
101 bool cond = true; |
|
102 while (cond) { |
|
103 act = actual.at(gi - 1); |
|
104 act.remove('\r'); |
|
105 cond = (ei - 1) >= i && (gi - 1) >= i && |
|
106 (err ? QRegExp(expected.at(ei - 1)).exactMatch(act) : |
|
107 (act == expected.at(ei - 1))); |
|
108 if (cond) { |
|
109 ei--, gi--; |
|
110 } |
|
111 } |
|
112 break; |
|
113 } |
|
114 } |
|
115 } |
|
116 QByteArray diff; |
|
117 for (int j = qMax(0, i - 3); j < i; j++) |
|
118 diff += expected.at(j) + '\n'; |
|
119 diff += "<<<<<<< got\n"; |
|
120 for (int j = i; j < gi; j++) { |
|
121 diff += actual.at(j) + '\n'; |
|
122 if (j >= i + 5) { |
|
123 diff += "...\n"; |
|
124 break; |
|
125 } |
|
126 } |
|
127 diff += "=========\n"; |
|
128 for (int j = i; j < ei; j++) { |
|
129 diff += expected.at(j) + '\n'; |
|
130 if (j >= i + 5) { |
|
131 diff += "...\n"; |
|
132 break; |
|
133 } |
|
134 } |
|
135 diff += ">>>>>>> expected\n"; |
|
136 for (int j = ei; j < qMin(ei + 3, expected.size()); j++) |
|
137 diff += expected.at(j) + '\n'; |
|
138 QFAIL(qPrintable((err ? "Output for " : "Result for ") + expectedFn + " does not meet expectations:\n" + diff)); |
|
139 } |
|
140 |
|
141 void tst_lupdate::doCompare(const QString &actualFn, const QString &expectedFn, bool err) |
|
142 { |
|
143 QFile afile(actualFn); |
|
144 QVERIFY(afile.open(QIODevice::ReadOnly)); |
|
145 QStringList actual = QString(afile.readAll()).trimmed().remove('\r').split('\n'); |
|
146 |
|
147 doCompare(actual, expectedFn, err); |
|
148 } |
|
149 |
|
150 void tst_lupdate::good_data() |
|
151 { |
|
152 QTest::addColumn<QString>("directory"); |
|
153 |
|
154 QDir parsingDir(m_basePath + "good"); |
|
155 QStringList dirs = parsingDir.entryList(QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name); |
|
156 |
|
157 #ifndef Q_OS_WIN |
|
158 dirs.removeAll(QLatin1String("backslashes")); |
|
159 #endif |
|
160 |
|
161 foreach (const QString &dir, dirs) |
|
162 QTest::newRow(dir.toLocal8Bit()) << dir; |
|
163 } |
|
164 |
|
165 void tst_lupdate::good() |
|
166 { |
|
167 QFETCH(QString, directory); |
|
168 |
|
169 QString dir = m_basePath + "good/" + directory; |
|
170 QString expectedFile = dir + QLatin1String("/project.ts.result"); |
|
171 |
|
172 qDebug() << "Checking..."; |
|
173 |
|
174 // qmake will delete the previous one, to ensure that we don't do any merging.... |
|
175 QString generatedtsfile(QLatin1String("project.ts")); |
|
176 |
|
177 m_lupdate.setWorkingDirectory(dir); |
|
178 m_lupdate.qmake(); |
|
179 // look for a command |
|
180 QString lupdatecmd; |
|
181 QFile file(dir + "/lupdatecmd"); |
|
182 if (file.exists()) { |
|
183 QVERIFY(file.open(QIODevice::ReadOnly)); |
|
184 while (!file.atEnd()) { |
|
185 QByteArray cmdstring = file.readLine().simplified(); |
|
186 if (cmdstring.startsWith('#')) |
|
187 continue; |
|
188 if (cmdstring.startsWith("lupdate")) { |
|
189 cmdstring.remove(0, 8); |
|
190 lupdatecmd.append(cmdstring); |
|
191 break; |
|
192 } else if (cmdstring.startsWith("TRANSLATION:")) { |
|
193 cmdstring.remove(0, 12); |
|
194 generatedtsfile = dir + QLatin1Char('/') + cmdstring.trimmed(); |
|
195 } |
|
196 } |
|
197 file.close(); |
|
198 } |
|
199 |
|
200 if (lupdatecmd.isEmpty()) { |
|
201 lupdatecmd = QLatin1String("project.pro -ts project.ts"); |
|
202 } |
|
203 lupdatecmd.prepend("-silent "); |
|
204 m_lupdate.updateProFile(lupdatecmd); |
|
205 |
|
206 // If the file expectedoutput.txt exists, compare the |
|
207 // console output with the content of that file |
|
208 QFile outfile(dir + "/expectedoutput.txt"); |
|
209 if (outfile.exists()) { |
|
210 QString errs = m_lupdate.getErrorMessages().at(1).trimmed(); |
|
211 QStringList errslist = errs.split(QLatin1Char('\n')); |
|
212 doCompare(errslist, outfile.fileName(), true); |
|
213 if (QTest::currentTestFailed()) |
|
214 return; |
|
215 } |
|
216 |
|
217 doCompare(generatedtsfile, expectedFile, false); |
|
218 } |
|
219 |
|
220 void tst_lupdate::output_ts() |
|
221 { |
|
222 QString dir = m_basePath + "output_ts"; |
|
223 m_lupdate.setWorkingDirectory(dir); |
|
224 |
|
225 // look for a command |
|
226 QString lupdatecmd; |
|
227 QFile file(dir + "/lupdatecmd"); |
|
228 if (file.exists()) { |
|
229 QVERIFY(file.open(QIODevice::ReadOnly)); |
|
230 while (!file.atEnd()) { |
|
231 QByteArray cmdstring = file.readLine().simplified(); |
|
232 if (cmdstring.startsWith('#')) |
|
233 continue; |
|
234 if (cmdstring.startsWith("lupdate")) { |
|
235 cmdstring.remove(0, 8); |
|
236 lupdatecmd.append(cmdstring); |
|
237 break; |
|
238 } |
|
239 } |
|
240 file.close(); |
|
241 } |
|
242 |
|
243 QDir parsingDir(m_basePath + "output_ts"); |
|
244 |
|
245 QString generatedtsfile = |
|
246 dir + QLatin1String("/toplevel/library/tools/translations/project.ts"); |
|
247 |
|
248 QFile::remove(generatedtsfile); |
|
249 |
|
250 lupdatecmd.prepend("-silent "); |
|
251 m_lupdate.qmake(); |
|
252 m_lupdate.updateProFile(lupdatecmd); |
|
253 |
|
254 // If the file expectedoutput.txt exists, compare the |
|
255 // console output with the content of that file |
|
256 QFile outfile(dir + "/expectedoutput.txt"); |
|
257 if (outfile.exists()) { |
|
258 QString errs = m_lupdate.getErrorMessages().at(1).trimmed(); |
|
259 QStringList errslist = errs.split(QLatin1Char('\n')); |
|
260 doCompare(errslist, outfile.fileName(), true); |
|
261 if (QTest::currentTestFailed()) |
|
262 return; |
|
263 } |
|
264 |
|
265 doCompare(generatedtsfile, dir + QLatin1String("/project.ts.result"), false); |
|
266 } |
|
267 |
|
268 void tst_lupdate::commandline_data() |
|
269 { |
|
270 QTest::addColumn<QString>("currentPath"); |
|
271 QTest::addColumn<QString>("commandline"); |
|
272 QTest::addColumn<QString>("generatedtsfile"); |
|
273 QTest::addColumn<QString>("expectedtsfile"); |
|
274 |
|
275 QTest::newRow("Recursive scan") << QString("recursivescan") |
|
276 << QString(". -ts foo.ts") << QString("foo.ts") << QString("foo.ts.result"); |
|
277 QTest::newRow("Deep path argument") << QString("recursivescan") |
|
278 << QString("sub/finddialog.cpp -ts bar.ts") << QString("bar.ts") << QString("bar.ts.result"); |
|
279 } |
|
280 |
|
281 void tst_lupdate::commandline() |
|
282 { |
|
283 QFETCH(QString, currentPath); |
|
284 QFETCH(QString, commandline); |
|
285 QFETCH(QString, generatedtsfile); |
|
286 QFETCH(QString, expectedtsfile); |
|
287 |
|
288 m_lupdate.setWorkingDirectory(m_basePath + currentPath); |
|
289 QString generated = |
|
290 m_basePath + currentPath + QLatin1Char('/') + generatedtsfile; |
|
291 QFile gen(generated); |
|
292 if (gen.exists()) |
|
293 QVERIFY(gen.remove()); |
|
294 if (!m_lupdate.run("-silent " + commandline)) |
|
295 qDebug() << m_lupdate.getErrorMessages().last(); |
|
296 |
|
297 doCompare(generated, m_basePath + currentPath + QLatin1Char('/') + expectedtsfile, false); |
|
298 } |
|
299 |
|
300 #if CHECK_SIMTEXTH |
|
301 void tst_lupdate::simtexth() |
|
302 { |
|
303 QFETCH(QString, one); |
|
304 QFETCH(QString, two); |
|
305 QFETCH(int, expected); |
|
306 |
|
307 int measured = getSimilarityScore(one, two.toLatin1()); |
|
308 QCOMPARE(measured, expected); |
|
309 } |
|
310 |
|
311 |
|
312 void tst_lupdate::simtexth_data() |
|
313 { |
|
314 using namespace QTest; |
|
315 |
|
316 addColumn<QString>("one"); |
|
317 addColumn<QString>("two"); |
|
318 addColumn<int>("expected"); |
|
319 |
|
320 newRow("00") << "" << "" << 1024; |
|
321 newRow("01") << "a" << "a" << 1024; |
|
322 newRow("02") << "ab" << "ab" << 1024; |
|
323 newRow("03") << "abc" << "abc" << 1024; |
|
324 newRow("04") << "abcd" << "abcd" << 1024; |
|
325 } |
|
326 #endif |
|
327 |
|
328 QTEST_MAIN(tst_lupdate) |
|
329 #include "tst_lupdate.moc" |