aboutsummaryrefslogtreecommitdiffstats
path: root/tests/auto/tools
diff options
context:
space:
mode:
authorJake Petroules <jake.petroules@qt.io>2017-06-14 15:48:47 -0700
committerJake Petroules <jake.petroules@qt.io>2017-08-23 08:32:06 +0000
commitca1d2a7ffdabcb3de4477427d23b7d309ec33c02 (patch)
treee7f5e91a9c7d981a84074911e4e5613a2ed0cfe3 /tests/auto/tools
parent6c19708039d0553cf79c68ff00ad1415f7d826e0 (diff)
Add a header file for string utility functions
Will be used by a subsequent patch. Change-Id: Ia88b27bbd4f698c2069f0641d713f2b47d093f5c Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
Diffstat (limited to 'tests/auto/tools')
-rw-r--r--tests/auto/tools/tst_tools.cpp189
-rw-r--r--tests/auto/tools/tst_tools.h9
2 files changed, 198 insertions, 0 deletions
diff --git a/tests/auto/tools/tst_tools.cpp b/tests/auto/tools/tst_tools.cpp
index 13bc18b36..9cfbb08e3 100644
--- a/tests/auto/tools/tst_tools.cpp
+++ b/tests/auto/tools/tst_tools.cpp
@@ -52,6 +52,7 @@
#include <tools/set.h>
#include <tools/settings.h>
#include <tools/setupprojectparameters.h>
+#include <tools/stringutils.h>
#include <tools/version.h>
#include <QtCore/qdir.h>
@@ -948,6 +949,194 @@ void TestTools::set_intersects()
QVERIFY(s1.intersects(s3));
}
+void TestTools::stringutils_join()
+{
+ QFETCH(std::vector<std::string>, input);
+ QFETCH(std::string, separator);
+ QFETCH(std::string, expectedResult);
+
+ QCOMPARE(join(input, separator), expectedResult);
+}
+
+void TestTools::stringutils_join_data()
+{
+ QTest::addColumn<std::vector<std::string>>("input");
+ QTest::addColumn<std::string>("separator");
+ QTest::addColumn<std::string>("expectedResult");
+
+ QTest::newRow("data1")
+ << std::vector<std::string>()
+ << std::string()
+ << std::string();
+
+ QTest::newRow("data2")
+ << std::vector<std::string>()
+ << std::string("separator")
+ << std::string();
+
+ QTest::newRow("data3")
+ << std::vector<std::string>({"one"})
+ << std::string("separator")
+ << std::string("one");
+
+ QTest::newRow("data4")
+ << std::vector<std::string>({"one"})
+ << std::string("separator")
+ << std::string("one");
+
+
+ QTest::newRow("data5")
+ << std::vector<std::string>({"a", "b"})
+ << std::string(" ")
+ << std::string("a b");
+
+ QTest::newRow("data6")
+ << std::vector<std::string>({"a", "b", "c"})
+ << std::string(" ")
+ << std::string("a b c");
+}
+
+void TestTools::stringutils_join_empty()
+{
+ std::vector<std::string> list;
+ std::string string = join(list, std::string());
+
+ QVERIFY(string.empty());
+}
+
+void TestTools::stringutils_join_char()
+{
+ QFETCH(std::vector<std::string>, input);
+ QFETCH(char, separator);
+ QFETCH(std::string, expectedResult);
+
+ QCOMPARE(join(input, separator), expectedResult);
+}
+
+void TestTools::stringutils_join_char_data()
+{
+ QTest::addColumn<std::vector<std::string>>("input");
+ QTest::addColumn<char>("separator");
+ QTest::addColumn<std::string>("expectedResult");
+
+ QTest::newRow("data1")
+ << std::vector<std::string>()
+ << ' '
+ << std::string();
+
+ QTest::newRow("data5")
+ << std::vector<std::string>({"a", "b"})
+ << ' '
+ << std::string("a b");
+
+ QTest::newRow("data6")
+ << std::vector<std::string>({"a", "b", "c"})
+ << ' '
+ << std::string("a b c");
+}
+
+void TestTools::stringutils_startsWith()
+{
+ std::string a;
+ a = "AB";
+ QVERIFY( startsWith(a, "A") );
+ QVERIFY( startsWith(a, "AB") );
+ QVERIFY( !startsWith(a, "C") );
+ QVERIFY( !startsWith(a, "ABCDEF") );
+ QVERIFY( startsWith(a, "") );
+ QVERIFY( startsWith(a, 'A') );
+ QVERIFY( !startsWith(a, 'C') );
+ QVERIFY( !startsWith(a, char()) );
+
+ QVERIFY( startsWith(a, "A") );
+ QVERIFY( startsWith(a, "AB") );
+ QVERIFY( !startsWith(a, "C") );
+ QVERIFY( !startsWith(a, "ABCDEF") );
+ QVERIFY( startsWith(a, "") );
+
+ a = "";
+ QVERIFY( startsWith(a, "") );
+ QVERIFY( !startsWith(a, "ABC") );
+
+ QVERIFY( startsWith(a, "") );
+ QVERIFY( !startsWith(a, "ABC") );
+
+ QVERIFY( !startsWith(a, 'x') );
+ QVERIFY( !startsWith(a, char()) );
+
+ a = std::string();
+ QVERIFY( startsWith(a, "") ); // different from QString::startsWith
+ QVERIFY( !startsWith(a, "ABC") );
+
+ QVERIFY( !startsWith(a, 'x') );
+ QVERIFY( !startsWith(a, char()) );
+
+ a = u8"\xc3\xa9";
+ QVERIFY( startsWith(a, u8"\xc3\xa9") );
+ QVERIFY( !startsWith(a, u8"\xc3\xa1") );
+}
+
+void TestTools::stringutils_endsWith()
+{
+ std::string a;
+ a = "AB";
+ QVERIFY( endsWith(a, "B") );
+ QVERIFY( endsWith(a, "AB") );
+ QVERIFY( !endsWith(a, "C") );
+ QVERIFY( !endsWith(a, "ABCDEF") );
+ QVERIFY( endsWith(a, "") );
+ QVERIFY( endsWith(a, 'B') );
+ QVERIFY( !endsWith(a, 'C') );
+ QVERIFY( !endsWith(a, char()) );
+
+ QVERIFY( endsWith(a, "B") );
+ QVERIFY( endsWith(a, "AB") );
+ QVERIFY( !endsWith(a, "C") );
+ QVERIFY( !endsWith(a, "ABCDEF") );
+ QVERIFY( endsWith(a, "") );
+
+ a = "";
+ QVERIFY( endsWith(a, "") );
+ QVERIFY( !endsWith(a, "ABC") );
+ QVERIFY( !endsWith(a, 'x') );
+ QVERIFY( !endsWith(a, char()) );
+
+ QVERIFY( endsWith(a, "") );
+ QVERIFY( !endsWith(a, "ABC") );
+
+ a = std::string();
+ QVERIFY( endsWith(a, "") ); // different from QString::endsWith
+ QVERIFY( !endsWith(a, "ABC") );
+
+ QVERIFY( !endsWith(a, 'x') );
+ QVERIFY( !endsWith(a, char()) );
+
+ a = u8"\xc3\xa9";
+ QVERIFY( endsWith(a, u8"\xc3\xa9") );
+ QVERIFY( !endsWith(a, u8"\xc3\xa1") );
+}
+
+void TestTools::stringutils_trimmed()
+{
+ std::string a;
+ a = "Text";
+ QCOMPARE(a, std::string("Text"));
+ QCOMPARE(trimmed(a), std::string("Text"));
+ QCOMPARE(a, std::string("Text"));
+ a = " ";
+ QCOMPARE(trimmed(a), std::string(""));
+ QCOMPARE(a, std::string(" "));
+ a = " a ";
+ QCOMPARE(trimmed(a), std::string("a"));
+
+ a = "Text";
+ QCOMPARE(trimmed(std::move(a)), std::string("Text"));
+ a = " ";
+ QCOMPARE(trimmed(std::move(a)), std::string(""));
+ a = " a ";
+ QCOMPARE(trimmed(std::move(a)), std::string("a"));
+}
+
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
diff --git a/tests/auto/tools/tst_tools.h b/tests/auto/tools/tst_tools.h
index cb6828ee8..e80d5fbf1 100644
--- a/tests/auto/tools/tst_tools.h
+++ b/tests/auto/tools/tst_tools.h
@@ -85,6 +85,15 @@ private slots:
void set_initializerList();
void set_intersects();
+ void stringutils_join();
+ void stringutils_join_data();
+ void stringutils_join_empty();
+ void stringutils_join_char();
+ void stringutils_join_char_data();
+ void stringutils_startsWith();
+ void stringutils_endsWith();
+ void stringutils_trimmed();
+
private:
QString setupSettingsDir1();
QString setupSettingsDir2();