AutoTest: Replace QRegExp by QRegularExpression

Change-Id: I0e7de5482786105d21765fdf7c1180c16414bc01
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Christian Stenger
2019-05-24 10:12:43 +02:00
parent 07b1d2aa56
commit a2fa8ce2e8
3 changed files with 62 additions and 40 deletions

View File

@@ -31,7 +31,7 @@
#include <QDir> #include <QDir>
#include <QFileInfo> #include <QFileInfo>
#include <QRegExp> #include <QRegularExpression>
namespace Autotest { namespace Autotest {
namespace Internal { namespace Internal {
@@ -63,24 +63,31 @@ GTestOutputReader::GTestOutputReader(const QFutureInterface<TestResultPtr> &futu
void GTestOutputReader::processOutputLine(const QByteArray &outputLineWithNewLine) void GTestOutputReader::processOutputLine(const QByteArray &outputLineWithNewLine)
{ {
static QRegExp newTestStarts("^\\[-{10}\\] \\d+ tests? from (.*)$"); static const QRegularExpression newTestStarts("^\\[-{10}\\] \\d+ tests? from (.*)$");
static QRegExp testEnds("^\\[-{10}\\] \\d+ tests? from (.*) \\((.*)\\)$"); static const QRegularExpression testEnds("^\\[-{10}\\] \\d+ tests? from (.*) \\((.*)\\)$");
static QRegExp newTestSetStarts("^\\[ RUN \\] (.*)$"); static const QRegularExpression newTestSetStarts("^\\[ RUN \\] (.*)$");
static QRegExp testSetSuccess("^\\[ OK \\] (.*) \\((.*)\\)$"); static const QRegularExpression testSetSuccess("^\\[ OK \\] (.*) \\((.*)\\)$");
static QRegExp testSetFail("^\\[ FAILED \\] (.*) \\((\\d+ ms)\\)$"); static const QRegularExpression testSetFail("^\\[ FAILED \\] (.*) \\((\\d+ ms)\\)$");
static QRegExp disabledTests("^ YOU HAVE (\\d+) DISABLED TESTS?$"); static const QRegularExpression disabledTests("^ YOU HAVE (\\d+) DISABLED TESTS?$");
static QRegExp failureLocation("^(.*):(\\d+): Failure$"); static const QRegularExpression failureLocation("^(.*):(\\d+): Failure$");
static QRegExp errorLocation("^(.*)\\((\\d+)\\): error:.*$"); static const QRegularExpression errorLocation("^(.*)\\((\\d+)\\): error:.*$");
static QRegExp iterations("^Repeating all tests \\(iteration (\\d+)\\) \\. \\. \\.$"); static const QRegularExpression iterations("^Repeating all tests "
"\\(iteration (\\d+)\\) \\. \\. \\.$");
const QString line = QString::fromLatin1(chopLineBreak(outputLineWithNewLine)); const QString line = QString::fromLatin1(chopLineBreak(outputLineWithNewLine));
if (line.trimmed().isEmpty()) if (line.trimmed().isEmpty())
return; return;
struct ExactMatch : public QRegularExpressionMatch
{
ExactMatch(const QRegularExpressionMatch &other) : QRegularExpressionMatch(other) {}
operator bool() const { return hasMatch(); }
};
if (!line.startsWith('[')) { if (!line.startsWith('[')) {
m_description.append(line).append('\n'); m_description.append(line).append('\n');
if (iterations.exactMatch(line)) { if (ExactMatch match = iterations.match(line)) {
m_iteration = iterations.cap(1).toInt(); m_iteration = match.captured(1).toInt();
m_description.clear(); m_description.clear();
} else if (line.startsWith(QStringLiteral("Note:"))) { } else if (line.startsWith(QStringLiteral("Note:"))) {
m_description = line; m_description = line;
@@ -91,22 +98,22 @@ void GTestOutputReader::processOutputLine(const QByteArray &outputLineWithNewLin
testResult->setDescription(m_description); testResult->setDescription(m_description);
reportResult(testResult); reportResult(testResult);
m_description.clear(); m_description.clear();
} else if (disabledTests.exactMatch(line)) { } else if (ExactMatch match = disabledTests.match(line)) {
m_disabled = disabledTests.cap(1).toInt(); m_disabled = match.captured(1).toInt();
m_description.clear(); m_description.clear();
} }
return; return;
} }
if (testEnds.exactMatch(line)) { if (ExactMatch match = testEnds.match(line)) {
TestResultPtr testResult = createDefaultResult(); TestResultPtr testResult = createDefaultResult();
testResult->setResult(ResultType::TestEnd); testResult->setResult(ResultType::TestEnd);
testResult->setDescription(tr("Test execution took %1").arg(testEnds.cap(2))); testResult->setDescription(tr("Test execution took %1").arg(match.captured(2)));
reportResult(testResult); reportResult(testResult);
m_currentTestSuite.clear(); m_currentTestSuite.clear();
m_currentTestCase.clear(); m_currentTestCase.clear();
} else if (newTestStarts.exactMatch(line)) { } else if (ExactMatch match = newTestStarts.match(line)) {
setCurrentTestSuite(newTestStarts.cap(1)); setCurrentTestSuite(match.captured(1));
TestResultPtr testResult = createDefaultResult(); TestResultPtr testResult = createDefaultResult();
testResult->setResult(ResultType::TestStart); testResult->setResult(ResultType::TestStart);
if (m_iteration > 1) { if (m_iteration > 1) {
@@ -116,15 +123,15 @@ void GTestOutputReader::processOutputLine(const QByteArray &outputLineWithNewLin
testResult->setDescription(tr("Executing test suite %1").arg(m_currentTestSuite)); testResult->setDescription(tr("Executing test suite %1").arg(m_currentTestSuite));
} }
reportResult(testResult); reportResult(testResult);
} else if (newTestSetStarts.exactMatch(line)) { } else if (ExactMatch match = newTestSetStarts.match(line)) {
setCurrentTestCase(newTestSetStarts.cap(1)); setCurrentTestCase(match.captured(1));
TestResultPtr testResult = TestResultPtr(new GTestResult(QString(), m_projectFile, TestResultPtr testResult = TestResultPtr(new GTestResult(QString(), m_projectFile,
QString())); QString()));
testResult->setResult(ResultType::MessageCurrentTest); testResult->setResult(ResultType::MessageCurrentTest);
testResult->setDescription(tr("Entering test case %1").arg(m_currentTestCase)); testResult->setDescription(tr("Entering test case %1").arg(m_currentTestCase));
reportResult(testResult); reportResult(testResult);
m_description.clear(); m_description.clear();
} else if (testSetSuccess.exactMatch(line)) { } else if (ExactMatch match = testSetSuccess.match(line)) {
TestResultPtr testResult = createDefaultResult(); TestResultPtr testResult = createDefaultResult();
testResult->setResult(ResultType::Pass); testResult->setResult(ResultType::Pass);
testResult->setDescription(m_description); testResult->setDescription(m_description);
@@ -132,33 +139,32 @@ void GTestOutputReader::processOutputLine(const QByteArray &outputLineWithNewLin
m_description.clear(); m_description.clear();
testResult = createDefaultResult(); testResult = createDefaultResult();
testResult->setResult(ResultType::MessageInternal); testResult->setResult(ResultType::MessageInternal);
testResult->setDescription(tr("Execution took %1.").arg(testSetSuccess.cap(2))); testResult->setDescription(tr("Execution took %1.").arg(match.captured(2)));
reportResult(testResult); reportResult(testResult);
m_futureInterface.setProgressValue(m_futureInterface.progressValue() + 1); m_futureInterface.setProgressValue(m_futureInterface.progressValue() + 1);
} else if (testSetFail.exactMatch(line)) { } else if (ExactMatch match = testSetFail.match(line)) {
TestResultPtr testResult = createDefaultResult(); TestResultPtr testResult = createDefaultResult();
testResult->setResult(ResultType::Fail); testResult->setResult(ResultType::Fail);
m_description.chop(1); m_description.chop(1);
QStringList resultDescription; QStringList resultDescription;
for (const QString &output : m_description.split('\n')) { for (const QString &output : m_description.split('\n')) {
QRegExp *match = nullptr; QRegularExpressionMatch innerMatch = failureLocation.match(output);
if (failureLocation.exactMatch(output)) if (!innerMatch.hasMatch()) {
match = &failureLocation; innerMatch = errorLocation.match(output);
else if (errorLocation.exactMatch(output)) if (!innerMatch.hasMatch()) {
match = &errorLocation;
if (!match) {
resultDescription << output; resultDescription << output;
continue; continue;
} }
}
testResult->setDescription(resultDescription.join('\n')); testResult->setDescription(resultDescription.join('\n'));
reportResult(testResult); reportResult(testResult);
resultDescription.clear(); resultDescription.clear();
testResult = createDefaultResult(); testResult = createDefaultResult();
testResult->setResult(ResultType::MessageLocation); testResult->setResult(ResultType::MessageLocation);
testResult->setLine(match->cap(2).toInt()); testResult->setLine(innerMatch.captured(2).toInt());
QString file = constructSourceFilePath(m_buildDir, match->cap(1)); QString file = constructSourceFilePath(m_buildDir, innerMatch.captured(1));
if (!file.isEmpty()) if (!file.isEmpty())
testResult->setFileName(file); testResult->setFileName(file);
resultDescription << output; resultDescription << output;
@@ -168,7 +174,7 @@ void GTestOutputReader::processOutputLine(const QByteArray &outputLineWithNewLin
m_description.clear(); m_description.clear();
testResult = createDefaultResult(); testResult = createDefaultResult();
testResult->setResult(ResultType::MessageInternal); testResult->setResult(ResultType::MessageInternal);
testResult->setDescription(tr("Execution took %1.").arg(testSetFail.cap(2))); testResult->setDescription(tr("Execution took %1.").arg(match.captured(2)));
reportResult(testResult); reportResult(testResult);
m_futureInterface.setProgressValue(m_futureInterface.progressValue() + 1); m_futureInterface.setProgressValue(m_futureInterface.progressValue() + 1);
} }

View File

@@ -30,6 +30,8 @@
#include <coreplugin/id.h> #include <coreplugin/id.h>
#include <QRegularExpression>
namespace Autotest { namespace Autotest {
namespace Internal { namespace Internal {
@@ -76,7 +78,7 @@ bool GTestResult::isDirectParentOf(const TestResult *other, bool *needsIntermedi
static QString normalizeName(const QString &name) static QString normalizeName(const QString &name)
{ {
static QRegExp parameterIndex("/\\d+"); static QRegularExpression parameterIndex("/\\d+");
QString nameWithoutParameterIndices = name; QString nameWithoutParameterIndices = name;
nameWithoutParameterIndices.remove(parameterIndex); nameWithoutParameterIndices.remove(parameterIndex);

View File

@@ -37,7 +37,7 @@
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
#include <utils/theme/theme.h> #include <utils/theme/theme.h>
#include <QRegExp> #include <QRegularExpression>
namespace Autotest { namespace Autotest {
namespace Internal { namespace Internal {
@@ -71,6 +71,20 @@ TestTreeItem *GTestTreeItem::copyWithoutChildren()
return copied; return copied;
} }
static QString wildCardPattern(const QString &original)
{
QString pattern = original;
pattern.replace('.', "\\.");
pattern.replace('$', "\\$");
pattern.replace('(', "\\(").replace(')', "\\)");
pattern.replace('[', "\\[").replace(']', "\\]");
pattern.replace('{', "\\{").replace('}', "\\}");
pattern.replace('+', "\\+");
pattern.replace('*', ".*");
pattern.replace('?', '.');
return pattern;
}
static bool matchesFilter(const QString &filter, const QString &fullTestName) static bool matchesFilter(const QString &filter, const QString &fullTestName)
{ {
QStringList positive; QStringList positive;
@@ -88,13 +102,13 @@ static bool matchesFilter(const QString &filter, const QString &fullTestName)
testName.append('.'); testName.append('.');
for (const QString &curr : negative) { for (const QString &curr : negative) {
QRegExp regex(curr, Qt::CaseSensitive, QRegExp::Wildcard); QRegularExpression regex(wildCardPattern(curr));
if (regex.exactMatch(testName)) if (regex.match(testName).hasMatch())
return false; return false;
} }
for (const QString &curr : positive) { for (const QString &curr : positive) {
QRegExp regex(curr, Qt::CaseSensitive, QRegExp::Wildcard); QRegularExpression regex(wildCardPattern(curr));
if (regex.exactMatch(testName)) if (regex.match(testName).hasMatch())
return true; return true;
} }
return positive.isEmpty(); return positive.isEmpty();