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 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
|
/*
SPDX-FileCopyrightText: 2012 Milian Wolff <mail@milianw.de>
SPDX-FileCopyrightText: 2015 Kevin Funk <kfunk@kde.org>
SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
#include "test_path.h"
#include <util/path.h>
#include <KIO/Global>
#include <QTest>
#include <QStandardPaths>
#include <type_traits>
#include <utility>
QTEST_MAIN(TestPath)
using namespace KDevelop;
static_assert(std::is_nothrow_move_assignable<Path>(), "Why would a move assignment operator throw?");
static_assert(std::is_nothrow_move_constructible<Path>(), "Why would a move constructor throw?");
static const int FILES_PER_FOLDER = 10;
static const int FOLDERS_PER_FOLDER = 5;
static const int TREE_DEPTH = 5;
template<typename T>
T stringToUrl(const QString& path)
{
return T(path);
}
template<>
QStringList stringToUrl(const QString& path)
{
return path.split('/');
}
template<typename T>
T childUrl(const T& parent, const QString& child)
{
return T(parent, child);
}
template<>
QStringList childUrl(const QStringList& parent, const QString& child)
{
QStringList ret = parent;
ret << child;
return ret;
}
template<>
QUrl childUrl(const QUrl& parent, const QString& child)
{
QUrl ret = parent;
ret.setPath(ret.path() + '/' + child);
return ret;
}
template<typename T>
QVector<T> generateData(const T& parent, int level)
{
QVector<T> ret;
// files per folder
for (int i = 0; i < FILES_PER_FOLDER; ++i) {
const QString fileName = QStringLiteral("file%1.txt").arg(i);
const T file = childUrl<T>(parent, fileName);
Q_ASSERT(!ret.contains(file));
ret << file;
}
// nesting depth
if (level < TREE_DEPTH) {
// folders per folder
for (int i = 0; i < FOLDERS_PER_FOLDER; ++i) {
const QString folderName = QStringLiteral("folder%1").arg(i);
const T folder = childUrl<T>(parent, folderName);
Q_ASSERT(!ret.contains(folder));
ret << folder;
ret += generateData<T>(folder, level + 1);
}
}
return ret;
}
template<typename T>
void runBenchmark()
{
QBENCHMARK {
const T base = stringToUrl<T>("/tmp/foo/bar");
generateData(base, 0);
}
}
void TestPath::initTestCase()
{
QStandardPaths::setTestModeEnabled(true);
}
void TestPath::bench_qurl()
{
runBenchmark<QUrl>();
}
void TestPath::bench_qstringlist()
{
runBenchmark<QStringList>();
}
void TestPath::bench_path()
{
runBenchmark<Path>();
}
void TestPath::bench_fromLocalPath()
{
QFETCH(int, variant);
const QString input(QStringLiteral("/foo/bar/asdf/bla/blub.h"));
const int repeat = 1000;
if (variant == 1) {
QBENCHMARK {
for (int i = 0; i < repeat; ++i) {
Path path = Path(QUrl::fromLocalFile(input));
Q_UNUSED(path);
}
}
} else if (variant == 2) {
QBENCHMARK {
for (int i = 0; i < repeat; ++i) {
Path path = Path(input);
Q_UNUSED(path);
}
}
} else {
QFAIL("unexpected variant");
}
}
void TestPath::bench_fromLocalPath_data()
{
QTest::addColumn<int>("variant");
QTest::newRow("QUrl::fromLocalFile") << 1;
QTest::newRow("QString") << 2;
}
void TestPath::bench_swap()
{
Path a(QStringLiteral("/foo/a.cpp"));
Path b(QStringLiteral("/bar/b.cc"));
QBENCHMARK {
using std::swap;
swap(a, b);
}
}
void TestPath::bench_hash()
{
const Path path(QStringLiteral("/my/very/long/path/to/a/file.cpp"));
QBENCHMARK {
auto hash = qHash(path);
Q_UNUSED(hash);
}
}
/// Invoke @p op on URL @p base, but preserve drive letter if @p op removes it
template<typename Func>
QUrl preserveWindowsDriveLetter(const QUrl& base, Func op)
{
#ifndef Q_OS_WIN
return op(base);
#else
// only apply to local files
if (!base.isLocalFile()) {
return op(base);
}
// save drive letter
const QString windowsDriveLetter = base.toLocalFile().mid(0, 2);
QUrl url = op(base);
// restore drive letter
if (url.toLocalFile().startsWith('/')) {
url = QUrl::fromLocalFile(windowsDriveLetter + url.toLocalFile());
}
return url;
#endif
}
QUrl resolvedUrl(const QUrl& base, const QUrl& relative)
{
return preserveWindowsDriveLetter(base, [&](const QUrl& url) {
return url.resolved(relative);
});
}
QUrl comparableUpUrl(const QUrl& url)
{
QUrl ret = preserveWindowsDriveLetter(url, [&](const QUrl& url) {
return KIO::upUrl(url).adjusted(QUrl::RemovePassword);
});
return ret.adjusted(QUrl::StripTrailingSlash);
}
void TestPath::testPath()
{
QFETCH(QString, input);
QUrl url = QUrl::fromUserInput(input);
url = url.adjusted(QUrl::StripTrailingSlash | QUrl::NormalizePathSegments);
Path optUrl(input);
if (!url.password().isEmpty()) {
QUrl urlNoPass = url.adjusted(QUrl::RemovePassword);
QCOMPARE(optUrl.toUrl(), urlNoPass);
} else {
QCOMPARE(optUrl.toUrl(), url);
}
QCOMPARE(optUrl.isLocalFile(), url.isLocalFile());
QCOMPARE(optUrl.pathOrUrl(), toUrlOrLocalFile(url, QUrl::RemovePassword));
QCOMPARE(optUrl.isValid(), url.isValid());
QCOMPARE(optUrl.isEmpty(), url.isEmpty());
QCOMPARE(optUrl.lastPathSegment(), url.fileName());
QCOMPARE(optUrl.path(), url.isLocalFile() ? url.toLocalFile() : url.path());
QCOMPARE(optUrl.parent().toUrl(), comparableUpUrl(url));
QCOMPARE(optUrl.toLocalFile(), url.toLocalFile());
QCOMPARE(optUrl, Path(input));
QCOMPARE(optUrl, Path(optUrl));
QVERIFY(optUrl != Path(input + "/asdf"));
if (url.isLocalFile() && !input.startsWith(QLatin1String("file://"))) {
QCOMPARE(optUrl, Path(QUrl::fromLocalFile(input)));
}
QCOMPARE(optUrl, Path(url));
if (url.isValid()) {
QVERIFY(optUrl.relativePath(optUrl).isEmpty());
Path relativePath(optUrl, QStringLiteral("foo/bar"));
QCOMPARE(optUrl.relativePath(relativePath), QLatin1String("foo/bar"));
QCOMPARE(relativePath.relativePath(optUrl), QLatin1String("../../"));
QVERIFY(optUrl.isParentOf(relativePath));
QVERIFY(!relativePath.isParentOf(optUrl));
#ifndef Q_OS_WIN
Path absolutePath(optUrl, QStringLiteral("/laa/loo"));
QCOMPARE(absolutePath.path(), QLatin1String("/laa/loo"));
QCOMPARE(url.resolved(QUrl(QStringLiteral("/laa/loo"))).path(), QLatin1String("/laa/loo"));
Path absolutePath2(optUrl, QStringLiteral("/"));
QCOMPARE(absolutePath2.path(), QLatin1String("/"));
QCOMPARE(url.resolved(QUrl(QStringLiteral("/"))).path(), QLatin1String("/"));
#endif
Path unrelatedPath(QStringLiteral("https://test@blubasdf.com:12345/"));
QCOMPARE(optUrl.relativePath(unrelatedPath), unrelatedPath.pathOrUrl());
QCOMPARE(unrelatedPath.relativePath(optUrl), optUrl.pathOrUrl());
QVERIFY(!unrelatedPath.isParentOf(optUrl));
QVERIFY(!optUrl.isParentOf(unrelatedPath));
}
QCOMPARE(Path().relativePath(optUrl), optUrl.pathOrUrl());
QVERIFY(optUrl.relativePath(Path()).isEmpty());
QVERIFY(Path().relativePath(Path()).isEmpty());
QVERIFY(!optUrl.isParentOf(Path()));
QVERIFY(!Path().isParentOf(optUrl));
QVERIFY(!Path().isParentOf(Path()));
QVERIFY(!optUrl.isParentOf(optUrl));
QCOMPARE(optUrl.isRemote(), optUrl.isValid() && !optUrl.isLocalFile());
QCOMPARE(optUrl.isRemote(), optUrl.isValid() && !optUrl.remotePrefix().isEmpty());
if (url.path() == QLatin1String("/")) {
url.setPath("/test/foo/bar");
} else {
url.setPath(url.path() + "/test/foo/bar");
}
if (url.scheme().isEmpty()) {
url.setScheme(QStringLiteral("file"));
}
optUrl.addPath(QStringLiteral("test/foo/bar"));
QCOMPARE(optUrl.lastPathSegment(), url.fileName());
QCOMPARE(optUrl.path(), url.isLocalFile() ? url.toLocalFile() : url.path());
url = url.adjusted(QUrl::RemoveFilename);
url.setPath(url.path() + "lalalala_adsf.txt");
optUrl.setLastPathSegment(QStringLiteral("lalalala_adsf.txt"));
QCOMPARE(optUrl.lastPathSegment(), url.fileName());
QCOMPARE(optUrl.path(), url.isLocalFile() ? url.toLocalFile() : url.path());
QCOMPARE(optUrl.parent().toUrl(), comparableUpUrl(url));
QVERIFY(optUrl.parent().isDirectParentOf(optUrl));
QVERIFY(!optUrl.parent().parent().isDirectParentOf(optUrl));
#ifndef Q_OS_WIN
Path a(QStringLiteral("/foo/bar/asdf/"));
Path b(QStringLiteral("/foo/bar/"));
QVERIFY(b.isDirectParentOf(a));
Path c(QStringLiteral("/foo/bar"));
QVERIFY(c.isDirectParentOf(a));
#endif
optUrl = Path{};
url.clear();
QCOMPARE(optUrl.toUrl(), url);
}
void TestPath::testPath_data()
{
QTest::addColumn<QString>("input");
#ifndef Q_OS_WIN
QTest::newRow("invalid") << "";
QTest::newRow("path") << "/tmp/foo/asdf.txt";
QTest::newRow("path-folder") << "/tmp/foo/asdf/";
QTest::newRow("root") << "/";
QTest::newRow("clean-path") << "/tmp/..///asdf/";
QTest::newRow("file") << "file:///tmp/foo/asdf.txt";
QTest::newRow("file-folder") << "file:///tmp/foo/bar/";
#else
QTest::newRow("path") << "C:/tmp/foo/asdf.txt";
QTest::newRow("path-folder") << "C:/tmp/foo/asdf/";
QTest::newRow("root") << "C:/";
QTest::newRow("clean-path") << "C:/tmp/..///asdf/";
QTest::newRow("file") << "file:///C:/tmp/foo/asdf.txt";
QTest::newRow("file-folder") << "file:///C:/tmp/foo/bar/";
#endif
QTest::newRow("remote-root") << "http://www.test.com/";
QTest::newRow("http") << "http://www.test.com/tmp/asdf.txt";
QTest::newRow("ftps") << "ftps://user@host.com/tmp/foo/asdf.txt";
QTest::newRow("password") << "ftps://user:password@host.com/tmp/asdf.txt";
QTest::newRow("port") << "http://localhost:8080/foo/bar/test.txt";
}
void TestPath::testPathInvalid()
{
QFETCH(QString, input);
Path url(input);
QVERIFY(!url.isValid());
QVERIFY(url.isEmpty());
}
void TestPath::testPathInvalid_data()
{
QTest::addColumn<QString>("input");
QTest::newRow("empty") << "";
QTest::newRow("fragment") << "http://test.com/#hello";
QTest::newRow("query") << "http://test.com/?hello";
QTest::newRow("suburl") << "file:///home/weis/kde.tgz#gzip:/#tar:/kdebase";
QTest::newRow("relative") << "../foo/bar";
QTest::newRow("name") << "asdfasdf";
QTest::newRow("remote-nopath") << "http://www.test.com";
}
void TestPath::testPathComparison()
{
QFETCH(Path, left);
QFETCH(Path, right);
QFETCH(int, leftCompareRight);
QFETCH(int, leftCompareRightCi);
const bool equal = leftCompareRight == 0;
const bool less = leftCompareRight < 0;
const bool greater = leftCompareRight > 0;
QVERIFY(left == left);
QVERIFY(right == right);
QCOMPARE(left == right, equal);
QCOMPARE(right == left, equal);
QVERIFY(!(left != left));
QVERIFY(!(right != right));
QCOMPARE(left != right, !equal);
QCOMPARE(right != left, !equal);
QCOMPARE(left < right, less);
QCOMPARE(left <= right, less || equal);
QCOMPARE(left > right, greater);
QCOMPARE(left >= right, greater || equal);
QCOMPARE(right < left, greater);
QCOMPARE(right <= left, greater || equal);
QCOMPARE(right > left, less);
QCOMPARE(right >= left, less || equal);
QCOMPARE(left.compare(left), 0);
QCOMPARE(right.compare(right), 0);
QCOMPARE(left.compare(right) < 0, leftCompareRight < 0);
QCOMPARE(right.compare(left) < 0, leftCompareRight > 0);
QCOMPARE(left.compare(left, Qt::CaseSensitive), 0);
QCOMPARE(right.compare(right, Qt::CaseSensitive), 0);
QCOMPARE(left.compare(right, Qt::CaseSensitive) < 0, leftCompareRight < 0);
QCOMPARE(right.compare(left, Qt::CaseSensitive) < 0, leftCompareRight > 0);
QCOMPARE(left.compare(left, Qt::CaseInsensitive), 0);
QCOMPARE(right.compare(right, Qt::CaseInsensitive), 0);
QCOMPARE(left.compare(right, Qt::CaseInsensitive) < 0, leftCompareRightCi < 0);
QCOMPARE(right.compare(left, Qt::CaseInsensitive) < 0, leftCompareRightCi > 0);
}
void TestPath::testPathComparison_data()
{
QTest::addColumn<Path>("left");
QTest::addColumn<Path>("right");
QTest::addColumn<int>("leftCompareRight");
QTest::addColumn<int>("leftCompareRightCi");
Path a(QStringLiteral("/tmp/a"));
Path b(QStringLiteral("/tmp/b"));
Path c(QStringLiteral("/tmp/ac"));
Path d(QStringLiteral("/d"));
Path e(QStringLiteral("/tmp"));
Path f(QStringLiteral("/tmp/"));
Path invalid;
QTest::newRow("a-b") << a << b << -1 << -1;
QTest::newRow("a-copy") << a << Path(a) << 0 << 0;
QTest::newRow("c-a") << c << a << 1 << 1;
QTest::newRow("c-invalid") << c << invalid << 1 << 1;
QTest::newRow("c-d") << c << d << 1 << 1;
QTest::newRow("e-f") << e << f << 0 << 0;
Path A(QStringLiteral("/tmp/A"));
Path B(QStringLiteral("/tmp/B"));
Path C(QStringLiteral("/tmp/aC"));
Path D(QStringLiteral("/D"));
Path E(QStringLiteral("/TMP"));
Path F(QStringLiteral("/TmP/F"));
QTest::newRow("a-A") << a << A << 1 << 0;
QTest::newRow("a-B") << a << B << 1 << -1;
QTest::newRow("A-b") << A << b << -1 << -1;
QTest::newRow("A-C") << A << C << -1 << -1;
QTest::newRow("c-C") << c << C << 1 << 0;
QTest::newRow("d-D") << d << D << 1 << 0;
QTest::newRow("F-A") << F << A << -1 << 1;
QTest::newRow("f-E") << f << E << 1 << 0;
QTest::newRow("E-F") << E << F << -1 << -1;
}
void TestPath::testPathSwap()
{
Path a(QStringLiteral("/foo/22/x.C"));
Path b(QStringLiteral("/home/bar/at.7z"));
QVERIFY(!(a == b));
const auto aCopy = a;
const auto bCopy = b;
QCOMPARE(aCopy, a);
QCOMPARE(bCopy, b);
using std::swap;
swap(a, b);
QCOMPARE(a, bCopy);
QCOMPARE(b, aCopy);
}
void TestPath::testPathAddData()
{
QFETCH(QString, pathToAdd);
const QStringList bases = {
#ifndef Q_OS_WIN
QStringLiteral("/"),
QStringLiteral("/foo/bar/asdf/"),
QStringLiteral("file:///foo/bar/asdf/"),
#else
"C:/",
"C:/foo/bar/asdf/",
"file:///C:/foo/bar/asdf/",
#endif
QStringLiteral("http://www.asdf.com/foo/bar/asdf/"),
};
for (const QString& base : bases) {
QUrl baseUrl = QUrl::fromUserInput(base);
if (QDir::isRelativePath(pathToAdd)) {
baseUrl = resolvedUrl(baseUrl, QUrl(pathToAdd));
} else if (QDir::isRelativePath(pathToAdd) || baseUrl.path() != QLatin1String("/")) {
// if pathToAdd == /absolute && baseUrl == "/", below call would lead to an invalid QUrl
// with qtbase.git/f62768d046528636789f901ac79e2cfa1843a7b7
baseUrl.setPath(baseUrl.path() + pathToAdd);
} else {
baseUrl.setPath(pathToAdd);
}
baseUrl = baseUrl.adjusted(QUrl::NormalizePathSegments);
if (baseUrl.path().contains(QLatin1String("//"))) {
// odd, this should have been normalized, no?
auto path = baseUrl.path();
path.replace(QLatin1String("//"), QLatin1String("/"));
baseUrl.setPath(path);
}
// QUrl::StripTrailingSlash converts file:/// to file: which is not what we want
if (baseUrl.path() != QLatin1String("/")) {
baseUrl = baseUrl.adjusted(QUrl::StripTrailingSlash);
}
Path basePath(base);
basePath.addPath(pathToAdd);
QCOMPARE(basePath.pathOrUrl(), toUrlOrLocalFile(baseUrl));
QCOMPARE(basePath.toUrl(), baseUrl);
}
}
void TestPath::testPathAddData_data()
{
QTest::addColumn<QString>("pathToAdd");
const QStringList paths = QStringList()
<< QStringLiteral("file.txt")
<< QStringLiteral("path/file.txt")
<< QStringLiteral("path//file.txt")
<< QStringLiteral("/absolute")
<< QStringLiteral("../")
<< QStringLiteral("..")
<< QStringLiteral("../../../")
<< QStringLiteral("./foo")
<< QStringLiteral("../relative")
<< QStringLiteral("../../relative")
<< QStringLiteral("../foo/../bar")
<< QStringLiteral("../foo/./bar")
<< QStringLiteral("../../../../../../../invalid");
for (const QString& path : paths) {
QTest::addRow("%s", qPrintable(path)) << path;
}
}
void TestPath::testPathBaseCtor()
{
QFETCH(QString, base);
QFETCH(QString, subPath);
QFETCH(QString, expected);
const Path basePath(base);
const Path path(basePath, subPath);
QCOMPARE(path.pathOrUrl(), expected);
}
void TestPath::testPathBaseCtor_data()
{
QTest::addColumn<QString>("base");
QTest::addColumn<QString>("subPath");
QTest::addColumn<QString>("expected");
QTest::newRow("empty") << "" << "" << "";
QTest::newRow("empty-relative") << "" << "foo" << "";
#ifndef Q_OS_WIN
QTest::newRow("root-empty") << "/" << "" << "/";
QTest::newRow("root-root") << "/" << "/" << "/";
QTest::newRow("root-relative") << "/" << "bar" << "/bar";
QTest::newRow("root-relative-dirty") << "/" << "bar//foo/a/.." << "/bar/foo";
QTest::newRow("path-relative") << "/foo/bar" << "bar/foo" << "/foo/bar/bar/foo";
QTest::newRow("path-absolute") << "/foo/bar" << "/bar/foo" << "/bar/foo";
#else
QTest::newRow("root-empty") << "C:/" << "" << "C:";
QTest::newRow("root1-empty") << "C:" << "" << "C:";
QTest::newRow("root-root") << "C:/" << "C:/" << "C:";
QTest::newRow("root-relative") << "C:/" << "bar" << "C:/bar";
QTest::newRow("root1-relative") << "C:" << "bar" << "C:/bar";
QTest::newRow("root-relative-dirty") << "C:/" << "bar//foo/a/.." << "C:/bar/foo";
QTest::newRow("path-relative") << "C:/foo/bar" << "bar/foo" << "C:/foo/bar/bar/foo";
QTest::newRow("path-absolute") << "C:/foo/bar" << "C:/bar/foo" << "C:/bar/foo";
#endif
QTest::newRow("remote-path-absolute") << "http://foo.com/foo/bar" << "/bar/foo" << "http://foo.com/bar/foo";
QTest::newRow("remote-path-relative") << "http://foo.com/foo/bar" << "bar/foo" << "http://foo.com/foo/bar/bar/foo";
}
// there is no cd() in QUrl, emulate what KUrl did
static bool cdQUrl(QUrl& url, const QString& path)
{
if (path.isEmpty() || !url.isValid()) {
return false;
}
// have to append slash otherwise last segment is treated as a file name and not a directory
if (!url.path().endsWith('/')) {
url.setPath(url.path() + '/');
}
url = url.resolved(QUrl(path)).adjusted(QUrl::RemoveFragment | QUrl::RemoveQuery);
return true;
}
void TestPath::testPathCd()
{
QFETCH(QString, base);
QFETCH(QString, change);
Path path = base.isEmpty() ? Path() : Path(base);
QUrl url = QUrl::fromUserInput(base);
Path changed = path.cd(change);
if (cdQUrl(url, change)) {
QVERIFY(changed.isValid());
}
url = url.adjusted(QUrl::NormalizePathSegments);
QCOMPARE(changed.pathOrUrl(), toUrlOrLocalFile(url, QUrl::StripTrailingSlash));
}
void TestPath::testPathCd_data()
{
QTest::addColumn<QString>("base");
QTest::addColumn<QString>("change");
const QVector<QString> bases{
QString(),
#ifndef Q_OS_WIN
QStringLiteral("/foo"), QStringLiteral("/foo/bar/asdf"),
#else
"C:/foo", "C:/foo/bar/asdf",
#endif
QStringLiteral("http://foo.com/"), QStringLiteral("http://foo.com/foo"), QStringLiteral(
"http://foo.com/foo/bar/asdf")
};
const QVector<QString> suffixes {
QString(),
QStringLiteral(".."),
QStringLiteral("../"),
QStringLiteral("../foo"),
QStringLiteral("."),
QStringLiteral("./"),
QStringLiteral("./foo"),
QStringLiteral("./foo/bar"),
QStringLiteral("./foo/.."),
QStringLiteral("./foo/"),
QStringLiteral("./foo/../bar"),
};
const QVector<QString> extraSuffixes {
QStringLiteral("/foo"),
QStringLiteral("/foo/../bar"),
};
for (const QString& base : bases) {
for (const auto& suffix : suffixes) {
QTest::addRow("%s-%s", qPrintable(base), qPrintable(suffix)) << base << suffix;
}
#ifdef Q_OS_WIN
// only add next rows for remote URLs on Windows
if (base.startsWith("C:/")) {
continue;
}
#endif
for (const auto& suffix : extraSuffixes) {
QTest::addRow("%s-%s", qPrintable(base), qPrintable(suffix)) << base << suffix;
}
}
}
void TestPath::testHasParent_data()
{
QTest::addColumn<QString>("input");
QTest::addColumn<bool>("hasParent");
QTest::newRow("empty") << QString() << false;
#ifdef Q_OS_WIN
QTest::newRow("\\") << QStringLiteral("\\") << true; // true b/c parent could be e.g. 'C:'
#else
QTest::newRow("/") << QStringLiteral("/") << false;
QTest::newRow("/foo") << QStringLiteral("/foo") << true;
QTest::newRow("/foo/bar") << QStringLiteral("/foo/bar") << true;
QTest::newRow("//foo/bar") << QStringLiteral("//foo/bar") << true;
#endif
QTest::newRow("http://foo.bar") << QStringLiteral("http://foo.bar") << false;
QTest::newRow("http://foo.bar/") << QStringLiteral("http://foo.bar/") << false;
QTest::newRow("http://foo.bar/asdf") << QStringLiteral("http://foo.bar/asdf") << true;
QTest::newRow("http://foo.bar/asdf/asdf") << QStringLiteral("http://foo.bar/asdf/asdf") << true;
}
void TestPath::testHasParent()
{
QFETCH(QString, input);
Path path(input);
QTEST(path.hasParent(), "hasParent");
}
void TestPath::QUrl_acceptance()
{
const QUrl baseLocal = QUrl(QStringLiteral("file:///foo.h"));
QCOMPARE(baseLocal.isValid(), true);
QCOMPARE(baseLocal.isRelative(), false);
QCOMPARE(baseLocal, QUrl::fromLocalFile(QStringLiteral("/foo.h")));
QCOMPARE(baseLocal, QUrl::fromUserInput(QStringLiteral("/foo.h")));
QUrl relative(QStringLiteral("bar.h"));
QCOMPARE(relative.isRelative(), true);
QCOMPARE(baseLocal.resolved(relative), QUrl(QStringLiteral("file:///bar.h")));
QUrl relative2(QStringLiteral("/foo/bar.h"));
QCOMPARE(relative2.isRelative(), true);
QCOMPARE(baseLocal.resolved(relative2), QUrl(QStringLiteral("file:///foo/bar.h")));
const QUrl baseRemote = QUrl(QStringLiteral("http://foo.org/asdf/foo/asdf"));
QCOMPARE(baseRemote.resolved(relative), QUrl(QStringLiteral("http://foo.org/asdf/foo/bar.h")));
}
|