From 1817dd40679240ab50ea52891d53802ef15b5723 Mon Sep 17 00:00:00 2001 From: Wendi Gan Date: Thu, 21 Dec 2023 20:31:18 +0800 Subject: [PATCH] Add URL test cases in HotSpotFilterTest Add test cases for URLs ending with invalid characters like comma, dot, colon, and semicolon. --- src/autotests/HotSpotFilterTest.cpp | 77 +++++++++++++++++++++++++++++ src/autotests/HotSpotFilterTest.h | 3 ++ src/filterHotSpots/UrlFilter.h | 1 - tests/text-files/cat_test_urls.txt | 12 +++++ 4 files changed, 92 insertions(+), 1 deletion(-) diff --git a/src/autotests/HotSpotFilterTest.cpp b/src/autotests/HotSpotFilterTest.cpp index 48473c06d..417c351f0 100644 --- a/src/autotests/HotSpotFilterTest.cpp +++ b/src/autotests/HotSpotFilterTest.cpp @@ -5,6 +5,7 @@ */ #include "HotSpotFilterTest.h" +#include "filterHotSpots/HotSpot.h" #include QTEST_GUILESS_MAIN(HotSpotFilterTest) @@ -95,6 +96,8 @@ void HotSpotFilterTest::testUrlFilterRegex_data() QTest::newRow("query_with_question_marks") << "ldap://[2001:db8::7]/c=GB?objectClass?one" << "ldap://[2001:db8::7]/c=GB?objectClass?one" << true; + QTest::newRow("two_fragments") << "https://example.com#1#2" + << "https://example.com#1" << true; QTest::newRow("path_with_parens") << "https://en.wikipedia.org/wiki/C_(programming_language)" << "https://en.wikipedia.org/wiki/C_(programming_language)" << true; @@ -122,4 +125,78 @@ void HotSpotFilterTest::testUrlFilterRegex() } } +void HotSpotFilterTest::testUrlFilter_data() +{ + QTest::addColumn("url"); + QTest::addColumn("expectedUrl"); + QTest::addColumn("matchResult"); + + // If no invalid character is found at the end, the resultUrl should equal the FullUrlRegExp match. + QTest::newRow("url_simple") << " https://api.kde.org" + << "https://api.kde.org" << true; + QTest::newRow("url_with_port") << "\nhttps://api.kde.org:2098" + << "https://api.kde.org:2098" << true; + QTest::newRow("empty_query") << "http://example.com/?" + << "http://example.com/?" << true; + QTest::newRow("empty_fragment") << "http://example.com/#" + << "http://example.com/#" << true; + QTest::newRow("url_all_bells") << " https://user:pass@api.kde.org:2098/path/to/somewhere?somequery=foo#fragment" + << "https://user:pass@api.kde.org:2098/path/to/somewhere?somequery=foo#fragment" << true; + + // with an invalid character at the end + QTest::newRow("url_with_single_quote_end") << "https://example.com'" + << "https://example.com" << true; + QTest::newRow("url_with_comma_end") << "https://example.com," + << "https://example.com" << true; + QTest::newRow("url_with_dot_end") << "https://example.com." + << "https://example.com" << true; + QTest::newRow("url_with_colon_end") << "https://example.com/:" + << "https://example.com/" << true; + QTest::newRow("url_with_semicolon_end") << "https://example.com;" + << "https://example.com" << true; + + // complex cases + QTest::newRow("url_with_double_dot_end") << "https://example.com.." + << "https://example.com" << true; + QTest::newRow("url_with_dot_start_and_end") << ".https://example.com." + << "https://example.com" << true; + QTest::newRow("url_with_single_quote_comma_end") << "'https://example.com'," + << "https://example.com" << true; + QTest::newRow("url_with_double_quote_comma_end") << "\"https://example.com\"," + << "https://example.com" << true; + QTest::newRow("url_with_single_quote_inside") << "'https://en.wikipedia.org/wiki/Earth's_rotation'," + << "https://en.wikipedia.org/wiki/Earth's_rotation" << true; +} + +void HotSpotFilterTest::testUrlFilter() +{ + QFETCH(QString, url); + QFETCH(QString, expectedUrl); + QFETCH(bool, matchResult); + + const QRegularExpression ®ex = Konsole::UrlFilter::FullUrlRegExp; + const QRegularExpressionMatch match = regex.match(url); + // qDebug() << match; + + QCOMPARE(match.hasMatch(), matchResult); + if (matchResult) { + auto capturedText = match.capturedTexts()[0]; + + // The capturedText is placed at the location from (0, 0) to (0, length). + // After processing, the resulting position is extracted to obtain resultUrl. + int startLine = 0; + int startColumn = 0; + int endLine = 0; + int endColumn = capturedText.length(); + + QSharedPointer hotSpot = Konsole::UrlFilter().newHotSpot(startLine, startColumn, endLine, endColumn, match.capturedTexts()); + + int resultStartColumn = hotSpot->startColumn(); + int resultEndColumn = hotSpot->endColumn(); + + QString resultUrl = capturedText.mid(resultStartColumn, resultEndColumn - resultStartColumn); + QCOMPARE(resultUrl, expectedUrl); + } +} + #include "moc_HotSpotFilterTest.cpp" diff --git a/src/autotests/HotSpotFilterTest.h b/src/autotests/HotSpotFilterTest.h index 728ebbe40..a207b0d54 100644 --- a/src/autotests/HotSpotFilterTest.h +++ b/src/autotests/HotSpotFilterTest.h @@ -16,6 +16,9 @@ class HotSpotFilterTest : public QObject private Q_SLOTS: void testUrlFilterRegex_data(); void testUrlFilterRegex(); + + void testUrlFilter_data(); + void testUrlFilter(); }; #endif // HOTSPOTFILTERTEST_H diff --git a/src/filterHotSpots/UrlFilter.h b/src/filterHotSpots/UrlFilter.h index 75a2fe65e..1c9bdb542 100644 --- a/src/filterHotSpots/UrlFilter.h +++ b/src/filterHotSpots/UrlFilter.h @@ -21,7 +21,6 @@ class KONSOLEPRIVATE_EXPORT UrlFilter : public RegExpFilter public: UrlFilter(); -protected: QSharedPointer newHotSpot(int beginRow, int beginColumn, int endRow, int endColumn, const QStringList &list) override; public: diff --git a/tests/text-files/cat_test_urls.txt b/tests/text-files/cat_test_urls.txt index 5dd97ca3d..80234c34e 100644 --- a/tests/text-files/cat_test_urls.txt +++ b/tests/text-files/cat_test_urls.txt @@ -249,6 +249,18 @@ xmpp:ogoffart@kde.org 'https://en.wikipedia.org/wiki/Earths_rotation' "https://en.wikipedia.org/wiki/Earth's_rotation" +https://example.com' +https://example.com, +https://example.com. +https://example.com/: +https://example.com; + +https://example.com.. +.https://example.com. +'https://example.com', +"https://example.com", +'https://en.wikipedia.org/wiki/Earth's_rotation', + # testing edge cases - if 80 columns http://www.website.com/directory/relative.html?query=test&name=harry http://www.website.com/directory/relative.html?query=test&name=harryhttp://www.website.com/directory/relative.html?query=test&name=harry --------------