diff --git a/src/plugins/autotest/autotestplugin.cpp b/src/plugins/autotest/autotestplugin.cpp index 7fdedda74ef..9a513bd2d48 100644 --- a/src/plugins/autotest/autotestplugin.cpp +++ b/src/plugins/autotest/autotestplugin.cpp @@ -57,6 +57,7 @@ #include #include #include +#include #include #include @@ -261,11 +262,22 @@ void AutotestPlugin::onRunFileTriggered() runner->prepareToRunTests(TestRunMode::Run); } +static QList testItemsToTestConfigurations(const QList &items, + TestRunMode mode) +{ + QList configs; + for (const TestTreeItem * item : items) { + if (TestConfiguration *currentConfig = item->asConfiguration(mode)) + configs << currentConfig; + } + return configs; +} + void AutotestPlugin::onRunUnderCursorTriggered(TestRunMode mode) { - QTextCursor cursor = Utils::Text::wordStartCursor( - TextEditor::BaseTextEditor::currentTextEditor()->editorWidget()->textCursor()); - cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor); + TextEditor::BaseTextEditor *currentEditor = TextEditor::BaseTextEditor::currentTextEditor(); + QTextCursor cursor = currentEditor->editorWidget()->textCursor(); + cursor.select(QTextCursor::WordUnderCursor); const QString text = cursor.selectedText(); if (text.isEmpty()) return; // Do not trigger when no name under cursor @@ -274,11 +286,15 @@ void AutotestPlugin::onRunUnderCursorTriggered(TestRunMode mode) if (testsItems.isEmpty()) return; // Wrong location triggered - QList testsToRun; - for (const TestTreeItem * item : testsItems){ - if (TestConfiguration *cfg = item->asConfiguration(mode)) - testsToRun << cfg; - } + // check whether we have been triggered on a test function definition + const uint line = uint(currentEditor->currentLine()); + const QString &filePath = currentEditor->textDocument()->filePath().toString(); + const QList filteredItems = Utils::filtered(testsItems, [&](TestTreeItem *it){ + return it->line() == line && it->filePath() == filePath; + }); + + const QList testsToRun = testItemsToTestConfigurations( + filteredItems.size() == 1 ? filteredItems : testsItems, mode); if (testsToRun.isEmpty()) { MessageManager::write(tr("Selected test was not found (%1).").arg(text), MessageManager::Flash);