diff options
Diffstat (limited to 'tests/auto/corelib/tools/qregularexpression/tst_qregularexpression.cpp')
-rw-r--r-- | tests/auto/corelib/tools/qregularexpression/tst_qregularexpression.cpp | 217 |
1 files changed, 205 insertions, 12 deletions
diff --git a/tests/auto/corelib/tools/qregularexpression/tst_qregularexpression.cpp b/tests/auto/corelib/tools/qregularexpression/tst_qregularexpression.cpp index 7073805db4..909725f4b8 100644 --- a/tests/auto/corelib/tools/qregularexpression/tst_qregularexpression.cpp +++ b/tests/auto/corelib/tools/qregularexpression/tst_qregularexpression.cpp @@ -1,6 +1,7 @@ /**************************************************************************** ** ** Copyright (C) 2012 Giuseppe D'Angelo <dangelog@gmail.com>. +** Copyright (C) 2013 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> ** Contact: http://www.qt-project.org/legal ** ** This file is part of the test suite of the Qt Toolkit. @@ -126,8 +127,6 @@ bool operator!=(const Match &m, const QRegularExpressionMatch &rem) bool operator==(const QRegularExpressionMatchIterator &iterator, const QList<Match> &expectedMatchList) { QRegularExpressionMatchIterator i = iterator; - if (i.isValid() != (!expectedMatchList.isEmpty())) - return false; foreach (const Match &expectedMatch, expectedMatchList) { @@ -293,6 +292,31 @@ void tst_QRegularExpression::provideRegularExpressions() | QRegularExpression::InvertedGreedinessOption); } +void tst_QRegularExpression::defaultConstructors() +{ + QRegularExpression re; + QCOMPARE(re.pattern(), QString()); + QCOMPARE(re.patternOptions(), QRegularExpression::NoPatternOption); + + QRegularExpressionMatch match; + QCOMPARE(match.regularExpression(), QRegularExpression()); + QCOMPARE(match.regularExpression(), re); + QCOMPARE(match.matchType(), QRegularExpression::NoMatch); + QCOMPARE(match.matchOptions(), QRegularExpression::NoMatchOption); + QCOMPARE(match.hasMatch(), false); + QCOMPARE(match.hasPartialMatch(), false); + QCOMPARE(match.isValid(), true); + QCOMPARE(match.lastCapturedIndex(), -1); + + QRegularExpressionMatchIterator iterator; + QCOMPARE(iterator.regularExpression(), QRegularExpression()); + QCOMPARE(iterator.regularExpression(), re); + QCOMPARE(iterator.matchType(), QRegularExpression::NoMatch); + QCOMPARE(iterator.matchOptions(), QRegularExpression::NoMatchOption); + QCOMPARE(iterator.isValid(), true); + QCOMPARE(iterator.hasNext(), false); +} + void tst_QRegularExpression::gettersSetters_data() { provideRegularExpressions(); @@ -693,12 +717,31 @@ void tst_QRegularExpression::normalMatch() QFETCH(QRegularExpression::MatchOptions, matchOptions); QFETCH(Match, match); - QRegularExpressionMatch m = regexp.match(subject, offset, QRegularExpression::NormalMatch, matchOptions); - consistencyCheck(m); - QVERIFY(m == match); + { + 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); + } } - void tst_QRegularExpression::partialMatch_data() { QTest::addColumn<QRegularExpression>("regexp"); @@ -952,9 +995,29 @@ void tst_QRegularExpression::partialMatch() QFETCH(QRegularExpression::MatchOptions, matchOptions); QFETCH(Match, match); - QRegularExpressionMatch m = regexp.match(subject, offset, matchType, matchOptions); - consistencyCheck(m); - QVERIFY(m == match); + { + 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); + } } void tst_QRegularExpression::globalMatch_data() @@ -1223,10 +1286,28 @@ void tst_QRegularExpression::globalMatch() QFETCH(QRegularExpression::MatchType, matchType); QFETCH(QRegularExpression::MatchOptions, matchOptions); QFETCH(QList<Match>, matchList); + { + 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<Match> 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); + } - QRegularExpressionMatchIterator iterator = regexp.globalMatch(subject, offset, matchType, matchOptions); - consistencyCheck(iterator); - QVERIFY(iterator == matchList); } void tst_QRegularExpression::serialize_data() @@ -1338,6 +1419,86 @@ void tst_QRegularExpression::captureCount() QCOMPARE(re.captureCount(), -1); } +// the comma in the template breaks QFETCH... +typedef QMultiHash<QString, int> StringToIntMap; +Q_DECLARE_METATYPE(StringToIntMap) + +void tst_QRegularExpression::captureNames_data() +{ + QTest::addColumn<QString>("pattern"); + QTest::addColumn<StringToIntMap>("namedCapturesIndexMap"); + StringToIntMap map; + + QTest::newRow("captureNames01") << "a pattern" << map; + QTest::newRow("captureNames02") << "a.*pattern" << map; + QTest::newRow("captureNames03") << "(a) pattern" << map; + QTest::newRow("captureNames04") << "(a).*(pattern)" << map; + + map.clear(); + map.replace("named", 1); + QTest::newRow("captureNames05") << "a.*(?<named>pattern)" << map; + + map.clear(); + map.replace("named", 2); + QTest::newRow("captureNames06") << "(a).*(?<named>pattern)" << map; + + map.clear(); + map.replace("name1", 1); + map.replace("name2", 2); + QTest::newRow("captureNames07") << "(?<name1>a).*(?<name2>pattern)" << map; + + map.clear(); + map.replace("name1", 2); + map.replace("name2", 1); + QTest::newRow("captureNames08") << "(?<name2>a).*(?<name1>pattern)" << map; + + map.clear(); + map.replace("date", 1); + map.replace("month", 2); + map.replace("year", 3); + QTest::newRow("captureNames09") << "^(?<date>\\d\\d)/(?<month>\\d\\d)/(?<year>\\d\\d\\d\\d)$" << map; + + map.clear(); + map.replace("date", 2); + map.replace("month", 1); + map.replace("year", 3); + QTest::newRow("captureNames10") << "^(?<month>\\d\\d)/(?<date>\\d\\d)/(?<year>\\d\\d\\d\\d)$" << map; + + map.clear(); + map.replace("noun", 2); + QTest::newRow("captureNames11") << "(a)(?|(?<noun>b)|(?<noun>c))(d)" << map; + + map.clear(); + QTest::newRow("captureNames_invalid01") << "(.*" << map; + QTest::newRow("captureNames_invalid02") << "\\" << map; + QTest::newRow("captureNames_invalid03") << "(?<noun)" << map; + QTest::newRow("captureNames_invalid04") << "(?|(?<noun1>a)|(?<noun2>b))" << map; +} + +void tst_QRegularExpression::captureNames() +{ + QFETCH(QString, pattern); + QFETCH(StringToIntMap, namedCapturesIndexMap); + + const QRegularExpression re(pattern); + QStringList namedCaptureGroups = re.namedCaptureGroups(); + int namedCaptureGroupsCount = namedCaptureGroups.size(); + + QCOMPARE(namedCaptureGroupsCount, re.captureCount() + 1); + + for (int i = 0; i < namedCaptureGroupsCount; ++i) { + const QString &name = namedCaptureGroups.at(i); + + if (name.isEmpty()) { + QVERIFY(!namedCapturesIndexMap.contains(name)); + } else { + QVERIFY(namedCapturesIndexMap.contains(name)); + QCOMPARE(i, namedCapturesIndexMap.value(name)); + } + } + +} + void tst_QRegularExpression::pcreJitStackUsage_data() { QTest::addColumn<QString>("pattern"); @@ -1389,3 +1550,35 @@ void tst_QRegularExpression::regularExpressionMatch() QTest::ignoreMessage(QtWarningMsg, "QRegularExpressionMatch::captured: empty capturing group name passed"); QCOMPARE(match.captured(QString()).isNull(), true); } + +void tst_QRegularExpression::JOptionUsage_data() +{ + QTest::addColumn<QString>("pattern"); + QTest::addColumn<bool>("isValid"); + QTest::addColumn<bool>("JOptionUsed"); + + QTest::newRow("joption-notused-01") << "a.*b" << true << false; + QTest::newRow("joption-notused-02") << "^a(b)(c)$" << true << false; + QTest::newRow("joption-notused-03") << "a(b)(?<c>d)|e" << true << false; + QTest::newRow("joption-notused-04") << "(?<a>.)(?<a>.)" << false << false; + + QTest::newRow("joption-used-01") << "(?J)a.*b" << true << true; + QTest::newRow("joption-used-02") << "(?-J)a.*b" << true << true; + QTest::newRow("joption-used-03") << "(?J)(?<a>.)(?<a>.)" << true << true; + QTest::newRow("joption-used-04") << "(?-J)(?<a>.)(?<a>.)" << false << true; + +} + +void tst_QRegularExpression::JOptionUsage() +{ + QFETCH(QString, pattern); + QFETCH(bool, isValid); + QFETCH(bool, JOptionUsed); + + const QString warningMessage = QStringLiteral("QRegularExpressionPrivate::getPatternInfo(): the pattern '%1'\n is using the (?J) option; duplicate capturing group names are not supported by Qt"); + + QRegularExpression re(pattern); + if (isValid && JOptionUsed) + QTest::ignoreMessage(QtWarningMsg, qPrintable(warningMessage.arg(pattern))); + QCOMPARE(re.isValid(), isValid); +} |