File indexing completed on 2024-05-12 05:17:32

0001 /*
0002     SPDX-FileCopyrightText: 2018 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "../src/lib/stringutil.cpp"
0008 
0009 #include <QObject>
0010 #include <QTest>
0011 
0012 #define _(x) QStringLiteral(x)
0013 
0014 using namespace KItinerary;
0015 
0016 class StringUtilTest : public QObject
0017 {
0018     Q_OBJECT
0019 private Q_SLOTS:
0020     void testNormalize_data()
0021     {
0022         QTest::addColumn<QString>("in");
0023         QTest::addColumn<QString>("out");
0024 
0025         QTest::newRow("empty") << QString() << QString();
0026         QTest::newRow("normalized") << _("normal") << _("normal");
0027         QTest::newRow("case-folding") << _("NORMAL") << _("normal");
0028         QTest::newRow("umlaut") << _("NöRMÄl") << _("normal");
0029         QTest::newRow("ligature1") << _("finish") << _("finish");
0030         QTest::newRow("ligature2") << _("off") << _("off");
0031     }
0032 
0033     void testNormalize()
0034     {
0035         QFETCH(QString, in);
0036         QFETCH(QString, out);
0037         QCOMPARE(StringUtil::normalize(in), out);
0038     }
0039 
0040     void testPrefixSimilarity()
0041     {
0042         QCOMPARE(StringUtil::prefixSimilarity(QString(), QString()), 0.0f);
0043         QCOMPARE(StringUtil::prefixSimilarity(u"aaa", QString()), 0.0f);
0044         QCOMPARE(StringUtil::prefixSimilarity(u"aaaa", u"AA"), 0.5f);
0045         QCOMPARE(StringUtil::prefixSimilarity(u"aba", u"aBa"), 1.0f);
0046         QCOMPARE(StringUtil::prefixSimilarity(u"ab", u"aa"), 0.5f);
0047         QCOMPARE(StringUtil::prefixSimilarity(u"ac", u"abbb"), 0.25f);
0048     }
0049 
0050     void testClean()
0051     {
0052         QCOMPARE(StringUtil::clean(QString()), QString());
0053         QCOMPARE(StringUtil::clean(QStringLiteral("Lech Wa&#322;&#281;sa Airport")), QStringLiteral("Lech Wałęsa Airport"));
0054         QCOMPARE(StringUtil::clean(
0055                      QStringLiteral("On-demand services: MobilityData&#39;s "
0056                                     "GOFS project &amp;amp; going further")),
0057                  QLatin1StringView("On-demand services: MobilityData's GOFS "
0058                                    "project &amp; going further"));
0059     }
0060 
0061     void testBetterString_data()
0062     {
0063         QTest::addColumn<QString>("s1");
0064         QTest::addColumn<QString>("s2");
0065         QTest::addColumn<QString>("result");
0066 
0067         QTest::newRow("empty") << QString() << QString() << QString();
0068         QTest::newRow("single-case") << _("TEST") << _("test") << _("TEST");
0069         QTest::newRow("single-case-inv") << _("test") << _("TEST") << _("TEST");
0070         QTest::newRow("mixed-case") << _("TEST") << _("Test") << _("Test");
0071         QTest::newRow("mixed-case-inv") << _("Test") << _("TEST") << _("Test");
0072         QTest::newRow("single-case-space") << _("TEST NAME 1") << _("test name 1") << _("TEST NAME 1");
0073         QTest::newRow("mixed-case-space") << _("TEST NAME 2") << _("Test Name 2") << _("Test Name 2");
0074         QTest::newRow("mixed-case-space-inv") << _("Test name 2") << _("TEST NAME 2") << _("Test name 2");
0075         QTest::newRow("french-name") << _("Test NAME") << _("Test Name") << _("Test Name");
0076         QTest::newRow("french-name-inv") << _("Test Name") << _("Test NAME") << _("Test Name");
0077     }
0078 
0079     void testBetterString()
0080     {
0081         QFETCH(QString, s1);
0082         QFETCH(QString, s2);
0083         QFETCH(QString, result);
0084 
0085         QCOMPARE(StringUtil::betterString(s1, s2), result);
0086     }
0087 };
0088 
0089 QTEST_APPLESS_MAIN(StringUtilTest)
0090 
0091 #include "stringutiltest.moc"