summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Schulz <david.schulz@qt.io>2023-05-12 10:59:04 +0200
committerDavid Schulz <david.schulz@qt.io>2023-05-16 08:24:28 +0000
commitb4734ff7279284eec4f0ced0e4de07cb348c481e (patch)
tree803cbf99748e3022f383c51ced2fe7d2df803871
parente2df60abc2acf465c2941719ab6e56573d07a762 (diff)
downloadqt-creator-b4734ff7279284eec4f0ced0e4de07cb348c481e.tar.gz
Utils: add tests for Position::fromFileName
Change-Id: I321b91567e47e08883c7b991cd24d02bb8a9b9c6 Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
-rw-r--r--src/libs/utils/textutils.cpp9
-rw-r--r--src/libs/utils/textutils.h2
-rw-r--r--tests/auto/utils/filepath/tst_filepath.cpp28
-rw-r--r--tests/auto/utils/text/tst_text.cpp95
4 files changed, 106 insertions, 28 deletions
diff --git a/src/libs/utils/textutils.cpp b/src/libs/utils/textutils.cpp
index 8cf7a41ed6..9cb7c1e011 100644
--- a/src/libs/utils/textutils.cpp
+++ b/src/libs/utils/textutils.cpp
@@ -32,7 +32,6 @@ Position Position::fromFileName(QStringView fileName, int &postfixPos)
Position pos;
if (match.hasMatch()) {
postfixPos = match.capturedStart(0);
- pos.line = 0; // for the case that there's only a : at the end
if (match.lastCapturedIndex() > 0) {
pos.line = match.captured(1).toInt();
if (match.lastCapturedIndex() > 2) // index 2 includes the + or : for the column number
@@ -44,6 +43,8 @@ Position Position::fromFileName(QStringView fileName, int &postfixPos)
if (vsMatch.lastCapturedIndex() > 1) // index 1 includes closing )
pos.line = vsMatch.captured(2).toInt();
}
+ if (pos.line > 0 && pos.column < 0)
+ pos.column = 0; // if we got a valid line make sure to return a valid TextPosition
return pos;
}
@@ -264,4 +265,10 @@ void applyReplacements(QTextDocument *doc, const Replacements &replacements)
editCursor.endEditBlock();
}
+QDebug &operator<<(QDebug &stream, const Position &pos)
+{
+ stream << "line: " << pos.line << ", column: " << pos.column;
+ return stream;
+}
+
} // namespace Utils::Text
diff --git a/src/libs/utils/textutils.h b/src/libs/utils/textutils.h
index 6ee82274dc..df1eb73d8e 100644
--- a/src/libs/utils/textutils.h
+++ b/src/libs/utils/textutils.h
@@ -92,6 +92,8 @@ QTCREATOR_UTILS_EXPORT int utf8NthLineOffset(const QTextDocument *textDocument,
QTCREATOR_UTILS_EXPORT QString utf16LineTextInUtf8Buffer(const QByteArray &utf8Buffer,
int currentUtf8Offset);
+QTCREATOR_UTILS_EXPORT QDebug &operator<<(QDebug &stream, const Position &pos);
+
} // Text
} // Utils
diff --git a/tests/auto/utils/filepath/tst_filepath.cpp b/tests/auto/utils/filepath/tst_filepath.cpp
index f2ecac2bb6..3699ec6b2e 100644
--- a/tests/auto/utils/filepath/tst_filepath.cpp
+++ b/tests/auto/utils/filepath/tst_filepath.cpp
@@ -1096,36 +1096,10 @@ void tst_filepath::linkFromString_data()
QTest::newRow("no-line-no-column")
<< QString("someFile.txt") << FilePath("someFile.txt") << 0 << -1;
- QTest::newRow(": at end") << QString::fromLatin1("someFile.txt:") << FilePath("someFile.txt")
- << 0 << -1;
- QTest::newRow("+ at end") << QString::fromLatin1("someFile.txt+") << FilePath("someFile.txt")
- << 0 << -1;
- QTest::newRow(": for column") << QString::fromLatin1("someFile.txt:10:")
- << FilePath("someFile.txt") << 10 << -1;
- QTest::newRow("+ for column") << QString::fromLatin1("someFile.txt:10+")
- << FilePath("someFile.txt") << 10 << -1;
- QTest::newRow(": and + at end")
- << QString::fromLatin1("someFile.txt:+") << FilePath("someFile.txt") << 0 << -1;
- QTest::newRow("empty line") << QString::fromLatin1("someFile.txt:+10")
- << FilePath("someFile.txt") << 0 << 9;
- QTest::newRow(":line-no-column") << QString::fromLatin1("/some/path/file.txt:42")
- << FilePath("/some/path/file.txt") << 42 << -1;
- QTest::newRow("+line-no-column") << QString::fromLatin1("/some/path/file.txt+42")
- << FilePath("/some/path/file.txt") << 42 << -1;
QTest::newRow(":line-:column") << QString::fromLatin1("/some/path/file.txt:42:3")
<< FilePath("/some/path/file.txt") << 42 << 2;
- QTest::newRow(":line-+column") << QString::fromLatin1("/some/path/file.txt:42+33")
- << FilePath("/some/path/file.txt") << 42 << 32;
- QTest::newRow("+line-:column") << QString::fromLatin1("/some/path/file.txt+142:30")
- << FilePath("/some/path/file.txt") << 142 << 29;
- QTest::newRow("+line-+column") << QString::fromLatin1("/some/path/file.txt+142+33")
- << FilePath("/some/path/file.txt") << 142 << 32;
- QTest::newRow("( at end") << QString::fromLatin1("/some/path/file.txt(")
- << FilePath("/some/path/file.txt") << 0 << -1;
- QTest::newRow("(42 at end") << QString::fromLatin1("/some/path/file.txt(42")
- << FilePath("/some/path/file.txt") << 42 << -1;
QTest::newRow("(42) at end") << QString::fromLatin1("/some/path/file.txt(42)")
- << FilePath("/some/path/file.txt") << 42 << -1;
+ << FilePath("/some/path/file.txt") << 42 << 0;
}
void tst_filepath::pathAppended()
diff --git a/tests/auto/utils/text/tst_text.cpp b/tests/auto/utils/text/tst_text.cpp
index a2e9dc5037..d683ab3542 100644
--- a/tests/auto/utils/text/tst_text.cpp
+++ b/tests/auto/utils/text/tst_text.cpp
@@ -12,10 +12,105 @@ class tst_Text : public QObject
Q_OBJECT
private slots:
+ void testPositionFromFileName_data();
+ void testPositionFromFileName();
+
void testRangeLength_data();
void testRangeLength();
};
+void tst_Text::testPositionFromFileName_data()
+{
+ QTest::addColumn<QString>("fileName");
+ QTest::addColumn<Position>("pos");
+ QTest::addColumn<int>("expectedPostfixPos");
+
+ const QString file("/foo/bar");
+ const QString fileWin("C:\\foo\\bar");
+
+ QTest::newRow("no pos") << file << Position() << -1;
+ QTest::newRow("no pos win") << fileWin << Position() << -1;
+
+ QTest::newRow("empty:") << file + ":" << Position() << 8;
+ QTest::newRow("empty: win") << fileWin + ":" << Position() << 10;
+
+ QTest::newRow("empty+") << file + "+" << Position() << 8;
+ QTest::newRow("empty+ win") << fileWin + "+" << Position() << 10;
+
+ QTest::newRow("empty::") << file + "::" << Position() << 8;
+ QTest::newRow("empty:: win") << fileWin + "::" << Position() << 10;
+
+ QTest::newRow("empty++") << file + "++" << Position() << 8;
+ QTest::newRow("empty++ win") << fileWin + "++" << Position() << 10;
+
+ QTest::newRow("line:") << file + ":1" << Position{1, 0} << 8;
+ QTest::newRow("line: win") << fileWin + ":1" << Position{1, 0} << 10;
+
+ QTest::newRow("line+") << file + "+8" << Position{8, 0} << 8;
+ QTest::newRow("line+ win") << fileWin + "+8" << Position{8, 0} << 10;
+
+ QTest::newRow("multi digit line:") << file + ":42" << Position{42, 0} << 8;
+ QTest::newRow("multi digit line: win") << fileWin + ":42" << Position{42, 0} << 10;
+
+ QTest::newRow("multi digit line+") << file + "+1234567890" << Position{1234567890, 0} << 8;
+ QTest::newRow("multi digit line+ win")
+ << fileWin + "+1234567890" << Position{1234567890, 0} << 10;
+
+ QTest::newRow("multi digit line+") << file + "+1234567890" << Position{1234567890, 0} << 8;
+ QTest::newRow("multi digit line+ win")
+ << fileWin + "+1234567890" << Position{1234567890, 0} << 10;
+
+ QTest::newRow("line: empty column:") << file + ":1:" << Position{1, 0} << 8;
+ QTest::newRow("line: empty column: win") << fileWin + ":1:" << Position{1, 0} << 10;
+
+ QTest::newRow("line+ empty column+") << file + "+8+" << Position{8, 0} << 8;
+ QTest::newRow("line+ empty column+ win") << fileWin + "+8+" << Position{8, 0} << 10;
+
+ QTest::newRow("line: column:") << file + ":1:2" << Position{1, 1} << 8;
+ QTest::newRow("line: column: win") << fileWin + ":1:2" << Position{1, 1} << 10;
+
+ QTest::newRow("line+ column+") << file + "+8+3" << Position{8, 2} << 8;
+ QTest::newRow("line+ column+ win") << fileWin + "+8+3" << Position{8, 2} << 10;
+
+ QTest::newRow("mixed:+") << file + ":1+2" << Position{1, 1} << 8;
+ QTest::newRow("mixed:+ win") << fileWin + ":1+2" << Position{1, 1} << 10;
+
+ QTest::newRow("mixed+:") << file + "+8:3" << Position{8, 2} << 8;
+ QTest::newRow("mixed+: win") << fileWin + "+8:3" << Position{8, 2} << 10;
+
+ QTest::newRow("garbage:") << file + ":foo" << Position() << -1;
+ QTest::newRow("garbage: win") << fileWin + ":bar" << Position() << -1;
+
+ QTest::newRow("garbage+") << file + "+snu" << Position() << -1;
+ QTest::newRow("garbage+ win") << fileWin + "+snu" << Position() << -1;
+
+ QTest::newRow("msvc(") << file + "(" << Position() << 8;
+ QTest::newRow("msvc( win") << fileWin + "(" << Position() << 10;
+
+ QTest::newRow("msvc(empty)") << file + "()" << Position() << -1;
+ QTest::newRow("msvc(empty) win") << fileWin + "()" << Position() << -1;
+
+ QTest::newRow("msvc(line") << file + "(1" << Position{1, 0} << 8;
+ QTest::newRow("msvc(line win") << fileWin + "(4569871" << Position{4569871, 0} << 10;
+
+ QTest::newRow("msvc(line)") << file + "(1)" << Position{1, 0} << 8;
+ QTest::newRow("msvc(line) win") << fileWin + "(4569871)" << Position{4569871, 0} << 10;
+
+ QTest::newRow("msvc(garbage)") << file + "(foo)" << Position() << -1;
+ QTest::newRow("msvc(garbage) win") << fileWin + "(bar)" << Position() << -1;
+}
+
+void tst_Text::testPositionFromFileName()
+{
+ QFETCH(QString, fileName);
+ QFETCH(Position, pos);
+ QFETCH(int, expectedPostfixPos);
+
+ int postfixPos = -1;
+ QCOMPARE(Position::fromFileName(fileName, postfixPos), pos);
+ QCOMPARE(postfixPos, expectedPostfixPos);
+}
+
void tst_Text::testRangeLength_data()
{
QTest::addColumn<QString>("text");