From c2266981c91825838a0d5fe9ec28267c2ede42c1 Mon Sep 17 00:00:00 2001 From: Eike Ziller Date: Tue, 21 Feb 2023 10:03:11 +0100 Subject: [PATCH] Add tests to Utils::wildcardToRegularExpression It was unclear if the new non-filepath version that is added to Qt is similar enough to replace our own version, so just add the new tests from Qt to check. Change-Id: I9ac2739500ab3b8557c0e6223489e0fdddd79091 Reviewed-by: Jarek Kobus Reviewed-by: Qt CI Bot Reviewed-by: Reviewed-by: Christian Stenger --- .../utils/stringutils/tst_stringutils.cpp | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/tests/auto/utils/stringutils/tst_stringutils.cpp b/tests/auto/utils/stringutils/tst_stringutils.cpp index 6b481f06025..e13e8411a5b 100644 --- a/tests/auto/utils/stringutils/tst_stringutils.cpp +++ b/tests/auto/utils/stringutils/tst_stringutils.cpp @@ -77,6 +77,8 @@ private slots: void testJoinStrings(); void testTrim_data(); void testTrim(); + void testWildcardToRegularExpression_data(); + void testWildcardToRegularExpression(); private: TestMacroExpander mx; @@ -330,6 +332,78 @@ void tst_StringUtils::testTrim() QCOMPARE(Utils::trim(data.input, data.ch), data.bothSides); } +void tst_StringUtils::testWildcardToRegularExpression_data() +{ + QTest::addColumn("pattern"); + QTest::addColumn("string"); + QTest::addColumn("matches"); + auto addRow = [](const char *pattern, const char *string, bool matchesNonPathGlob) { + QTest::addRow("%s@%s", pattern, string) << pattern << string << matchesNonPathGlob; + }; + addRow("*.html", "test.html", true); + addRow("*.html", "test.htm", false); + addRow("*bar*", "foobarbaz", true); + addRow("*", "Qt Rocks!", true); + addRow("*.h", "test.cpp", false); + addRow("*.???l", "test.html", true); + addRow("*?", "test.html", true); + addRow("*?ml", "test.html", true); + addRow("*[*]", "test.html", false); + addRow("*[?]", "test.html", false); + addRow("*[?]ml", "test.h?ml", true); + addRow("*[[]ml", "test.h[ml", true); + addRow("*[]]ml", "test.h]ml", true); + addRow("*.h[a-z]ml", "test.html", true); + addRow("*.h[A-Z]ml", "test.html", false); + addRow("*.h[A-Z]ml", "test.hTml", true); + addRow("*.h[!A-Z]ml", "test.hTml", false); + addRow("*.h[!A-Z]ml", "test.html", true); + addRow("*.h[!T]ml", "test.hTml", false); + addRow("*.h[!T]ml", "test.html", true); + addRow("*.h[!T]m[!L]", "test.htmL", false); + addRow("*.h[!T]m[!L]", "test.html", true); + addRow("*.h[][!]ml", "test.h]ml", true); + addRow("*.h[][!]ml", "test.h[ml", true); + addRow("*.h[][!]ml", "test.h!ml", true); + addRow("foo/*/bar", "foo/baz/bar", true); + addRow("foo/*/bar", "foo/fie/baz/bar", true); + addRow("foo?bar", "foo/bar", true); + addRow("foo/(*)/bar", "foo/baz/bar", false); + addRow("foo/(*)/bar", "foo/(baz)/bar", true); + addRow("foo/?/bar", "foo/Q/bar", true); + addRow("foo/?/bar", "foo/Qt/bar", false); + addRow("foo/(?)/bar", "foo/Q/bar", false); + addRow("foo/(?)/bar", "foo/(Q)/bar", true); + addRow("foo\\*\\bar", "foo\\baz\\bar", true); + addRow("foo\\*\\bar", "foo/baz/bar", false); + addRow("foo\\*\\bar", "foo/baz\\bar", false); + addRow("foo\\*\\bar", "foo\\fie\\baz\\bar", true); + addRow("foo\\*\\bar", "foo/fie/baz/bar", false); + addRow("foo/*/bar", "foo\\baz\\bar", false); + addRow("foo/*/bar", "foo/baz/bar", true); + addRow("foo/*/bar", "foo\\fie\\baz\\bar", false); + addRow("foo/*/bar", "foo/fie/baz/bar", true); + addRow("foo\\(*)\\bar", "foo\\baz\\bar", false); + addRow("foo\\(*)\\bar", "foo\\(baz)\\bar", true); + addRow("foo\\?\\bar", "foo\\Q\\bar", true); + addRow("foo\\?\\bar", "foo\\Qt\\bar", false); + addRow("foo\\(?)\\bar", "foo\\Q\\bar", false); + addRow("foo\\(?)\\bar", "foo\\(Q)\\bar", true); + + addRow("foo*bar", "foo/fie/baz/bar", true); + addRow("fie*bar", "foo/fie/baz/bar", false); +} + +void tst_StringUtils::testWildcardToRegularExpression() +{ + QFETCH(QString, pattern); + QFETCH(QString, string); + QFETCH(bool, matches); + + const QRegularExpression re(Utils::wildcardToRegularExpression(pattern)); + QCOMPARE(string.contains(re), matches); +} + QTEST_GUILESS_MAIN(tst_StringUtils) #include "tst_stringutils.moc"