Warning, file /frameworks/kcoreaddons/autotests/kstringhandlertest.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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 #if KCOREADDONS_BUILD_DEPRECATED_SINCE(5, 67)
0053     QCOMPARE(KStringHandler::perlSplit(QRegExp(QStringLiteral("[! ]")), QStringLiteral("Split me up ! I'm bored ! OK ?"), 3),
0054              (QStringList{QStringLiteral("Split"), QStringLiteral("me"), QStringLiteral("up ! I'm bored ! OK ?")}));
0055 #endif
0056     QCOMPARE(KStringHandler::perlSplit(QRegularExpression(QStringLiteral("[! ]")), QStringLiteral("Split me up ! I'm bored ! OK ?"), 3),
0057              (QStringList{QStringLiteral("Split"), QStringLiteral("me"), QStringLiteral("up ! I'm bored ! OK ?")}));
0058 
0059     QCOMPARE(KStringHandler::perlSplit(QRegularExpression(QStringLiteral("\\W")), QStringLiteral("aaa ggg cd ef"), 3),
0060              (QStringList{QStringLiteral("aaa"), QStringLiteral("ggg"), QStringLiteral("cd ef")}));
0061 
0062     // Test with Unicode characters
0063     QCOMPARE(KStringHandler::perlSplit(QRegularExpression(QStringLiteral("\\W")), QStringLiteral("aaa gǵg cd ef"), 3),
0064              (QStringList{QStringLiteral("aaa"), QStringLiteral("gǵg"), QStringLiteral("cd ef")}));
0065 }
0066 
0067 void KStringHandlerTest::obscure()
0068 {
0069     // See bug 167900, obscure() produced chars that could not properly be converted to and from
0070     // UTF8. The result was that storing passwords with '!' in them did not work.
0071     QString test = QStringLiteral("!TEST!");
0072     QString obscured = KStringHandler::obscure(test);
0073     QByteArray obscuredBytes = obscured.toUtf8();
0074     QCOMPARE(KStringHandler::obscure(QString::fromUtf8(obscuredBytes.constData())), test);
0075 }
0076 
0077 // Zero-Width Space
0078 static const QChar ZWSP(0x200b);
0079 // Word Joiner
0080 static const QChar WJ(0x2060);
0081 
0082 void KStringHandlerTest::preProcessWrap_data()
0083 {
0084     QTest::addColumn<QString>("string");
0085     QTest::addColumn<QString>("expected");
0086 
0087     // Should result in no additional breaks
0088     QTest::newRow("spaces") << "foo bar baz"
0089                             << "foo bar baz";
0090 
0091     // Should insert a ZWSP after each '_'
0092     QTest::newRow("underscores") << "foo_bar_baz" << QString(QStringLiteral("foo_") + ZWSP + QStringLiteral("bar_") + ZWSP + QStringLiteral("baz"));
0093 
0094     // Should insert a ZWSP after each '-'
0095     QTest::newRow("hyphens") << "foo-bar-baz" << QString(QStringLiteral("foo-") + ZWSP + QStringLiteral("bar-") + ZWSP + QStringLiteral("baz"));
0096 
0097     // Should insert a ZWSP after each '.'
0098     QTest::newRow("periods") << "foo.bar.baz" << QString(QStringLiteral("foo.") + ZWSP + QStringLiteral("bar.") + ZWSP + QStringLiteral("baz"));
0099 
0100     // Should insert a ZWSP after each ','
0101     QTest::newRow("commas") << "foo,bar,baz" << QString(QStringLiteral("foo,") + ZWSP + QStringLiteral("bar,") + ZWSP + QStringLiteral("baz"));
0102 
0103     // Should result in no additional breaks since the '_'s are followed by spaces
0104     QTest::newRow("mixed underscores and spaces") << "foo_ bar_ baz"
0105                                                   << "foo_ bar_ baz";
0106 
0107     // Should result in no additional breaks since the '_' is the last char
0108     QTest::newRow("ends with underscore") << "foo_"
0109                                           << "foo_";
0110 
0111     // Should insert a ZWSP before '(' and after ')'
0112     QTest::newRow("parens") << "foo(bar)baz" << QString(QStringLiteral("foo") + ZWSP + QStringLiteral("(bar)") + ZWSP + QStringLiteral("baz"));
0113 
0114     // Should insert a ZWSP before '[' and after ']'
0115     QTest::newRow("brackets") << "foo[bar]baz" << QString(QStringLiteral("foo") + ZWSP + QStringLiteral("[bar]") + ZWSP + QStringLiteral("baz"));
0116 
0117     // Should insert a ZWSP before '{' and after '}'
0118     QTest::newRow("curly braces") << "foo{bar}baz" << QString(QStringLiteral("foo") + ZWSP + QStringLiteral("{bar}") + ZWSP + QStringLiteral("baz"));
0119 
0120     // Should insert a ZWSP before '(' but not after ')' since it's the last char
0121     QTest::newRow("ends with ')'") << "foo(bar)" << QString(QStringLiteral("foo") + ZWSP + QStringLiteral("(bar)"));
0122 
0123     // Should insert a single ZWSP between the '_' and the '('
0124     QTest::newRow("'_' followed by '('") << "foo_(bar)" << QString(QStringLiteral("foo_") + ZWSP + QStringLiteral("(bar)"));
0125 
0126     // Should insert ZWSP's between the '_' and the '[', between the double
0127     // '['s and the double ']'s, but not before and after 'bar'
0128     QTest::newRow("'_' before double brackets") << "foo_[[bar]]"
0129                                                 << QString(QStringLiteral("foo_") + ZWSP + QStringLiteral("[") + ZWSP + QStringLiteral("[bar]") + ZWSP
0130                                                            + QStringLiteral("]"));
0131 
0132     // Should only insert ZWSP's between the double '['s and the double ']'s
0133     QTest::newRow("space before double brackets") << "foo [[bar]]"
0134                                                   << QString(QStringLiteral("foo [") + ZWSP + QStringLiteral("[bar]") + ZWSP + QStringLiteral("]"));
0135 
0136     // Shouldn't result in any additional breaks since the '(' is preceded
0137     // by a space, and the ')' is followed by a space.
0138     QTest::newRow("parens with spaces") << "foo (bar) baz"
0139                                         << "foo (bar) baz";
0140 
0141     // Should insert a WJ (Word Joiner) before a single quote
0142     QTest::newRow("single quote") << "foo'bar" << QString(QStringLiteral("foo") + WJ + QStringLiteral("'bar"));
0143 
0144     // Should insert a ZWSP between sub-words, but not before nor after the word
0145     QTest::newRow("camelCase") << "camelCase" << QString(QStringLiteral("camel") + ZWSP + QStringLiteral("Case"));
0146 
0147     // Why limiting yourself to ASCII? More and more programming languages these days allow for Unicode identifiers.
0148     QTest::newRow("camelCase international") << "приветМир" << QString(QStringLiteral("привет") + ZWSP + QStringLiteral("Мир"));
0149 
0150     // Should insert a ZWSP between sub-words, but not before first (upper case) letter
0151     QTest::newRow("PascalCase") << "PascalCase" << QString(QStringLiteral("Pascal") + ZWSP + QStringLiteral("Case"));
0152 }
0153 
0154 // Little helper function to make tests diagnostics more readable by humans
0155 static QString replaceZwsp(const QString &string)
0156 {
0157     const QString replacement = QStringLiteral("<ZWSP>");
0158     QString result;
0159     result.reserve(string.length() + string.count(ZWSP) * replacement.length());
0160     for (const auto i : string) {
0161         if (i == ZWSP) {
0162             result += replacement;
0163         } else {
0164             result += i;
0165         }
0166     }
0167 
0168     return result;
0169 }
0170 
0171 void KStringHandlerTest::preProcessWrap()
0172 {
0173     QFETCH(QString, string);
0174     QFETCH(QString, expected);
0175 
0176     QCOMPARE(replaceZwsp(KStringHandler::preProcessWrap(string)), replaceZwsp(expected));
0177 }
0178 
0179 void KStringHandlerTest::logicalLength_data()
0180 {
0181     QTest::addColumn<QString>("string");
0182     QTest::addColumn<int>("expected");
0183 
0184     QTest::newRow("Latin") << "foo bar baz" << 11;
0185     QTest::newRow("Chinese") << QString::fromUtf8("\xe4\xbd\xa0\xe5\xa5\xbd") << 4;
0186     QTest::newRow("Japanese") << QString::fromUtf8("\xe9\x9d\x92\xe3\x81\x84\xe7\xa9\xba") << 6;
0187     QTest::newRow("Korean") << QString::fromUtf8("\xed\x95\x9c\xea\xb5\xad\xec\x96\xb4") << 6;
0188     QTest::newRow("Mixed") << QString::fromUtf8("KDE\xe6\xa1\x8c\xe9\x9d\xa2") << 7;
0189 }
0190 
0191 void KStringHandlerTest::logicalLength()
0192 {
0193     QFETCH(QString, string);
0194     QFETCH(int, expected);
0195     QCOMPARE(KStringHandler::logicalLength(string), expected);
0196 }
0197 
0198 void KStringHandlerTest::lsqueeze_data()
0199 {
0200     QTest::addColumn<QString>("string");
0201     QTest::addColumn<int>("length");
0202     QTest::addColumn<QString>("expected");
0203 
0204     QTest::newRow("kde_is_awesome") << "KDE is awesome" << 11 << "... awesome";
0205     QTest::newRow("kde_is_really_awesome") << "KDE is really awesome" << 20 << "...is really awesome";
0206     QTest::newRow("kde_is_really_awesome_full") << "KDE is really awesome" << 30 << "KDE is really awesome";
0207 }
0208 
0209 void KStringHandlerTest::lsqueeze()
0210 {
0211     QFETCH(QString, string);
0212     QFETCH(int, length);
0213     QFETCH(QString, expected);
0214 
0215     QCOMPARE(KStringHandler::lsqueeze(string, length), expected);
0216 }
0217 
0218 void KStringHandlerTest::csqueeze_data()
0219 {
0220     QTest::addColumn<QString>("string");
0221     QTest::addColumn<int>("length");
0222     QTest::addColumn<QString>("expected");
0223 
0224     QTest::newRow("kde_is_awesome") << "KDE is awesome" << 11 << "KDE ...some";
0225     QTest::newRow("kde_is_really_awesome") << "KDE is really awesome" << 20 << "KDE is r... awesome";
0226     QTest::newRow("kde_is_really_awesome_full") << "KDE is really awesome" << 30 << "KDE is really awesome";
0227 }
0228 
0229 void KStringHandlerTest::csqueeze()
0230 {
0231     QFETCH(QString, string);
0232     QFETCH(int, length);
0233     QFETCH(QString, expected);
0234 
0235     QCOMPARE(KStringHandler::csqueeze(string, length), expected);
0236 }
0237 
0238 void KStringHandlerTest::rsqueeze_data()
0239 {
0240     QTest::addColumn<QString>("string");
0241     QTest::addColumn<int>("length");
0242     QTest::addColumn<QString>("expected");
0243 
0244     QTest::newRow("kde_is_awesome") << "KDE is awesome" << 11 << "KDE is a...";
0245     QTest::newRow("kde_is_really_awesome") << "KDE is really awesome" << 20 << "KDE is really awe...";
0246     QTest::newRow("kde_is_really_awesome_full") << "KDE is really awesome" << 30 << "KDE is really awesome";
0247 }
0248 
0249 void KStringHandlerTest::rsqueeze()
0250 {
0251     QFETCH(QString, string);
0252     QFETCH(int, length);
0253     QFETCH(QString, expected);
0254 
0255     QCOMPARE(KStringHandler::rsqueeze(string, length), expected);
0256 }
0257 
0258 #include "moc_kstringhandlertest.cpp"