File indexing completed on 2024-05-12 05:52:03

0001 /*
0002     SPDX-FileCopyrightText: 2020 Christoph Cullmann <cullmann@kde.org>
0003 
0004     SPDX-License-Identifier: MIT
0005 */
0006 
0007 #include "urlinfo_test.h"
0008 
0009 #include <QDir>
0010 #include <QFile>
0011 #include <QFileInfo>
0012 #include <QTemporaryDir>
0013 #include <QTest>
0014 
0015 QTEST_MAIN(UrlInfoTest)
0016 
0017 void UrlInfoTest::someUrls()
0018 {
0019     // check that some things convert correctly to urls
0020     QCOMPARE(UrlInfo(QStringLiteral("file:///for_sure_not_there_path_xxcv123/to/file")).url.toString(),
0021              QStringLiteral("file:///for_sure_not_there_path_xxcv123/to/file"));
0022     QCOMPARE(UrlInfo(QStringLiteral("sftp://127.0.0.1:1234/path/to/file")).url.toString(), QStringLiteral("sftp://127.0.0.1:1234/path/to/file"));
0023 }
0024 
0025 void UrlInfoTest::someCursors()
0026 {
0027     // check that some things convert correctly to urls + cursors
0028     QCOMPARE(UrlInfo(QStringLiteral("file:///for_sure_not_there_path_xxcv123/to/file:1234:12")).url.toString(),
0029              QStringLiteral("file:///for_sure_not_there_path_xxcv123/to/file"));
0030     QCOMPARE(UrlInfo(QStringLiteral("file:///for_sure_not_there_path_xxcv123/to/file:1234:12")).cursor, KTextEditor::Cursor(1233, 11));
0031     QCOMPARE(UrlInfo(QStringLiteral("sftp://127.0.0.1:1234/path/to/file:1:1")).url.toString(), QStringLiteral("sftp://127.0.0.1:1234/path/to/file"));
0032     QCOMPARE(UrlInfo(QStringLiteral("sftp://127.0.0.1:1234/path/to/file:1:1")).cursor, KTextEditor::Cursor(0, 0));
0033     // check url query string to cursor
0034     QCOMPARE(UrlInfo(QStringLiteral("sftp://127.0.0.1:1234/path/to/file?line=2&column=3")).cursor, KTextEditor::Cursor(1, 2));
0035     QCOMPARE(UrlInfo(QStringLiteral("fish://remote/file?some=variable&line=4&")).cursor, KTextEditor::Cursor(3, 0));
0036     QCOMPARE(UrlInfo(QStringLiteral("file:///directory/file?some=variable&column=5&other=value&line=6")).cursor, KTextEditor::Cursor(5, 4));
0037     QCOMPARE(UrlInfo(QStringLiteral("~/file?line=7")).url.hasQuery(), false);
0038 }
0039 
0040 void UrlInfoTest::urlWithColonAtStart()
0041 {
0042 #ifndef WIN32 // : invalid for filenames on Windows
0043 
0044     // create test file in temporary directory, as qt sees :test..... as absolute, hack with ./:
0045     QTemporaryDir dir;
0046     const auto oldCurrent = QDir::currentPath();
0047     QDir::setCurrent(dir.path());
0048     QFile test(QStringLiteral("./:test.txt"));
0049     QVERIFY(test.open(QFile::WriteOnly));
0050 
0051     // see bug 430216 => before this was some absolute file name
0052     const UrlInfo info(QStringLiteral(":test.txt:123:1"));
0053     QCOMPARE(info.cursor, KTextEditor::Cursor(122, 0));
0054     QVERIFY(info.url.isLocalFile());
0055     QVERIFY(QFileInfo::exists(info.url.toLocalFile()));
0056 
0057     // back to old working dir
0058     QDir::setCurrent(oldCurrent);
0059 #endif
0060 }
0061 
0062 void UrlInfoTest::nonExistingRelativePath()
0063 {
0064     QTemporaryDir dir;
0065     const auto oldCurrent = QDir::currentPath();
0066     QDir::setCurrent(dir.path());
0067 
0068     const QString fileName = QStringLiteral("doesnotexist.txt");
0069     const UrlInfo info(fileName);
0070 
0071     QVERIFY(info.url.isLocalFile());
0072     QCOMPARE(info.url.toLocalFile(), dir.filePath(fileName));
0073 
0074     QDir::setCurrent(oldCurrent);
0075 }
0076 
0077 #include "moc_urlinfo_test.cpp"