From c714f58085859024b4a3f6ed1968b716b5a70036 Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Sun, 31 Aug 2014 17:51:00 +0200 Subject: QRegularExpression: lift common code in the autotest Unify the code paths which test the normal match, the partial match and the global matches. Not only does this remove the duplication, but it will also enable the corresponding tests to be run over QStringRefs. Change-Id: Ia2cbe7a540bbe864b0975c9353697bc06c99c397 Reviewed-by: Marc Mutz --- .../qregularexpression/tst_qregularexpression.cpp | 129 +++++++++------------ 1 file changed, 52 insertions(+), 77 deletions(-) (limited to 'tests/auto/corelib/tools/qregularexpression') diff --git a/tests/auto/corelib/tools/qregularexpression/tst_qregularexpression.cpp b/tests/auto/corelib/tools/qregularexpression/tst_qregularexpression.cpp index 5fad1bb738..7322384803 100644 --- a/tests/auto/corelib/tools/qregularexpression/tst_qregularexpression.cpp +++ b/tests/auto/corelib/tools/qregularexpression/tst_qregularexpression.cpp @@ -245,6 +245,55 @@ void consistencyCheck(const QRegularExpressionMatchIterator &iterator) } +template +static void prepareResultForNoMatchType(Result *r, const Result &orig) +{ + Q_UNUSED(r); + Q_UNUSED(orig); +} + +static void prepareResultForNoMatchType(Match *m, const Match &orig) +{ + m->isValid = orig.isValid; +} + +template +static void testMatch(const QRegularExpression ®exp, + QREMatchFunc matchingMethod, + const QString &subject, + int offset, + QRegularExpression::MatchType matchType, + QRegularExpression::MatchOptions matchOptions, + const Result &result) +{ + if (forceOptimize) + regexp.optimize(); + + { + const QREMatch m = (regexp.*matchingMethod)(subject, offset, matchType, matchOptions); + consistencyCheck(m); + QVERIFY(m == result); + QCOMPARE(m.regularExpression(), regexp); + QCOMPARE(m.matchType(), matchType); + QCOMPARE(m.matchOptions(), matchOptions); + } + { + // ignore the expected results provided by the match object -- + // we'll never get any result when testing the NoMatch type. + // Just check the validity of the match here. + Result realMatch; + prepareResultForNoMatchType(&realMatch, result); + + const QREMatch m = (regexp.*matchingMethod)(subject, offset, QRegularExpression::NoMatch, matchOptions); + consistencyCheck(m); + QVERIFY(m == realMatch); + QCOMPARE(m.regularExpression(), regexp); + QCOMPARE(m.matchType(), QRegularExpression::NoMatch); + QCOMPARE(m.matchOptions(), matchOptions); + } +} + + void tst_QRegularExpression::provideRegularExpressions() { QTest::addColumn("pattern"); @@ -736,32 +785,7 @@ void tst_QRegularExpression::normalMatch() QFETCH(QRegularExpression::MatchOptions, matchOptions); QFETCH(Match, match); - if (forceOptimize) - regexp.optimize(); - - { - QRegularExpressionMatch m = regexp.match(subject, offset, QRegularExpression::NormalMatch, matchOptions); - consistencyCheck(m); - QVERIFY(m == match); - QCOMPARE(m.regularExpression(), regexp); - QCOMPARE(m.matchType(), QRegularExpression::NormalMatch); - QCOMPARE(m.matchOptions(), matchOptions); - } - { - // ignore the expected results provided by the match object -- - // we'll never get any result when testing the NoMatch type. - // Just check the validity of the match here. - Match realMatch; - realMatch.clear(); - realMatch.isValid = match.isValid; - - QRegularExpressionMatch m = regexp.match(subject, offset, QRegularExpression::NoMatch, matchOptions); - consistencyCheck(m); - QVERIFY(m == realMatch); - QCOMPARE(m.regularExpression(), regexp); - QCOMPARE(m.matchType(), QRegularExpression::NoMatch); - QCOMPARE(m.matchOptions(), matchOptions); - } + testMatch(regexp, &QRegularExpression::match, subject, offset, QRegularExpression::NormalMatch, matchOptions, match); } void tst_QRegularExpression::partialMatch_data() @@ -1017,32 +1041,7 @@ void tst_QRegularExpression::partialMatch() QFETCH(QRegularExpression::MatchOptions, matchOptions); QFETCH(Match, match); - if (forceOptimize) - regexp.optimize(); - - { - QRegularExpressionMatch m = regexp.match(subject, offset, matchType, matchOptions); - consistencyCheck(m); - QVERIFY(m == match); - QCOMPARE(m.regularExpression(), regexp); - QCOMPARE(m.matchType(), matchType); - QCOMPARE(m.matchOptions(), matchOptions); - } - { - // ignore the expected results provided by the match object -- - // we'll never get any result when testing the NoMatch type. - // Just check the validity of the match here. - Match realMatch; - realMatch.clear(); - realMatch.isValid = match.isValid; - - QRegularExpressionMatch m = regexp.match(subject, offset, QRegularExpression::NoMatch, matchOptions); - consistencyCheck(m); - QVERIFY(m == realMatch); - QCOMPARE(m.regularExpression(), regexp); - QCOMPARE(m.matchType(), QRegularExpression::NoMatch); - QCOMPARE(m.matchOptions(), matchOptions); - } + testMatch(regexp, &QRegularExpression::match, subject, offset, matchType, matchOptions, match); } void tst_QRegularExpression::globalMatch_data() @@ -1312,31 +1311,7 @@ void tst_QRegularExpression::globalMatch() QFETCH(QRegularExpression::MatchOptions, matchOptions); QFETCH(QList, matchList); - if (forceOptimize) - regexp.optimize(); - - { - QRegularExpressionMatchIterator iterator = regexp.globalMatch(subject, offset, matchType, matchOptions); - consistencyCheck(iterator); - QVERIFY(iterator == matchList); - QCOMPARE(iterator.regularExpression(), regexp); - QCOMPARE(iterator.matchType(), matchType); - QCOMPARE(iterator.matchOptions(), matchOptions); - } - { - // ignore the expected results provided by the match object -- - // we'll never get any result when testing the NoMatch type. - // Just check the validity of the match here. - QList realMatchList; - - QRegularExpressionMatchIterator iterator = regexp.globalMatch(subject, offset, QRegularExpression::NoMatch, matchOptions); - consistencyCheck(iterator); - QVERIFY(iterator == realMatchList); - QCOMPARE(iterator.regularExpression(), regexp); - QCOMPARE(iterator.matchType(), QRegularExpression::NoMatch); - QCOMPARE(iterator.matchOptions(), matchOptions); - } - + testMatch(regexp, &QRegularExpression::globalMatch, subject, offset, matchType, matchOptions, matchList); } void tst_QRegularExpression::serialize_data() -- cgit v1.2.3