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 <david.schulz@qt.io>
This commit is contained in:
Christian Stenger
2022-11-30 10:10:41 +01:00
parent 39ffdb416f
commit ad072db2b6

View File

@@ -32,7 +32,11 @@
#include <coreplugin/icontext.h> #include <coreplugin/icontext.h>
#include <coreplugin/icore.h> #include <coreplugin/icore.h>
#include <coreplugin/messagemanager.h> #include <coreplugin/messagemanager.h>
#include <cplusplus/CppDocument.h>
#include <cplusplus/LookupContext.h>
#include <cplusplus/Overview.h>
#include <cppeditor/cppeditorconstants.h> #include <cppeditor/cppeditorconstants.h>
#include <cppeditor/cppmodelmanager.h>
#include <extensionsystem/pluginmanager.h> #include <extensionsystem/pluginmanager.h>
#include <projectexplorer/buildmanager.h> #include <projectexplorer/buildmanager.h>
#include <projectexplorer/project.h> #include <projectexplorer/project.h>
@@ -405,10 +409,33 @@ void AutotestPluginPrivate::onRunUnderCursorTriggered(TestRunMode mode)
// check whether we have been triggered on a test function definition // check whether we have been triggered on a test function definition
const int line = currentEditor->currentLine(); const int line = currentEditor->currentLine();
const Utils::FilePath &filePath = currentEditor->textDocument()->filePath(); const Utils::FilePath &filePath = currentEditor->textDocument()->filePath();
const QList<ITestTreeItem *> filteredItems = Utils::filtered(testsItems, [&](ITestTreeItem *it){ QList<ITestTreeItem *> filteredItems = Utils::filtered(testsItems, [&](ITestTreeItem *it){
return it->line() == line && it->filePath() == filePath; 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<const CPlusPlus::Name *> 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<ITestTreeItem *>(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<ITestConfiguration *> testsToRun = testItemsToTestConfigurations( const QList<ITestConfiguration *> testsToRun = testItemsToTestConfigurations(
filteredItems.size() == 1 ? filteredItems : testsItems, mode); filteredItems.size() == 1 ? filteredItems : testsItems, mode);