From 924806e10b34aa07e6da2ba11fac3d0c23e794c8 Mon Sep 17 00:00:00 2001 From: Christian Stenger Date: Fri, 10 Jun 2022 10:46:35 +0200 Subject: [PATCH] AutoTest: Fix catch2 parsing when using prefixed commands If Catch2 is used with CATCH_CONFIG_PREFIX_ALL the relevant macros get a prefix. This patch enhances the respective parser to be able to handle these appropriate. This does not really take into account whether the define is set or not, so the parse results will only be correct if the project can be built. Fixes: QTCREATORBUG-27704 Change-Id: I935de752ac6106524c45c027af3e0f43673c4578 Reviewed-by: David Schulz --- .../autotest/catch/catchcodeparser.cpp | 24 ++++++++++--------- .../autotest/catch/catchtestparser.cpp | 6 +++-- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/plugins/autotest/catch/catchcodeparser.cpp b/src/plugins/autotest/catch/catchcodeparser.cpp index c1368c1aa72..350b34f5dc6 100644 --- a/src/plugins/autotest/catch/catchcodeparser.cpp +++ b/src/plugins/autotest/catch/catchcodeparser.cpp @@ -90,22 +90,24 @@ void CatchCodeParser::handleIdentifier() QTC_ASSERT(m_currentIndex < m_tokens.size(), return); const Token &token = m_tokens.at(m_currentIndex); const QByteArray &identifier = m_source.mid(int(token.bytesBegin()), int(token.bytes())); - if (identifier == "TEST_CASE") { + const QByteArray unprefixed = identifier.startsWith("CATCH_") ? identifier.mid(6) : identifier; + + if (unprefixed == "TEST_CASE") { handleTestCase(false); - } else if (identifier == "SCENARIO") { + } else if (unprefixed == "SCENARIO") { handleTestCase(true); - } else if (identifier == "TEMPLATE_TEST_CASE" || identifier == "TEMPLATE_PRODUCT_TEST_CASE" - || identifier == "TEMPLATE_LIST_TEST_CASE" || identifier == "TEMPLATE_TEST_CASE_SIG" - || identifier == "TEMPLATE_PRODUCT_TEST_CASE_SIG") { + } else if (unprefixed == "TEMPLATE_TEST_CASE" || unprefixed == "TEMPLATE_PRODUCT_TEST_CASE" + || unprefixed == "TEMPLATE_LIST_TEST_CASE" || unprefixed == "TEMPLATE_TEST_CASE_SIG" + || unprefixed == "TEMPLATE_PRODUCT_TEST_CASE_SIG") { handleParameterizedTestCase(false); - } else if (identifier == "TEST_CASE_METHOD") { + } else if (unprefixed == "TEST_CASE_METHOD") { handleFixtureOrRegisteredTestCase(true); - } else if (identifier == "TEMPLATE_TEST_CASE_METHOD_SIG" - || identifier == "TEMPLATE_PRODUCT_TEST_CASE_METHOD_SIG" - || identifier == "TEMPLATE_TEST_CASE_METHOD" - || identifier == "TEMPLATE_LIST_TEST_CASE_METHOD") { + } else if (unprefixed == "TEMPLATE_TEST_CASE_METHOD_SIG" + || unprefixed == "TEMPLATE_PRODUCT_TEST_CASE_METHOD_SIG" + || unprefixed == "TEMPLATE_TEST_CASE_METHOD" + || unprefixed == "TEMPLATE_LIST_TEST_CASE_METHOD") { handleParameterizedTestCase(true); - } else if (identifier == "METHOD_AS_TEST_CASE" || identifier == "REGISTER_TEST_CASE") { + } else if (unprefixed == "METHOD_AS_TEST_CASE" || unprefixed == "REGISTER_TEST_CASE") { handleFixtureOrRegisteredTestCase(false); } } diff --git a/src/plugins/autotest/catch/catchtestparser.cpp b/src/plugins/autotest/catch/catchtestparser.cpp index da6f6ca423a..c878da135e3 100644 --- a/src/plugins/autotest/catch/catchtestparser.cpp +++ b/src/plugins/autotest/catch/catchtestparser.cpp @@ -57,10 +57,11 @@ static bool isCatchTestCaseMacro(const QString ¯oName) static bool isCatchMacro(const QString ¯oName) { + QString unprefixed = macroName.startsWith("CATCH_") ? macroName.mid(6) : macroName; const QStringList validSectionMacros = { QStringLiteral("SECTION"), QStringLiteral("WHEN") }; - return isCatchTestCaseMacro(macroName) || validSectionMacros.contains(macroName); + return isCatchTestCaseMacro(unprefixed) || validSectionMacros.contains(unprefixed); } static bool includesCatchHeader(const CPlusPlus::Document::Ptr &doc, @@ -123,7 +124,8 @@ bool CatchTestParser::processDocument(QFutureInterface &futu const QByteArray &fileContent = getFileContent(fileName); if (!hasCatchNames(doc)) { - const QRegularExpression regex("\\b(SCENARIO|(TEMPLATE_(PRODUCT_)?)?TEST_CASE(_METHOD)?|" + const QRegularExpression regex("\\b(CATCH_)?" + "(SCENARIO|(TEMPLATE_(PRODUCT_)?)?TEST_CASE(_METHOD)?|" "TEMPLATE_TEST_CASE(_METHOD)?_SIG|" "TEMPLATE_PRODUCT_TEST_CASE(_METHOD)?_SIG|" "TEMPLATE_LIST_TEST_CASE_METHOD|METHOD_AS_TEST_CASE|"