diff options
author | Christian Stenger <christian.stenger@qt.io> | 2022-11-30 10:10:41 +0100 |
---|---|---|
committer | Christian Stenger <christian.stenger@qt.io> | 2022-12-01 05:51:01 +0000 |
commit | ad072db2b65e79806c56db794399f6683ef0c9e9 (patch) | |
tree | da1ad5ed9b105caa0f587ec0358437f05417db4f | |
parent | 39ffdb416ff3381515b7b4bf579acb23941cd6fc (diff) |
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>
-rw-r--r-- | src/plugins/autotest/autotestplugin.cpp | 29 |
1 files changed, 28 insertions, 1 deletions
diff --git a/src/plugins/autotest/autotestplugin.cpp b/src/plugins/autotest/autotestplugin.cpp index 4457c7531f..3c9fbbcad5 100644 --- a/src/plugins/autotest/autotestplugin.cpp +++ b/src/plugins/autotest/autotestplugin.cpp @@ -32,7 +32,11 @@ #include <coreplugin/icontext.h> #include <coreplugin/icore.h> #include <coreplugin/messagemanager.h> +#include <cplusplus/CppDocument.h> +#include <cplusplus/LookupContext.h> +#include <cplusplus/Overview.h> #include <cppeditor/cppeditorconstants.h> +#include <cppeditor/cppmodelmanager.h> #include <extensionsystem/pluginmanager.h> #include <projectexplorer/buildmanager.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 const int line = currentEditor->currentLine(); 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; }); + 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( filteredItems.size() == 1 ? filteredItems : testsItems, mode); |