From ad072db2b65e79806c56db794399f6683ef0c9e9 Mon Sep 17 00:00:00 2001 From: Christian Stenger Date: Wed, 30 Nov 2022 10:10:41 +0100 Subject: [PATCH] AutoTest: Improve run and debug under cursor Especially when running tests that are not macro-based (e.g. QTest) it could happen that more functions had been executed as the filtering only applied when the cursor was located at the definition of the function. Try further filtering using the code model to avoid running functions that match by name, but are located in a different class. Fixes: QTCREATORBUG-28496 Change-Id: I6c543f028aa20a75cc091e8db87207c41be580aa Reviewed-by: David Schulz --- src/plugins/autotest/autotestplugin.cpp | 29 ++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/plugins/autotest/autotestplugin.cpp b/src/plugins/autotest/autotestplugin.cpp index 4457c7531fc..3c9fbbcad5b 100644 --- a/src/plugins/autotest/autotestplugin.cpp +++ b/src/plugins/autotest/autotestplugin.cpp @@ -32,7 +32,11 @@ #include #include #include +#include +#include +#include #include +#include #include #include #include @@ -405,10 +409,33 @@ void AutotestPluginPrivate::onRunUnderCursorTriggered(TestRunMode mode) // check whether we have been triggered on a test function definition const int line = currentEditor->currentLine(); const Utils::FilePath &filePath = currentEditor->textDocument()->filePath(); - const QList filteredItems = Utils::filtered(testsItems, [&](ITestTreeItem *it){ + QList filteredItems = Utils::filtered(testsItems, [&](ITestTreeItem *it){ return it->line() == line && it->filePath() == filePath; }); + if (filteredItems.size() == 0 && testsItems.size() > 1) { + const CPlusPlus::Snapshot snapshot = CppEditor::CppModelManager::instance()->snapshot(); + const CPlusPlus::Document::Ptr doc = snapshot.document(filePath); + if (!doc.isNull()) { + CPlusPlus::Scope *scope = doc->scopeAt(line, currentEditor->currentColumn()); + if (scope->asClass()) { + const QList fullName + = CPlusPlus::LookupContext::fullyQualifiedName(scope); + const QString className = CPlusPlus::Overview().prettyName(fullName); + + filteredItems = Utils::filtered(testsItems, + [&text, &className](ITestTreeItem *it){ + return it->name() == text + && static_cast(it->parent())->name() == className; + }); + } + } + } + if ((filteredItems.size() != 1 && testsItems.size() > 1) + && (mode == TestRunMode::Debug || mode == TestRunMode::DebugWithoutDeploy)) { + MessageManager::writeFlashing(Tr::tr("Cannot debug multiple tests at once.")); + return; + } const QList testsToRun = testItemsToTestConfigurations( filteredItems.size() == 1 ? filteredItems : testsItems, mode);