forked from qt-creator/qt-creator
Utils: add tests for Position::fromFileName
Change-Id: I321b91567e47e08883c7b991cd24d02bb8a9b9c6 Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
This commit is contained in:
@@ -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
|
||||
|
@@ -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
|
||||
|
||||
|
@@ -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()
|
||||
|
@@ -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");
|
||||
|
Reference in New Issue
Block a user