StringUtils: Add joinStrings method

See QTBUG-110900. In meantime, StringUtils addition.

Change-Id: I700321a65d2d00b85daa412c77b573457312ad3c
Reviewed-by: Orgad Shaneh <orgads@gmail.com>
This commit is contained in:
Jarek Kobus
2023-02-03 04:01:00 +01:00
parent f48b8b7bcb
commit 2d8d27c9ba
3 changed files with 82 additions and 13 deletions

View File

@@ -454,6 +454,19 @@ QTCREATOR_UTILS_EXPORT QString normalizeNewlines(const QString &text)
return res;
}
QTCREATOR_UTILS_EXPORT QString joinStrings(const QStringList &strings, QChar separator)
{
QString result;
for (const QString &string : strings) {
if (string.isEmpty())
continue;
if (!result.isEmpty())
result += separator;
result += string;
}
return result;
}
QTCREATOR_UTILS_EXPORT QString appendHelper(const QString &base, int n)
{
return base + QString::number(n);

View File

@@ -109,5 +109,8 @@ QTCREATOR_UTILS_EXPORT QStringView chopIfEndsWith(QStringView str, QChar c);
QTCREATOR_UTILS_EXPORT QString normalizeNewlines(const QString &text);
// Skips empty parts - see QTBUG-110900
QTCREATOR_UTILS_EXPORT QString joinStrings(const QStringList &strings, QChar separator);
} // namespace Utils

View File

@@ -69,10 +69,12 @@ private slots:
void testWithTildeHomePath();
void testMacroExpander_data();
void testMacroExpander();
void testStripAccelerator();
void testStripAccelerator_data();
void testParseUsedPortFromNetstatOutput();
void testStripAccelerator();
void testParseUsedPortFromNetstatOutput_data();
void testParseUsedPortFromNetstatOutput();
void testJoinStrings_data();
void testJoinStrings();
private:
TestMacroExpander mx;
@@ -169,13 +171,6 @@ void tst_StringUtils::testMacroExpander()
QCOMPARE(in, out);
}
void tst_StringUtils::testStripAccelerator()
{
QFETCH(QString, expected);
QCOMPARE(Utils::stripAccelerator(QString::fromUtf8(QTest::currentDataTag())), expected);
}
void tst_StringUtils::testStripAccelerator_data()
{
QTest::addColumn<QString>("expected");
@@ -193,12 +188,11 @@ void tst_StringUtils::testStripAccelerator_data()
QTest::newRow("Test&") << "Test";
}
void tst_StringUtils::testParseUsedPortFromNetstatOutput()
void tst_StringUtils::testStripAccelerator()
{
QFETCH(QString, line);
QFETCH(int, port);
QFETCH(QString, expected);
QCOMPARE(Utils::parseUsedPortFromNetstatOutput(line.toUtf8()), port);
QCOMPARE(Utils::stripAccelerator(QString::fromUtf8(QTest::currentDataTag())), expected);
}
void tst_StringUtils::testParseUsedPortFromNetstatOutput_data()
@@ -246,6 +240,65 @@ void tst_StringUtils::testParseUsedPortFromNetstatOutput_data()
QTest::newRow("QnxA") << "tcp6 0 0 *.22 *.* LISTEN " << 22;
}
void tst_StringUtils::testParseUsedPortFromNetstatOutput()
{
QFETCH(QString, line);
QFETCH(int, port);
QCOMPARE(Utils::parseUsedPortFromNetstatOutput(line.toUtf8()), port);
}
struct JoinData
{
QStringList input;
QString output = {};
QChar separator = '\n';
};
void tst_StringUtils::testJoinStrings_data()
{
QTest::addColumn<JoinData>("data");
QTest::newRow("0") << JoinData{};
QTest::newRow("1") << JoinData{{"one"}, "one"};
QTest::newRow("1_Null") << JoinData{{{}}};
QTest::newRow("1_Empty") << JoinData{{""}};
QTest::newRow("2") << JoinData{{"first", "second"}, "first\nsecond"};
QTest::newRow("2_Null") << JoinData{{{}, {}}};
QTest::newRow("2_Empty") << JoinData{{"", ""}};
QTest::newRow("2_1stNull") << JoinData{{{}, "second"}, "second"};
QTest::newRow("2_1stEmpty") << JoinData{{"", "second"}, "second"};
QTest::newRow("2_2ndNull") << JoinData{{"first", {}}, "first"};
QTest::newRow("2_2ndEmpty") << JoinData{{"first", ""}, "first"};
QTest::newRow("3") << JoinData{{"first", "second", "third"}, "first\nsecond\nthird"};
QTest::newRow("3_Null") << JoinData{{{}, {}, {}}};
QTest::newRow("3_Empty") << JoinData{{"", "", ""}};
QTest::newRow("3_1stNull") << JoinData{{{}, "second", "third"}, "second\nthird"};
QTest::newRow("3_1stEmpty") << JoinData{{"", "second", "third"}, "second\nthird"};
QTest::newRow("3_2ndNull") << JoinData{{"first", {}, "third"}, "first\nthird"};
QTest::newRow("3_2ndEmpty") << JoinData{{"first", "", "third"}, "first\nthird"};
QTest::newRow("3_3rdNull") << JoinData{{"first", "second", {}}, "first\nsecond"};
QTest::newRow("3_3rdEmpty") << JoinData{{"first", "second", ""}, "first\nsecond"};
QTest::newRow("3_1stNonNull") << JoinData{{"first", {}, {}}, "first"};
QTest::newRow("3_1stNonEmpty") << JoinData{{"first", "", ""}, "first"};
QTest::newRow("3_2ndNonNull") << JoinData{{{}, "second", {}}, "second"};
QTest::newRow("3_2ndNonEmpty") << JoinData{{"", "second", ""}, "second"};
QTest::newRow("3_2ndNonNull") << JoinData{{{}, {}, "third"}, "third"};
QTest::newRow("3_3ndNonEmpty") << JoinData{{"", "", "third"}, "third"};
QTest::newRow("DotSeparator") << JoinData{{"first", "second"}, "first.second", '.'};
}
void tst_StringUtils::testJoinStrings()
{
QFETCH(JoinData, data);
QCOMPARE(Utils::joinStrings(data.input, data.separator), data.output);
}
QTEST_GUILESS_MAIN(tst_StringUtils)
#include "tst_stringutils.moc"