From 6f73b3b175086f948eee6b3097fa50aa0a52ab53 Mon Sep 17 00:00:00 2001 From: Christian Stenger Date: Thu, 11 Oct 2018 12:45:13 +0200 Subject: [PATCH] AutoTest: Limit test execution under cursor Run and debug under cursor simply used the literal under the cursor to find matching test cases or functions. This obviously finds more than the test under cursor. This patch limits this behavior by taking current file and line number into account as well. If the action gets triggered on a test definition then the found tests are limited to this position. If the literal under cursor does not belong to the definition of the test (declaration, string, comment) the original behavior gets triggered. Fixes: QTCREATORBUG-21093 Change-Id: I051d25c1d18e819caf2950b5c62d51e6d70d1b22 Reviewed-by: pawelrutka Reviewed-by: Christian Stenger Reviewed-by: David Schulz --- src/plugins/autotest/autotestplugin.cpp | 32 ++++++++++++++++++------- 1 file changed, 24 insertions(+), 8 deletions(-) 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);