1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
|
/*
SPDX-FileCopyrightText: Milian Wolff <mail@milianw.de>
SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
#include "test_quickopen.h"
#include <interfaces/idocumentcontroller.h>
#include <project/projectutils.h>
#include <QTemporaryDir>
#include <QTest>
#include <QTemporaryFile>
#include <type_traits>
#include <utility>
QTEST_MAIN(TestQuickOpen)
using namespace KDevelop;
static_assert(std::is_nothrow_move_assignable<ProjectFile>(), "Why would a move assignment operator throw?");
static_assert(std::is_nothrow_move_constructible<ProjectFile>(), "Why would a move constructor throw?");
using ItemList = QVector<DUChainItem>;
using StringList = QVector<QString>;
TestQuickOpen::TestQuickOpen(QObject* parent)
: QuickOpenTestBase(Core::Default, parent)
{
QStandardPaths::setTestModeEnabled(true);
}
void TestQuickOpen::testProjectFileSwap()
{
QScopedPointer<TestProject> project(getProjectWithFiles(2));
QVector<ProjectFile> projectFiles;
KDevelop::forEachFile(project->projectItem(), [&projectFiles](ProjectFileItem* fileItem) {
projectFiles.push_back(ProjectFile{fileItem});
});
QCOMPARE(projectFiles.size(), 2);
const auto equivalent = [](const ProjectFile &x, const ProjectFile &y) {
return !(x < y) && !(y < x);
};
ProjectFile a = projectFiles.at(0);
ProjectFile b = projectFiles.at(1);
QCOMPARE(a.projectPath, b.projectPath);
QVERIFY(!equivalent(a, b));
const auto aCopy = a;
const auto bCopy = b;
QCOMPARE(aCopy.projectPath, a.projectPath);
QVERIFY(equivalent(aCopy, a));
QCOMPARE(bCopy.projectPath, b.projectPath);
QVERIFY(equivalent(bCopy, b));
using std::swap;
swap(a, b);
QCOMPARE(a.projectPath, bCopy.projectPath);
QVERIFY(equivalent(a, bCopy));
QCOMPARE(b.projectPath, aCopy.projectPath);
QVERIFY(equivalent(b, aCopy));
QString anotherProjectPath = "/some/special/path/to/a-project";
#ifdef Q_OS_WIN
anotherProjectPath.prepend("C:");
#endif
a.projectPath = Path{anotherProjectPath};
QCOMPARE(a.projectPath.pathOrUrl(), anotherProjectPath);
swap(a, b);
QCOMPARE(a.projectPath, aCopy.projectPath);
QVERIFY(equivalent(a, aCopy));
QCOMPARE(b.projectPath.pathOrUrl(), anotherProjectPath);
QVERIFY(equivalent(b, bCopy));
QVERIFY(a.projectPath != b.projectPath);
QVERIFY(!equivalent(a, b));
}
void TestQuickOpen::testDuchainFilter()
{
QFETCH(ItemList, items);
QFETCH(QString, filter);
QFETCH(ItemList, filtered);
auto toStringList = [](const ItemList& items) {
QStringList result;
for (const DUChainItem& item: items) {
result << item.m_text;
}
return result;
};
TestFilter filterItems;
filterItems.setItems(items);
filterItems.setFilter(filter);
QCOMPARE(toStringList(filterItems.filteredItems()), toStringList(filtered));
}
void TestQuickOpen::testDuchainFilter_data()
{
QTest::addColumn<ItemList>("items");
QTest::addColumn<QString>("filter");
QTest::addColumn<ItemList>("filtered");
auto i = [](const QString& text) {
auto item = DUChainItem();
item.m_text = text;
return item;
};
auto items = ItemList()
<< i(QStringLiteral("KTextEditor::Cursor"))
<< i(QStringLiteral("void KTextEditor::Cursor::explode()"))
<< i(QStringLiteral("QVector<int> SomeNamespace::SomeClass::func(int)"));
QTest::newRow("prefix") << items << "KTE" << (ItemList() << items.at(0) << items.at(1));
QTest::newRow("prefix_mismatch") << items << "KTEY" << (ItemList());
QTest::newRow("prefix_colon") << items << "KTE:" << (ItemList() << items.at(0) << items.at(1));
QTest::newRow("prefix_colon_mismatch") << items << "KTE:Y" << (ItemList());
QTest::newRow("prefix_colon_mismatch2") << items << "XKTE:" << (ItemList());
QTest::newRow("prefix_two_colon") << items << "KTE::" << (ItemList() << items.at(0) << items.at(1));
QTest::newRow("prefix_two_colon_mismatch") << items << "KTE::Y" << (ItemList());
QTest::newRow("prefix_two_colon_mismatch2") << items << "XKTE::" << (ItemList());
QTest::newRow("suffix") << items << "Curs" << (ItemList() << items.at(0) << items.at(1));
QTest::newRow("suffix2") << items << "curs" << (ItemList() << items.at(0) << items.at(1));
QTest::newRow("mid") << items << "SomeClass" << (ItemList() << items.at(2));
QTest::newRow("mid_abbrev") << items << "SClass" << (ItemList() << items.at(2));
}
void TestQuickOpen::testAbbreviations()
{
QFETCH(StringList, items);
QFETCH(QString, filter);
QFETCH(StringList, filtered);
PathTestFilter filterItems;
filterItems.setItems(std::move(items));
filterItems.setFilter(filter.split('/', Qt::SkipEmptyParts));
QCOMPARE(filterItems.filteredItems(), filtered);
}
void TestQuickOpen::testAbbreviations_data()
{
QTest::addColumn<StringList>("items");
QTest::addColumn<QString>("filter");
QTest::addColumn<StringList>("filtered");
const StringList items = {
QStringLiteral("/foo/bar/caz/a.h"),
QStringLiteral("/KateThing/CMakeLists.txt"),
QStringLiteral("/FooBar/FooBar/Footestfoo.h") };
QTest::newRow("path_segments") << items << "fbc" << StringList();
QTest::newRow("path_segment_abbrev") << items << "cmli" << StringList({ items.at(1) });
QTest::newRow("path_segment_old") << items << "kate/cmake" << StringList({ items.at(1) });
QTest::newRow("path_segment_multi_mixed") << items << "ftfoo.h" << StringList({ items.at(2) });
}
void TestQuickOpen::testSorting()
{
QFETCH(StringList, items);
QFETCH(QString, filter);
QFETCH(StringList, filtered);
const auto filterList = filter.split('/', Qt::SkipEmptyParts);
PathTestFilter filterItems;
filterItems.setItems(std::move(items));
filterItems.setFilter(filterList);
QEXPECT_FAIL("bar7", "empty parts are skipped", Abort);
if (filterItems.filteredItems() != filtered)
qWarning() << filterItems.filteredItems() << filtered;
QCOMPARE(filterItems.filteredItems(), filtered);
// check whether sorting is stable
filterItems.setFilter(filterList);
QCOMPARE(filterItems.filteredItems(), filtered);
}
void TestQuickOpen::testSorting_data()
{
QTest::addColumn<StringList>("items");
QTest::addColumn<QString>("filter");
QTest::addColumn<StringList>("filtered");
const StringList items({
QStringLiteral("/foo/a.h"),
QStringLiteral("/foo/ab.h"),
QStringLiteral("/foo/bc.h"),
QStringLiteral("/bar/a.h")});
{
QTest::newRow("no-filter") << items << QString() << items;
}
{
const StringList filtered = { QStringLiteral("/bar/a.h") };
QTest::newRow("bar1") << items << QStringLiteral("bar") << filtered;
QTest::newRow("bar2") << items << QStringLiteral("/bar") << filtered;
QTest::newRow("bar3") << items << QStringLiteral("/bar/") << filtered;
QTest::newRow("bar4") << items << QStringLiteral("bar/") << filtered;
QTest::newRow("bar5") << items << QStringLiteral("ar/") << filtered;
QTest::newRow("bar6") << items << QStringLiteral("r/") << filtered;
QTest::newRow("bar7") << items << QStringLiteral("b/") << filtered;
QTest::newRow("bar8") << items << QStringLiteral("b/a") << filtered;
QTest::newRow("bar9") << items << QStringLiteral("b/a.h") << filtered;
QTest::newRow("bar10") << items << QStringLiteral("b/a.") << filtered;
}
{
const StringList filtered = { QStringLiteral("/foo/a.h"), QStringLiteral("/foo/ab.h") };
QTest::newRow("foo_a1") << items << QStringLiteral("foo/a") << filtered;
QTest::newRow("foo_a2") << items << QStringLiteral("/f/a") << filtered;
}
{
// now matches ab.h too because of abbreviation matching, but should be sorted last
const StringList filtered = { QStringLiteral("/foo/a.h"), QStringLiteral("/bar/a.h"), QStringLiteral("/foo/ab.h") };
QTest::newRow("a_h") << items << QStringLiteral("a.h") << filtered;
}
{
const StringList base = { QStringLiteral("/foo/a_test"), QStringLiteral("/foo/test_b_1"), QStringLiteral("/foo/test_b") };
const StringList sorted = { QStringLiteral("/foo/test_b"), QStringLiteral("/foo/test_b_1") };
QTest::newRow("prefer_exact") << base << QStringLiteral("test_b") << sorted;
}
{
// from commit: 769491f06a4560a4798592ff060675ffb0d990a6
const QString file = QStringLiteral("/myProject/someStrangePath/anItem.cpp");
const StringList base = { QStringLiteral("/foo/a"), file };
const StringList filtered = { file };
QTest::newRow("strange") << base << QStringLiteral("strange/item") << filtered;
}
{
const StringList base = { QStringLiteral("/foo/a_test"), QStringLiteral("/foo/test_b_1"),
QStringLiteral("/foo/test_b"), QStringLiteral("/foo/test/a") };
const StringList sorted = { QStringLiteral("/foo/test_b_1"), QStringLiteral("/foo/test_b"),
QStringLiteral("/foo/a_test"), QStringLiteral("/foo/test/a") };
QTest::newRow("prefer_start1") << base << QStringLiteral("test") << sorted;
QTest::newRow("prefer_start2") << base << QStringLiteral("foo/test") << sorted;
}
{
const StringList base = { QStringLiteral("/muh/kuh/asdf/foo"), QStringLiteral("/muh/kuh/foo/asdf") };
const StringList reverse = { QStringLiteral("/muh/kuh/foo/asdf"), QStringLiteral("/muh/kuh/asdf/foo") };
QTest::newRow("prefer_start3") << base << QStringLiteral("f") << base;
QTest::newRow("prefer_start4") << base << QStringLiteral("/fo") << base;
QTest::newRow("prefer_start5") << base << QStringLiteral("/foo") << base;
QTest::newRow("prefer_start6") << base << QStringLiteral("a") << reverse;
QTest::newRow("prefer_start7") << base << QStringLiteral("/a") << reverse;
QTest::newRow("prefer_start8") << base << QStringLiteral("uh/as") << reverse;
QTest::newRow("prefer_start9") << base << QStringLiteral("asdf") << reverse;
}
{
QTest::newRow("duplicate") << StringList({ QStringLiteral("/muh/kuh/asdf/foo") }) << QStringLiteral("kuh/kuh") << StringList();
}
{
const StringList fuzzyItems = {
QStringLiteral("/foo/bar.h"),
QStringLiteral("/foo/fooXbar.h"),
QStringLiteral("/foo/fXoXoXbXaXr.h"),
QStringLiteral("/bar/FOOxBAR.h")
};
QTest::newRow("fuzzy1") << fuzzyItems << QStringLiteral("br") << fuzzyItems;
QTest::newRow("fuzzy2") << fuzzyItems << QStringLiteral("foo/br") << StringList({
QStringLiteral("/foo/bar.h"),
QStringLiteral("/foo/fooXbar.h"),
QStringLiteral("/foo/fXoXoXbXaXr.h")
});
QTest::newRow("fuzzy3") << fuzzyItems << QStringLiteral("b/br") << StringList({
QStringLiteral("/bar/FOOxBAR.h")
});
QTest::newRow("fuzzy4") << fuzzyItems << QStringLiteral("br/br") << StringList();
QTest::newRow("fuzzy5") << fuzzyItems << QStringLiteral("foo/bar") << StringList({
QStringLiteral("/foo/bar.h"),
QStringLiteral("/foo/fooXbar.h"),
QStringLiteral("/foo/fXoXoXbXaXr.h")
});
QTest::newRow("fuzzy6") << fuzzyItems << QStringLiteral("foobar") << StringList({
QStringLiteral("/foo/fooXbar.h"),
QStringLiteral("/foo/fXoXoXbXaXr.h"),
QStringLiteral("/bar/FOOxBAR.h")
});
}
{
const StringList a = {
QStringLiteral("/home/user/src/code/user/something"),
QStringLiteral("/home/user/src/code/home/else"),
};
const StringList b = {
QStringLiteral("/home/user/src/code/home/else"),
QStringLiteral("/home/user/src/code/user/something"),
};
QTest::newRow("prefer_multimatch_a_home") << a << QStringLiteral("home") << b;
QTest::newRow("prefer_multimatch_b_home") << b << QStringLiteral("home") << b;
QTest::newRow("prefer_multimatch_a_user") << a << QStringLiteral("user") << a;
QTest::newRow("prefer_multimatch_b_user") << b << QStringLiteral("user") << a;
}
{
const StringList a = {
QStringLiteral("/home/user/project/A/file"),
QStringLiteral("/home/user/project/B/project/A/file"),
QStringLiteral("/home/user/project/user/C/D/E"),
};
const StringList b = {
QStringLiteral("/home/user/project/B/project/A/file"),
QStringLiteral("/home/user/project/A/file"),
};
const StringList c = {
QStringLiteral("/home/user/project/user/C/D/E"),
QStringLiteral("/home/user/project/A/file"),
QStringLiteral("/home/user/project/B/project/A/file"),
};
QTest::newRow("prefer_multimatch_a_project/file") << a << QStringLiteral("project/file") << b;
QTest::newRow("prefer_multimatch_b_project/file") << b << QStringLiteral("project/file") << b;
QTest::newRow("prefer_multimatch_a_project/le") << a << QStringLiteral("project/le") << b;
QTest::newRow("prefer_multimatch_b_project/le") << b << QStringLiteral("project/le") << b;
QTest::newRow("prefer_multimatch_a_project/a/file") << a << QStringLiteral("project/a/file") << b;
QTest::newRow("prefer_multimatch_b_project/a/file") << b << QStringLiteral("project/a/file") << b;
QTest::newRow("prefer_multimatch_a_project_user") << a << QStringLiteral("user") << c;
QTest::newRow("prefer_multimatch_c_project_user") << c << QStringLiteral("user") << c;
}
}
void TestQuickOpen::testStableSort()
{
const StringList items = {
QStringLiteral("a/c/CMakeLists.txt"),
QStringLiteral("a/d/CMakeLists.txt"),
QStringLiteral("b/e/CMakeLists.txt"),
QStringLiteral("b/f/CMakeLists.txt")
};
PathTestFilter filterItems;
filterItems.setItems(items);
QStringList filter = {QString()};
const auto cmakeListsString = QStringLiteral("CMakeLists.txt");
for (auto c : cmakeListsString) {
filter[0].append(c);
filterItems.setFilter(filter);
QCOMPARE(filterItems.filteredItems(), items);
}
}
void TestQuickOpen::testProjectFileFilter()
{
QTemporaryDir dir;
auto* project = new TestProject(Path(dir.path()));
auto* foo = createChild<ProjectFolderItem>(project->projectItem(), QStringLiteral("foo"));
createChild<ProjectFileItem>(foo, QStringLiteral("bar"));
createChild<ProjectFileItem>(foo, QStringLiteral("asdf"));
createChild<ProjectFileItem>(foo, QStringLiteral("space bar"));
auto* asdf = createChild<ProjectFolderItem>(project->projectItem(), QStringLiteral("asdf"));
createChild<ProjectFileItem>(asdf, QStringLiteral("bar"));
QTemporaryFile tmpFile;
tmpFile.setFileName(dir.path() + "/aaaa");
QVERIFY(tmpFile.open());
auto* aaaa = new ProjectFileItem(QStringLiteral("aaaa"), project->projectItem());
QCOMPARE(project->fileSet().size(), 5);
ProjectFileDataProvider provider;
QCOMPARE(provider.itemCount(), 0u);
projectController->addProject(project);
const QStringList original = QStringList()
<< QStringLiteral("aaaa") << QStringLiteral("asdf/bar") << QStringLiteral("foo/asdf") << QStringLiteral("foo/bar") << QStringLiteral("foo/space bar");
// lazy load
QCOMPARE(provider.itemCount(), 0u);
provider.reset();
QCOMPARE(items(provider), original);
QCOMPARE(provider.itemPath(provider.items().first()), aaaa->path());
QCOMPARE(provider.data(0)->text(), QStringLiteral("aaaa"));
// don't show opened file
QVERIFY(core->documentController()->openDocument(QUrl::fromLocalFile(tmpFile.fileName())));
// lazy load again
QCOMPARE(items(provider), original);
provider.reset();
QCOMPARE(items(provider), QStringList() << QStringLiteral("asdf/bar") << QStringLiteral("foo/asdf") << QStringLiteral("foo/bar") << QStringLiteral("foo/space bar"));
// prefer files starting with filter
provider.setFilterText(QStringLiteral("as"));
qDebug() << items(provider);
QCOMPARE(items(provider), QStringList() << QStringLiteral("foo/asdf") << QStringLiteral("asdf/bar"));
// clear filter
provider.reset();
QCOMPARE(items(provider), QStringList() << QStringLiteral("asdf/bar") << QStringLiteral("foo/asdf") << QStringLiteral("foo/bar") << QStringLiteral("foo/space bar"));
// update on document close, lazy load again
core->documentController()->closeAllDocuments();
QCOMPARE(items(provider), QStringList() << QStringLiteral("asdf/bar") << QStringLiteral("foo/asdf") << QStringLiteral("foo/bar") << QStringLiteral("foo/space bar"));
provider.reset();
QCOMPARE(items(provider), original);
auto* blub = createChild<ProjectFileItem>(project->projectItem(), QStringLiteral("blub"));
// lazy load
QCOMPARE(provider.itemCount(), 5u);
provider.reset();
QCOMPARE(provider.itemCount(), 6u);
// ensure we don't add stuff multiple times
QMetaObject::invokeMethod(&provider, "fileAddedToSet",
Q_ARG(KDevelop::ProjectFileItem*, blub));
QCOMPARE(provider.itemCount(), 6u);
provider.reset();
QCOMPARE(provider.itemCount(), 6u);
// lazy load in this implementation
delete blub;
QCOMPARE(provider.itemCount(), 6u);
provider.reset();
QCOMPARE(provider.itemCount(), 5u);
QCOMPARE(items(provider), original);
// allow filtering by path to project
provider.setFilterText(dir.path());
QCOMPARE(items(provider), original);
Path buildFolderItem(project->path().parent(), QStringLiteral(".build/generated.h"));
new ProjectFileItem(project, buildFolderItem, project->projectItem());
// lazy load
QCOMPARE(items(provider), original);
provider.reset();
QCOMPARE(items(provider), QStringList() << QStringLiteral("aaaa") << QStringLiteral("asdf/bar") << QStringLiteral("foo/asdf")
<< QStringLiteral("foo/bar") << QStringLiteral("foo/space bar") << QStringLiteral("../.build/generated.h"));
projectController->closeProject(project);
provider.reset();
QVERIFY(!provider.itemCount());
}
#include "moc_test_quickopen.cpp"
|