diff options
author | David Schulz <david.schulz@qt.io> | 2023-05-11 09:34:53 +0200 |
---|---|---|
committer | David Schulz <david.schulz@qt.io> | 2023-05-12 11:03:33 +0000 |
commit | 1a98dda5c417e892cadbedb656829cf2ba4d1d0a (patch) | |
tree | 8b6bb6b91796e16f0a95ec7ec1003d4f49d68523 /tests | |
parent | 70609cdec6fa3115af215f16fc5ae48602736027 (diff) | |
download | qt-creator-1a98dda5c417e892cadbedb656829cf2ba4d1d0a.tar.gz |
Utils: fix Text::Range length and remove mid
Those functions are based on the assumption that the passed text starts
at the begin position, which was good enough for search results, but if
used in other parts of the codebase it might give unwanted results.
Calculate the length of the range now as expected and subtract the
beginning lines.
In order to still got the correct results for the text result texts
modify the result range to always start at the first line before
calculating the length of the range.
Also add tests for the modified functionality
Change-Id: I7ccd75b642dda6dd4f738877cbe3543d46c03652
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/utils/CMakeLists.txt | 1 | ||||
-rw-r--r-- | tests/auto/utils/text/CMakeLists.txt | 4 | ||||
-rw-r--r-- | tests/auto/utils/text/text.qbs | 7 | ||||
-rw-r--r-- | tests/auto/utils/text/tst_text.cpp | 58 |
4 files changed, 70 insertions, 0 deletions
diff --git a/tests/auto/utils/CMakeLists.txt b/tests/auto/utils/CMakeLists.txt index 36f20fdac5..1bb4bb641a 100644 --- a/tests/auto/utils/CMakeLists.txt +++ b/tests/auto/utils/CMakeLists.txt @@ -17,4 +17,5 @@ add_subdirectory(stringutils) add_subdirectory(tasktree) add_subdirectory(templateengine) add_subdirectory(treemodel) +add_subdirectory(text) add_subdirectory(unixdevicefileaccess) diff --git a/tests/auto/utils/text/CMakeLists.txt b/tests/auto/utils/text/CMakeLists.txt new file mode 100644 index 0000000000..78fd3838f4 --- /dev/null +++ b/tests/auto/utils/text/CMakeLists.txt @@ -0,0 +1,4 @@ +add_qtc_test(tst_utils_text + DEPENDS Utils + SOURCES tst_text.cpp +) diff --git a/tests/auto/utils/text/text.qbs b/tests/auto/utils/text/text.qbs new file mode 100644 index 0000000000..828dec8d11 --- /dev/null +++ b/tests/auto/utils/text/text.qbs @@ -0,0 +1,7 @@ +import qbs + +QtcAutotest { + name: "Utils::Text autotest" + Depends { name: "Utils" } + files: "tst_text.cpp" +} diff --git a/tests/auto/utils/text/tst_text.cpp b/tests/auto/utils/text/tst_text.cpp new file mode 100644 index 0000000000..a2e9dc5037 --- /dev/null +++ b/tests/auto/utils/text/tst_text.cpp @@ -0,0 +1,58 @@ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 + +#include <utils/textutils.h> + +#include <QtTest> + +using namespace Utils::Text; + +class tst_Text : public QObject +{ + Q_OBJECT + +private slots: + void testRangeLength_data(); + void testRangeLength(); +}; + +void tst_Text::testRangeLength_data() +{ + QTest::addColumn<QString>("text"); + QTest::addColumn<Range>("range"); + QTest::addColumn<int>("length"); + + QTest::newRow("empty range") << QString() << Range{{1, 0}, {1, 0}} << 0; + + QTest::newRow("range on same line") << QString() << Range{{1, 0}, {1, 1}} << 1; + + QTest::newRow("range spanning lines") << QString("foo\nbar") << Range{{1, 0}, {2, 0}} << 4; + + QTest::newRow("range spanning lines and column offset") + << QString("foo\nbar") << Range{{1, 1}, {2, 1}} << 4; + + QTest::newRow("range spanning lines and begin column offset") + << QString("foo\nbar") << Range{{1, 1}, {2, 0}} << 3; + + QTest::newRow("range spanning lines and end column offset") + << QString("foo\nbar") << Range{{1, 0}, {2, 1}} << 5; + + QTest::newRow("hyper range") << QString("foo\nbar\nfoobar") << Range{{2, 1}, {3, 1}} << 4; + + QTest::newRow("flipped range") << QString() << Range{{2, 0}, {1, 0}} << -1; + + QTest::newRow("out of range") << QString() << Range{{1, 0}, {2, 0}} << -1; +} + +void tst_Text::testRangeLength() +{ + QFETCH(QString, text); + QFETCH(Range, range); + QFETCH(int, length); + + QCOMPARE(range.length(text), length); +} + +QTEST_GUILESS_MAIN(tst_Text) + +#include "tst_text.moc" |