diff options
Diffstat (limited to 'tests/auto/corelib/text/qregularexpression/tst_qregularexpression.cpp')
-rw-r--r-- | tests/auto/corelib/text/qregularexpression/tst_qregularexpression.cpp | 171 |
1 files changed, 106 insertions, 65 deletions
diff --git a/tests/auto/corelib/text/qregularexpression/tst_qregularexpression.cpp b/tests/auto/corelib/text/qregularexpression/tst_qregularexpression.cpp index 37fa8147ed..72c49d2a9c 100644 --- a/tests/auto/corelib/text/qregularexpression/tst_qregularexpression.cpp +++ b/tests/auto/corelib/text/qregularexpression/tst_qregularexpression.cpp @@ -1,6 +1,6 @@ // Copyright (C) 2015 Giuseppe D'Angelo <dangelog@gmail.com>. // Copyright (C) 2015 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <QTest> #include <qstring.h> @@ -393,6 +393,7 @@ static void testMatch(const QRegularExpression ®exp, result); } +// ### Qt 7: there should no longer be the need for these typedef QRegularExpressionMatch (QRegularExpression::*QREMatchStringPMF)(const QString &, qsizetype, QRegularExpression::MatchType, QRegularExpression::MatchOptions) const; typedef QRegularExpressionMatch (QRegularExpression::*QREMatchStringViewPMF)(QStringView, qsizetype, QRegularExpression::MatchType, QRegularExpression::MatchOptions) const; typedef QRegularExpressionMatchIterator (QRegularExpression::*QREGlobalMatchStringPMF)(const QString &, qsizetype, QRegularExpression::MatchType, QRegularExpression::MatchOptions) const; @@ -1096,7 +1097,7 @@ void tst_QRegularExpression::normalMatch() testMatch<QRegularExpressionMatch>(regexp, static_cast<QREMatchStringPMF>(&QRegularExpression::match), - static_cast<QREMatchStringViewPMF>(&QRegularExpression::match), + static_cast<QREMatchStringViewPMF>(&QRegularExpression::matchView), subject, offset, QRegularExpression::NormalMatch, @@ -1368,7 +1369,7 @@ void tst_QRegularExpression::partialMatch() testMatch<QRegularExpressionMatch>(regexp, static_cast<QREMatchStringPMF>(&QRegularExpression::match), - static_cast<QREMatchStringViewPMF>(&QRegularExpression::match), + static_cast<QREMatchStringViewPMF>(&QRegularExpression::matchView), subject, offset, matchType, @@ -1645,7 +1646,7 @@ void tst_QRegularExpression::globalMatch() testMatch<QRegularExpressionMatchIterator>(regexp, static_cast<QREGlobalMatchStringPMF>(&QRegularExpression::globalMatch), - static_cast<QREGlobalMatchStringViewPMF>(&QRegularExpression::globalMatch), + static_cast<QREGlobalMatchStringViewPMF>(&QRegularExpression::globalMatchView), subject, offset, matchType, @@ -1873,12 +1874,14 @@ void tst_QRegularExpression::captureNamesNul() QString captureName("name"); QCOMPARE(m.captured(captureName), "456"); QCOMPARE(m.captured(QStringView(captureName)), "456"); + QCOMPARE(m.captured(QAnyStringView(captureName)), "456"); QCOMPARE(m.captured(qToStringViewIgnoringNull(captureName)), "456"); QCOMPARE(m.captured(u"name"), "456"); captureName = "anotherName"; QCOMPARE(m.captured(captureName), "789"); QCOMPARE(m.captured(QStringView(captureName)), "789"); + QCOMPARE(m.captured(QAnyStringView(captureName)), "789"); QCOMPARE(m.captured(qToStringViewIgnoringNull(captureName)), "789"); QCOMPARE(m.captured(u"anotherName"), "789"); } @@ -1985,7 +1988,7 @@ void tst_QRegularExpression::QStringAndQStringViewEquivalence() QCOMPARE(match.capturedEnd(), 4); } { - const QRegularExpressionMatch match = re.match(QStringView(subject)); + const QRegularExpressionMatch match = re.matchView(QStringView(subject)); consistencyCheck(match); QVERIFY(match.isValid()); QVERIFY(match.hasMatch()); @@ -2003,7 +2006,7 @@ void tst_QRegularExpression::QStringAndQStringViewEquivalence() QCOMPARE(match.capturedEnd(), 4); } { - const QRegularExpressionMatch match = re.match(QStringView(subject), 1); + const QRegularExpressionMatch match = re.matchView(QStringView(subject), 1); consistencyCheck(match); QVERIFY(match.isValid()); QVERIFY(match.hasMatch()); @@ -2021,7 +2024,7 @@ void tst_QRegularExpression::QStringAndQStringViewEquivalence() QCOMPARE(match.capturedEnd(), 6); } { - const QRegularExpressionMatch match = re.match(QStringView(subject).mid(1)); + const QRegularExpressionMatch match = re.matchView(QStringView(subject).mid(1)); consistencyCheck(match); QVERIFY(match.isValid()); QVERIFY(match.hasMatch()); @@ -2039,7 +2042,7 @@ void tst_QRegularExpression::QStringAndQStringViewEquivalence() QCOMPARE(match.capturedEnd(), 6); } { - const QRegularExpressionMatch match = re.match(QStringView(subject).mid(1), 1); + const QRegularExpressionMatch match = re.matchView(QStringView(subject).mid(1), 1); consistencyCheck(match); QVERIFY(match.isValid()); QVERIFY(match.hasMatch()); @@ -2057,7 +2060,7 @@ void tst_QRegularExpression::QStringAndQStringViewEquivalence() QCOMPARE(match.capturedEnd(), 7); } { - const QRegularExpressionMatch match = re.match(QStringView(subject), 4); + const QRegularExpressionMatch match = re.matchView(QStringView(subject), 4); consistencyCheck(match); QVERIFY(match.isValid()); QVERIFY(match.hasMatch()); @@ -2072,7 +2075,7 @@ void tst_QRegularExpression::QStringAndQStringViewEquivalence() QVERIFY(!match.hasMatch()); } { - const QRegularExpressionMatch match = re.match(QStringView(subject).mid(4)); + const QRegularExpressionMatch match = re.matchView(QStringView(subject).mid(4)); consistencyCheck(match); QVERIFY(match.isValid()); QVERIFY(!match.hasMatch()); @@ -2105,7 +2108,7 @@ void tst_QRegularExpression::QStringAndQStringViewEquivalence() QVERIFY(!i.hasNext()); } { - QRegularExpressionMatchIterator i = re.globalMatch(QStringView(subject)); + QRegularExpressionMatchIterator i = re.globalMatchView(QStringView(subject)); QVERIFY(i.isValid()); consistencyCheck(i); @@ -2157,7 +2160,7 @@ void tst_QRegularExpression::QStringAndQStringViewEquivalence() QVERIFY(!i.hasNext()); } { - QRegularExpressionMatchIterator i = re.globalMatch(QStringView(subject), 1); + QRegularExpressionMatchIterator i = re.globalMatchView(QStringView(subject), 1); QVERIFY(i.isValid()); consistencyCheck(i); @@ -2199,7 +2202,7 @@ void tst_QRegularExpression::QStringAndQStringViewEquivalence() QVERIFY(!i.hasNext()); } { - QRegularExpressionMatchIterator i = re.globalMatch(QStringView(subject).mid(1)); + QRegularExpressionMatchIterator i = re.globalMatchView(QStringView(subject).mid(1)); QVERIFY(i.isValid()); consistencyCheck(i); @@ -2231,7 +2234,7 @@ void tst_QRegularExpression::QStringAndQStringViewEquivalence() QVERIFY(!i.hasNext()); } { - QRegularExpressionMatchIterator i = re.globalMatch(QStringView(subject).mid(1), 1); + QRegularExpressionMatchIterator i = re.globalMatchView(QStringView(subject).mid(1), 1); QVERIFY(i.isValid()); consistencyCheck(i); @@ -2263,7 +2266,7 @@ void tst_QRegularExpression::QStringAndQStringViewEquivalence() QVERIFY(!i.hasNext()); } { - QRegularExpressionMatchIterator i = re.globalMatch(QStringView(subject).mid(1), 1); + QRegularExpressionMatchIterator i = re.globalMatchView(QStringView(subject).mid(1), 1); QVERIFY(i.isValid()); consistencyCheck(i); @@ -2296,7 +2299,7 @@ void tst_QRegularExpression::QStringAndQStringViewEquivalence() QVERIFY(!i.hasNext()); } { - QRegularExpressionMatchIterator i = re.globalMatch(QStringView(subject), 4); + QRegularExpressionMatchIterator i = re.globalMatchView(QStringView(subject), 4); QVERIFY(i.isValid()); consistencyCheck(i); @@ -2318,7 +2321,7 @@ void tst_QRegularExpression::QStringAndQStringViewEquivalence() QVERIFY(!i.hasNext()); } { - QRegularExpressionMatchIterator i = re.globalMatch(QStringView(subject).mid(4)); + QRegularExpressionMatchIterator i = re.globalMatchView(QStringView(subject).mid(4)); consistencyCheck(i); QVERIFY(i.isValid()); QVERIFY(!i.hasNext()); @@ -2388,6 +2391,10 @@ void tst_QRegularExpression::threadSafety_data() void tst_QRegularExpression::threadSafety() { +#if defined(Q_OS_WASM) + QSKIP("This test misbehaves on WASM. Investigation needed (QTBUG-110067)"); +#endif + QFETCH(QString, pattern); QFETCH(QString, subject); @@ -2445,55 +2452,79 @@ void tst_QRegularExpression::wildcard_data() { QTest::addColumn<QString>("pattern"); QTest::addColumn<QString>("string"); - QTest::addColumn<qsizetype>("foundIndex"); + QTest::addColumn<bool>("matchesPathGlob"); + QTest::addColumn<bool>("matchesNonPathGlob"); + QTest::addColumn<bool>("anchored"); - auto addRow = [](const char *pattern, const char *string, qsizetype foundIndex) { - QTest::newRow(pattern) << pattern << string << foundIndex; + auto addRow = [](const char *pattern, const char *string, bool matchesPathGlob, bool matchesNonPathGlob, bool anchored = true) { + QTest::addRow("%s@%s", pattern, string) << pattern << string << matchesPathGlob << matchesNonPathGlob << anchored; }; - addRow("*.html", "test.html", 0); - addRow("*.html", "test.htm", -1); - addRow("*bar*", "foobarbaz", 0); - addRow("*", "Qt Rocks!", 0); - addRow("*.html", "test.html", 0); - addRow("*.h", "test.cpp", -1); - addRow("*.???l", "test.html", 0); - addRow("*?", "test.html", 0); - addRow("*?ml", "test.html", 0); - addRow("*[*]", "test.html", -1); - addRow("*[?]","test.html", -1); - addRow("*[?]ml","test.h?ml", 0); - addRow("*[[]ml","test.h[ml", 0); - addRow("*[]]ml","test.h]ml", 0); - addRow("*.h[a-z]ml", "test.html", 0); - addRow("*.h[A-Z]ml", "test.html", -1); - addRow("*.h[A-Z]ml", "test.hTml", 0); - addRow("*.h[!A-Z]ml", "test.hTml", -1); - addRow("*.h[!A-Z]ml", "test.html", 0); - addRow("*.h[!T]ml", "test.hTml", -1); - addRow("*.h[!T]ml", "test.html", 0); - addRow("*.h[!T]m[!L]", "test.htmL", -1); - addRow("*.h[!T]m[!L]", "test.html", 0); - addRow("*.h[][!]ml", "test.h]ml", 0); - addRow("*.h[][!]ml", "test.h[ml", 0); - addRow("*.h[][!]ml", "test.h!ml", 0); - - addRow("foo/*/bar", "foo/baz/bar", 0); - addRow("foo/(*)/bar", "foo/baz/bar", -1); - addRow("foo/(*)/bar", "foo/(baz)/bar", 0); - addRow("foo/?/bar", "foo/Q/bar", 0); - addRow("foo/?/bar", "foo/Qt/bar", -1); - addRow("foo/(?)/bar", "foo/Q/bar", -1); - addRow("foo/(?)/bar", "foo/(Q)/bar", 0); + addRow("*.html", "test.html", true, true); + addRow("*.html", "test.htm", false, false); + addRow("*bar*", "foobarbaz", true, true); + addRow("*", "Qt Rocks!", true, true); + addRow("*.h", "test.cpp", false, false); + addRow("*.???l", "test.html", true, true); + addRow("*?", "test.html", true, true); + addRow("*?ml", "test.html", true, true); + addRow("*[*]", "test.html", false, false); + addRow("*[?]","test.html", false, false); + addRow("*[?]ml","test.h?ml", true, true); + addRow("*[[]ml","test.h[ml", true, true); + addRow("*[]]ml","test.h]ml", true, true); + addRow("*.h[a-z]ml", "test.html", true, true); + addRow("*.h[A-Z]ml", "test.html", false, false); + addRow("*.h[A-Z]ml", "test.hTml", true, true); + addRow("*.h[!A-Z]ml", "test.hTml", false, false); + addRow("*.h[!A-Z]ml", "test.html", true, true); + addRow("*.h[!T]ml", "test.hTml", false, false); + addRow("*.h[!T]ml", "test.html", true, true); + addRow("*.h[!T]m[!L]", "test.htmL", false, false); + addRow("*.h[!T]m[!L]", "test.html", true, true); + addRow("*.h[][!]ml", "test.h]ml", true, true); + addRow("*.h[][!]ml", "test.h[ml", true, true); + addRow("*.h[][!]ml", "test.h!ml", true, true); + + addRow("foo/*/bar", "foo/baz/bar", true, true); + addRow("foo/*/bar", "foo/fie/baz/bar", false, true); + addRow("foo?bar", "foo/bar", false, true); + addRow("foo/(*)/bar", "foo/baz/bar", false, false); + addRow("foo/(*)/bar", "foo/(baz)/bar", true, true); + addRow("foo/?/bar", "foo/Q/bar", true, true); + addRow("foo/?/bar", "foo/Qt/bar", false, false); + addRow("foo/(?)/bar", "foo/Q/bar", false, false); + addRow("foo/(?)/bar", "foo/(Q)/bar", true, true); + + addRow("foo*bar", "foo/fie/baz/bar", false, true); + addRow("foo*bar", "foo bar", true, true); + addRow("foo*bar", "foo\tbar", true, true); + addRow("foo*bar", "foo\nbar", true, true); + addRow("foo*bar", "foo\r\nbar", true, true); + + // different anchor modes + addRow("foo", "afoob", false, false, true); + addRow("foo", "afoob", true, true, false); + + addRow("fie*bar", "foo/fie/baz/bar", false, false, true); + addRow("fie*bar", "foo/fie/baz/bar", false, true, false); #ifdef Q_OS_WIN - addRow("foo\\*\\bar", "foo\\baz\\bar", 0); - addRow("foo\\(*)\\bar", "foo\\baz\\bar", -1); - addRow("foo\\(*)\\bar", "foo\\(baz)\\bar", 0); - addRow("foo\\?\\bar", "foo\\Q\\bar", 0); - addRow("foo\\?\\bar", "foo\\Qt\\bar", -1); - addRow("foo\\(?)\\bar", "foo\\Q\\bar", -1); - addRow("foo\\(?)\\bar", "foo\\(Q)\\bar", 0); + addRow("foo\\*\\bar", "foo\\baz\\bar", true, true); + addRow("foo\\*\\bar", "foo/baz/bar", true, false); + addRow("foo\\*\\bar", "foo/baz\\bar", true, false); + addRow("foo\\*\\bar", "foo\\fie\\baz\\bar", false, true); + addRow("foo\\*\\bar", "foo/fie/baz/bar", false, false); + addRow("foo/*/bar", "foo\\baz\\bar", true, false); + addRow("foo/*/bar", "foo/baz/bar", true, true); + addRow("foo/*/bar", "foo\\fie\\baz\\bar", false, false); + addRow("foo/*/bar", "foo/fie/baz/bar", false, true); + addRow("foo\\(*)\\bar", "foo\\baz\\bar", false, false); + addRow("foo\\(*)\\bar", "foo\\(baz)\\bar", true, true); + addRow("foo\\?\\bar", "foo\\Q\\bar", true, true); + addRow("foo\\?\\bar", "foo\\Qt\\bar", false, false); + addRow("foo\\(?)\\bar", "foo\\Q\\bar", false, false); + addRow("foo\\(?)\\bar", "foo\\(Q)\\bar", true, true); #endif } @@ -2501,12 +2532,22 @@ void tst_QRegularExpression::wildcard() { QFETCH(QString, pattern); QFETCH(QString, string); - QFETCH(qsizetype, foundIndex); + QFETCH(bool, matchesPathGlob); + QFETCH(bool, matchesNonPathGlob); + QFETCH(bool, anchored); - QRegularExpression re(QRegularExpression::wildcardToRegularExpression(pattern)); - QRegularExpressionMatch match = re.match(string); + QRegularExpression::WildcardConversionOptions options = {}; + if (!anchored) + options |= QRegularExpression::UnanchoredWildcardConversion; - QCOMPARE(match.capturedStart(), foundIndex); + { + QRegularExpression re(QRegularExpression::wildcardToRegularExpression(pattern, options)); + QCOMPARE(string.contains(re), matchesPathGlob); + } + { + QRegularExpression re(QRegularExpression::wildcardToRegularExpression(pattern, options | QRegularExpression::NonPathWildcardConversion)); + QCOMPARE(string.contains(re), matchesNonPathGlob); + } } void tst_QRegularExpression::testInvalidWildcard_data() |