Commit a87fa1e2 authored by Nikolai Kosjar's avatar Nikolai Kosjar Committed by Erik Verbruggen

CppEditor: Add tests for use selections

Change-Id: I5f958f0afbee076a8bac40e6e3c383f0c558d34a
Reviewed-by: default avatarErik Verbruggen <erik.verbruggen@digia.com>
parent 001683e9
......@@ -84,6 +84,7 @@ equals(TEST, 1) {
cppeditortestcase.cpp \
cppincludehierarchy_test.cpp \
cppquickfix_test.cpp \
cppuseselections_test.cpp \
fileandtokenactions_test.cpp \
followsymbol_switchmethoddecldef_test.cpp
DEFINES += SRCDIR=\\\"$$PWD\\\"
......
......@@ -65,6 +65,7 @@ QtcPlugin {
"cppincludehierarchy_test.cpp",
"cppquickfix_test.cpp",
"cppquickfix_test.h",
"cppuseselections_test.cpp",
"fileandtokenactions_test.cpp",
"followsymbol_switchmethoddecldef_test.cpp",
]
......
......@@ -208,6 +208,9 @@ private slots:
void test_quickfix_InsertVirtualMethods_implementationFile();
void test_quickfix_InsertVirtualMethods_BaseClassInNamespace();
void test_useSelections_data();
void test_useSelections();
// tests for "Include Hierarchy"
void test_includehierarchy_data();
void test_includehierarchy();
......
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "cppeditor.h"
#include "cppeditorplugin.h"
#include "cppeditortestcase.h"
#include <QElapsedTimer>
#include <QtTest>
struct Selection {
Selection(int line, int column, int length) : line(line), column(column), length(length) {}
int line;
int column;
int length;
};
typedef QList<Selection> SelectionList;
Q_DECLARE_METATYPE(SelectionList)
inline bool operator==(const Selection &l, const Selection &r)
{ return l.line == r.line && l.column == r.column && l.length == r.length; }
QT_BEGIN_NAMESPACE
namespace QTest {
template<> char *toString(const Selection &selection)
{
const QByteArray ba = "Selection("
+ QByteArray::number(selection.line) + ", "
+ QByteArray::number(selection.column) + ", "
+ QByteArray::number(selection.length)
+ ")";
return qstrdup(ba.data());
}
}
QT_END_NAMESPACE
namespace CppEditor {
namespace Internal {
namespace Tests {
// Check: If the user puts the cursor on e.g. a function-local variable,
// a type name or a macro use, all occurrences of that entity are highlighted.
class UseSelectionsTestCase : public TestCase
{
public:
UseSelectionsTestCase(TestDocument &testDocument,
const SelectionList &expectedSelections);
private:
SelectionList toSelectionList(const QList<QTextEdit::ExtraSelection> &extraSelections) const;
QList<QTextEdit::ExtraSelection> getExtraSelections() const;
SelectionList waitForUseSelections(bool *hasTimedOut) const;
private:
CppEditorWidget *m_editorWidget;
};
UseSelectionsTestCase::UseSelectionsTestCase(TestDocument &testFile,
const SelectionList &expectedSelections)
{
QVERIFY(succeededSoFar());
QVERIFY(testFile.hasCursorMarker());
testFile.m_source.remove(testFile.m_cursorPosition, 1);
testFile.writeToDisk();
QVERIFY(openCppEditor(testFile.filePath(), &testFile.m_editor, &m_editorWidget));
closeEditorAtEndOfTestCase(testFile.m_editor);
testFile.m_editor->setCursorPosition(testFile.m_cursorPosition);
waitForRehighlightedSemanticDocument(m_editorWidget);
bool hasTimedOut;
const SelectionList selections = waitForUseSelections(&hasTimedOut);
QEXPECT_FAIL("non-local use as macro argument - argument expanded 1", "TODO", Abort);
QEXPECT_FAIL("macro use 2", "TODO", Abort);
QVERIFY(!hasTimedOut);
// foreach (const Selection &selection, selections)
// qDebug() << QTest::toString(selection);
QEXPECT_FAIL("non-local use as macro argument - argument expanded 2", "TODO", Abort);
QCOMPARE(selections, expectedSelections);
}
SelectionList UseSelectionsTestCase::toSelectionList(
const QList<QTextEdit::ExtraSelection> &extraSelections) const
{
SelectionList result;
foreach (const QTextEdit::ExtraSelection &selection, extraSelections) {
int line, column;
const int position = qMin(selection.cursor.position(), selection.cursor.anchor());
m_editorWidget->convertPosition(position, &line, &column);
result << Selection(line, column, selection.cursor.selectedText().length());
}
return result;
}
QList<QTextEdit::ExtraSelection> UseSelectionsTestCase::getExtraSelections() const
{
return m_editorWidget->extraSelections(
TextEditor::BaseTextEditorWidget::CodeSemanticsSelection);
}
SelectionList UseSelectionsTestCase::waitForUseSelections(bool *hasTimedOut) const
{
QElapsedTimer timer;
timer.start();
if (hasTimedOut)
*hasTimedOut = false;
QList<QTextEdit::ExtraSelection> extraSelections = getExtraSelections();
while (extraSelections.isEmpty()) {
if (timer.hasExpired(500)) {
if (hasTimedOut)
*hasTimedOut = true;
break;
}
QCoreApplication::processEvents();
extraSelections = getExtraSelections();
}
return toSelectionList(extraSelections);
}
} // namespace Tests
void CppEditorPlugin::test_useSelections_data()
{
QTest::addColumn<QByteArray>("source");
QTest::addColumn<SelectionList>("expectedSelections");
typedef QByteArray _;
QTest::newRow("local uses")
<< _("int f(int arg) { return @arg; }\n")
<< (SelectionList()
<< Selection(1, 10, 3)
<< Selection(1, 24, 3)
);
QTest::newRow("local use as macro argument 1 - argument expanded")
<< _("#define Q_UNUSED(x) (void)x;\n"
"void f(int arg)\n"
"{\n"
" Q_UNUSED(@arg)\n"
"}\n")
<< (SelectionList()
<< Selection(2, 11, 3)
<< Selection(4, 13, 3)
);
QTest::newRow("local use as macro argument 2 - argument eaten")
<< _("inline void qt_noop(void) {}\n"
"#define Q_ASSERT(cond) qt_noop()\n"
"void f(char *text)\n"
"{\n"
" Q_ASSERT(@text);\n"
"}\n")
<< (SelectionList()
<< Selection(3, 13, 4)
<< Selection(5, 13, 4)
);
QTest::newRow("non-local uses")
<< _("struct Foo { @Foo(); };\n")
<< (SelectionList()
<< Selection(1, 7, 3)
<< Selection(1, 13, 3)
);
QTest::newRow("non-local use as macro argument - argument expanded 1")
<< _("#define QT_FORWARD_DECLARE_CLASS(name) class name;\n"
"QT_FORWARD_DECLARE_CLASS(Class)\n"
"@Class *foo;\n")
<< (SelectionList()
<< Selection(2, 25, 5)
<< Selection(3, 1, 5)
);
QTest::newRow("non-local use as macro argument - argument expanded 2")
<< _("#define Q_DISABLE_COPY(Class) \\\n"
" Class(const Class &);\\\n"
" Class &operator=(const Class &);\n"
"class @Super\n"
"{\n"
" Q_DISABLE_COPY(Super)\n"
"};\n")
<< (SelectionList()
<< Selection(4, 6, 5)
<< Selection(6, 19, 5)
);
const SelectionList macroUseSelections = SelectionList()
<< Selection(1, 8, 3)
<< Selection(2, 0, 3);
QTest::newRow("macro use 1")
<< _("#define FOO\n"
"@FOO\n")
<< macroUseSelections;
QTest::newRow("macro use 2")
<< _("#define FOO\n"
"FOO@\n")
<< macroUseSelections;
QTest::newRow("macro use 3")
<< _("#define @FOO\n"
"FOO\n")
<< macroUseSelections;
QTest::newRow("macro use 4")
<< _("#define FOO@\n"
"FOO\n")
<< macroUseSelections;
}
void CppEditorPlugin::test_useSelections()
{
QFETCH(QByteArray, source);
QFETCH(SelectionList, expectedSelections);
Tests::TestDocument testDocument("file.cpp", source);
Tests::UseSelectionsTestCase(testDocument, expectedSelections);
}
} // namespace Internal
} // namespace CppEditor
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment