1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29#include <QtTest/QtTest>
30
31#include <QtCore/private/qfilesystementry_p.h>
32
33class tst_QFileSystemEntry : public QObject
34{
35 Q_OBJECT
36
37private slots:
38 void getSetCheck_data();
39 void getSetCheck();
40 void suffix_data();
41 void suffix();
42 void completeSuffix_data();
43 void completeSuffix();
44 void baseName_data();
45 void baseName();
46 void completeBaseName_data();
47 void completeBaseName();
48#if defined(Q_OS_WIN)
49 void absoluteOrRelative_data();
50 void absoluteOrRelative();
51#endif
52 void isClean_data();
53 void isClean();
54 void defaultCtor();
55};
56
57#if defined(Q_OS_WIN)
58void tst_QFileSystemEntry::getSetCheck_data()
59{
60 QTest::addColumn<QString>("nativeFilePath");
61 QTest::addColumn<QString>("internalnativeFilePath");
62 QTest::addColumn<QString>("filepath");
63 QTest::addColumn<QString>("filename");
64 QTest::addColumn<QString>("baseName");
65 QTest::addColumn<QString>("completeBasename");
66 QTest::addColumn<QString>("suffix");
67 QTest::addColumn<QString>("completeSuffix");
68 QTest::addColumn<bool>("absolute");
69 QTest::addColumn<bool>("relative");
70
71 QString absPrefix = QLatin1String("\\\\?\\");
72 QString relPrefix = absPrefix
73 + QDir::toNativeSeparators(QDir::currentPath())
74 + QLatin1String("\\");
75
76 QTest::newRow("simple")
77 << QString("A:\\home\\qt\\in\\a\\dir.tar.gz")
78 << absPrefix + QString("A:\\home\\qt\\in\\a\\dir.tar.gz")
79 << "A:/home/qt/in/a/dir.tar.gz"
80 << "dir.tar.gz" << "dir" << "dir.tar" << "gz" << "tar.gz" << true << false;
81
82 QTest::newRow("relative")
83 << QString("in\\a\\dir.tar.gz")
84 << relPrefix + QString("in\\a\\dir.tar.gz")
85 << "in/a/dir.tar.gz"
86 << "dir.tar.gz" << "dir" << "dir.tar" << "gz" << "tar.gz" << false <<true;
87
88 QTest::newRow("noSuffix")
89 << QString("myDir\\myfile")
90 << relPrefix + QString("myDir\\myfile")
91 << "myDir/myfile" << "myfile" << "myfile" << "myfile" << "" << "" << false <<true;
92
93 QTest::newRow("noLongSuffix")
94 << QString("myDir\\myfile.txt")
95 << relPrefix + QString("myDir\\myfile.txt")
96 << "myDir/myfile.txt" << "myfile.txt" << "myfile" << "myfile" << "txt" << "txt" << false << true;
97
98 QTest::newRow("endingSlash")
99 << QString("myDir\\myfile.bla\\")
100 << relPrefix + QString("myDir\\myfile.bla\\")
101 << "myDir/myfile.bla/" << "" << "" << "" << "" << "" << false << true;
102
103 QTest::newRow("absolutePath")
104 << QString("A:dir\\without\\leading\\backslash.bat")
105 << absPrefix + QString("A:\\dir\\without\\leading\\backslash.bat")
106 << "A:dir/without/leading/backslash.bat" << "backslash.bat" << "backslash" << "backslash" << "bat" << "bat" << false << false;
107}
108
109void tst_QFileSystemEntry::getSetCheck()
110{
111 QFETCH(QString, nativeFilePath);
112 QFETCH(QString, internalnativeFilePath);
113 QFETCH(QString, filepath);
114 QFETCH(QString, filename);
115 QFETCH(QString, baseName);
116 QFETCH(QString, completeBasename);
117 QFETCH(QString, suffix);
118 QFETCH(QString, completeSuffix);
119 QFETCH(bool, absolute);
120 QFETCH(bool, relative);
121
122 QFileSystemEntry entry1(filepath);
123 QCOMPARE(entry1.filePath(), filepath);
124 QCOMPARE(entry1.nativeFilePath().toLower(), internalnativeFilePath.toLower());
125 QCOMPARE(entry1.fileName(), filename);
126 QCOMPARE(entry1.suffix(), suffix);
127 QCOMPARE(entry1.completeSuffix(), completeSuffix);
128 QCOMPARE(entry1.isAbsolute(), absolute);
129 QCOMPARE(entry1.isRelative(), relative);
130 QCOMPARE(entry1.baseName(), baseName);
131 QCOMPARE(entry1.completeBaseName(), completeBasename);
132
133 QFileSystemEntry entry2(nativeFilePath, QFileSystemEntry::FromNativePath());
134 QCOMPARE(entry2.suffix(), suffix);
135 QCOMPARE(entry2.completeSuffix(), completeSuffix);
136 QCOMPARE(entry2.isAbsolute(), absolute);
137 QCOMPARE(entry2.isRelative(), relative);
138 QCOMPARE(entry2.filePath(), filepath);
139 // Since this entry was created using the native path,
140 // the object shouldnot change nativeFilePath.
141 QCOMPARE(entry2.nativeFilePath(), nativeFilePath);
142 QCOMPARE(entry2.fileName(), filename);
143 QCOMPARE(entry2.baseName(), baseName);
144 QCOMPARE(entry2.completeBaseName(), completeBasename);
145}
146
147#else
148
149void tst_QFileSystemEntry::getSetCheck_data()
150{
151 QTest::addColumn<QByteArray>(name: "nativeFilePath");
152 QTest::addColumn<QString>(name: "filepath");
153 QTest::addColumn<QString>(name: "filename");
154 QTest::addColumn<QString>(name: "basename");
155 QTest::addColumn<QString>(name: "completeBasename");
156 QTest::addColumn<QString>(name: "suffix");
157 QTest::addColumn<QString>(name: "completeSuffix");
158 QTest::addColumn<bool>(name: "absolute");
159
160 QTest::newRow(dataTag: "empty")
161 << QByteArray()
162 << QString()
163 << QString() << QString() << QString() << QString() << QString() << false;
164
165 QTest::newRow(dataTag: "simple")
166 << QByteArray("/home/qt/in/a/dir.tar.gz")
167 << "/home/qt/in/a/dir.tar.gz"
168 << "dir.tar.gz" << "dir" << "dir.tar" << "gz" << "tar.gz" << true;
169
170 QTest::newRow(dataTag: "relative")
171 << QByteArray("in/a/dir.tar.gz")
172 << "in/a/dir.tar.gz"
173 << "dir.tar.gz" << "dir" << "dir.tar" << "gz" << "tar.gz" << false;
174
175 QTest::newRow(dataTag: "noSuffix")
176 << QByteArray("myDir/myfile")
177 << "myDir/myfile" << "myfile" << "myfile" << "myfile" << "" << "" << false;
178
179 QTest::newRow(dataTag: "noLongSuffix")
180 << QByteArray("myDir/myfile.txt")
181 << "myDir/myfile.txt" << "myfile.txt" << "myfile" << "myfile" << "txt" << "txt" << false;
182
183 QTest::newRow(dataTag: "endingSlash")
184 << QByteArray("myDir/myfile.bla/")
185 << "myDir/myfile.bla/" << "" << "" << "" << "" << "" << false;
186
187 QTest::newRow(dataTag: "relativePath")
188 << QByteArray("A:dir/without/leading/backslash.bat")
189 << "A:dir/without/leading/backslash.bat" << "backslash.bat" << "backslash" << "backslash" << "bat" << "bat" << false;
190}
191
192void tst_QFileSystemEntry::getSetCheck()
193{
194 QFETCH(QByteArray, nativeFilePath);
195 QFETCH(QString, filepath);
196 QFETCH(QString, filename);
197 QFETCH(QString, basename);
198 QFETCH(QString, completeBasename);
199 QFETCH(QString, suffix);
200 QFETCH(QString, completeSuffix);
201 QFETCH(bool, absolute);
202
203 QFileSystemEntry entry1(filepath);
204 QCOMPARE(entry1.filePath(), filepath);
205 QCOMPARE(entry1.nativeFilePath(), nativeFilePath);
206 QCOMPARE(entry1.fileName(), filename);
207 QCOMPARE(entry1.suffix(), suffix);
208 QCOMPARE(entry1.completeSuffix(), completeSuffix);
209 QCOMPARE(entry1.isAbsolute(), absolute);
210 QCOMPARE(entry1.isRelative(), !absolute);
211 QCOMPARE(entry1.baseName(), basename);
212 QCOMPARE(entry1.completeBaseName(), completeBasename);
213
214 QFileSystemEntry entry2(nativeFilePath, QFileSystemEntry::FromNativePath());
215 QCOMPARE(entry2.suffix(), suffix);
216 QCOMPARE(entry2.completeSuffix(), completeSuffix);
217 QCOMPARE(entry2.isAbsolute(), absolute);
218 QCOMPARE(entry2.isRelative(), !absolute);
219 QCOMPARE(entry2.filePath(), filepath);
220 QCOMPARE(entry2.nativeFilePath(), nativeFilePath);
221 QCOMPARE(entry2.fileName(), filename);
222 QCOMPARE(entry2.baseName(), basename);
223 QCOMPARE(entry2.completeBaseName(), completeBasename);
224}
225#endif
226
227void tst_QFileSystemEntry::suffix_data()
228{
229 QTest::addColumn<QString>(name: "file");
230 QTest::addColumn<QString>(name: "expected");
231
232 QTest::newRow(dataTag: "empty") << QString() << QString();
233 QTest::newRow(dataTag: "noextension0") << "file" << "";
234 QTest::newRow(dataTag: "noextension1") << "/path/to/file" << "";
235 QTest::newRow(dataTag: "data0") << "file.tar" << "tar";
236 QTest::newRow(dataTag: "data1") << "file.tar.gz" << "gz";
237 QTest::newRow(dataTag: "data2") << "/path/file/file.tar.gz" << "gz";
238 QTest::newRow(dataTag: "data3") << "/path/file.tar" << "tar";
239 QTest::newRow(dataTag: "hidden1-1") << ".ext1" << "ext1";
240 QTest::newRow(dataTag: "hidden1-2") << ".ext" << "ext";
241 QTest::newRow(dataTag: "hidden1-3") << ".ex" << "ex";
242 QTest::newRow(dataTag: "hidden1-4") << ".e" << "e";
243 QTest::newRow(dataTag: "hidden2-1") << ".ext1.ext2" << "ext2";
244 QTest::newRow(dataTag: "hidden2-2") << ".ext.ext2" << "ext2";
245 QTest::newRow(dataTag: "hidden2-3") << ".ex.ext2" << "ext2";
246 QTest::newRow(dataTag: "hidden2-4") << ".e.ext2" << "ext2";
247 QTest::newRow(dataTag: "hidden2-5") << "..ext2" << "ext2";
248 QTest::newRow(dataTag: "dots") << "/path/file.with.dots/file..ext2" << "ext2";
249 QTest::newRow(dataTag: "dots2") << "/path/file.with.dots/.file..ext2" << "ext2";
250}
251
252void tst_QFileSystemEntry::suffix()
253{
254 QFETCH(QString, file);
255 QFETCH(QString, expected);
256
257 QFileSystemEntry fe(file);
258 QCOMPARE(fe.suffix(), expected);
259
260 QFileSystemEntry fi2(file);
261 // first resolve the last slash
262 (void) fi2.path();
263 QCOMPARE(fi2.suffix(), expected);
264}
265
266void tst_QFileSystemEntry::completeSuffix_data()
267{
268 QTest::addColumn<QString>(name: "file");
269 QTest::addColumn<QString>(name: "expected");
270
271 QTest::newRow(dataTag: "empty") << QString() << QString();
272 QTest::newRow(dataTag: "noextension0") << "file" << "";
273 QTest::newRow(dataTag: "noextension1") << "/path/to/file" << "";
274 QTest::newRow(dataTag: "data0") << "file.tar" << "tar";
275 QTest::newRow(dataTag: "data1") << "file.tar.gz" << "tar.gz";
276 QTest::newRow(dataTag: "data2") << "/path/file/file.tar.gz" << "tar.gz";
277 QTest::newRow(dataTag: "data3") << "/path/file.tar" << "tar";
278 QTest::newRow(dataTag: "dots") << "/path/file.with.dots/file..ext2" << ".ext2";
279 QTest::newRow(dataTag: "dots2") << "/path/file.with.dots/.file..ext2" << "file..ext2";
280}
281
282void tst_QFileSystemEntry::completeSuffix()
283{
284 QFETCH(QString, file);
285 QFETCH(QString, expected);
286
287 QFileSystemEntry fi(file);
288 QCOMPARE(fi.completeSuffix(), expected);
289
290 QFileSystemEntry fi2(file);
291 // first resolve the last slash
292 (void) fi2.path();
293 QCOMPARE(fi2.completeSuffix(), expected);
294}
295
296void tst_QFileSystemEntry::baseName_data()
297{
298 QTest::addColumn<QString>(name: "file");
299 QTest::addColumn<QString>(name: "expected");
300
301 QTest::newRow(dataTag: "empty") << QString() << QString();
302 QTest::newRow(dataTag: "data0") << "file.tar" << "file";
303 QTest::newRow(dataTag: "data1") << "file.tar.gz" << "file";
304 QTest::newRow(dataTag: "data2") << "/path/file/file.tar.gz" << "file";
305 QTest::newRow(dataTag: "data3") << "/path/file.tar" << "file";
306 QTest::newRow(dataTag: "data4") << "/path/file" << "file";
307 QTest::newRow(dataTag: "dots") << "/path/file.with.dots/file..ext2" << "file";
308 QTest::newRow(dataTag: "dots2") << "/path/file.with.dots/.file..ext2" << "";
309}
310
311void tst_QFileSystemEntry::baseName()
312{
313 QFETCH(QString, file);
314 QFETCH(QString, expected);
315
316 QFileSystemEntry fi(file);
317 QCOMPARE(fi.baseName(), expected);
318
319 QFileSystemEntry fi2(file);
320 // first resolve the last slash
321 (void) fi2.path();
322 QCOMPARE(fi2.baseName(), expected);
323}
324
325void tst_QFileSystemEntry::completeBaseName_data()
326{
327 QTest::addColumn<QString>(name: "file");
328 QTest::addColumn<QString>(name: "expected");
329
330 QTest::newRow(dataTag: "empty") << QString() << QString();
331 QTest::newRow(dataTag: "data0") << "file.tar" << "file";
332 QTest::newRow(dataTag: "data1") << "file.tar.gz" << "file.tar";
333 QTest::newRow(dataTag: "data2") << "/path/file/file.tar.gz" << "file.tar";
334 QTest::newRow(dataTag: "data3") << "/path/file.tar" << "file";
335 QTest::newRow(dataTag: "data4") << "/path/file" << "file";
336 QTest::newRow(dataTag: "dots") << "/path/file.with.dots/file..ext2" << "file.";
337 QTest::newRow(dataTag: "dots2") << "/path/file.with.dots/.file..ext2" << ".file.";
338}
339
340void tst_QFileSystemEntry::completeBaseName()
341{
342 QFETCH(QString, file);
343 QFETCH(QString, expected);
344
345 QFileSystemEntry fi(file);
346 QCOMPARE(fi.completeBaseName(), expected);
347
348 QFileSystemEntry fi2(file);
349 // first resolve the last slash
350 (void) fi2.path();
351 QCOMPARE(fi2.completeBaseName(), expected);
352}
353
354#if defined(Q_OS_WIN)
355void tst_QFileSystemEntry::absoluteOrRelative_data()
356{
357 QTest::addColumn<QString>("path");
358 QTest::addColumn<bool>("isAbsolute");
359 QTest::addColumn<bool>("isRelative");
360
361 QTest::newRow("empty") << QString() << false << true;
362 QTest::newRow("data0") << "file.tar" << false << true;
363 QTest::newRow("data1") << "/path/file/file.tar.gz" << false << false;
364 QTest::newRow("data1") << "C:path/file/file.tar.gz" << false << false;
365 QTest::newRow("data3") << "C:/path/file" << true << false;
366 QTest::newRow("data3") << "//machine/share" << true << false;
367}
368
369void tst_QFileSystemEntry::absoluteOrRelative()
370{
371 QFETCH(QString, path);
372 QFETCH(bool, isAbsolute);
373 QFETCH(bool, isRelative);
374
375 QFileSystemEntry fi(path);
376 QCOMPARE(fi.isAbsolute(), isAbsolute);
377 QCOMPARE(fi.isRelative(), isRelative);
378}
379#endif
380
381void tst_QFileSystemEntry::isClean_data()
382{
383 QTest::addColumn<QString>(name: "path");
384 QTest::addColumn<bool>(name: "isClean");
385
386 QTest::newRow(dataTag: "empty") << QString() << true;
387 QTest::newRow(dataTag: "simple") << "foo" << true;
388 QTest::newRow(dataTag: "complex") << "/foo/bar/bz" << true;
389 QTest::newRow(dataTag: ".file") << "/foo/.file" << true;
390 QTest::newRow(dataTag: "..file") << "/foo/..file" << true;
391 QTest::newRow(dataTag: "...") << "/foo/.../bar" << true;
392 QTest::newRow(dataTag: "./") << "./" << false;
393 QTest::newRow(dataTag: "../") << "../" << false;
394 QTest::newRow(dataTag: ".") << "." << false;
395 QTest::newRow(dataTag: "..") << ".." << false;
396 QTest::newRow(dataTag: "/.") << "/." << false;
397 QTest::newRow(dataTag: "/..") << "/.." << false;
398 QTest::newRow(dataTag: "/../") << "foo/../bar" << false;
399 QTest::newRow(dataTag: "/./") << "foo/./bar" << false;
400 QTest::newRow(dataTag: "//") << "foo//bar" << false;
401}
402
403void tst_QFileSystemEntry::isClean()
404{
405 QFETCH(QString, path);
406 QFETCH(bool, isClean);
407
408 QFileSystemEntry fi(path);
409 QCOMPARE(fi.isClean(), isClean);
410}
411
412void tst_QFileSystemEntry::defaultCtor()
413{
414 QFileSystemEntry entry;
415
416 QVERIFY(entry.filePath().isNull());
417 QVERIFY(entry.nativeFilePath().isNull());
418
419 QVERIFY(entry.fileName().isNull());
420 QCOMPARE(entry.path(), QString("."));
421
422 QVERIFY(entry.baseName().isNull());
423 QVERIFY(entry.completeBaseName().isNull());
424 QVERIFY(entry.suffix().isNull());
425 QVERIFY(entry.completeSuffix().isNull());
426
427 QVERIFY(!entry.isAbsolute());
428 QVERIFY(entry.isRelative());
429
430 QVERIFY(entry.isClean());
431
432#if defined(Q_OS_WIN)
433 QVERIFY(!entry.isDriveRoot());
434#endif
435 QVERIFY(!entry.isRoot());
436
437 QVERIFY(entry.isEmpty());
438}
439
440QTEST_MAIN(tst_QFileSystemEntry)
441#include <tst_qfilesystementry.moc>
442

source code of qtbase/tests/auto/corelib/io/qfilesystementry/tst_qfilesystementry.cpp