AutoTest: Improve QRegularExpression usages

Use static const if possible to avoid re-initializations.

Change-Id: Ibee9e320dccb90cd928737b809db3b56369961ec
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Christian Stenger
2023-07-04 08:31:37 +02:00
parent a85d7ef538
commit acad4b6b2e
8 changed files with 35 additions and 35 deletions

View File

@@ -62,7 +62,7 @@ static QString caseFromContent(const QString &content)
} }
QString result = content.mid(index + 5); QString result = content.mid(index + 5);
static QRegularExpression functionName("\"(.+)\":.*"); static const QRegularExpression functionName("\"(.+)\":.*");
const QRegularExpressionMatch matcher = functionName.match(result); const QRegularExpressionMatch matcher = functionName.match(result);
if (!matcher.hasMatch()) { if (!matcher.hasMatch()) {
qCDebug(orLog) << "got no match"; qCDebug(orLog) << "got no match";
@@ -173,29 +173,29 @@ void BoostTestOutputReader::handleMessageMatch(const QRegularExpressionMatch &ma
void BoostTestOutputReader::processOutputLine(const QByteArray &outputLine) void BoostTestOutputReader::processOutputLine(const QByteArray &outputLine)
{ {
static QRegularExpression newTestStart("^Running (\\d+) test cases?\\.\\.\\.$"); static const QRegularExpression newTestStart("^Running (\\d+) test cases?\\.\\.\\.$");
static QRegularExpression dependency("^Including test case (.+) as a dependency of " static const QRegularExpression dependency("^Including test case (.+) as a dependency of "
"test case (.+)$"); "test case (.+)$");
static QRegularExpression messages("^(.+)\\((\\d+)\\): (info: (.+)|error: (.+)|" static const QRegularExpression messages("^(.+)\\((\\d+)\\): (info: (.+)|error: (.+)|"
"fatal error: (.+)|last checkpoint: (.+)" "fatal error: (.+)|last checkpoint: (.+)"
"|Entering test (case|suite) \"(.+)\"" "|Entering test (case|suite) \"(.+)\""
"|Leaving test (case|suite) \"(.+)\"; testing time: (\\d+.+)" "|Leaving test (case|suite) \"(.+)\"; testing time: (\\d+.+)"
"|Test case \"(.+)\" is skipped because .+$)$"); "|Test case \"(.+)\" is skipped because .+$)$");
static QRegularExpression moduleMssg("^(Entering test module \"(.+)\"|" static const QRegularExpression moduleMssg("^(Entering test module \"(.+)\"|"
"Leaving test module \"(.+)\"; testing time: (\\d+.+))$"); "Leaving test module \"(.+)\"; testing time: (\\d+.+))$");
static QRegularExpression noAssertion("^Test case (.*) did not check any assertions$"); static const QRegularExpression noAssertion("^Test case (.*) did not check any assertions$");
static QRegularExpression summaryPreamble("^\\s*Test (module|suite|case) \"(.*)\" has " static const QRegularExpression summaryPreamble("^\\s*Test (module|suite|case) \"(.*)\" has "
"(failed|passed)( with:)?$"); "(failed|passed)( with:)?$");
static QRegularExpression summarySkip("^\\s+Test case \"(.*)\" was skipped$"); static const QRegularExpression summarySkip("^\\s+Test case \"(.*)\" was skipped$");
static QRegularExpression summaryDetail("^\\s+(\\d+) test cases? out of (\\d+) " static const QRegularExpression summaryDetail("^\\s+(\\d+) test cases? out of (\\d+) "
"(failed|passed|skipped)$"); "(failed|passed|skipped)$");
static QRegularExpression summaryAssertion("^\\s+(\\d+) assertions? out of (\\d+) " static const QRegularExpression summaryAssertion("^\\s+(\\d+) assertions? out of (\\d+) "
"(failed|passed)$"); "(failed|passed)$");
static QRegularExpression finish("^\\*{3} (\\d+) failure(s are| is) detected in the " static const QRegularExpression finish("^\\*{3} (\\d+) failure(s are| is) detected in the "
"test module \"(.*)\"$"); "test module \"(.*)\"$");
QString noErrors("*** No errors detected"); const QString noErrors("*** No errors detected");
const QString line = removeCommandlineColors(QString::fromUtf8(outputLine)); const QString line = removeCommandlineColors(QString::fromUtf8(outputLine));
if (line.trimmed().isEmpty()) if (line.trimmed().isEmpty())

View File

@@ -41,7 +41,7 @@ static QStringList parseTags(const QString &tagsString)
{ {
QStringList tagsList; QStringList tagsList;
const QRegularExpression tagRegEx("\\[(.*?)\\]",QRegularExpression::CaseInsensitiveOption); static const QRegularExpression tagRegEx("\\[(.*?)\\]", QRegularExpression::CaseInsensitiveOption);
int pos = 0; int pos = 0;
QRegularExpressionMatch it = tagRegEx.match(tagsString, pos); QRegularExpressionMatch it = tagRegEx.match(tagsString, pos);
while (it.hasMatch()) { while (it.hasMatch()) {

View File

@@ -104,7 +104,7 @@ bool CatchTestParser::processDocument(QPromise<TestParseResultPtr> &promise,
const QByteArray &fileContent = getFileContent(fileName); const QByteArray &fileContent = getFileContent(fileName);
if (!hasCatchNames(doc)) { if (!hasCatchNames(doc)) {
const QRegularExpression regex("\\b(CATCH_)?" static const QRegularExpression regex("\\b(CATCH_)?"
"(SCENARIO|(TEMPLATE_(PRODUCT_)?)?TEST_CASE(_METHOD)?|" "(SCENARIO|(TEMPLATE_(PRODUCT_)?)?TEST_CASE(_METHOD)?|"
"TEMPLATE_TEST_CASE(_METHOD)?_SIG|" "TEMPLATE_TEST_CASE(_METHOD)?_SIG|"
"TEMPLATE_PRODUCT_TEST_CASE(_METHOD)?_SIG|" "TEMPLATE_PRODUCT_TEST_CASE(_METHOD)?_SIG|"

View File

@@ -80,7 +80,7 @@ bool GTestParser::processDocument(QPromise<TestParseResultPtr> &promise,
const QByteArray &fileContent = getFileContent(fileName); const QByteArray &fileContent = getFileContent(fileName);
if (!hasGTestNames(doc)) { if (!hasGTestNames(doc)) {
const QRegularExpression regex("\\b(TEST(_[FP])?|TYPED_TEST(_P)?|(GTEST_TEST))"); static const QRegularExpression regex("\\b(TEST(_[FP])?|TYPED_TEST(_P)?|(GTEST_TEST))");
if (!regex.match(QString::fromUtf8(fileContent)).hasMatch()) if (!regex.match(QString::fromUtf8(fileContent)).hasMatch())
return false; return false;
} }

View File

@@ -39,7 +39,7 @@ static ResultHooks::OutputStringHook outputStringHook(const QString &testCaseNam
static QString normalizeName(const QString &name) static QString normalizeName(const QString &name)
{ {
static QRegularExpression parameterIndex("/\\d+"); static const QRegularExpression parameterIndex("/\\d+");
QString nameWithoutParameterIndices = name; QString nameWithoutParameterIndices = name;
nameWithoutParameterIndices.remove(parameterIndex); nameWithoutParameterIndices.remove(parameterIndex);

View File

@@ -62,7 +62,7 @@ bool GTestVisitor::visit(CPlusPlus::FunctionDefinitionAST *ast)
testSuiteName = QString::fromUtf8(parameter1.spell()); testSuiteName = QString::fromUtf8(parameter1.spell());
testCaseName = QString::fromUtf8(parameter2.spell()); testCaseName = QString::fromUtf8(parameter2.spell());
// test (suite) name needs to be a alpha numerical literal ( _ and $ allowed) // test (suite) name needs to be a alpha numerical literal ( _ and $ allowed)
const QRegularExpression alnum("^[[:alnum:]_$]+$"); static const QRegularExpression alnum("^[[:alnum:]_$]+$");
// test suite must not start with a number, test case may // test suite must not start with a number, test case may
if (!alnum.match(testSuiteName).hasMatch() if (!alnum.match(testSuiteName).hasMatch()
|| (!testSuiteName.isEmpty() && testSuiteName.at(0).isNumber())) { || (!testSuiteName.isEmpty() && testSuiteName.at(0).isNumber())) {

View File

@@ -112,7 +112,7 @@ TestCases QtTestParser::testCases(const CppEditor::CppModelManager *modelManager
return astVisitor.testCases(); return astVisitor.testCases();
TestCases result; TestCases result;
const QRegularExpression regex("\\b(QTEST_(APPLESS_|GUILESS_)?MAIN)" static const QRegularExpression regex("\\b(QTEST_(APPLESS_|GUILESS_)?MAIN)"
"\\s*\\(\\s*([[:alnum:]]+)\\s*\\)"); "\\s*\\(\\s*([[:alnum:]]+)\\s*\\)");
QRegularExpressionMatchIterator it = regex.globalMatch(QString::fromUtf8(document->utf8Source())); QRegularExpressionMatchIterator it = regex.globalMatch(QString::fromUtf8(document->utf8Source()));
while (it.hasNext()) { while (it.hasNext()) {

View File

@@ -146,7 +146,7 @@ QString QuickTestParser::quickTestName(const CPlusPlus::Document::Ptr &doc) cons
} }
if (pchIncludes) { if (pchIncludes) {
const QRegularExpression regex("\\bQUICK_TEST_(MAIN|OPENGL_MAIN|MAIN_WITH_SETUP)"); static const QRegularExpression regex("\\bQUICK_TEST_(MAIN|OPENGL_MAIN|MAIN_WITH_SETUP)");
const QRegularExpressionMatch match = regex.match(QString::fromUtf8(fileContent)); const QRegularExpressionMatch match = regex.match(QString::fromUtf8(fileContent));
if (match.hasMatch()) if (match.hasMatch())
return match.captured(); // we do not care for the name, just return something non-empty return match.captured(); // we do not care for the name, just return something non-empty