kstringhandlertest.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #include "kstringhandlertest.h"
  2. #include <QRegularExpression>
  3. #include <QTest>
  4. QTEST_MAIN(KStringHandlerTest)
  5. #include "kstringhandler.h"
  6. QString KStringHandlerTest::test = QStringLiteral("The quick brown fox jumped over the lazy bridge. ");
  7. void KStringHandlerTest::capwords()
  8. {
  9. QCOMPARE(KStringHandler::capwords(test), QStringLiteral("The Quick Brown Fox Jumped Over The Lazy Bridge. "));
  10. }
  11. void KStringHandlerTest::tagURLs()
  12. {
  13. QString test = QStringLiteral("Click on https://foo@bar:www.kde.org/yoyo/dyne.html#a1 for info.");
  14. QCOMPARE(KStringHandler::tagUrls(test),
  15. 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."));
  16. test = QStringLiteral("http://www.foo.org/story$806");
  17. QCOMPARE(KStringHandler::tagUrls(test), QStringLiteral("<a href=\"http://www.foo.org/story$806\">http://www.foo.org/story$806</a>"));
  18. test = QStringLiteral("http://www.foo.org/bla-(bli)");
  19. QCOMPARE(KStringHandler::tagUrls(test), QStringLiteral("<a href=\"http://www.foo.org/bla-(bli)\">http://www.foo.org/bla-(bli)</a>"));
  20. test = QStringLiteral("http://www.foo.org/bla-bli");
  21. QCOMPARE(KStringHandler::tagUrls(test), QStringLiteral("<a href=\"http://www.foo.org/bla-bli\">http://www.foo.org/bla-bli</a>"));
  22. // Test with Unicode characters
  23. test = QStringLiteral("Click on https://foo@bar:www.kde.org/ÿöyo/dyne.html#a1 for info.");
  24. QCOMPARE(KStringHandler::tagUrls(test),
  25. 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."));
  26. }
  27. void KStringHandlerTest::perlSplitTextSep()
  28. {
  29. QStringList expected;
  30. expected << QStringLiteral("some") << QStringLiteral("string") << QStringLiteral("for") << QStringLiteral("you__here");
  31. QCOMPARE(KStringHandler::perlSplit(QStringLiteral("__"), QStringLiteral("some__string__for__you__here"), 4), expected);
  32. expected.clear();
  33. expected << QStringLiteral("kparts") << QStringLiteral("reaches") << QStringLiteral("the parts other parts can't");
  34. QCOMPARE(KStringHandler::perlSplit(QLatin1Char(' '), QStringLiteral("kparts reaches the parts other parts can't"), 3), expected);
  35. }
  36. void KStringHandlerTest::perlSplitRegexSep()
  37. {
  38. #if KCOREADDONS_BUILD_DEPRECATED_SINCE(5, 67)
  39. QCOMPARE(KStringHandler::perlSplit(QRegExp(QStringLiteral("[! ]")), QStringLiteral("Split me up ! I'm bored ! OK ?"), 3),
  40. (QStringList{QStringLiteral("Split"), QStringLiteral("me"), QStringLiteral("up ! I'm bored ! OK ?")}));
  41. #endif
  42. QCOMPARE(KStringHandler::perlSplit(QRegularExpression(QStringLiteral("[! ]")), QStringLiteral("Split me up ! I'm bored ! OK ?"), 3),
  43. (QStringList{QStringLiteral("Split"), QStringLiteral("me"), QStringLiteral("up ! I'm bored ! OK ?")}));
  44. QCOMPARE(KStringHandler::perlSplit(QRegularExpression(QStringLiteral("\\W")), QStringLiteral("aaa ggg cd ef"), 3),
  45. (QStringList{QStringLiteral("aaa"), QStringLiteral("ggg"), QStringLiteral("cd ef")}));
  46. // Test with Unicode characters
  47. QCOMPARE(KStringHandler::perlSplit(QRegularExpression(QStringLiteral("\\W")), QStringLiteral("aaa gǵg cd ef"), 3),
  48. (QStringList{QStringLiteral("aaa"), QStringLiteral("gǵg"), QStringLiteral("cd ef")}));
  49. }
  50. void KStringHandlerTest::obscure()
  51. {
  52. // See bug 167900, obscure() produced chars that could not properly be converted to and from
  53. // UTF8. The result was that storing passwords with '!' in them did not work.
  54. QString test = QStringLiteral("!TEST!");
  55. QString obscured = KStringHandler::obscure(test);
  56. QByteArray obscuredBytes = obscured.toUtf8();
  57. QCOMPARE(KStringHandler::obscure(QString::fromUtf8(obscuredBytes.constData())), test);
  58. }
  59. // Zero-Width Space
  60. static const QChar ZWSP(0x200b);
  61. // Word Joiner
  62. static const QChar WJ(0x2060);
  63. void KStringHandlerTest::preProcessWrap_data()
  64. {
  65. QTest::addColumn<QString>("string");
  66. QTest::addColumn<QString>("expected");
  67. // Should result in no additional breaks
  68. QTest::newRow("spaces") << "foo bar baz"
  69. << "foo bar baz";
  70. // Should insert a ZWSP after each '_'
  71. QTest::newRow("underscores") << "foo_bar_baz" << QString(QStringLiteral("foo_") + ZWSP + QStringLiteral("bar_") + ZWSP + QStringLiteral("baz"));
  72. // Should insert a ZWSP after each '-'
  73. QTest::newRow("hyphens") << "foo-bar-baz" << QString(QStringLiteral("foo-") + ZWSP + QStringLiteral("bar-") + ZWSP + QStringLiteral("baz"));
  74. // Should insert a ZWSP after each '.'
  75. QTest::newRow("periods") << "foo.bar.baz" << QString(QStringLiteral("foo.") + ZWSP + QStringLiteral("bar.") + ZWSP + QStringLiteral("baz"));
  76. // Should insert a ZWSP after each ','
  77. QTest::newRow("commas") << "foo,bar,baz" << QString(QStringLiteral("foo,") + ZWSP + QStringLiteral("bar,") + ZWSP + QStringLiteral("baz"));
  78. // Should result in no additional breaks since the '_'s are followed by spaces
  79. QTest::newRow("mixed underscores and spaces") << "foo_ bar_ baz"
  80. << "foo_ bar_ baz";
  81. // Should result in no additional breaks since the '_' is the last char
  82. QTest::newRow("ends with underscore") << "foo_"
  83. << "foo_";
  84. // Should insert a ZWSP before '(' and after ')'
  85. QTest::newRow("parens") << "foo(bar)baz" << QString(QStringLiteral("foo") + ZWSP + QStringLiteral("(bar)") + ZWSP + QStringLiteral("baz"));
  86. // Should insert a ZWSP before '[' and after ']'
  87. QTest::newRow("brackets") << "foo[bar]baz" << QString(QStringLiteral("foo") + ZWSP + QStringLiteral("[bar]") + ZWSP + QStringLiteral("baz"));
  88. // Should insert a ZWSP before '{' and after '}'
  89. QTest::newRow("curly braces") << "foo{bar}baz" << QString(QStringLiteral("foo") + ZWSP + QStringLiteral("{bar}") + ZWSP + QStringLiteral("baz"));
  90. // Should insert a ZWSP before '(' but not after ')' since it's the last char
  91. QTest::newRow("ends with ')'") << "foo(bar)" << QString(QStringLiteral("foo") + ZWSP + QStringLiteral("(bar)"));
  92. // Should insert a single ZWSP between the '_' and the '('
  93. QTest::newRow("'_' followed by '('") << "foo_(bar)" << QString(QStringLiteral("foo_") + ZWSP + QStringLiteral("(bar)"));
  94. // Should insert ZWSP's between the '_' and the '[', between the double
  95. // '['s and the double ']'s, but not before and after 'bar'
  96. QTest::newRow("'_' before double brackets") << "foo_[[bar]]"
  97. << QString(QStringLiteral("foo_") + ZWSP + QStringLiteral("[") + ZWSP + QStringLiteral("[bar]") + ZWSP
  98. + QStringLiteral("]"));
  99. // Should only insert ZWSP's between the double '['s and the double ']'s
  100. QTest::newRow("space before double brackets") << "foo [[bar]]"
  101. << QString(QStringLiteral("foo [") + ZWSP + QStringLiteral("[bar]") + ZWSP + QStringLiteral("]"));
  102. // Shouldn't result in any additional breaks since the '(' is preceded
  103. // by a space, and the ')' is followed by a space.
  104. QTest::newRow("parens with spaces") << "foo (bar) baz"
  105. << "foo (bar) baz";
  106. // Should insert a WJ (Word Joiner) before a single quote
  107. QTest::newRow("single quote") << "foo'bar" << QString(QStringLiteral("foo") + WJ + QStringLiteral("'bar"));
  108. // Should insert a ZWSP between sub-words, but not before nor after the word
  109. QTest::newRow("camelCase") << "camelCase" << QString(QStringLiteral("camel") + ZWSP + QStringLiteral("Case"));
  110. // Why limiting yourself to ASCII? More and more programming languages these days allow for Unicode identifiers.
  111. QTest::newRow("camelCase international") << "приветМир" << QString(QStringLiteral("привет") + ZWSP + QStringLiteral("Мир"));
  112. // Should insert a ZWSP between sub-words, but not before first (upper case) letter
  113. QTest::newRow("PascalCase") << "PascalCase" << QString(QStringLiteral("Pascal") + ZWSP + QStringLiteral("Case"));
  114. }
  115. // Little helper function to make tests diagnostics more readable by humans
  116. static QString replaceZwsp(const QString &string)
  117. {
  118. const QString replacement = QStringLiteral("<ZWSP>");
  119. QString result;
  120. result.reserve(string.length() + string.count(ZWSP) * replacement.length());
  121. for (const auto i : string) {
  122. if (i == ZWSP) {
  123. result += replacement;
  124. } else {
  125. result += i;
  126. }
  127. }
  128. return result;
  129. }
  130. void KStringHandlerTest::preProcessWrap()
  131. {
  132. QFETCH(QString, string);
  133. QFETCH(QString, expected);
  134. QCOMPARE(replaceZwsp(KStringHandler::preProcessWrap(string)), replaceZwsp(expected));
  135. }
  136. void KStringHandlerTest::logicalLength_data()
  137. {
  138. QTest::addColumn<QString>("string");
  139. QTest::addColumn<int>("expected");
  140. QTest::newRow("Latin") << "foo bar baz" << 11;
  141. QTest::newRow("Chinese") << QString::fromUtf8("\xe4\xbd\xa0\xe5\xa5\xbd") << 4;
  142. QTest::newRow("Japanese") << QString::fromUtf8("\xe9\x9d\x92\xe3\x81\x84\xe7\xa9\xba") << 6;
  143. QTest::newRow("Korean") << QString::fromUtf8("\xed\x95\x9c\xea\xb5\xad\xec\x96\xb4") << 6;
  144. QTest::newRow("Mixed") << QString::fromUtf8("KDE\xe6\xa1\x8c\xe9\x9d\xa2") << 7;
  145. }
  146. void KStringHandlerTest::logicalLength()
  147. {
  148. QFETCH(QString, string);
  149. QFETCH(int, expected);
  150. QCOMPARE(KStringHandler::logicalLength(string), expected);
  151. }
  152. void KStringHandlerTest::lsqueeze_data()
  153. {
  154. QTest::addColumn<QString>("string");
  155. QTest::addColumn<int>("length");
  156. QTest::addColumn<QString>("expected");
  157. QTest::newRow("kde_is_awesome") << "KDE is awesome" << 11 << "... awesome";
  158. QTest::newRow("kde_is_really_awesome") << "KDE is really awesome" << 20 << "...is really awesome";
  159. QTest::newRow("kde_is_really_awesome_full") << "KDE is really awesome" << 30 << "KDE is really awesome";
  160. }
  161. void KStringHandlerTest::lsqueeze()
  162. {
  163. QFETCH(QString, string);
  164. QFETCH(int, length);
  165. QFETCH(QString, expected);
  166. QCOMPARE(KStringHandler::lsqueeze(string, length), expected);
  167. }
  168. void KStringHandlerTest::csqueeze_data()
  169. {
  170. QTest::addColumn<QString>("string");
  171. QTest::addColumn<int>("length");
  172. QTest::addColumn<QString>("expected");
  173. QTest::newRow("kde_is_awesome") << "KDE is awesome" << 11 << "KDE ...some";
  174. QTest::newRow("kde_is_really_awesome") << "KDE is really awesome" << 20 << "KDE is r... awesome";
  175. QTest::newRow("kde_is_really_awesome_full") << "KDE is really awesome" << 30 << "KDE is really awesome";
  176. }
  177. void KStringHandlerTest::csqueeze()
  178. {
  179. QFETCH(QString, string);
  180. QFETCH(int, length);
  181. QFETCH(QString, expected);
  182. QCOMPARE(KStringHandler::csqueeze(string, length), expected);
  183. }
  184. void KStringHandlerTest::rsqueeze_data()
  185. {
  186. QTest::addColumn<QString>("string");
  187. QTest::addColumn<int>("length");
  188. QTest::addColumn<QString>("expected");
  189. QTest::newRow("kde_is_awesome") << "KDE is awesome" << 11 << "KDE is a...";
  190. QTest::newRow("kde_is_really_awesome") << "KDE is really awesome" << 20 << "KDE is really awe...";
  191. QTest::newRow("kde_is_really_awesome_full") << "KDE is really awesome" << 30 << "KDE is really awesome";
  192. }
  193. void KStringHandlerTest::rsqueeze()
  194. {
  195. QFETCH(QString, string);
  196. QFETCH(int, length);
  197. QFETCH(QString, expected);
  198. QCOMPARE(KStringHandler::rsqueeze(string, length), expected);
  199. }
  200. #include "moc_kstringhandlertest.cpp"