forked from qt-creator/qt-creator
Utils: add tests for Position::fromPositionInDocument
Change-Id: I2b530cf62a4defe0292c51834b1e5093a7d5e55f Reviewed-by: <github-actions-qt-creator@cristianadam.eu> Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io> Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
||||||
|
|
||||||
#include "textutils.h"
|
#include "textutils.h"
|
||||||
|
#include "qtcassert.h"
|
||||||
|
|
||||||
#include <QRegularExpression>
|
#include <QRegularExpression>
|
||||||
#include <QTextBlock>
|
#include <QTextBlock>
|
||||||
@@ -50,6 +51,7 @@ Position Position::fromFileName(QStringView fileName, int &postfixPos)
|
|||||||
|
|
||||||
Position Position::fromPositionInDocument(const QTextDocument *document, int pos)
|
Position Position::fromPositionInDocument(const QTextDocument *document, int pos)
|
||||||
{
|
{
|
||||||
|
QTC_ASSERT(document, return {});
|
||||||
const QTextBlock block = document->findBlock(pos);
|
const QTextBlock block = document->findBlock(pos);
|
||||||
if (block.isValid())
|
if (block.isValid())
|
||||||
return {block.blockNumber() + 1, pos - block.position()};
|
return {block.blockNumber() + 1, pos - block.position()};
|
||||||
|
@@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include <utils/textutils.h>
|
#include <utils/textutils.h>
|
||||||
|
|
||||||
|
#include <QTextDocument>
|
||||||
#include <QtTest>
|
#include <QtTest>
|
||||||
|
|
||||||
using namespace Utils::Text;
|
using namespace Utils::Text;
|
||||||
@@ -15,6 +16,9 @@ private slots:
|
|||||||
void testPositionFromFileName_data();
|
void testPositionFromFileName_data();
|
||||||
void testPositionFromFileName();
|
void testPositionFromFileName();
|
||||||
|
|
||||||
|
void testPositionFromPositionInDocument_data();
|
||||||
|
void testPositionFromPositionInDocument();
|
||||||
|
|
||||||
void testRangeLength_data();
|
void testRangeLength_data();
|
||||||
void testRangeLength();
|
void testRangeLength();
|
||||||
};
|
};
|
||||||
@@ -107,6 +111,30 @@ void tst_Text::testPositionFromFileName()
|
|||||||
QCOMPARE(postfixPos, expectedPostfixPos);
|
QCOMPARE(postfixPos, expectedPostfixPos);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void tst_Text::testPositionFromPositionInDocument_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();
|
||||||
|
}
|
||||||
|
|
||||||
|
void tst_Text::testPositionFromPositionInDocument()
|
||||||
|
{
|
||||||
|
QFETCH(QString, text);
|
||||||
|
QFETCH(int, documentPosition);
|
||||||
|
QFETCH(Position, position);
|
||||||
|
|
||||||
|
const QTextDocument doc(text);
|
||||||
|
QCOMPARE(Position::fromPositionInDocument(&doc, documentPosition), 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