File indexing completed on 2024-04-21 03:53:41
0001 0002 #include "kstringhandlertest.h" 0003 0004 #include <QRegularExpression> 0005 #include <QTest> 0006 0007 QTEST_MAIN(KStringHandlerTest) 0008 0009 #include "kstringhandler.h" 0010 0011 QString KStringHandlerTest::test = QStringLiteral("The quick brown fox jumped over the lazy bridge. "); 0012 0013 void KStringHandlerTest::capwords() 0014 { 0015 QCOMPARE(KStringHandler::capwords(test), QStringLiteral("The Quick Brown Fox Jumped Over The Lazy Bridge. ")); 0016 } 0017 0018 void KStringHandlerTest::tagURLs() 0019 { 0020 QString test = QStringLiteral("Click on https://foo@bar:www.kde.org/yoyo/dyne.html#a1 for info."); 0021 QCOMPARE(KStringHandler::tagUrls(test), 0022 QStringLiteral("Click on <a href=\"https://foo@bar:www.kde.org/yoyo/dyne.html#a1\">https://foo@bar:www.kde.org/yoyo/dyne.html#a1</a> for info.")); 0023 0024 test = QStringLiteral("http://www.foo.org/story$806"); 0025 QCOMPARE(KStringHandler::tagUrls(test), QStringLiteral("<a href=\"http://www.foo.org/story$806\">http://www.foo.org/story$806</a>")); 0026 0027 test = QStringLiteral("http://www.foo.org/bla-(bli)"); 0028 QCOMPARE(KStringHandler::tagUrls(test), QStringLiteral("<a href=\"http://www.foo.org/bla-(bli)\">http://www.foo.org/bla-(bli)</a>")); 0029 0030 test = QStringLiteral("http://www.foo.org/bla-bli"); 0031 QCOMPARE(KStringHandler::tagUrls(test), QStringLiteral("<a href=\"http://www.foo.org/bla-bli\">http://www.foo.org/bla-bli</a>")); 0032 0033 // Test with Unicode characters 0034 test = QStringLiteral("Click on https://foo@bar:www.kde.org/ÿöyo/dyne.html#a1 for info."); 0035 QCOMPARE(KStringHandler::tagUrls(test), 0036 QStringLiteral("Click on <a href=\"https://foo@bar:www.kde.org/ÿöyo/dyne.html#a1\">https://foo@bar:www.kde.org/ÿöyo/dyne.html#a1</a> for info.")); 0037 } 0038 0039 void KStringHandlerTest::perlSplitTextSep() 0040 { 0041 QStringList expected; 0042 expected << QStringLiteral("some") << QStringLiteral("string") << QStringLiteral("for") << QStringLiteral("you__here"); 0043 QCOMPARE(KStringHandler::perlSplit(QStringLiteral("__"), QStringLiteral("some__string__for__you__here"), 4), expected); 0044 0045 expected.clear(); 0046 expected << QStringLiteral("kparts") << QStringLiteral("reaches") << QStringLiteral("the parts other parts can't"); 0047 QCOMPARE(KStringHandler::perlSplit(QLatin1Char(' '), QStringLiteral("kparts reaches the parts other parts can't"), 3), expected); 0048 } 0049 0050 void KStringHandlerTest::perlSplitRegexSep() 0051 { 0052 QCOMPARE(KStringHandler::perlSplit(QRegularExpression(QStringLiteral("[! ]")), QStringLiteral("Split me up ! I'm bored ! OK ?"), 3), 0053 (QStringList{QStringLiteral("Split"), QStringLiteral("me"), QStringLiteral("up ! I'm bored ! OK ?")})); 0054 0055 QCOMPARE(KStringHandler::perlSplit(QRegularExpression(QStringLiteral("\\W")), QStringLiteral("aaa ggg cd ef"), 3), 0056 (QStringList{QStringLiteral("aaa"), QStringLiteral("ggg"), QStringLiteral("cd ef")})); 0057 0058 // Test with Unicode characters 0059 QCOMPARE(KStringHandler::perlSplit(QRegularExpression(QStringLiteral("\\W")), QStringLiteral("aaa gǵg cd ef"), 3), 0060 (QStringList{QStringLiteral("aaa"), QStringLiteral("gǵg"), QStringLiteral("cd ef")})); 0061 } 0062 0063 void KStringHandlerTest::obscure() 0064 { 0065 // See bug 167900, obscure() produced chars that could not properly be converted to and from 0066 // UTF8. The result was that storing passwords with '!' in them did not work. 0067 QString test = QStringLiteral("!TEST!"); 0068 QString obscured = KStringHandler::obscure(test); 0069 QByteArray obscuredBytes = obscured.toUtf8(); 0070 QCOMPARE(KStringHandler::obscure(QString::fromUtf8(obscuredBytes.constData())), test); 0071 } 0072 0073 // Zero-Width Space 0074 static const QChar ZWSP(0x200b); 0075 // Word Joiner 0076 static const QChar WJ(0x2060); 0077 0078 void KStringHandlerTest::preProcessWrap_data() 0079 { 0080 QTest::addColumn<QString>("string"); 0081 QTest::addColumn<QString>("expected"); 0082 0083 // Should result in no additional breaks 0084 QTest::newRow("spaces") << "foo bar baz" 0085 << "foo bar baz"; 0086 0087 // Should insert a ZWSP after each '_' 0088 QTest::newRow("underscores") << "foo_bar_baz" << QString(QStringLiteral("foo_") + ZWSP + QStringLiteral("bar_") + ZWSP + QStringLiteral("baz")); 0089 0090 // Should insert a ZWSP after each '-' 0091 QTest::newRow("hyphens") << "foo-bar-baz" << QString(QStringLiteral("foo-") + ZWSP + QStringLiteral("bar-") + ZWSP + QStringLiteral("baz")); 0092 0093 // Should insert a ZWSP after each '.' 0094 QTest::newRow("periods") << "foo.bar.baz" << QString(QStringLiteral("foo.") + ZWSP + QStringLiteral("bar.") + ZWSP + QStringLiteral("baz")); 0095 0096 // Should insert a ZWSP after each ',' 0097 QTest::newRow("commas") << "foo,bar,baz" << QString(QStringLiteral("foo,") + ZWSP + QStringLiteral("bar,") + ZWSP + QStringLiteral("baz")); 0098 0099 // Should result in no additional breaks since the '_'s are followed by spaces 0100 QTest::newRow("mixed underscores and spaces") << "foo_ bar_ baz" 0101 << "foo_ bar_ baz"; 0102 0103 // Should result in no additional breaks since the '_' is the last char 0104 QTest::newRow("ends with underscore") << "foo_" 0105 << "foo_"; 0106 0107 // Should insert a ZWSP before '(' and after ')' 0108 QTest::newRow("parens") << "foo(bar)baz" << QString(QStringLiteral("foo") + ZWSP + QStringLiteral("(bar)") + ZWSP + QStringLiteral("baz")); 0109 0110 // Should insert a ZWSP before '[' and after ']' 0111 QTest::newRow("brackets") << "foo[bar]baz" << QString(QStringLiteral("foo") + ZWSP + QStringLiteral("[bar]") + ZWSP + QStringLiteral("baz")); 0112 0113 // Should insert a ZWSP before '{' and after '}' 0114 QTest::newRow("curly braces") << "foo{bar}baz" << QString(QStringLiteral("foo") + ZWSP + QStringLiteral("{bar}") + ZWSP + QStringLiteral("baz")); 0115 0116 // Should insert a ZWSP before '(' but not after ')' since it's the last char 0117 QTest::newRow("ends with ')'") << "foo(bar)" << QString(QStringLiteral("foo") + ZWSP + QStringLiteral("(bar)")); 0118 0119 // Should insert a single ZWSP between the '_' and the '(' 0120 QTest::newRow("'_' followed by '('") << "foo_(bar)" << QString(QStringLiteral("foo_") + ZWSP + QStringLiteral("(bar)")); 0121 0122 // Should insert ZWSP's between the '_' and the '[', between the double 0123 // '['s and the double ']'s, but not before and after 'bar' 0124 QTest::newRow("'_' before double brackets") << "foo_[[bar]]" 0125 << QString(QStringLiteral("foo_") + ZWSP + QStringLiteral("[") + ZWSP + QStringLiteral("[bar]") + ZWSP 0126 + QStringLiteral("]")); 0127 0128 // Should only insert ZWSP's between the double '['s and the double ']'s 0129 QTest::newRow("space before double brackets") << "foo [[bar]]" 0130 << QString(QStringLiteral("foo [") + ZWSP + QStringLiteral("[bar]") + ZWSP + QStringLiteral("]")); 0131 0132 // Shouldn't result in any additional breaks since the '(' is preceded 0133 // by a space, and the ')' is followed by a space. 0134 QTest::newRow("parens with spaces") << "foo (bar) baz" 0135 << "foo (bar) baz"; 0136 0137 // Should insert a WJ (Word Joiner) before a single quote 0138 QTest::newRow("single quote") << "foo'bar" << QString(QStringLiteral("foo") + WJ + QStringLiteral("'bar")); 0139 0140 // Should insert a ZWSP between sub-words, but not before nor after the word 0141 QTest::newRow("camelCase") << "camelCase" << QString(QStringLiteral("camel") + ZWSP + QStringLiteral("Case")); 0142 0143 // Why limiting yourself to ASCII? More and more programming languages these days allow for Unicode identifiers. 0144 QTest::newRow("camelCase international") << "приветМир" << QString(QStringLiteral("привет") + ZWSP + QStringLiteral("Мир")); 0145 0146 // Should insert a ZWSP between sub-words, but not before first (upper case) letter 0147 QTest::newRow("PascalCase") << "PascalCase" << QString(QStringLiteral("Pascal") + ZWSP + QStringLiteral("Case")); 0148 0149 // But if a string already contains whitespaces, no need to insert ZWSPs 0150 QTest::newRow("camelCase with whitespaces") << "camelCase PascalCase" 0151 << "camelCase PascalCase"; 0152 0153 // However, single quote and other tricks should still work. 0154 QTest::newRow("single quote with whitespaces") << "foo(bar) PascalCase'camelCase" 0155 << QString(QStringLiteral("foo") + ZWSP + QStringLiteral("(bar) PascalCase") + WJ 0156 + QStringLiteral("'camelCase")); 0157 } 0158 0159 // Little helper function to make tests diagnostics more readable by humans 0160 static QString replaceZwsp(const QString &string) 0161 { 0162 const QString replacement = QStringLiteral("<ZWSP>"); 0163 QString result; 0164 result.reserve(string.length() + string.count(ZWSP) * replacement.length()); 0165 for (const auto i : string) { 0166 if (i == ZWSP) { 0167 result += replacement; 0168 } else { 0169 result += i; 0170 } 0171 } 0172 0173 return result; 0174 } 0175 0176 void KStringHandlerTest::preProcessWrap() 0177 { 0178 QFETCH(QString, string); 0179 QFETCH(QString, expected); 0180 0181 QCOMPARE(replaceZwsp(KStringHandler::preProcessWrap(string)), replaceZwsp(expected)); 0182 } 0183 0184 void KStringHandlerTest::logicalLength_data() 0185 { 0186 QTest::addColumn<QString>("string"); 0187 QTest::addColumn<int>("expected"); 0188 0189 QTest::newRow("Latin") << "foo bar baz" << 11; 0190 QTest::newRow("Chinese") << QString::fromUtf8("\xe4\xbd\xa0\xe5\xa5\xbd") << 4; 0191 QTest::newRow("Japanese") << QString::fromUtf8("\xe9\x9d\x92\xe3\x81\x84\xe7\xa9\xba") << 6; 0192 QTest::newRow("Korean") << QString::fromUtf8("\xed\x95\x9c\xea\xb5\xad\xec\x96\xb4") << 6; 0193 QTest::newRow("Mixed") << QString::fromUtf8("KDE\xe6\xa1\x8c\xe9\x9d\xa2") << 7; 0194 } 0195 0196 void KStringHandlerTest::logicalLength() 0197 { 0198 QFETCH(QString, string); 0199 QFETCH(int, expected); 0200 QCOMPARE(KStringHandler::logicalLength(string), expected); 0201 } 0202 0203 void KStringHandlerTest::lsqueeze_data() 0204 { 0205 QTest::addColumn<QString>("string"); 0206 QTest::addColumn<int>("length"); 0207 QTest::addColumn<QString>("expected"); 0208 0209 QTest::newRow("kde_is_awesome") << "KDE is awesome" << 11 << "... awesome"; 0210 QTest::newRow("kde_is_really_awesome") << "KDE is really awesome" << 20 << "...is really awesome"; 0211 QTest::newRow("kde_is_really_awesome_full") << "KDE is really awesome" << 30 << "KDE is really awesome"; 0212 } 0213 0214 void KStringHandlerTest::lsqueeze() 0215 { 0216 QFETCH(QString, string); 0217 QFETCH(int, length); 0218 QFETCH(QString, expected); 0219 0220 QCOMPARE(KStringHandler::lsqueeze(string, length), expected); 0221 } 0222 0223 void KStringHandlerTest::csqueeze_data() 0224 { 0225 QTest::addColumn<QString>("string"); 0226 QTest::addColumn<int>("length"); 0227 QTest::addColumn<QString>("expected"); 0228 0229 QTest::newRow("kde_is_awesome") << "KDE is awesome" << 11 << "KDE ...some"; 0230 QTest::newRow("kde_is_really_awesome") << "KDE is really awesome" << 20 << "KDE is r... awesome"; 0231 QTest::newRow("kde_is_really_awesome_full") << "KDE is really awesome" << 30 << "KDE is really awesome"; 0232 } 0233 0234 void KStringHandlerTest::csqueeze() 0235 { 0236 QFETCH(QString, string); 0237 QFETCH(int, length); 0238 QFETCH(QString, expected); 0239 0240 QCOMPARE(KStringHandler::csqueeze(string, length), expected); 0241 } 0242 0243 void KStringHandlerTest::rsqueeze_data() 0244 { 0245 QTest::addColumn<QString>("string"); 0246 QTest::addColumn<int>("length"); 0247 QTest::addColumn<QString>("expected"); 0248 0249 QTest::newRow("kde_is_awesome") << "KDE is awesome" << 11 << "KDE is a..."; 0250 QTest::newRow("kde_is_really_awesome") << "KDE is really awesome" << 20 << "KDE is really awe..."; 0251 QTest::newRow("kde_is_really_awesome_full") << "KDE is really awesome" << 30 << "KDE is really awesome"; 0252 } 0253 0254 void KStringHandlerTest::rsqueeze() 0255 { 0256 QFETCH(QString, string); 0257 QFETCH(int, length); 0258 QFETCH(QString, expected); 0259 0260 QCOMPARE(KStringHandler::rsqueeze(string, length), expected); 0261 } 0262 0263 #include "moc_kstringhandlertest.cpp"