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 <prutka13@gmail.com>
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Christian Stenger
2018-10-11 12:45:13 +02:00
parent 613280bbe4
commit 6f73b3b175

View File

@@ -57,6 +57,7 @@
#include <projectexplorer/session.h>
#include <projectexplorer/target.h>
#include <texteditor/texteditor.h>
#include <texteditor/textdocument.h>
#include <utils/textutils.h>
#include <utils/utilsicons.h>
@@ -261,11 +262,22 @@ void AutotestPlugin::onRunFileTriggered()
runner->prepareToRunTests(TestRunMode::Run);
}
static QList<TestConfiguration *> testItemsToTestConfigurations(const QList<TestTreeItem *> &items,
TestRunMode mode)
{
QList<TestConfiguration *> 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<TestConfiguration *> 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<TestTreeItem *> filteredItems = Utils::filtered(testsItems, [&](TestTreeItem *it){
return it->line() == line && it->filePath() == filePath;
});
const QList<TestConfiguration *> testsToRun = testItemsToTestConfigurations(
filteredItems.size() == 1 ? filteredItems : testsItems, mode);
if (testsToRun.isEmpty()) {
MessageManager::write(tr("Selected test was not found (%1).").arg(text), MessageManager::Flash);