1 /*
2     SPDX-FileCopyrightText: 2020 Christoph Cullmann <cullmann@kde.org>
3 
4     SPDX-License-Identifier: MIT
5 */
6 
7 #include "urlinfo_test.h"
8 
9 #include <QDir>
10 #include <QFile>
11 #include <QFileInfo>
12 #include <QTemporaryDir>
13 #include <QTest>
14 
QTEST_MAIN(UrlInfoTest)15 QTEST_MAIN(UrlInfoTest)
16 
17 void UrlInfoTest::someUrls()
18 {
19     // check that some things convert correctly to urls
20     QCOMPARE(UrlInfo(QStringLiteral("file:///for_sure_not_there_path_xxcv123/to/file")).url.toString(),
21              QStringLiteral("file:///for_sure_not_there_path_xxcv123/to/file"));
22     QCOMPARE(UrlInfo(QStringLiteral("sftp://127.0.0.1:1234/path/to/file")).url.toString(), QStringLiteral("sftp://127.0.0.1:1234/path/to/file"));
23 }
24 
someCursors()25 void UrlInfoTest::someCursors()
26 {
27     // check that some things convert correctly to urls + cursors
28     QCOMPARE(UrlInfo(QStringLiteral("file:///for_sure_not_there_path_xxcv123/to/file:1234:12")).url.toString(),
29              QStringLiteral("file:///for_sure_not_there_path_xxcv123/to/file"));
30     QCOMPARE(UrlInfo(QStringLiteral("file:///for_sure_not_there_path_xxcv123/to/file:1234:12")).cursor, KTextEditor::Cursor(1233, 11));
31     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"));
32     QCOMPARE(UrlInfo(QStringLiteral("sftp://127.0.0.1:1234/path/to/file:1:1")).cursor, KTextEditor::Cursor(0, 0));
33     // check url query string to cursor
34     QCOMPARE(UrlInfo(QStringLiteral("sftp://127.0.0.1:1234/path/to/file?line=2&column=3")).cursor, KTextEditor::Cursor(1, 2));
35     QCOMPARE(UrlInfo(QStringLiteral("fish://remote/file?some=variable&line=4&")).cursor, KTextEditor::Cursor(3, 0));
36     QCOMPARE(UrlInfo(QStringLiteral("file:///directory/file?some=variable&column=5&other=value&line=6")).cursor, KTextEditor::Cursor(5, 4));
37     QCOMPARE(UrlInfo(QStringLiteral("~/file?line=7")).url.hasQuery(), false);
38 }
39 
urlWithColonAtStart()40 void UrlInfoTest::urlWithColonAtStart()
41 {
42 #ifndef WIN32 // : invalid for filenames on Windows
43 
44     // create test file in temporary directory, as qt sees :test..... as absolute, hack with ./:
45     QTemporaryDir dir;
46     const auto oldCurrent = QDir::currentPath();
47     QDir::setCurrent(dir.path());
48     QFile test(QStringLiteral("./:test.txt"));
49     QVERIFY(test.open(QFile::WriteOnly));
50 
51     // see bug 430216 => before this was some absolute file name
52     const UrlInfo info(QStringLiteral(":test.txt:123:1"));
53     QCOMPARE(info.cursor, KTextEditor::Cursor(122, 0));
54     QVERIFY(info.url.isLocalFile());
55     QVERIFY(QFileInfo::exists(info.url.toLocalFile()));
56 
57     // back to old working dir
58     QDir::setCurrent(oldCurrent);
59 #endif
60 }
61