From 6d0044f1dcffebc29dccd9d37d90f8abdb941a88 Mon Sep 17 00:00:00 2001 From: Samuel Gaist Date: Fri, 10 Nov 2017 16:48:50 +0100 Subject: Add wildcard-to-regexp support to QRegularExpression This method will make QRegularExpression on par with QRegExp and will allow to replace this class when a wildcard expression can be set through an API (e.g. QSortFilterProxyModel::setFilterWildcard). For other use cases, see QTBUG-34052. [ChangeLog][QRegularExpression] Implemented support for wildcard patterns. Warning: QRegularExpression might not give the exact same result as QRegExp as its implementation follows strictly the glob patterns definition for the wildcard expressions. Change-Id: I5ed4617ca679159430c3d46da3449f6b3100e366 Reviewed-by: Thiago Macieira Reviewed-by: Oswald Buddenhagen --- .../qregularexpression/tst_qregularexpression.cpp | 77 ++++++++++++++++++++++ .../qregularexpression/tst_qregularexpression.h | 5 ++ 2 files changed, 82 insertions(+) (limited to 'tests/auto/corelib/tools') diff --git a/tests/auto/corelib/tools/qregularexpression/tst_qregularexpression.cpp b/tests/auto/corelib/tools/qregularexpression/tst_qregularexpression.cpp index c828551e44..ec495fd272 100644 --- a/tests/auto/corelib/tools/qregularexpression/tst_qregularexpression.cpp +++ b/tests/auto/corelib/tools/qregularexpression/tst_qregularexpression.cpp @@ -2066,3 +2066,80 @@ void tst_QRegularExpression::QStringAndQStringRefEquivalence() } } } + +void tst_QRegularExpression::wildcard_data() +{ + QTest::addColumn("pattern"); + QTest::addColumn("string"); + QTest::addColumn("foundIndex"); + + auto addRow = [](const char *pattern, const char *string, int foundIndex) { + QTest::addRow(pattern) << pattern << string << foundIndex; + }; + + addRow("*.html", "test.html", 0); + addRow("*.html", "test.htm", -1); + addRow("bar*", "foobarbaz", 3); + addRow("*", "Qt Rocks!", 0); + addRow(".html", "test.html", 4); + addRow(".h", "test.cpp", -1); + addRow(".???l", "test.html", 4); + addRow("?", "test.html", 0); + addRow("?m", "test.html", 6); + addRow(".h[a-z]ml", "test.html", 4); + addRow(".h[A-Z]ml", "test.html", -1); + addRow(".h[A-Z]ml", "test.hTml", 4); + addRow(".h[!A-Z]ml", "test.hTml", -1); + addRow(".h[!A-Z]ml", "test.html", 4); + addRow(".h[!T]ml", "test.hTml", -1); + addRow(".h[!T]ml", "test.html", 4); + addRow(".h[!T]m[!L]", "test.htmL", -1); + addRow(".h[!T]m[!L]", "test.html", 4); + addRow(".h[][!]", "test.h]ml", 4); + addRow(".h[][!]", "test.h[ml", 4); + addRow(".h[][!]", "test.h!ml", 4); +} + +void tst_QRegularExpression::wildcard() +{ + QFETCH(QString, pattern); + QFETCH(QString, string); + QFETCH(int, foundIndex); + + QRegularExpression re; + re.setWildcardPattern(pattern); + if (forceOptimize) + re.optimize(); + + QRegularExpressionMatch match = re.match(string); + + QCOMPARE(match.capturedStart(), foundIndex); +} + +void tst_QRegularExpression::testInvalidWildcard_data() +{ + QTest::addColumn("pattern"); + QTest::addColumn("isValid"); + + QTest::newRow("valid []") << "[abc]" << true; + QTest::newRow("valid ending ]") << "abc]" << true; + QTest::newRow("invalid [") << "[abc" << false; + QTest::newRow("ending [") << "abc[" << false; + QTest::newRow("ending [^") << "abc[^" << false; + QTest::newRow("ending [\\") << "abc[\\" << false; + QTest::newRow("ending []") << "abc[]" << false; + QTest::newRow("ending [[") << "abc[[" << false; +} + +void tst_QRegularExpression::testInvalidWildcard() +{ + QFETCH(QString, pattern); + + QRegularExpression re; + re.setWildcardPattern(pattern); + if (forceOptimize) + re.optimize(); + + QFETCH(bool, isValid); + QCOMPARE(re.isValid(), isValid); +} diff --git a/tests/auto/corelib/tools/qregularexpression/tst_qregularexpression.h b/tests/auto/corelib/tools/qregularexpression/tst_qregularexpression.h index 8bb4aa0cce..db5b15be66 100644 --- a/tests/auto/corelib/tools/qregularexpression/tst_qregularexpression.h +++ b/tests/auto/corelib/tools/qregularexpression/tst_qregularexpression.h @@ -69,6 +69,11 @@ private slots: void JOptionUsage(); void QStringAndQStringRefEquivalence(); + void wildcard_data(); + void wildcard(); + void testInvalidWildcard_data(); + void testInvalidWildcard(); + private: void provideRegularExpressions(); }; -- cgit v1.2.3