forked from qt-creator/qt-creator
Utils: Improve FilePath::sort
Remove the conversion to/from QString for sorting. Change-Id: I89921328b6d9e952c802d41998495bd2ffbb9f99 Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
@@ -1531,11 +1531,17 @@ void FilePath::removeDuplicates(FilePaths &files)
|
||||
|
||||
void FilePath::sort(FilePaths &files)
|
||||
{
|
||||
// FIXME: Improve.
|
||||
// FIXME: This drops the osType information, which is not correct.
|
||||
QStringList list = transform<QStringList>(files, &FilePath::toString);
|
||||
list.sort();
|
||||
files = FileUtils::toFilePathList(list);
|
||||
std::sort(files.begin(), files.end(), [](const FilePath &a, const FilePath &b) {
|
||||
const int scheme = a.scheme().compare(b.scheme());
|
||||
if (scheme != 0)
|
||||
return scheme < 0;
|
||||
|
||||
const int host = a.host().compare(b.host());
|
||||
if (host != 0)
|
||||
return host < 0;
|
||||
|
||||
return a.pathView() < b.pathView();
|
||||
});
|
||||
}
|
||||
|
||||
void join(QString &left, const QString &right)
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <QRandomGenerator>
|
||||
#include <QtTest>
|
||||
|
||||
#include <utils/algorithm.h>
|
||||
#include <utils/filepath.h>
|
||||
#include <utils/hostosinfo.h>
|
||||
#include <utils/link.h>
|
||||
@@ -109,6 +110,9 @@ private slots:
|
||||
|
||||
void searchInWithFilter();
|
||||
|
||||
void sort();
|
||||
void sort_data();
|
||||
|
||||
private:
|
||||
QTemporaryDir tempDir;
|
||||
QString rootPath;
|
||||
@@ -1657,6 +1661,54 @@ void tst_filepath::tmp()
|
||||
}
|
||||
}
|
||||
|
||||
void tst_filepath::sort()
|
||||
{
|
||||
QFETCH(QStringList, input);
|
||||
|
||||
FilePaths filePaths = Utils::transform(input, &FilePath::fromString);
|
||||
QStringList sorted = input;
|
||||
sorted.sort();
|
||||
|
||||
FilePath::sort(filePaths);
|
||||
QStringList sortedPaths = Utils::transform(filePaths, &FilePath::toString);
|
||||
|
||||
QCOMPARE(sortedPaths, sorted);
|
||||
}
|
||||
|
||||
void tst_filepath::sort_data()
|
||||
{
|
||||
QTest::addColumn<QStringList>("input");
|
||||
|
||||
QTest::addRow("empty") << QStringList{};
|
||||
|
||||
QTest::addRow("one") << QStringList{"foo"};
|
||||
QTest::addRow("two") << QStringList{"foo", "bar"};
|
||||
QTest::addRow("three") << QStringList{"foo", "bar", "baz"};
|
||||
|
||||
QTest::addRow("one-absolute") << QStringList{"/foo"};
|
||||
QTest::addRow("two-absolute") << QStringList{"/foo", "/bar"};
|
||||
|
||||
QTest::addRow("one-relative") << QStringList{"foo"};
|
||||
|
||||
QTest::addRow("one-absolute-one-relative") << QStringList{"/foo", "bar"};
|
||||
|
||||
QTest::addRow("host") << QStringList{"ssh://test/blah", "ssh://gulp/blah", "ssh://zzz/blah"};
|
||||
|
||||
QTest::addRow("scheme") << QStringList{"ssh://test/blah",
|
||||
"ssh://gulp/blah",
|
||||
"ssh://zzz/blah",
|
||||
"aaa://gulp/blah",
|
||||
"xyz://test/blah"};
|
||||
|
||||
QTest::addRow("others") << QStringList{"a://a//a",
|
||||
"a://b//a",
|
||||
"a://a//b",
|
||||
"a://b//b",
|
||||
"b://b//b"};
|
||||
QTest::addRow("others-reversed")
|
||||
<< QStringList{"b://b//b", "a://b//b", "a://a//b", "a://b//a", "a://a//a"};
|
||||
}
|
||||
|
||||
QTEST_GUILESS_MAIN(tst_filepath)
|
||||
|
||||
#include "tst_filepath.moc"
|
||||
|
||||
Reference in New Issue
Block a user