forked from qt-creator/qt-creator
Utils: add Position::fromCursor with tests
Change-Id: I1cd989eaf7e75bc04f171989f9f9fe932402abef Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
This commit is contained in:
@@ -59,6 +59,11 @@ Position Position::fromPositionInDocument(const QTextDocument *document, int pos
|
|||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Position Position::fromCursor(const QTextCursor &c)
|
||||||
|
{
|
||||||
|
return c.isNull() ? Position{} : Position{c.blockNumber() + 1, c.positionInBlock()};
|
||||||
|
}
|
||||||
|
|
||||||
int Range::length(const QString &text) const
|
int Range::length(const QString &text) const
|
||||||
{
|
{
|
||||||
if (end.line < begin.line)
|
if (end.line < begin.line)
|
||||||
|
@@ -32,6 +32,7 @@ public:
|
|||||||
|
|
||||||
static Position fromFileName(QStringView fileName, int &postfixPos);
|
static Position fromFileName(QStringView fileName, int &postfixPos);
|
||||||
static Position fromPositionInDocument(const QTextDocument *document, int pos);
|
static Position fromPositionInDocument(const QTextDocument *document, int pos);
|
||||||
|
static Position fromCursor(const QTextCursor &cursor);
|
||||||
};
|
};
|
||||||
|
|
||||||
class QTCREATOR_UTILS_EXPORT Range
|
class QTCREATOR_UTILS_EXPORT Range
|
||||||
|
@@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include <utils/textutils.h>
|
#include <utils/textutils.h>
|
||||||
|
|
||||||
|
#include <QTextCursor>
|
||||||
#include <QTextDocument>
|
#include <QTextDocument>
|
||||||
#include <QtTest>
|
#include <QtTest>
|
||||||
|
|
||||||
@@ -19,6 +20,9 @@ private slots:
|
|||||||
void testPositionFromPositionInDocument_data();
|
void testPositionFromPositionInDocument_data();
|
||||||
void testPositionFromPositionInDocument();
|
void testPositionFromPositionInDocument();
|
||||||
|
|
||||||
|
void testPositionFromCursor_data();
|
||||||
|
void testPositionFromCursor();
|
||||||
|
|
||||||
void testRangeLength_data();
|
void testRangeLength_data();
|
||||||
void testRangeLength();
|
void testRangeLength();
|
||||||
};
|
};
|
||||||
@@ -135,6 +139,36 @@ void tst_Text::testPositionFromPositionInDocument()
|
|||||||
QCOMPARE(Position::fromPositionInDocument(&doc, documentPosition), position);
|
QCOMPARE(Position::fromPositionInDocument(&doc, documentPosition), position);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void tst_Text::testPositionFromCursor_data()
|
||||||
|
{
|
||||||
|
QTest::addColumn<QString>("text");
|
||||||
|
QTest::addColumn<int>("documentPosition");
|
||||||
|
QTest::addColumn<Position>("position");
|
||||||
|
|
||||||
|
QTest::newRow("0") << QString() << 0 << Position{1, 0};
|
||||||
|
QTest::newRow("-1") << QString() << -1 << Position{};
|
||||||
|
QTest::newRow("mid line") << QString("foo\n") << 1 << Position{1, 1};
|
||||||
|
QTest::newRow("second line") << QString("foo\n") << 4 << Position{2, 0};
|
||||||
|
QTest::newRow("second mid line") << QString("foo\nbar") << 5 << Position{2, 1};
|
||||||
|
QTest::newRow("behind content") << QString("foo\nbar") << 8 << Position{1, 0};
|
||||||
|
}
|
||||||
|
|
||||||
|
void tst_Text::testPositionFromCursor()
|
||||||
|
{
|
||||||
|
QFETCH(QString, text);
|
||||||
|
QFETCH(int, documentPosition);
|
||||||
|
QFETCH(Position, position);
|
||||||
|
|
||||||
|
if (documentPosition < 0) {// test invalid Cursor {
|
||||||
|
QCOMPARE(Position::fromCursor(QTextCursor()), position);
|
||||||
|
} else {
|
||||||
|
QTextDocument doc(text);
|
||||||
|
QTextCursor cursor(&doc);
|
||||||
|
cursor.setPosition(documentPosition);
|
||||||
|
QCOMPARE(Position::fromCursor(cursor), position);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void tst_Text::testRangeLength_data()
|
void tst_Text::testRangeLength_data()
|
||||||
{
|
{
|
||||||
QTest::addColumn<QString>("text");
|
QTest::addColumn<QString>("text");
|
||||||
|
Reference in New Issue
Block a user