diff --git a/addons/tabswitcher/autotests/tabswitchertest.h b/addons/tabswitcher/autotests/tabswitchertest.h --- a/addons/tabswitcher/autotests/tabswitchertest.h +++ b/addons/tabswitcher/autotests/tabswitchertest.h @@ -32,5 +32,6 @@ private Q_SLOTS: void testLongestCommonPrefix(); + void testLongestCommonPrefix_data(); }; diff --git a/addons/tabswitcher/autotests/tabswitchertest.cpp b/addons/tabswitcher/autotests/tabswitchertest.cpp --- a/addons/tabswitcher/autotests/tabswitchertest.cpp +++ b/addons/tabswitcher/autotests/tabswitchertest.cpp @@ -35,30 +35,43 @@ void KateTabSwitcherTest::testLongestCommonPrefix() { - // standard case + QFETCH(std::vector, input_strings); + QFETCH(QString, expected); + + QCOMPARE(detail::longestCommonPrefix(input_strings), expected); +} + +void KateTabSwitcherTest::testLongestCommonPrefix_data() +{ + QTest::addColumn>("input_strings"); + QTest::addColumn("expected"); std::vector strs; + + strs.clear(); strs.push_back(QLatin1String("/home/user1/a")); strs.push_back(QLatin1String("/home/user1/bc")); - QCOMPARE(detail::longestCommonPrefix(strs), QLatin1String("/home/user1/")); + QTest::newRow("standard case") << strs << QString(QLatin1String("/home/user1/")); - // empty string at the end of the list strs.clear(); strs.push_back(QLatin1String("/home/a")); strs.push_back(QLatin1String("/home/b")); strs.push_back(QLatin1String("")); - QCOMPARE(detail::longestCommonPrefix(strs), QLatin1String("")); + QTest::newRow("empty string at the end of the list") << strs << QString(); - // empty string not only at the end of the list strs.clear(); strs.push_back(QLatin1String("")); strs.push_back(QLatin1String("/home/a")); strs.push_back(QLatin1String("/home/b")); strs.push_back(QLatin1String("")); - QCOMPARE(detail::longestCommonPrefix(strs), QLatin1String("")); + QTest::newRow("empty string not only at the end of the list") << strs << QString(); - // a prefix with length 1 strs.clear(); strs.push_back(QLatin1String("/home/a")); strs.push_back(QLatin1String("/etc/b")); - QCOMPARE(detail::longestCommonPrefix(strs), QLatin1String("/")); + QTest::newRow("a prefix with length 1") << strs << QString(QLatin1String("/")); + + strs.clear(); + strs.push_back(QLatin1String("a")); + strs.push_back(QLatin1String("a")); + QTest::newRow("two equal strings") << strs << QString(QLatin1String("a")); } diff --git a/addons/tabswitcher/tabswitcherfilesmodel.h b/addons/tabswitcher/tabswitcherfilesmodel.h --- a/addons/tabswitcher/tabswitcherfilesmodel.h +++ b/addons/tabswitcher/tabswitcherfilesmodel.h @@ -80,6 +80,6 @@ FilenameList data_; }; -QString longestCommonPrefix(std::vector& strs); +QString longestCommonPrefix(std::vector const & strs); } diff --git a/addons/tabswitcher/tabswitcherfilesmodel.cpp b/addons/tabswitcher/tabswitcherfilesmodel.cpp --- a/addons/tabswitcher/tabswitcherfilesmodel.cpp +++ b/addons/tabswitcher/tabswitcherfilesmodel.cpp @@ -30,7 +30,7 @@ * see also http://www.cplusplus.com/forum/beginner/83540/ * Note that if strs contains the empty string, the result will be "" */ - QString longestCommonPrefix(std::vector& strs) { + QString longestCommonPrefix(std::vector const & strs) { int n = INT_MAX; if (strs.size() <= 0) { return QString();