forked from qt-creator/qt-creator
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:
@@ -454,6 +454,19 @@ QTCREATOR_UTILS_EXPORT QString normalizeNewlines(const QString &text)
|
|||||||
return res;
|
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)
|
QTCREATOR_UTILS_EXPORT QString appendHelper(const QString &base, int n)
|
||||||
{
|
{
|
||||||
return base + QString::number(n);
|
return base + QString::number(n);
|
||||||
|
@@ -109,5 +109,8 @@ QTCREATOR_UTILS_EXPORT QStringView chopIfEndsWith(QStringView str, QChar c);
|
|||||||
|
|
||||||
QTCREATOR_UTILS_EXPORT QString normalizeNewlines(const QString &text);
|
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
|
} // namespace Utils
|
||||||
|
@@ -69,10 +69,12 @@ private slots:
|
|||||||
void testWithTildeHomePath();
|
void testWithTildeHomePath();
|
||||||
void testMacroExpander_data();
|
void testMacroExpander_data();
|
||||||
void testMacroExpander();
|
void testMacroExpander();
|
||||||
void testStripAccelerator();
|
|
||||||
void testStripAccelerator_data();
|
void testStripAccelerator_data();
|
||||||
void testParseUsedPortFromNetstatOutput();
|
void testStripAccelerator();
|
||||||
void testParseUsedPortFromNetstatOutput_data();
|
void testParseUsedPortFromNetstatOutput_data();
|
||||||
|
void testParseUsedPortFromNetstatOutput();
|
||||||
|
void testJoinStrings_data();
|
||||||
|
void testJoinStrings();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
TestMacroExpander mx;
|
TestMacroExpander mx;
|
||||||
@@ -169,13 +171,6 @@ void tst_StringUtils::testMacroExpander()
|
|||||||
QCOMPARE(in, out);
|
QCOMPARE(in, out);
|
||||||
}
|
}
|
||||||
|
|
||||||
void tst_StringUtils::testStripAccelerator()
|
|
||||||
{
|
|
||||||
QFETCH(QString, expected);
|
|
||||||
|
|
||||||
QCOMPARE(Utils::stripAccelerator(QString::fromUtf8(QTest::currentDataTag())), expected);
|
|
||||||
}
|
|
||||||
|
|
||||||
void tst_StringUtils::testStripAccelerator_data()
|
void tst_StringUtils::testStripAccelerator_data()
|
||||||
{
|
{
|
||||||
QTest::addColumn<QString>("expected");
|
QTest::addColumn<QString>("expected");
|
||||||
@@ -193,12 +188,11 @@ void tst_StringUtils::testStripAccelerator_data()
|
|||||||
QTest::newRow("Test&") << "Test";
|
QTest::newRow("Test&") << "Test";
|
||||||
}
|
}
|
||||||
|
|
||||||
void tst_StringUtils::testParseUsedPortFromNetstatOutput()
|
void tst_StringUtils::testStripAccelerator()
|
||||||
{
|
{
|
||||||
QFETCH(QString, line);
|
QFETCH(QString, expected);
|
||||||
QFETCH(int, port);
|
|
||||||
|
|
||||||
QCOMPARE(Utils::parseUsedPortFromNetstatOutput(line.toUtf8()), port);
|
QCOMPARE(Utils::stripAccelerator(QString::fromUtf8(QTest::currentDataTag())), expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
void tst_StringUtils::testParseUsedPortFromNetstatOutput_data()
|
void tst_StringUtils::testParseUsedPortFromNetstatOutput_data()
|
||||||
@@ -246,6 +240,65 @@ void tst_StringUtils::testParseUsedPortFromNetstatOutput_data()
|
|||||||
QTest::newRow("QnxA") << "tcp6 0 0 *.22 *.* LISTEN " << 22;
|
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)
|
QTEST_GUILESS_MAIN(tst_StringUtils)
|
||||||
|
|
||||||
#include "tst_stringutils.moc"
|
#include "tst_stringutils.moc"
|
||||||
|
Reference in New Issue
Block a user